簡體   English   中英

固定止盈和止損的問題

[英]Issue with Fixed Take Profit and Stop Loss

我對編碼很陌生,希望有人能幫助我完成下面的策略代碼。 該策略的目標是檢測 ADX/DI 何時高於用戶選擇的過濾器,EMA 何時高於用戶選擇的多頭過濾器和低於用戶選擇的空頭過濾器。

例如,用戶選擇 ADX/DI 過濾器為 35,EMA 過濾器為 29。

對於多頭交易 如果 ADX 和 DI+ 超過 35 且價格收盤價高於 EMA 29,則將開啟多頭交易。 當觸及用戶定義的固定止盈或止損或觸發空頭交易時,多頭交易將關閉

對於空頭交易 如果 ADX 和 DI- 高於 35 且價格收於 EMA 29 以下,則空頭交易將被打開。 當觸及用戶定義的固定止盈或止損或觸發多頭交易時,空頭交易將關閉

問題是我無法在代碼中使用獲利和止損。 有人可以提供幫助嗎?

//@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")

交易視圖圖表截圖

如果您想要 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