将策略转化为对 Pine Script 的研究

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

我正在尝试将策略转化为研究,以便使用在经纪人平台(bybit)上自动下订单的机器人。

这里是完整的代码:

//@version=5
study("Breakout Study",overlay=false)
 
// Define the length of the Bollinger Bands
bbLength = input.int (15,title="BBLength")
bbDev = input.float (2.0,title="BBDev")
 
trendfilterperiod=input(130,title="Trend Filter MA Period")
trendfiltertype=input.string (title="Trend Filter MA Type", defval="EMA",options=["EMA","SMA"])
 
volatilityfilter=input.bool(true,title="Volatility Filter")
volatilitystdevlength=input(20,title="Vol Filter STDev Length")
volatilitystdevmalength=input(30,title="Vol Filter STDev MA Length")

 
TrendConditionL=if trendfiltertype =="EMA"
    close>ta.ema(close,trendfilterperiod)
else
    close>ta.sma(close,trendfilterperiod)
 
TrendConditionS=if trendfiltertype =="EMA"
    close<ta.ema(close,trendfilterperiod)
else
    close<ta.sma(close,trendfilterperiod)
 
VolatilityCondition=if volatilityfilter
    ta.stdev(close,volatilitystdevlength)>ta.sma(ta.stdev(close,volatilitystdevlength),volatilitystdevmalength)
else 
    true
 

// Calculate the Bollinger Bands
[middle, upper, lower] = ta.bb (close, bbLength, bbDev)
 
// Buy if price crosses below upper Bollinger Band
if ta.crossover(close, upper)and TrendConditionL and VolatilityCondition and time > start and time < end
    strategy.entry("Long", strategy.long)
 
if ta.crossunder(close, lower)and TrendConditionS and VolatilityCondition and time > start and time < end
    strategy.entry("Short", strategy.short)
 
// Sell if price crosses above lower Bollinger Band
 
strategy.close("Long",when=ta.crossunder(close, lower))
 
strategy.close("Short",when=ta.crossover(close, upper))
 
// Plot the Bollinger Bands
plot(upper, color = color.red, linewidth = 2)
plot(lower, color = color.blue, linewidth = 2)
plot (ta.ema(close,trendfilterperiod))
plot (ta.sma(close,trendfilterperiod),color=color.yellow)
 

//definizione variabili posizioni aperte
 
IsLongOpen = false
IsLongOpen := ConditionEntryL[1] ? true : ConditionExitL[1] ? false : IsLongOpen[1]
 
IsShortOpen = false
IsShortOpen := ConditionEntryS[1] ? true : ConditionExitS[1] ? false : IsShortOpen [1]
 
IsFlat = true
IsFlat := not IsLongOpen and not IsShortOpen
 
//conversione bool -> float, per debug
 
IsLongOpenFloat = if IsLongOpen == true
    1
else
    0
 
IsShortOpenFloat = if IsShortOpen == true
    1
else
    0
 
IsFlatFloat = if IsFlat == true
    1
else
    0
 
//plot posizioni aperte, per debug
 
//plot (IsLongOpenFloat)
//plot (IsShortOpenFloat,color=color.yellow)
//plot (IsFlatFloat,color=color.red)
 
//variabili apertura e chiusura posizione
 
OpenLong = ConditionEntryL and not IsLongOpen
CloseLong = ConditionExitL and IsLongOpen
 
OpenShort = ConditionEntryS and not IsShortOpen
CloseShort = ConditionExitS and IsShortOpen
 
//conversione bool -> float, per debug
 
OpenShortFloat = if OpenShort == true
    1
else
    0
 
CloseShortFloat = if CloseShort == true
    1
else
    0
 
OpenLongFloat = if OpenLong == true
    1
else
    0
 
CloseLongFloat = if CloseLong == true
    1
else
    0
 
//plot aperture ordini, per debug
 
//plot (OpenShortFloat,color=color.blue)
//plot (CloseShortFloat,color=color.white)
//plot (OpenLongFloat,color=color.yellow)
//plot (CloseLongFloat,color=color.red)
 
//alert
 
alertcondition(OpenLong,title="Open Long")
alertcondition(CloseLong,title="Close Long")
alertcondition(OpenShort,title="Open Short")
alertcondition(CloseShort,title="Close Short")

这是我无法转换的代码部分:

TrendConditionL=if trendfiltertype =="EMA"
    close>ta.ema(close,trendfilterperiod)
else
    close>ta.sma(close,trendfilterperiod)
 
TrendConditionS=if trendfiltertype =="EMA"
    close<ta.ema(close,trendfilterperiod)
else
    close<ta.sma(close,trendfilterperiod)
 
VolatilityCondition=if volatilityfilter
    ta.stdev(close,volatilitystdevlength)>ta.sma(ta.stdev(close,volatilitystdevlength),volatilitystdevmalength)
else 
    true
 

// Calculate the Bollinger Bands
[middle, upper, lower] = ta.bb (close, bbLength, bbDev)
 
// Buy if price crosses below upper Bollinger Band
if ta.crossover(close, upper)and TrendConditionL and VolatilityCondition and time > start and time < end
    strategy.entry("Long", strategy.long)
 
if ta.crossunder(close, lower)and TrendConditionS and VolatilityCondition and time > start and time < end
    strategy.entry("Short", strategy.short)
 
// Sell if price crosses above lower Bollinger Band
 
strategy.close("Long",when=ta.crossunder(close, lower))
 
strategy.close("Short",when=ta.crossover(close, upper))

有人可以帮助我吗? 当然,如果您发现其他地方不正确,请让我知道应该更改什么。

bots trading
© www.soinside.com 2019 - 2024. All rights reserved.