我要买多头头寸的条件是
在09:15到13:30之间下订单
最近蜡烛的开盘价大于EMA(20)
当价格超过最后7支蜡烛的高点时,我将做一个长期的打算
在下达了strategy.entry的买入订单后,我有三个目标(+ 20,+ 40,+ 60)和一个止损价(-20)。我想在以下情况下退出订单
- 以止损价(-20)卖出全部数量,或以目标1(+20)和Trail SL卖出25%的价格买入(0)
- 以买入价卖出剩余的75%(跟踪的SL)(0),或以目标2(+40)再卖出25%,然后将跟踪SL卖给目标1(+20)
- 以目标1(跟踪的SL)(+ 20)或目标3(+60)的价格最终出售50%
鉴于上述情况,请查看代码,并告诉我这是怎么回事?
//@version=4
strategy("BreakOutStrategy", overlay=true)
length = input(title="Length", type=input.integer, minval=1, maxval=1000, defval=7)
upBound = highest(high, length)
downBound = lowest(low, length)
timeFilter = (year == 2019) and (time > 0915 and time < 1330)
c1 = (open>ema(hl2,20))
buy_entry = (c1==true) and (timeFilter==true)
strategy.entry("buy", strategy.long, 4, stop=upBound + syminfo.mintick, when=buy_entry and strategy.position_size <= 0, comment="BUY")
strategy.exit("bracket1", "buy", stop=20, when=strategy.position_size==4)
strategy.exit("bracket2", "buy", 1, profit=20, when=strategy.position_size==4)
strategy.exit("bracket3", "buy", stop=0, when=strategy.position_size==3)
strategy.exit("bracket4", "buy", 1, profit=40, when=strategy.position_size==3)
strategy.exit("bracket5", "buy", 2, profit=60, stop=-20, when=strategy.position_size==2)
答案 0 :(得分:0)
让我通过您的代码澄清一下。
首先,time
返回自Unix纪元以来经过的毫秒数,因此从1970年1月1日开始,您的代码time > 0915
说,您希望时间从1970年开始大于915毫秒。因此,它不会随心所欲地工作。如果您想从9:15开始,那么您应该像年份那样做:
timeFilter = (year == 2019) and (hour > 9 or (hour == 9 and minute > 15)) and (hour < 13 or (hour == 13 and minute < 30))
接下来,关于追踪止损和零件出口,我举了一个小例子:
//@version=4
strategy("BreakOutStrategy", overlay=true)
if close < ema(close, 20)
strategy.entry("buy", strategy.long, qty = 4)
strategy.exit("exitId", "buy", trail_offset=200, trail_price=strategy.position_avg_price, profit=2000, qty_percent=25)
strategy.exit("exitId2", "buy", trail_offset=400, trail_price=strategy.position_avg_price, profit=4000, qty_percent=25)
strategy.exit("exitId3", "buy", trail_offset=800, trail_price=strategy.position_avg_price, profit=6000, qty_percent=50)
这并不是您想要的,但是我认为这足以开始。