我正在尝试在我的交易策略中设置止损(pinescript)

问题描述 投票:0回答:0

好吧,基本上我正在尝试找到该交易的最佳止损并执行该止损。

我使用3个指示器。 200 天 SMA、200 天 VWMA 和 55 Donchian 通道。并将止损设置为做最宽的。还可以使用 atr5 来确定该图形是否在该区域内。如果不是,我将 sl 设置为 atr5

//Stoploss

//---------------------------------------------------------------------------------------------------------------

atrValue = ta.atr(14)

StoplossFinder = input(5.0, "Stoploss Envelop", group="Stop Loss")

StoplossAtr = (close + ((ma_function(ta.tr(true), 200)) * StoplossFinder)) / close

var float diff_ma_vwma = 0.0

//var float long_stop_loss_vwma = 0.0

//var float short_stop_loss_vwma = 0.0

var float diff_ma_sma = 0.0

//var float long_stop_loss_ma = 0.0

//var float short_stop_loss_ma = 0.0

//FAKE VWMA 250

//-----------------------------------------------------------------------------------------------

Diff_ma_vwma = (Vwma- sma5) / sma5 * 100

var float long_stop_loss_vwma= na

if diff_ma_vwma < -atrValue * StoplossFinder

long_stop_loss_vwma := Vwma- atrValue

else if diff_ma_vwma >= -atrValue * StoplossFinder and diff_ma_vwma <= -atrValue * (StoplossFinder * 1.4)

long_stop_loss_vwma := Vwma



var float short_stop_loss_vwma = na

if diff_ma_vwma < atrValue * StoplossFinder

short_stop_loss_vwma := Vwma+ atrValue

else if diff_ma_vwma >= atrValue * StoplossFinder and diff_ma_vwma <= atrValue * (StoplossFinder * 1.4)

short_stop_loss_vwma := Vwma

//-----------------------------------------------------------------------------------------------

//SMA 200 //-----------------------------------------------------------------------------------------------

Diff_ma_sma = (sma200 - sma5) / sma5 * 100

var float long_stop_loss_ma = na

if diff_ma_sma >= -atrValue * StoplossFinder and diff_ma_sma <= atrValue * StoplossFinder

long_stop_loss_ma := sma200 - atrValue

else

long_stop_loss_ma := sma5 + atrValue * StoplossFinder



var float short_stop_loss_ma = na

if diff_ma_sma >= -atrValue * StoplossFinder and diff_ma_sma <= atrValue * StoplossFinder

short_stop_loss_ma := sma200 + atrValue

else

short_stop_loss_ma := sma5 + atrValue * StoplossFinder

//-----------------------------------------------------------------------------------------------

//Donchian 55

//-----------------------------------------------------------------------------------------------

lengthDonchian = input(55, title="Donchian Channel Length", group="Stop Loss")

DonchianLimitAtr = input(7.5, title="Donchian Limit Atr", group="Stop Loss")

donchianHigh = ta.highest(high, lengthDonchian)

donchianLow = ta.lowest(low, lengthDonchian)

long_stop_loss_dc = ta.valuewhen(donchianHigh < (sma5 - atrValue * DonchianLimitAtr) and donchianLow < (sma5 - atrValue * DonchianLimitAtr), donchianLow, 0)

short_stop_loss_dc = ta.valuewhen(donchianLow > (sma5 + atrValue * DonchianLimitAtr) and donchianHigh > (sma5 + atrValue * DonchianLimitAtr), donchianHigh, 0)

//-----------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------

long_stop_loss = long_stop_loss_dc if long_stop_loss_ma > long_stop_loss long_stop_loss := long_stop_loss_ma if long_stop_loss_vwma > long_stop_loss long_stop_loss := long_stop_loss_vwma

short_stop_loss = short_stop_loss_dc if short_stop_loss_ma < short_stop_loss short_stop_loss := short_stop_loss_ma if short_stop_loss_vwma < short_stop_loss short_stop_loss := short_stop_loss_vwma

//---------------------------------------------------------------------------------------------------------------

strategy.exit("Long Exit", "Long", loss=long_stop_loss)

strategy.exit("Short Exit", "Short", loss=short_stop_loss)

我对这个脚本编写方面很陌生,所以我没有尝试任何东西。但我向 Chat.gpt 寻求帮助,但效果不佳。

pine-script pine-script-v5 algorithmic-trading
© www.soinside.com 2019 - 2024. All rights reserved.