0

我在 pinescript 上的代码正在测试一种策略,如果一堆快速 EMA 越过一堆慢速 EMAS,就会发出做多的信号,而如果发生相反的情况,则发出做空的信号。为了维持风险/回报,我决定添加合理的止损和止盈。如果我排除止损并获利,它工作得很好,如果我添加它们,我会继续进入第 30 行“不匹配的输入 'enterShort' 期待'行尾没有续行'。” 您可以在下面找到代码:

'''
//@版本=4

strategy(title="Green White Red Strategy" , overlay=true)

// defining the moving averages 


maa = ema(close, 3)
mab = ema(close, 5)
mac= ema(close, 8)
mad= ema(close, 12)
mae = ema(close, 15)
maf = ema(close, 30)
mag = ema(close, 35)
mah = ema(close, 40)
mai = ema(close, 45)
maj = ema(close, 50)
mak = ema(close, 60)


// if the short term MAs crossover the long term MAs then we enter a long position and close 
(if true) the short position


enterLong=if maa > maf and mab > mag and mac > mah and mad>mai and mae > maj and mae > mak and 
not nz(enterLong[1], 0)

// if the long term MAs crossover the short term MAs then we enter a short position and close 
(if true) the long position


enterShort= if maa < maf and mab < mag and mac < mah and mad < mai and mae < maj and mae < mak 
and not nz(enterLong[1], 0)

//plotting the MAs


plot(maa, color = #00FF00,title="3 EMA", linewidth=1)

plot(mab, color = #00FF00, title="5 EMA", linewidth=1)

plot(mac, color = #00FF00,title="8 EMA", linewidth=1)

plot(mad, color = #00FF00, title="12 EMA", linewidth=1)

plot(mae, color = #00FF00,title="15 EMA", linewidth=1)

plot(maf, color = #FF0000, title="30 EMA", linewidth=1)

plot(mag, color = #FF0000,title="35 EMA", linewidth=1)

plot(mah, color = #FF0000, title="40 EMA", linewidth=1)

plot(mai, color = #FF0000,title="45 EMA", linewidth=1)

plot(maj, color = #FF0000, title="50 EMA", linewidth=1)

plot(mak, color = #FF0000, title="50 EMA", linewidth=1)


// defining our stop loss and take profit
sl=0.34
tp=0.67


float longstop= strategy.position_avg_price*(1-sl)

float longprofit= strategy.position_avg_price*(1+tp)

float shortstop= strategy.position_avg_price*(1+sl)

float shortprofit= strategy.position_avg_price*(1-sl)

inpos=abs(strategy.position_avg_price)

// here what we are doing is basically a series of if statements: if the value of enterLong 
becomes 1 and we are not in a position then we command a long entry
// however if enterShort becomes 1 and we are already in a longposition then we close the long 
position
// if entershort becomes 1 and we were not in a position then we directly enter a short 
position
// finally if we were in a position and either entershort or enter long were 1
x=if enterLong and inpos==0

    strategy.entry(id="Enter Long", long=strategy.long)

    strategy.exit(id= 'close',stop=longstop, limit=longprofit)


else if enterShort and inpos>1

    strategy.exit(id='close', stop=true)


else if enterShort and inpos==0

    strategy.entry(id="Enter Short", long=strategy.short)

    strategy.exit(id= 'close',stop=shortstop, limit=shortprofit)


else if enterLong and inpos>0

    strategy.exit(id='close', stop=true)'''
4

0 回答 0