简体   繁体   中英

Try to write my strategy on trading view with Pine

I need a LONG or SHORT signal to appear under these conditions: -The LONG signal is given when the RSI is < 30 and k<20 and provided that the price is now above 100 sma. -The SHORT signal is given when the RSI is >70 and k>80 and provided that the price is now below 100 sma. Now in the sma code, finding the price is not taken into account for me, I ask for your help in this

This is my code:

//@version=4

strategy("Buy&Sell Strategy depends on AO+Stoch+RSI+ATR by SerdarYILMAZ", shorttitle="Buy&Sell Strategy")

//RSI

rsisource=input(title="rsi source",type=input.source,defval=close)
rsilength=input(title="rsi length",type=input.integer,defval=7)

rsi=rsi(rsisource,rsilength)

hline(70,color=color.orange)
hline(30,color=color.orange)

plot(rsi,color=color.orange)



//Stoch

K=input(title="K",type=input.integer,defval=14)
D=input(title="D",type=input.integer,defval=3)
smooth=input(title="smooth",type=input.integer,defval=3)

k=sma(stoch(close,high,low,K),D)
d=sma(k,smooth)

hline(80)
hline(20)


//ATR

atrlen=input(title="ATR Length", type=input.integer,defval=14)

atrvalue=rma(tr,atrlen)


LongCondition=k<20 and rsi<30
ShortCondition=k>80 and rsi>70
if (LongCondition)
    stoploss=low-atrvalue
    takeprofit=close+atrvalue
    strategy.entry("LONG", strategy.long)
    strategy.exit("TP/SL",stop=stoploss,limit=takeprofit)
    
if (ShortCondition)
    stoploss=high+atrvalue
    takeprofit=close-atrvalue
    strategy.entry("SHORT",strategy.short)
    strategy.exit("TP/SL",stop=stoploss,limit=takeprofit)

price is now above/below 100 sma

However you have not included any condition based on sma 100. Introduce the sma100 variable in the global scope and add it to the Long/ShortCondition, as is shown in the example below:

sma100 = sma(close, 100)

LongCondition=k<20 and rsi<30 and close > sma100
ShortCondition=k>80 and rsi>70 and close < sma100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM