I have written a pinescript strategy, it compiles fine and looks like it is entering the trade correctly as all the conditions seem to be met. However, the trade is exiting when no conditions are met. The exit conditions are just the opposite of the entry and i set a label to show where my stop loss should be and it is definitely not exiting at that point. It seems all the strategies I create tend to exit the trades to quickly. Maybe there is just a setting I am missing in tradeviews somewhere? Can anyone help me figure out what I may be doing wrong?
strategy("ADX/Andean Strategy - phelix", shorttitle="ADX/Andean - p", overlay=true, initial_capital=250, commission_value=0.1, currency="USD", calc_on_every_tick=false, pyramiding=1, calc_on_order_fills=false, commission_type=strategy.commission.percent, margin_long=0, precision=8)
crossover(a, b) =>
a[1] < b[1] and a > b
crossunder(a, b) =>
a[1] > b[1] and a < b
length = input(50)
sig_length = input(50,'Signal Length')
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")
takeProfitLevel = input(1.015, title="Take Profit Level")
var alpha = 2/(length+1)
var up1 = 0.,var up2 = 0.
var dn1 = 0.,var dn2 = 0.
C = close
O = open
up1 := nz(math.max(C, O, up1[1] - (up1[1] - C) * alpha), C)
up2 := nz(math.max(C * C, O * O, up2[1] - (up2[1] - C * C) * alpha), C * C)
dn1 := nz(math.min(C, O, dn1[1] + (C - dn1[1]) * alpha), C)
dn2 := nz(math.min(C * C, O * O, dn2[1] + (C * C - dn2[1]) * alpha), C * C)
bull = math.sqrt(dn2 - dn1 * dn1)
bear = math.sqrt(up2 - up1 * up1)
signal = ta.ema(math.max(bull, bear), sig_length)
dirmov(len) =>
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
truerange = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / truerange)
minus = fixnan(100 * ta.rma(minusDM, len) / truerange)
[plus, minus]
adx(dilen, adxlen) =>
[plus, minus] = dirmov(dilen)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
sig = adx(dilen, adxlen)
longCondition = crossover(bull, signal) and bear == 0 and sig > 20
if (longCondition)
strategy.entry("Buy", strategy.long)
shortCondition = crossunder(bear, signal) and bull == 0 and sig > 20
if (shortCondition)
strategy.entry("Sell", strategy.short)
stopLevelLong = ta.lowest(low, 2)
stopLevelShort = ta.highest(high, 2)
plotshape(series=longCondition, location=location.belowbar, color=color.red, style=shape.labeldown, text="Stop Level Long")
plotshape(series=shortCondition, location=location.abovebar, color=color.green, style=shape.labelup, text="Stop Level Short")
strategy.exit("Exit Long", "Buy", stop=stopLevelLong, limit=close*takeProfitLevel)
strategy.exit("Exit Short", "Sell", stop=stopLevelShort, limit=close/takeProfitLevel)
What I have tried:
I have set a label to where the stop loss should be at. It is not closing from there. I can't figure out what is causing this to exit.