Would love someones help with the below code. I’m trying to open multiple buy and sell orders when the following conditions are met:
For a long trade to be opened, RSI has to be above 50, Tenkan crosses over the Kijun, price is above kumo
and if an open long trade has been closed the entry has to be above the previous closed long’s price with all long conditions still valid.
A long trade can be closed either with a stop loss or take profit.
For a short trade to be opened, RSI has to be below 50, Tenkan crosses under the Kijun, price is below kumo and if an short long trade has been closed, the entry has to be below the previous closed short’s price with all short conditions still valid.
Currently on the daily time-frame it only triggers 2 trades.
Any help for a novice would be much appreciated. Thanks in advance.
//@version=4
//Ichimoku Inputs
ts_bars = input(9, minval=1, title="Tenkan-Sen Bars", group="Ichimoku Cloud")
ks_bars = input(26, minval=1, title="Kijun-Sen Bars", group="Ichimoku Cloud")
ssb_bars = input(52, minval=1, title="Senkou-Span B Bars", group="Ichimoku Cloud")
cs_offset = input(26, minval=1, title="Chikou-Span Offset", group="Ichimoku Cloud")
ss_offset = input(26, minval=1, title="Senkou-Span Offset", group="Ichimoku Cloud")
trade_long = input(true, title="Long Entry", group="Ichimoku Cloud")
trade_short = input(true, title="Short Entry", group="Ichimoku Cloud")
//Stop Loss Inputs
use_short_stop_loss = input(true, title="Short Stop Loss", group="Stop Loss and Take Profit", inline="Short_SL")
short_stop_loss = input(title="(%)", type=input.float, minval=0.0, step=0.1, defval=5, group="Stop Loss and Take Profit", inline="Short_SL") * 0.01
use_long_stop_loss = input(true, title="Long Stop Loss", group="Stop Loss and Take Profit", inline="Long_SL")
long_stop_loss = input(title="(%)", type=input.float, minval=0.0, step=0.1, defval=5, group="Stop Loss and Take Profit", inline="Long_SL") * 0.01
//Take Profit Inputs
use_short_take_profit = input(true, title="Short Take Profit", group="Stop Loss and Take Profit", inline="Short_TP")
short_take_profit = input(title="(%)", type=input.float, minval=0.0, step=0.1, defval = 20, group="Stop Loss and Take Profit", inline="Short_TP")* .01
use_long_take_profit = input(true, title="Long Take Profit", group="Stop Loss and Take Profit", inline="Long_TP")
long_take_profit = input(title="(%)", type=input.float, minval=0.0, step=0.1, defval = 20, group="Stop Loss and Take Profit", inline="Long_TP") * .01
//RSI Inputs
rsi_length = input(defval=14, title="RSI Period", type=input.integer, group="RSI")
mid_rsi = input(defval=50, title="Oversold Level", type=input.float, group="RSI")
//SuperTrend Inputs
atrPeriod = input(10,"ATR Length", minval = 1)
factor = input(3.0, "Factor",minval = 0.01, step = 0.01)
[supertrend, direction] = supertrend(factor, atrPeriod)
//Plot Supertrend
supertrend := barstate.isfirst ? na : supertrend
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.blue, style = lot.style_linebr)
downTrend = plot(direction > 0 ? na : supertrend, "Down Trend", color = color.orange, style = plot.style_linebr)
bodyMiddle = plot(barstate.isfirst ? na : (open + close) / 2, "Body Middle",display = display.none)
middle(len) => avg(lowest(len), highest(len))
//Ichimoku Components
tenkan = middle(ts_bars)
kijun = middle(ks_bars)
senkouA = avg(tenkan, kijun)
senkouB = middle(ssb_bars)
chikou = close[cs_offset]
// Plot Ichimoku Kinko Hyo
plot(tenkan, color=#0496ff, title="Tenkan-Sen")
plot(kijun, color=#991515, title="Kijun-Sen")
plot(chikou, color=#caeb36, title="Chikou-Span")
sa=plot(senkouA, offset=ss_offset, color=color.green, title="Senkou-Span A")
sb=plot(senkouB, offset=ss_offset, color=color.red, title="Senkou-Span B")
fill(sa, sb, color = senkouA > senkouB ? color.green : color.red, title="Cloud color")
ss_high = max(senkouA[ss_offset], senkouB[ss_offset])
ss_low = min(senkouA[ss_offset], senkouB[ss_offset])`
// Long and Short Signals
tk_cross_bull = crossover(tenkan, kijun)
tk_cross_bear = crossunder(tenkan, kijun)
rsi = rsi(close, rsi_length)
Long_supertrend = change(direction) < 0
Short_supertrend = change(direction) > 0
// Entry/Exit Confirmation
cs_cross_bull = mom(close, cs_offset) > 0
cs_cross_bear = mom(close, cs_offset) < 0
price_above_kumo = close > ss_high
price_below_kumo = close < ss_low
senkou_green = senkouA > senkouB ? true : false
// Get previous close price of the strategy
prev_close = strategy.position_size[1] > 0 ? strategy.position_avg_price[1] : close[1]
// Entry condition: price above previous close
Closed_long = close > prev_close
Closed_short = close < prev_close
//Entry Conditions
long_entry = tk_cross_bull and rsi > 50 and Long_supertrend and Closed_long and not in_long
short_entry = tk_cross_bear and rsi < 50 and Short_supertrend and Closed_short and not in_short
longStopPrice = strategy.position_avg_price * (1 - long_stop_loss)
shortStopPrice = strategy.position_avg_price * (1 + short_stop_loss)
longLimitPrice = strategy.position_avg_price * (1 + long_take_profit)
shortLimitPrice = strategy.position_avg_price * (1 - short_take_profit)
strategy.order("Long", strategy.long, when=long_entry)
strategy.order("Short", strategy.short, when=short_entry)
strategy.exit("Long", stop = longStopPrice, limit = longLimitPrice)
strategy.exit("Short", stop = shortStopPrice, limit = shortLimitPrice)
Mathew Axhorn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.