'Shortening code with a for loop is possible?
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)
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | anonimm |
