Strategy.entry和strategy.exit什么时候执行?
我试图计算回测中虚拟策略完成的交易总数,但我无法计算同时进入和退出的交易。
这就是策略:
//@version=5
strategy("Super/MACD/RSI", overlay=false)
import keio/console/2 as c
var log = c.init()
// INDICATORS
[macdline, signalline, histline] = ta.macd(close, 12, 26, 9)
[supertrend, direction] = ta.supertrend(3, 10)
rsi = ta.rsi(close, 14)
// SIGNAL
buysignal = ta.crossover(macdline, signalline) and close > supertrend and rsi < 65
// SL & TP
var longSL = 0.0
var longTP = 0.0
if buysignal
longSL := close - close * 0.02
longTP := close + close * 0.06
// STRATEGY
if buysignal
strategy.entry(id = "Long", direction=strategy.long)
strategy.exit(id = "Long Exit", from_entry = "Long", limit=longTP, stop=longSL)
// DEBBUG
var bool intrade = false
var bool tradecounted = false
var int entries = 0
if strategy.opentrades != 0
// if strategy.position_size > 0
intrade := true
else
intrade := false
tradecounted := false
if intrade and not tradecounted
entries += 1
date = timestamp(year, month, dayofmonth, hour)
log := c.print(log,"Entry : " + str.tostring(entries) + " : " + str.format("{0,date,yyyy.MM.dd HH:mm}", date))
tradecounted := true
如果执行它,您将看到它打印与策略测试器完全相同的条目,除了那些同时输入和完成的条目。那些不见了。
即使我将行放在
strategy.exit(id = "Long Exit", from_entry = "Long", limit=longTP, stop=longSL)
脚本末尾,结果仍然相同。
回测时strategy.*相关的代码是否与其余代码分开执行?
I am trying to count the total number of trades done by a dummy strategy in backtesting but I have trouble counting the trades that enter and exit at the exact same time.
This is the strategy:
//@version=5
strategy("Super/MACD/RSI", overlay=false)
import keio/console/2 as c
var log = c.init()
// INDICATORS
[macdline, signalline, histline] = ta.macd(close, 12, 26, 9)
[supertrend, direction] = ta.supertrend(3, 10)
rsi = ta.rsi(close, 14)
// SIGNAL
buysignal = ta.crossover(macdline, signalline) and close > supertrend and rsi < 65
// SL & TP
var longSL = 0.0
var longTP = 0.0
if buysignal
longSL := close - close * 0.02
longTP := close + close * 0.06
// STRATEGY
if buysignal
strategy.entry(id = "Long", direction=strategy.long)
strategy.exit(id = "Long Exit", from_entry = "Long", limit=longTP, stop=longSL)
// DEBBUG
var bool intrade = false
var bool tradecounted = false
var int entries = 0
if strategy.opentrades != 0
// if strategy.position_size > 0
intrade := true
else
intrade := false
tradecounted := false
if intrade and not tradecounted
entries += 1
date = timestamp(year, month, dayofmonth, hour)
log := c.print(log,"Entry : " + str.tostring(entries) + " : " + str.format("{0,date,yyyy.MM.dd HH:mm}", date))
tradecounted := true
If you execute it you will see that it prints the exact same entries as the strategy tester, except for those that enter and finish at the exact same time. Those are missing.
Even if I put the line
strategy.exit(id = "Long Exit", from_entry = "Long", limit=longTP, stop=longSL)
At the end of the script the result is still the same.
Is the strategy.* related code executed separately from the rest of the code when backtesting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们在蜡烛收盘时被执行。
they are being executed at candle close.