【发布时间】:2020-05-20 02:09:03
【问题描述】:
我正在尝试使用 pine 脚本来转换我的一些策略。 我在下面写了代码
//@version=4
strategy(title="MACD+EMA test", shorttitle="MACD+EMA")
// Getting inputs
fast_length = input(title="Fast Length", type=input.integer, defval=12)
slow_length = input(title="Slow Length", type=input.integer, defval=26)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)
ema_trend = input(title="EMA Trendline", type=input.integer, defval=200)
// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #ff6a00
// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal
trend = ema(src, ema_trend)
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )
plot(macd, title="MACD", color=col_macd, transp=0)
plot(signal, title="Signal", color=col_signal, transp=0)
// plot(trend, title="Trend")
LONG = close > trend and macd < 0 and signal < 0 and crossover(signal, macd)
//strategy.entry("LONG", strategy.long, when=LONG))
我想设置一个变量,比如说 LONG_SL 来输入策略退出,它应该如下工作:
当 strategy.entry("LONG" ...) 被触发时,LONG_SL 应设置为与close 和trend 的差值。随着时间的推移,该值应保持固定,直到strategy.exit trigger
对于 strategy.exit 我会使用类似的东西
strategy.exit("EXIT", "LONG", profit=1.5*LONG_SL, loss=LONG_SL)
提前致谢
【问题讨论】:
标签: pine-script