Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

//@version=5
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

// Andean Oscillator settings
length     = input(50)
sig_length = input(50,'Signal Length')

// ADX settings
adxlen = input(14, title="ADX Smoothing")
dilen = input(14, title="DI Length")

// Take profit level
takeProfitLevel = input(1.015, title="Take Profit Level")

// Andean Oscillator calculations
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)

// ADX calculations
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)

// Entry conditions
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)

// Stop loss levels
stopLevelLong = ta.lowest(low, 2) // For long positions, stop loss is set at the lowest low of the previous 2 candles
stopLevelShort = ta.highest(high, 2) // For short positions, stop loss is set at the highest high of the previous 2 candles

// Plot stop levels when a new position is opened
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")

// Exit conditions with stop loss
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.
Posted
Comments
[no name] 6-Dec-23 17:23pm    
You "log" all the "numbers" (while testing) so you can confirm what is, or is not happening. It's obviously "data driven"; which no one else can see.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900