如何在策略中修改这项研究

时间:2021-02-26 08:01:31

标签: pine-script

我正在尝试将下面的脚本从研究转换为策略。

//@version=4
study("Alerts MACD of RSI", overlay=true)

//////////////////////// RSI ///////////////////////////
src = close, len = input(14, minval=1, title="Length")
up = sma(max(change(src), 0), len)
down = sma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
//////////////////////// RSI   //////////////////////////

//////////////// MACD  ////////////////////////////
sourcemacd = rsi 
fastLength = input(12, minval=1), slowLength=input(26,minval=1)
signalLength=input(9,minval=1)
fastMA = ema(sourcemacd, fastLength)
slowMA = ema(sourcemacd, slowLength)
macd = fastMA - slowMA
signal = ema(macd, signalLength)
delta=macd-signal
//swap1 = delta>0?green:red
//plot(delta,color=swap1,style=columns,title='Histo',histbase=0,transp=20)
//p1 = plot(macd,color=blue,title='MACD Line')
//p2 = plot(signal,color=red,title='Signal')
//fill(p1, p2, color=blue)
//hline(0)
/////////////////////////MACD  //////////////////////////
// Conditions
longCondition =  crossover(delta,0)
shortCondition =  crossunder(delta,0)
    
plotshape(longCondition , text='!BUY!', color=#24a103)
plotshape(shortCondition , text='!SELL!', color=#d00b27)

ConditionEntryL=crossover(delta,0)
ConditionEntryS=crossunder(delta,0)

ConditionExitL=crossunder(delta,0)
ConditionExitS=crossover(delta,0)

//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
 
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
  
//alert
 
alertcondition(OpenLong,title="Open Long")
alertcondition(CloseLong,title="Close Long")
alertcondition(OpenShort,title="Open Short")
alertcondition(CloseShort,title="Close Short")

我所有的转换尝试都报告了不同的买入/卖出信号,我不明白为什么。 我尝试将其转换如下。我还添加了一个简单的时间控制,但我认为这与错误的买入/卖出信号无关。

//@version=4
strategy(title = "Strategy MACD of RSI", overlay = false)

//////////////////////// RSI ///////////////////////////

src = close, len = input(14, minval=1, title="Length")
up = sma(max(change(src), 0), len)
down = sma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))


//////////////////////// RSI   //////////////////////////

//////////////// MACD  ////////////////////////////

sourcemacd = rsi 

fastLength = input(12, minval=1), slowLength=input(26,minval=1)
signalLength=input(9,minval=1)


fastMA = ema(sourcemacd, fastLength)
slowMA = ema(sourcemacd, slowLength)

macd = fastMA - slowMA
signal = ema(macd, signalLength)
delta=macd-signal

//swap1 = delta>0?green:red

//plot(delta,color=swap1,style=columns,title='Histo',histbase=0,transp=20)
//p1 = plot(macd,color=blue,title='MACD Line')
//p2 = plot(signal,color=red,title='Signal')
//fill(p1, p2, color=blue)
//hline(0)




/////////////////////////MACD  //////////////////////////
// Conditions

longCond =  crossover(delta,0)
sellCond =  crossunder(delta,0)


monthfrom =input(1)
monthuntil =input(12)
dayfrom=input(1)
dayuntil=input(31)

if (  longCond    and  month>=monthfrom and month <=monthuntil and dayofmonth>=dayfrom and dayofmonth < dayuntil) 
    strategy.entry("BUY", strategy.long, stop=close, oca_name="TREND", oca_type=strategy.oca.cancel, comment="BUY")
    
else
    strategy.cancel(id="BUY")

if ( sellCond   and month>=monthfrom and month <=monthuntil and dayofmonth>=dayfrom and dayofmonth < dayuntil ) 

    strategy.close("BUY")

谁能帮我正确转换?

0 个答案:

没有答案