简体   繁体   中英

When are strategy.entry and strategy.exit executed?

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?

they are being executed at candle close.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM