Strategy.entry和strategy.exit什么时候执行?

发布于 2025-01-18 23:39:38 字数 1445 浏览 0 评论 0原文

我试图计算回测中虚拟策略完成的交易总数,但我无法计算同时进入和退出的交易。

这就是策略:

//@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阪姬 2025-01-25 23:39:38

他们在蜡烛收盘时被执行。

they are being executed at candle close.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文