简体   繁体   English

固定止盈和止损的问题

[英]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.该策略的目标是检测 ADX/DI 何时高于用户选择的过滤器,EMA 何时高于用户选择的多头过滤器和低于用户选择的空头过滤器。

For example, User selects ADX/DI filter to be 35 and EMA filter to be 29.例如,用户选择 ADX/DI 过滤器为 35,EMA 过滤器为 29。

For a Long Trade If ADX and DI+ is over 35 and price closes above EMA 29 then long trade will be opened.对于多头交易 如果 ADX 和 DI+ 超过 35 且价格收盘价高于 EMA 29,则将开启多头交易。 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.对于空头交易 如果 ADX 和 DI- 高于 35 且价格收于 EMA 29 以下,则空头交易将被打开。 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:如果您想要 3.5% TP 和 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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