You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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) and if(EndDate) always evaluate to true (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.exit calls 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.long instead of strategy.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] < X condition works across all timeframes, even if your target time falls in the middle of a bar.
  • Order Sizing: Explicit initial_capital and default_qty_type settings eliminate ambiguity around fund availability and order sizing.

内容的提问来源于stack exchange,提问作者WoHev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 02:57:55