简体   繁体   中英

Issue with Fixed Take Profit and Stop Loss

I'm very new to coding and am hoping someone can assist me with the strategy code below. The goal of the strategy is to detect when the ADX/DI is above the user selected filter and the EMA is above the user selected filter for longs and below the user selected filter for shorts.

For example, User selects ADX/DI filter to be 35 and EMA filter to be 29.

For a Long Trade If ADX and DI+ is over 35 and price closes above EMA 29 then long trade will be opened. Long trade will close when the user defined fixed take profit or stop loss is hit or when a Short Trade is triggered

For a short trade If the ADX and DI- is above 35 and price closes below EMA 29 then the short trade will be opened. Short Trade will close when the user defined fixed take profit or stop loss is hit or when a Long Trade is triggered

The issue is I am not able to get the Take Profit and Stop Loss to work in the code. Would someone be able to assist?

//@version=5
strategy(title="Strategy Testing", overlay=true)

// Get User Inputs
adxSmoothing        = input.int(14, title="ADX Smoothing", minval=1, maxval=50)
diLength            = input.int(14, minval=1, title="DI Length")
adxThreshhold       = input.int(title="ADX Threshhold",defval=30)
emaFilter           = input.bool(title="EMA Filter?", defval=false, group="EMA")
emaInput            = input.int(title="EMA",defval=29,minval=5, maxval=52,tooltip="EMA Length",group="EMA")
takeProfit          = input.float(title="Take Profit",defval=1.5,minval=.5,maxval=3.5,step=.01,group="Take Profit and Stop Loss")
stopLoss            = input.float(title="Stop Loss", defval=.75, minval=.15,maxval=2.25,step=.01,group="Take Profit and Stop Loss")

// ADX Value
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)
trur    = ta.rma(ta.tr, diLength)
plus    = fixnan(100 * ta.rma(plusDM, diLength) / trur)
minus   = fixnan(100 * ta.rma(minusDM, diLength) / trur)
sum     = plus + minus
adx     = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxSmoothing)

ADXBuySignal    = plus  > adxThreshhold
ADXSellSignal   = minus > adxThreshhold
ADXTotal        = adx   > adxThreshhold

// EMA Value
ema         = ta.ema(close,emaInput)
emaabove    = low > ema or low[1] > ema[1] 
emabelow    = high < ema or high[1] < ema[1] 


// Enter Buy Order
longCondition = ADXBuySignal and ADXTotal and emaabove
if longCondition
    strategy.entry(id="Long", direction=strategy.long)
    
//Long Take Profit and Stop Loss
longTakeProfit  = strategy.position_avg_price*(1+takeProfit)
longStopLoss    = strategy.position_avg_price*(1-stopLoss)
    
// Manage Buy Exit Order
if strategy.position_avg_price>0
    strategy.exit(id = "Long Exit",from_entry="Long", limit=longTakeProfit,stop=longStopLoss, when=strategy.position_size > 0)
    
// Enter Sell Order
shortCondition = ADXSellSignal and ADXTotal and emabelow
if shortCondition
    strategy.entry(id="Sell", direction=strategy.short)

// Short Take Profit and Stop Loss    
shortTakeProfit = strategy.position_avg_price*(1-takeProfit)
shortStopLoss   = strategy.position_avg_price*(1+stopLoss) 

// Manage Sell Exit Order
if strategy.position_avg_price<0
    strategy.exit(id = "Short Exit",from_entry="Sell", limit=shortTakeProfit,stop=shortStopLoss, when=strategy.position_size < 0)    
    

// Draw Take Profit and Stops
plot(strategy.position_avg_price != 0 ? longTakeProfit : na, color=color.green, style=plot.style_linebr, title="Long Take Profit")
plot(strategy.position_avg_price != 0 ? longStopLoss : na, color=color.red, style=plot.style_linebr, title="Long Stop Loss")
plot(strategy.position_avg_price != 0 ? shortTakeProfit : na, color=color.green, style=plot.style_linebr, title="Short Take Profit")
plot(strategy.position_avg_price != 0 ? shortStopLoss : na, color=color.red, style=plot.style_linebr, title="Short Stop Loss")

Screenshot of Trading View Chart

If you want 3.5% TP and 2.25% SL:

//@version=5
strategy(title="Strategy Testing", overlay=true)

// Get User Inputs
adxSmoothing        = input.int(14, title="ADX Smoothing", minval=1, maxval=50)
diLength            = input.int(14, minval=1, title="DI Length")
adxThreshhold       = input.int(title="ADX Threshhold",defval=30)
emaFilter           = input.bool(title="EMA Filter?", defval=false, group="EMA")
emaInput            = input.int(title="EMA",defval=29,minval=5, maxval=52,tooltip="EMA Length",group="EMA")
takeProfit          = input.float(title="Take Profit %",defval=3.5,minval=.01,maxval=100,step=.01,group="Take Profit and Stop Loss")/100
stopLoss            = input.float(title="Stop Loss %", defval=2.25, minval=.01,maxval=100,step=.01,group="Take Profit and Stop Loss")/100

// ADX Value
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)
trur    = ta.rma(ta.tr, diLength)
plus    = fixnan(100 * ta.rma(plusDM, diLength) / trur)
minus   = fixnan(100 * ta.rma(minusDM, diLength) / trur)
sum     = plus + minus
adx     = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxSmoothing)

ADXBuySignal    = plus  > adxThreshhold
ADXSellSignal   = minus > adxThreshhold
ADXTotal        = adx   > adxThreshhold

// EMA Value
ema         = ta.ema(close,emaInput)
emaabove    = low > ema or low[1] > ema[1] 
emabelow    = high < ema or high[1] < ema[1] 


// Enter Buy Order
longCondition = ADXBuySignal and ADXTotal and emaabove
if longCondition
    strategy.entry(id="Long", direction=strategy.long)
    
//Long Take Profit and Stop Loss
longTakeProfit  = strategy.position_avg_price*(1+takeProfit)
longStopLoss    = strategy.position_avg_price*(1-stopLoss)
    
// Manage Buy Exit Order
if strategy.position_avg_price>0
    strategy.exit(id = "Long Exit",from_entry="Long", limit=longTakeProfit,stop=longStopLoss, when=strategy.position_size > 0)
    
// Enter Sell Order
shortCondition = ADXSellSignal and ADXTotal and emabelow
if shortCondition
    strategy.entry(id="Sell", direction=strategy.short)

// Short Take Profit and Stop Loss    
shortTakeProfit = strategy.position_avg_price*(1-takeProfit)
shortStopLoss   = strategy.position_avg_price*(1+stopLoss) 

// Manage Sell Exit Order
if strategy.position_avg_price<0
    strategy.exit(id = "Short Exit",from_entry="Sell", limit=shortTakeProfit,stop=shortStopLoss, when=strategy.position_size < 0)    
    

// Draw Take Profit and Stops
plot(strategy.position_avg_price != 0 ? longTakeProfit : na, color=color.green, style=plot.style_linebr, title="Long Take Profit")
plot(strategy.position_avg_price != 0 ? longStopLoss : na, color=color.red, style=plot.style_linebr, title="Long Stop Loss")
plot(strategy.position_avg_price != 0 ? shortTakeProfit : na, color=color.green, style=plot.style_linebr, title="Short Take Profit")
plot(strategy.position_avg_price != 0 ? shortStopLoss : na, color=color.red, style=plot.style_linebr, title="Short Stop Loss")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM