【问题标题】:Pine script - enter next position when price is 2% higher than previous longPine 脚本 - 当价格比上一个多头高 2% 时进入下一个位置
【发布时间】:2019-01-30 16:59:13
【问题描述】:

我正在尝试修改我的简单脚本,即在价格交叉 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 

【问题讨论】:

  • when price crossover 200 MA 你的意思是 200 EMA?

标签: pine-script


【解决方案1】:
//@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

我想这与您想要的相似。

【讨论】:

  • 我对其进行了测试,但它不能 100% 正确工作。我看到的是第一个多头后的每个头寸(头寸 2-5)应该比之前的头寸高出至少 2%。所以它应该是这样的:100、102、105、108、113,有时就像:100、106、105、107、110
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多