Pinescript - 简单的if else

Pinescript - Simple if else

我目前正试图重新开始编码(已经有一段时间了),但出于某种原因,我无法让我的 pinescipt 正常执行。

目的是

满足策略执行的条件,在第一个蜡烛上执行特定 strategy.entry 和 alert_message(3 个逗号交易开始)。

在交易开始时的下一根蜡烛或任何后续蜡烛上,如果满足相同条件,则会执行第二个 strategy.entry 和 alert_message(3 个逗号交易添加资金)

如果未满足这些事件,则触发策略平仓。

目前,出于某种原因,它只会重复触发交易开始,而不是跳转到添加 - 我知道这是一个愚蠢的错误 - 我只是看不到它!!!

base_Long_Order_Placed = 0

message_long_entry = "3commas deal start"
message_long_addition = "3commas deal add funds"
message_long_exit = "3commas close all deals"

longCondition = SECRET_SAUCE

if (longCondition == true and base_Long_Order_Placed == 0)
    strategy.entry('Long', strategy.long, when = longCondition ==true and barstate.isconfirmed, alert_message = message_long_entry) /
    base_Long_Order_Placed := 1
else if (longCondition == true and base_Long_Order_Placed == 1)
    strategy.entry('Long', strategy.long, when = base_Long_Order_Placed == 1 and longCondition ==true and barstate.isconfirmed, alert_message = message_long_addition)
else if (longCondition == false)
    strategy.close('Long', when=longCondition == false and barstate.isconfirmed, alert_message = message_long_exit)  
    base_Long_Order_Placed := 0
else
    na

您的脚本将在每个柱上执行。当您定义如下变量时

base_Long_Order_Placed = 0

每次都是re-defined,base_Long_Order_Placed == 0永远是true。如果你想在执行之间保持它的值,你需要使用 var 关键字。

var base_Long_Order_Placed = 0

编辑

var 只是一个关键字,告诉编译器在下次执行时保留它的值。否则它只是一个常规变量。除非您覆盖,否则它将保持其价值。除非你这样做,否则它不会迭代

base_Long_Order_Placed := base_Long_Order_Placed  + 1

所以,当你的条件满足时,你肯定可以重置它。

if (deal_closed)
    base_Long_Order_Placed := 0