目标 我正在尝试创建一个脚本,在该脚本中,当用户定义的交易量百分比相对于24小时交易量增加时,它会打开一个头寸,并使用用户定义的TP / SL来关闭该头寸。
示例 如果当前蜡烛的交易量比过去24小时的交易量增加3%,那么它将打开该仓位并以2%的追踪利润或-4%的止损平仓。
问题 它开设多头头寸,但由于某种原因,它没有在TP / SL上平仓,因此也没有开设其他头寸。
Click here to view the output that I'm getting
请帮助!
代码
//@version=3
//study("Intra-bar Volume", overlay=false)
strategy("Volume check", overlay=true)
//Input Time Frame and Bars (288 bars for 24hrs in 5mins time frame)
lower_tf = input("5", title='Lower Timeframe to Assess')
bars_in_tf = input(288, title='Bars of lower Timeframe')-1 // -1 because we count from zero in the loop
//Stoploss and Take Profit inputs
sl_inp = input(2.0, title='Stop Loss %', type=float)/100
tp_inp = input(4.0, title='Take Profit %', type=float)/100
//Backtesting Date Range
//From Date Inputs
fromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
fromMonth = input(defval = 9, title = "From Month", minval = 1, maxval = 12)
fromYear = input(defval = 2019, title = "From Year", minval = 1970)
//To Date Inputs
toDay = input(defval = 29, title = "To Day", minval = 1, maxval = 31)
toMonth = input(defval = 9, title = "To Month", minval = 1, maxval = 12)
toYear = input(defval = 2019, title = "To Year", minval = 1970)
//Calculate start/end date and time condition
startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond = time >= startDate and time <= finishDate
//Calculating 24hrs Volume
buying_volume(range)=>
vol = na
for i = 0 to range
if open[i] < close[i]
vol := na(vol) ? volume[i] : vol + volume[i]
vol
lower_buy_vol = security(tickerid, lower_tf, buying_volume(bars_in_tf))
//Calculating Percentage Change wrt 24hrs Volume
volbuy = (volume/lower_buy_vol)
//Long Entry if Buy Volume is more than 3%
longEntry = volbuy >= 0.03
//SL and TP
stop_level = strategy.position_avg_price * (1 - sl_inp)
take_level = strategy.position_avg_price * (1 + tp_inp)
//Submit orders
strategy.entry("Long Entry", true, when=longEntry and time_cond)
strategy.exit("Stop Loss/TP","Simple SMA Entry", stop=stop_level, limit=take_level)
//Plotting the SL/TP line
plot(stop_level, color=red, style=linebr, linewidth=2)
plot(take_level, color=green, style=linebr, linewidth=2)
答案 0 :(得分:0)
那是因为您设置了错误的退出职位ID:
strategy.exit("Stop Loss/TP","Simple SMA Entry", stop=stop_level, limit=take_level)
因此,您有一个条目ID Simple SMA Entry
,但是条目的ID是Long Entry
。在出口处更改它以更正它,它将起作用:
strategy.exit("Stop Loss/TP","Long Entry", stop=stop_level, limit=take_level)