0

I have a strategy including 20 poses pyramiding, and i follow the all pose seperately like this.

strategy.entry("BUY1", strategy.long, when= PoseCount==1 and PoseCount[1] !=1 and barstate.isconfirmed, alert_message = message_long_entry)
.....
strategy.entry("BUY20", strategy.long, when= PoseCount==20 and PoseCount[1] !=20  and barstate.isconfirmed, alert_message = message_long_entry)

 
strategy.entry("SELL1", strategy.short, when= PoseCount==-1 and PoseCount[1] !=-1  and barstate.isconfirmed, alert_message = message_short_entry)
....
strategy.entry("SELL20", strategy.short, when= PoseCount==-20 and PoseCount[1] !=-20  and barstate.isconfirmed, alert_message = message_short_entry)

 
 
strategy.close("BUY1", when= PoseCount==-1 and PoseCount[1] !=-1   and barstate.isconfirmed, alert_message = message_long_exit)
.....
strategy.close("BUY20", when= PoseCount==-20 and PoseCount[1] !=-20   and barstate.isconfirmed, alert_message = message_long_exit)


strategy.close("SELL1", when= PoseCount==1 and PoseCount[1] !=1  and barstate.isconfirmed, alert_message = message_short_exit)
....
strategy.close("SELL20", when= PoseCount==20 and PoseCount[1] !=20  and barstate.isconfirmed, alert_message = message_short_exit)

 
strategy.exit("BUY1 TP/SL",  "BUY1",    limit=long_profit_level,  stop=long_stop_level ,     alert_message = message_long_exit) 
....
strategy.exit("BUY20 TP/SL",  "BUY20",    limit=long_profit_level,  stop=long_stop_level ,     alert_message = message_long_exit) 


strategy.exit("SELL1 TP/SL",  "SELL1",   limit=short_profit_level,  stop=short_stop_level ,   alert_message = message_short_exit)
....
strategy.exit("SELL20 TP/SL",  "SELL20",   limit=short_profit_level,  stop=short_stop_level ,   alert_message = message_short_exit)

I want to shorten this structure using for loop. I tried the following but failed. Thanks in advance for your help.

for x = 1 to 20
   strategy.entry("BUY" + tostring(x), strategy.long, when= PoseCount==x and PoseCount[1] !=x and barstate.isconfirmed, alert_message = message_long_entry)
   strategy.entry("SELL" + tostring(x), strategy.short, when= PoseCount==-x and PoseCount[1] !=-x  and barstate.isconfirmed, alert_message = message_short_entry)
   strategy.close("BUY"+ tostring(x), when= PoseCount==-x and PoseCount[1] !=-x   and barstate.isconfirmed, alert_message = message_long_exit)
   strategy.close("SELL"+ tostring(x), when= PoseCount==x and PoseCount[1] !=x  and barstate.isconfirmed, alert_message = message_short_exit)
   strategy.exit("BUY TP/SL"+ tostring(x),  "BUY"+ tostring(x),    limit=long_profit_level,  stop=long_stop_level ,     alert_message = message_long_exit) 
   strategy.exit("SELL TP/SL"+ tostring(x),  "SELL"+ tostring(x),   limit=short_profit_level,  stop=short_stop_level ,   alert_message = message_short_exit)

edit

still same error (Mismatched input 'to' expecting 'end of line without line continuation') with the following

for x = 1 to 20
     strategy.entry("BUY" + tostring(x), strategy.long, when= PoseCount==x and PoseCount[1] !=x and barstate.isconfirmed, alert_message = message_long_entry)

for x = 1 to 20    
     strategy.entry("SELL" + tostring(x), strategy.short, when= PoseCount==-x and PoseCount[1] !=-x  and barstate.isconfirmed, alert_message = message_short_entry)
   
for x = 1 to 20      
     strategy.entry("SELL" + tostring(x), strategy.short, when= PoseCount==-x and PoseCount[1] !=-x  and barstate.isconfirmed, alert_message = message_short_entry)
   
for x = 1 to 20  
     strategy.close("BUY"+ tostring(x), when= PoseCount==-x and PoseCount[1] !=-x   and barstate.isconfirmed, alert_message = message_long_exit)
   
for x = 1 to 20      
     strategy.exit("BUY TP/SL"+ tostring(x),  "BUY"+ tostring(x),    limit=long_profit_level,  stop=long_stop_level ,     alert_message = message_long_exit) 
  
for x = 1 to 20
     strategy.exit("SELL TP/SL"+ tostring(x),  "SELL"+ tostring(x),   limit=short_profit_level,  stop=short_stop_level ,   alert_message = message_short_exit)
anonimm
  • 28
  • 4
  • 1
    What language do you use? How does it fail? The code in the loop is not the same your expanded one: you had 20 buys, then 20 sell… If this is important, you'll need several for-loops: first iterates 20 times for buy, second iterates 20 times for sell and so on. – Alexey Ivanov Feb 12 '22 at 08:11
  • I use Pine script, and get "Mismatched input 'to' expecting 'end of line without line continuation'." error at "for" line. – anonimm Feb 12 '22 at 08:23
  • I don't know Pine Script, yet I found the answer to a [question with similar error message](https://stackoverflow.com/a/51731929/572834). The answer says you have to **indent by four spaces or one tab**. In your code above, the body of for loop is indented by three spaces instead of four. – Alexey Ivanov Feb 12 '22 at 12:53
  • In the edited version, you using **five spaces**. In the body of the loop, `strategy` should start at the same level as `x` in for loop on the previous line. – Alexey Ivanov Feb 12 '22 at 17:30
  • Note that the last two lines for strategy, `"BUY TP/SL" + tostring(x)` would produce string like `"BUY TP/SL1"`. You should use `"BUY" + tostring(x) + " TP/SL"` to make it the same as your original code. – Alexey Ivanov Feb 12 '22 at 17:32

1 Answers1

0

this is the way. I solved :)

for x = 1 to 20
    strategy.entry("BUY"+str.tostring(x), strategy.long, when=   PoseCount==x and PoseCount[1] !=x and barstate.isconfirmed, alert_message = message_long_entry)
    strategy.entry("SELL"+str.tostring(x) , strategy.short, when=   PoseCount==-x and PoseCount[1] !=-x  and barstate.isconfirmed, alert_message = message_short_entry)
    strategy.close("BUY"+str.tostring(x), when= PoseCount==-x and PoseCount[1] !=-x   and barstate.isconfirmed, alert_message = message_long_exit)
    strategy.close("SELL"+str.tostring(x), when= PoseCount==x and PoseCount[1] !=x  and barstate.isconfirmed, alert_message = message_short_exit)
    strategy.exit("BUY TP/SL"+str.tostring(x),  "BUY"+str.tostring(x),     profit = per(tp), loss = per(stoploss) , trail_points=per(trpo) , trail_offset=per(trof), alert_message = message_long_exit)
    strategy.exit("SELL TP/SL"+str.tostring(x),  "SELL"+str.tostring(x),    profit = per(tp), loss = per(stoploss) ,trail_points=per(trpo),trail_offset=per(trof), alert_message = message_short_exit)
anonimm
  • 28
  • 4
  • So the problem was with the indentation of for body, wasn't it? And [the following answer](https://stackoverflow.com/a/51731929/572834) resolved your problem. – Alexey Ivanov Feb 12 '22 at 17:46
  • 1
    Yes, and I used **str.tostring** instead of **tostring** . Thank you – anonimm Feb 12 '22 at 18:02