[snippet] if Backtest then X, else Y

stable
By ProfGhibli in Trading Bots Published May 2020 👁 1,692 views 💬 0 comments

Description

This is a little snippet if you want to automatically have different settings for when you are running a backtest or running live trading. I used this for example to change order type to Market Order by default when backtesting. Other potential uses can be changing an order amount or changing some values that rely on info from INSIDE the candle (which is not available in the backtest). Important Limitation: the snippet only takes into account the very first 2 update cycles, which means if you first run this as a backtest and THEN start live trading, it will NOT update this_is_a_simulation status. This snippet only works reliably for the scripts with EnableHighSpeedUpdates() mode enabled, it may not work well without it. To make use of this, use logic like "if this_is_a_simulation == true then / settings to be changed / end". Note that this snippet needs to be inserted BELOW the initial setting, so that it can overwrite the setting. Note that all the variables used inside this snippet are not valid outside the snippet, since they are local (but of course you can change that), except for the boolean this_is_a_simulation variable. If you script requires to know whether it's a backtest or live trading BEFORE it makes it's first move (like send a trading signal), then add a line if ghibli > 3 to that first action. Basically the snippet needs to run 2 update cycles before it knows for sure if this is a simulation or not. -- DISCLAIMER: you should read the code of the snippet and understand how it works, -- the author takes no responsibility for any losses that may occur from either -- the user not implementing the snippet properly nor for the snippet not working as expected.
HaasScript
EnableHighSpeedUpdates()

if true then -- If Backtest then snippet
    local cntr = Load('cntr', 0)
    if cntr < 3 then
        cntr = cntr + 1
        local time = CurrentSecond()
        local oldtime = Load('oldtime', 61)
        if cntr == 1 then
            if time == 0 then
                Log('This is very likely a backtest, need 1 more update')
                this_is_a_simulation = true -- the chance is that it's a backtest in 59 out of 60 cases
            else
                Log('This not a backtest')
                this_is_a_simulation = false
            end
        elseif cntr == 2 and this_is_a_simulation == true then
            if time == 0 then
                Log('Yes, it is a backtest')
            elseif time > 1 then
                Log('Nope, it is not a backtest')
                this_is_a_simulation = false
            end
        end
        Save('cntr', cntr)
    end
    if this_is_a_simulation == true then
        SetOrderType = MarketOrderType -- or any other thing that you want to happen specifically when backtesting
    else
        -- something that should happen if this is not a backtest
    end
end

0 Comments

Sign in to leave a comment.

No comments yet. Be the first!