如何用PineScript指定时间生成交易?无交易执行问题求助
Pine Script Strategy: No Orders Triggered - Fixes & Explanations
Your strategy fails to generate orders due to several critical logic and syntax issues. Here's the breakdown and corrected implementation:
Key Issues in Original Code
- Incorrect Condition Checks:
if(StartDate)andif(EndDate)always evaluate totrue(since timestamps are non-zero values), causing the code to run on every bar instead of only at your target times. - Case Sensitivity Mismatch: Exit calls reference
from_entry="Entry long"(lowercase 'l'), but the entry is named"Entry Long"(uppercase 'L'). Pine Script is case-sensitive, so exits can't link to entries. - Duplicate Exit IDs: Two
strategy.exitcalls share the same ID (e.g.,"Exit Long"), overwriting the first exit rule. Profit and loss conditions should be combined into one call. - Wrong Short Entry Type: The "Entry Short" uses
strategy.longinstead ofstrategy.short, resulting in two long positions instead of a long-short hedge.
Corrected Code
//@version=5 strategy("News Hedge", overlay=true, initial_capital=10000, default_qty_type=strategy.cash) StartDate = input.time(timestamp("3 Dec 2021 14:30"), title="Entry Time") EndDate = input.time(timestamp("3 Dec 2021 15:00"), title="Close All Time") // Trigger entry on the first bar crossing the start timestamp if time >= StartDate and time[1] < StartDate // Enter long and short positions correctly strategy.entry("Entry Long", strategy.long, qty=100) strategy.entry("Entry Short", strategy.short, qty=100) // Combine profit/loss rules into a single exit per position strategy.exit("Exit Long", from_entry="Entry Long", qty=100, profit=30, loss=20) strategy.exit("Exit Short", from_entry="Entry Short", qty=100, profit=30, loss=20) // Close all positions on the first bar crossing the end timestamp if time >= EndDate and time[1] < EndDate strategy.close_all()
Additional Notes
- Time Zone Alignment: Ensure your chart's time zone matches the timestamp you input (e.g., adjust to UTC if your chart uses UTC).
- Timeframe Compatibility: The
time >= X and time[1] < Xcondition works across all timeframes, even if your target time falls in the middle of a bar. - Order Sizing: Explicit
initial_capitalanddefault_qty_typesettings eliminate ambiguity around fund availability and order sizing.
内容的提问来源于stack exchange,提问作者WoHev
相关产品推荐
相关产品推荐

