使用Python实现TradingView图表自动化操作咨询:截图、按下向下键及股票切换流程
TradingView Automation Script for Screenshots & Stock Switching
Hey there! No worries at all about grammar or being new to Python/Stack Overflow—we all start somewhere, and your request is totally clear. Let's put together a script that does exactly what you need using pyautogui (a great library for simulating keyboard/mouse actions and screenshots) and Python's built-in time module.
Step 1: Install Required Library
First, you'll need to install pyautogui if you haven't already. Open your terminal/command prompt and run:
pip install pyautogui
Step 2: The Automation Script
Here's a complete, commented script that matches your requirements. I'll explain each part so you understand what's happening:
import pyautogui import time # Optional: Set a small delay between pyautogui actions to avoid overwhelming the system pyautogui.PAUSE = 0.5 def process_single_stock(): # 1. Take first screenshot (using timestamp to ensure unique filenames) first_snap = pyautogui.screenshot() first_snap.save(f"stock_initial_{int(time.time())}.png") # 2. Press the Down arrow key to adjust the chart pyautogui.press('down') # 3. Take second screenshot after pressing Down second_snap = pyautogui.screenshot() second_snap.save(f"stock_after_down_{int(time.time())}.png") # 4. Wait 3 seconds before switching to the next stock time.sleep(3) # Example: Process 5 stocks (adjust the number to match your needs) for _ in range(5): process_single_stock() # Switch to next stock: Replace this with your actual shortcut! # Common options: Ctrl+Tab (if using browser tabs) or TradingView's custom shortcut pyautogui.hotkey('ctrl', 'tab')
Key Notes & Adjustments
- Stock Switch Shortcut: The
pyautogui.hotkey('ctrl', 'tab')line is a placeholder. You’ll need to replace this with the actual shortcut you use to switch stocks in TradingView (e.g.,alt+rightif you use a custom key combo, or whatever works for your setup). - Screenshot Naming: Timestamps (
int(time.time())) ensure no screenshots overwrite each other. Feel free to tweak the filename format (e.g., add stock symbols if you have a list to reference). - Active Window: Make sure the TradingView chart window is the foreground, active window when you run the script—otherwise, pyautogui will send actions to whatever window is on top.
- Safety Fail-Safe: If the script acts unexpectedly, quickly move your mouse to the top-left corner of the screen. PyAutoGUI has a built-in fail-safe that stops the script when the mouse hits this spot.
Troubleshooting Tips
- If screenshots won’t save, check that you have write permissions in the folder where you’re running the script.
- If keyboard presses don’t register, try increasing
pyautogui.PAUSEto1.0—this gives the system more time to process each action. - For large batches of stocks, you could expand the script to read stock symbols from a text file and loop through them, but this basic version is perfect for getting started.
Don’t hesitate to ask follow-up questions if something doesn’t work—we’re here to help!
内容的提问来源于stack exchange,提问作者NOWNEVER
相关产品推荐
相关产品推荐

