Pine脚本-当价格比前一个多头价格高2%时输入下一个头寸

时间:2019-01-30 16:59:14

标签: pine-script

我正在尝试修改我的简单脚本,即价格超过200 MA时买入头寸,价格超过200 EMA时卖出头寸。

我想添加金字塔: 最多5个职位-每个职位占总资本的20% 价格比前一个多头高2%时下一个头寸 当价格突破200 EMA时卖出所有头寸

@version=3
strategy("EMA200", shorttitle="EMA200", overlay=true, initial_capital=10000, pyramiding=1)

EMA=input(200, minval=1)
s=ema(close,EMA) // sma values stored in s1 and s2 variables
plot(s)
buy= close>s
sell= close<s
addToLong = (strategy.position_size > 0) and strategy.position_avg_price +
               strategy.position_avg_price*0.02 //close > high[1]
ordersize=floor(strategy.equity/close) // To dynamically calculate the order size as the account equity increases or decreases.
strategy.entry("long",strategy.long,ordersize,when=buy) // Buys when buy condition met
strategy.order(id="Additional Long", when = addToLong, long=true)
strategy.close("long", when = sell ) // Closes position when sell condition me 

1 个答案:

答案 0 :(得分:0)

//@version=3
strategy("EMA200", shorttitle="EMA200", overlay=true, initial_capital=10000, pyramiding=5, default_qty_type=strategy.percent_of_equity, default_qty_value=20)

EMA=input(200, minval=1)
s=ema(close,EMA) // sma values stored in s1 and s2 variables
plot(s)
sell= crossunder(close, s)
buy= (strategy.position_size > 0) ? (strategy.position_avg_price*1.02 < close) : crossover(close, s)

strategy.entry("long",strategy.long,when=buy) // Buys when buy condition met
strategy.close("long", when = sell ) // Closes position when sell condition me

我想这与您想要的类似。