如何在一天结束时或在高于先前高点的X利润处关闭交易?

时间:2019-09-23 12:30:45

标签: pine-script

我正在尝试整理一个松散的脚本来进行交易视图,我几乎完成了,但是我不确定如何在一天结束时(11:59 PM)结束交易),或者当当前价格等于前一天的蜡烛高点时(一天中蜡烛的高价[1] + X点)。

这是我到目前为止所拥有的:

//@version=2
strategy("Previous Day High and Low Breakout Strategy", overlay=true)
D_High = security(tickerid, 'D', high[1]) 
D_Low = security(tickerid, 'D', low[1]) 
D_Close =  security(tickerid, 'D', close[1]) 
D_Open =  security(tickerid, 'D', open[1]) 

// Go Long - if prev day high is broken and stop loss prev day low
// Go Short - if prev day low is broken and stop loss prev day high
plot(isintraday ? D_High : na, title="Daily High",style=line, color=blue,linewidth=2) 
plot(isintraday ? D_Low : na, title="Daily Low",style=line, color=red,linewidth=2) 

signal =crossover(high,D_High) ? true : crossover(D_Low,low) ? false : signal[1]

longCondition = signal
if (longCondition)
    strategy.entry("Long", strategy.long)

shortCondition = signal != true
if (shortCondition)
    strategy.entry("Short", strategy.short)

2 个答案:

答案 0 :(得分:0)

退出之前的high如下:

//@version=4
strategy("My Strategy", overlay=true)

DESIRED_PROFIT_TICKS = 5

exitPrice = 0.0
exitPrice := exitPrice[1]

longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)
    exitPrice := (high[1] - close) / syminfo.mintick + DESIRED_PROFIT_TICKS

strategy.exit("ExitId", "My Long Entry Id", profit = exitPrice)

但是我怀疑是否有办法在一天结束时退出,因为您无法确定这笔交易是最后一笔还是相反交易-我们跳过这些交易,希望过一会儿就退出,但是有机会直到一天结束都没有交易。

答案 1 :(得分:0)

我觉得你可以这样做,因为我是这样做的,反正有问题评论告诉我。

tomorrow=0
tomorrow:=strategy.opentrades==1 ? time : 0
strategy.close("long", when=tomorrow>0)