Pinescript - Simple If Else

- 1 answer

I am currently trying to get back into coding (its been some time) and for some reason I cant get my pinescipt to execute properly.

The intent is that

Condition for strategy execution is met, on the first candle a particular strategy.entry with alert_message is executed (3commas deal start).

On the next candle, or any subsequent candle WHILE the deal is open, if the same condition is met a second strategy.entry and alert_message is executed (3commas deal add funds)

If following these the event is NOT met, strategy close is triggered.

At the moment, for some reason its only triggering the deal start repeatedly, and not jumping to the add - I know its a dumb mistake - I just cant see it!!!

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


Your script will be executed on every bar. When you define a variable like below

base_Long_Order_Placed = 0

It will be re-defined every time and base_Long_Order_Placed == 0 will always be true. If you want to keep its value between executions, you need to use the var keyword.

var base_Long_Order_Placed = 0


var is just a keyword that tells the compiler to keep its value for the next execution. It is just a regular variable otherwise. It will keep its value unless you overwrite. It will not iterate unless you do

base_Long_Order_Placed := base_Long_Order_Placed  + 1

So, you can definitely reset it when your condition is met.

if (deal_closed)
    base_Long_Order_Placed := 0