首页 > 解决方案 > 尝试用 Pine 编写我的交易视图策略

问题描述

在这些条件下,我需要一个做多或做空信号: - 当 RSI < 30 和 k<20 并且假设价格现在高于 100 SMA 时,给出做多信号。- 当 RSI > 70 且 k > 80 并且价格现在低于 100 SMA 时,会发出 SHORT 信号。现在在sma代码中,我没有考虑到价格,我请求你的帮助

这是我的代码:

//@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)

标签: pine-scripttradingview-api

解决方案


价格现在高于/低于 100 SMA

但是,您没有包含任何基于 sma 100 的条件。在全局范围内引入 sma100 变量并将其添加到 Long/ShortCondition,如下例所示:

sma100 = sma(close, 100)

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

推荐阅读