My goal in writing the code is to backtest my trading strategy using the Ichi. When the conditions to go long have appeared an entry position will be opened. Immediately the stop loss is the cloud support level underneath that candle while the profit is 2x the risk as described in the picture. Since the cloud levels (leadLine 1 and leadLine2) are continuously changing I cannot set a fixed level which results in the trade being closed almost immediately after it opened. I don't know how to make it fixed on that position. I've tried several simple logics but so far any attempts have failed. Any idea how to make this will be highly appreciated.
Code: //@version=4
strategy(title=Ichimoku Cloud
, shorttitle=RISK REWARD Ichimoku
, overlay=true, initial_capital=1000, default_qty_value=100, default_qty_type=strategy.percent_of_equity)
conversionPeriods = input(20, minval=1, title=Conversion Line Length
)
basePeriods = input(60, minval=1, title=Base Line Length
)
laggingSpan2Periods = input(120, minval=1, title=Leading Span B Length
)
displacement = input(30, minval=1, title=Displacement
)
Stoploss = input(1, minval=0.1, title=Stoploss (% below cloud)
)
donchian(len) => avg(lowest(len), highest(len))
conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)
leadLine1 = avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)
plot(conversionLine, color=#2962FF, title=Conversion Line
)
plot(baseLine, color=#B71C1C, title=Base Line
)
plot(close, offset = -displacement + 1, color=#43A047, title=Lagging Span
)
p1 = plot(leadLine1, offset = displacement - 1, color=#A5D6A7,
title=Leading Span A
)
p2 = plot(leadLine2, offset = displacement - 1, color=#EF9A9A,
title=Leading Span B
)
fill(p1, p2, color = leadLine1 > leadLine2 ? color.rgb(67, 160, 71, 90) : color.rgb(244, 67, 54, 90))
bool TKcross = conversionLine > baseLine
bool aboveCloud = close > leadLine1 and close > leadLine2
bool greenCloud = leadLine1 > leadLine2
bool lagLong = close > leadLine1[2*displacement+1] and close > leadLine2[2*displacement+1] and close > close[displacement]
bool longCondition = false
bool close_trade = crossover(leadLine1[displacement], close) or crossover (leadLine2[displacement], close) or close < close[displacement] or crossover(baseLine, close)
var position_count = 0
var risk = 0
var reward= 0
if (TKcross and aboveCloud and greenCloud and lagLong and position_count==0)
strategy.entry(id=buy
, long=true)
if (leadLine1[displacement] > leadLine2 [displacement])
risk = leadLine2[displacement]
reward = strategy.position_avg_price*(1+(1-risk/strategy.position_avg_price))
if (leadLine2[displacement] > leadLine1 [displacement])
risk = leadLine1[displacement]
reward = strategy.position_avg_price*(1+(1-risk/strategy.position_avg_price))
position_count = 1
if (close >= reward or close <= risk)
strategy.close_all()
position_count = 0
plot(risk, color=#1462FF, title=Risk
)
plot(reward, color=#2342FF, title=Reward
)