提问人:Gavin Poynton 提问时间:10/4/2023 更新时间:10/4/2023 访问量:30
Pinescript 停止不工作 - 本地范围?
Pinescript Stop not working - Local Scope?
问:
我一直在尝试在pinescript(5)中获取一些进入/退出代码。它似乎适用于 strategy.exit 上的限制,但止损不会触发。
我一直在玩这个,但我无法让它工作 - 我认为这可能取决于当地人?全球范围。
- 从本质上讲,条目很好,而且有效
- 退出类型是从下拉列表中选择的,它是一组选项 - 大多数情况下,固定的 % TP 由入场价格变量 entryPriceB 计算。
- 但是,有一个“自定义”止盈期权,它是一个信号 - 因此在收到信号CLOSE_BUY_SIGNAL之前无法计算 TakeProfitB 变量。
if Trade_Direction_Filter_Long and dateRange and OPEN_BUY_SIGNAL and Long_Orders_Placed < Max_Long_Orders
entryPriceB := close
Long_Position := true
stopLossB := SL ? calculateSL() : na
takeProfitB := TP ? calculateTP() : na
if useLeverage
strategy.entry("Long", strategy.long, limit=entryPriceB, qty=math.min(math.max(.000001, ( strategy.equity / close ) * leverage), 1000000000), alert_message = alert_txt_entry_long)//ADD {{strategy.order.alert_message}} to the message box in Alerts to set up dynamic link
Long_Orders_Placed := Long_Orders_Placed + 1
alert("ADD Increased count - " + str.tostring(Long_Orders_Placed))
else
strategy.entry("Long", strategy.long, limit=entryPriceB, alert_message = alert_txt_entry_long)//ADD {{strategy.order.alert_message}} to the message box in Alerts to set up dynamic link
Long_Orders_Placed := Long_Orders_Placed + 1
alert("ADD Increased count - " + str.tostring(Long_Orders_Placed))
if Trade_Direction_Filter_Long and dateRange and Long_Orders_Placed > 0
if CLOSE_BUY_SIGNAL and optionTP == "Custom"
takeProfitB := TP ? calculateTP() : na
strategy.exit("Long Exit", "Long", stop=stopLossB, limit=takeProfitB, comment = "Signal Exit")
Long_Orders_Placed := Long_Orders_Placed - 1
else if (optionTP != "Custom" )
strategy.exit("Long Exit", "Long", stop=stopLossB, limit=takeProfitB, comment = "Option Exit")
Long_Orders_Placed := Long_Orders_Placed - 1
希望这是有道理的,任何意见都将不胜感激。
我尝试移动范围,但遇到了退出是有条件的问题,因为它是optionTP ==“自定义”和CLOSE_BUY_SIGNAL。或者 optionTP != “自定义”
答:
0赞
Liam Park
10/4/2023
#1
您面临的问题可能与您在 if 和 block 中有条件地更新值的方式有关。Pine脚本是一种前瞻性语言,在if条件中更改变量可能不会以您期望的方式更新变量。请尝试以下方法:takeProfitB
CLOSE_BUY_SIGNAL
optionTP == "Custom"
//@version=5
indicator("Custom TP Example", overlay=true)
var float entryPriceB = na
var bool Long_Position = na
var float stopLossB = na
var float takeProfitB = na
var bool CLOSE_BUY_SIGNAL = ta.crossover(close[1], close)
var string optionTP = "Custom" // Replace with your logic for optionTP
if ta.crossover(close[1], close)
// Entry conditions
entryPriceB := close
Long_Position := true
stopLossB := SL ? calculateSL() : na
takeProfitB := optionTP == "Custom" ? calculateTP() : na
if ta.crossover(close, close[1]) and Long_Position
// Exit conditions
if optionTP == "Custom"
takeProfitB := calculateTP()
strategy.exit("Long Exit", "Long", stop=stopLossB, limit=takeProfitB, comment = "Exit")
Long_Position := false
在此修改后的代码中:
我使用该函数来检测.根据您的实际信号逻辑修改此条件。
ta.crossover
CLOSE_BUY_SIGNAL
当我输入位置和您想退出时,我都会有条件地更新。这可确保在发生时正确设置。
takeProfitB
"Custom" TP
takeProfitB
CLOSE_BUY_SIGNAL
我用作标志来跟踪您是否处于多头头寸。这有助于确定何时退出。
Long_Position
请根据您的特定进入和退出条件调整代码,但此结构应有助于确保在需要时正确计算 takeProfitB。祝你好运!
评论