I'm having problem figuring out why my strategy opens trade to opposite direction when opposite direction condition is met (not exit condition) and it instantly closes its position.
Please kindly advise.
Code: Select all
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Locomoco_Trader
//@version=4
strategy("EMA+PSAR+HeikinAshi", overlay=true, initial_capital = 1000000, currency = "JPY", commission_type=strategy.commission.cash_per_contract, commission_value=0.003, default_qty_type = strategy.fixed, default_qty_value = 10000)
//inputs
EMAsource = input(title="EMA Source", type=input.source, defval=close)
EMAlen = input(title="EMA Length", type=input.integer, defval=200)
PSARstart = input(title="PSAR Start", type=input.float, defval=0.02)
PSARinc = input(title="PSAR Increment", type=input.float, defval=0.02)
PSARmax = input(title="PSAR Max Value", type=input.float, defval=0.2)
//indicators
EMA = ema(EMAsource, EMAlen)
PSAR = sar(PSARstart, PSARinc, PSARmax)
ATR = atr(7)
//setups
EMAlong = close > EMA
EMAshort = close < EMA
PSARlong = close > PSAR
PSARshort = close < PSAR
HA_open = security(heikinashi(syminfo.tickerid), timeframe.period, open)
HA_high = security(heikinashi(syminfo.tickerid), timeframe.period, high)
HA_low = security(heikinashi(syminfo.tickerid), timeframe.period, low)
HA_close = security(heikinashi(syminfo.tickerid), timeframe.period, close)
HA_green_5 = HA_close[5] > HA_open[5]
HA_red_5 = HA_close[5] < HA_open[5]
HA_green_4 = HA_close[4] > HA_open[4]
HA_red_4 = HA_close[4] < HA_open[4]
HA_green_3 = HA_close[3] > HA_open[3]
HA_red_3 = HA_close[3] < HA_open[3]
HA_green_2 = HA_close[2] > HA_open[2]
HA_red_2 = HA_close[2] < HA_open[2]
HA_green_1 = HA_close[1] > HA_open[1]
HA_red_1 = HA_close[1] < HA_open[1]
HA_up = (HA_green_5 and HA_green_4 and HA_green_3) or (HA_green_4 and HA_green_3 and HA_green_2) or (HA_green_3 and HA_green_2 and HA_green_1)
HA_down = (HA_red_5 and HA_red_4 and HA_red_3) or (HA_red_4 and HA_red_3 and HA_red_2) or (HA_red_3 and HA_red_2 and HA_red_1)
HA_no_bottom_shadow = HA_open == HA_low
HA_no_top_shadow = HA_open == HA_high
HAlong = HA_down and HA_no_bottom_shadow
HAshort = HA_up and HA_no_top_shadow
//entry conditions
longCondition = EMAlong and PSARlong and HAlong
shortCondition = EMAshort and PSARshort and HAshort
//entries
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
//closing conditions
var long_take_level = 0.0
var long_stop_level = 0.0
var short_take_level = 0.0
var short_stop_level = 0.0
if (strategy.position_size == 0)
long_take_level := (close - PSAR)*2 + close
long_stop_level := PSAR
short_take_level := close - (PSAR - close)*2
short_stop_level := PSAR
//close
strategy.exit("Long_Close", "Long", limit=long_take_level, stop=long_stop_level)
strategy.exit("Short_Close", "Short", limit=short_take_level, stop=short_stop_level)
//plots
plot(EMA, color=color.yellow, linewidth=2, title="EMA")
plot(PSAR, color=color.white, style=plot.style_circles, title="PSAR")
plot(strategy.position_size < 0 ? na : long_take_level[1], color=color.green, style=plot.style_linebr, linewidth=2)
plot(strategy.position_size < 0 ? na : long_stop_level[1], color=color.red, style=plot.style_linebr, linewidth=2)
plot(strategy.position_size > 0 ? na : short_take_level[1], color=color.green, style=plot.style_linebr, linewidth=2)
plot(strategy.position_size > 0 ? na : short_stop_level[1], color=color.red, style=plot.style_linebr, linewidth=2)