Understanding Runtime Code Execution

Forums ProRealTime English forum ProOrder support Understanding Runtime Code Execution

Viewing 15 posts - 1 through 15 (of 18 total)
  • #195805

    Hi,

    I experienced some weird behavior running some backtests. To explain see the following very simple code example:

    I start the test at 09:00 in the morning. So from my understanding when I run the backtest there should be one contract bought at 09:01 and then no more contracts should be bought.
    Instead what happens is that no contract at all will be bought.
    If I change the code to the following,

    it is working properly. Only one contract is being bought at 09:01, then no more!

    Can you help me clarify this behavior?

    Best regards,
    Nico

    #195806

    Hello Nico,

    This is because you never exit. Thus, your second example is OnMarket after the Buy, and after that the If will never run True anymore. If you create code for the Exit, it will buy again.

    Understand ?

    Peter

    1 user thanked author for this post.
    #195807
    Then about the first part :

    I start the test at 09:00 in the morning. So from my understanding when I run the backtest there should be one contract bought at 09:01 and then no more contracts should be bought. Instead what happens is that no contract at all will be bought.

    This is more difficult to grasp.
    ProrealCode has a default for bars not to execute code at first. This is about this command, would you like to overrule this default :

    I would call it a bug that your code surely is executed during this first phase of “preloadbars”, but that no orders will be handed to the (virtual) broker.
    Thus what happens is that your Start variable will be set to 1, after that it is set to 0 by yourself, and nothing happened.

    Start your program with my example with

    and it will work.

    1 user thanked author for this post.
    #195811

    Hey Peter,

    thank you for the quick answer!

    As for part 2, that was clear and is intended behavior, so all good.

    As for part1, when using the “NOT longonmarket” it does create a a buy order at the “virtual” broker at 09:01. I am using 1-minute bars, so can I conclude that ProRealCode does have a default “wait” of 1 bar before forwarding orders? In case example 2 would result in contracts being bought at 09:03, I would know that the default wait timer is 3 bars, 09:04 4 bars and so on.

    Am I getting there?

    #195812

    Try with:

    In this case your code will not be read before it actually starts for real in ProOrder, so your ONCE will be set at first bar and not back in history.

    1 user thanked author for this post.
    #195832

    Thank you Nicolas. I was actually on the phone and just hung up. So I made it a too quick one with hopefully still some sense.

    As for part1, when using the “NOT longonmarket” it does create a a buy order at the “virtual” broker at 09:01. I am using 1-minute bars, so can I conclude that ProRealCode does have a default “wait” of 1 bar before forwarding orders? In case example 2 would result in contracts being bought at 09:03, I would know that the default wait timer is 3 bars, 09:04 4 bars and so on. Am I getting there?

    Of course you are. 🙂 It works like this :

    When you use a 1 minute timeframe on your chart (and I estimate you do/did), then your code is called each minute. It is then executed BUT only at the next bar all will apply for real.
    It is not easy to explain this and best is to remember that your code is executed each minute and that it comes down to the “next minute” it applies.
    Thus suppose you look at your chart and it just has become 09:06. Your code is called and a buy (market) order (without further conditions) will be filled at 09:07.
    Supposed that in the bar which builds at 09:07 a sell condition occurs, then you will notice this at the call of 09:08. If you(r code) decides to sell, then this will happen at 09:09. That in itself will be noticed in the next call (09:10) by means of OnMarket not being true.

    You will get the hang of his quickly.

    Of course you can set the timeframe different for the chart you are using, hence what your code applies to. Make it one second, and the call will happen each second. The downside of this is that you will have less backtesting data.
    When got the hang of this all, it is good to investigate the TimeFrame command. But this is really a step further and more “complicating”.

    1 user thanked author for this post.
    #195833

    The DefParam Preloadbars exists, so your calculations get “headroom” before the real ordering starts. Thus, if you want to know an average of the last 250 bars, that average must have been calculated properly before your first condition is judged/set for a buy order.
    DefParam Preloadbars = 0 will readily execute your order indeed, but no e.g. Average(250) can have been calculated. Thus, Preloadbars should be set at the minimum number of bars you need ahead of your first order.

    The default is 1000 and the maximum is 10000.

     

    1 user thanked author for this post.
    #195872

    Thank you a lot!

    I think I got it.

    Regarding the once statement, is this only being called the very first time or does it apply every day, for example if I use FLATBEFORE 090000 and FLATAFTER 173000 does the once statement get repeteaed on each day?
    Furthermore does the code get run after 1730 and has all positions closed, and also does not accept other orders or does it stop to run until 0900 the next day?

    Another thing I am wondering about is according to this https://www.prorealcode.com/documentation/percenttrailing/ when setting a stoploss it will set it depending on the AVERAGE position price. Which average is meant by this?

    Best regards,
    Nico

    #195873

    Hi again Nico,

    Once : Only the very first time this is executed (the whole code is being called each bar). FlatBefore/-After is not related to this.

    The code keeps on running; it is only that no orders will be executed during the “flat” period. I am not even sure whether Exits will take place in the “flat” period (Sell and ExitShort). Hopefuly others will know this.

     

    when setting a stoploss it will set it depending on the AVERAGE position price. Which average is meant by this?

    This is about accumulated positions. Buy a piece at first, later add another, later add more. Sell one. All together this forms an average price which is a mixture of prices and quantities for those prices.
    Quite hard to check afterwards.

     

    1 user thanked author for this post.
    #195876

    Exits will take place in the “flat” period

    FlatAfter 173000 … open positons will be closed at 173000 (if running on a TF that has a bar that closes at 173000).

    If, for example, running on 1 hour TF then positions will be closed at 180000.

    https://www.prorealcode.com/documentation/flatafter/

    2 users thanked author for this post.
    #195996

    Thank you guys!

    In conclusion it means the code is running 24/7 and therefor variables would get overwritten in certain cases, so I need to make sure the code is “guarded” during non trading times if I want variables to only be updated during trading hours.

    In case I do backtesting for a longer period of time, where tick or second data is not available according to the backtest, does the stoploss apply to the tick data or does it apply to the bar data?
    I am wondering because if it applies to tick data I am curious why using tick/seconds data is only available for very short time periods for backtesting?

    Best regards,
    Nicolas

    #195998

    I am wondering because if it applies to tick data I am curious why using tick/seconds data is only available for very short time periods for backtesting?

    Simple : because it requires more data. You can calculate how many seconds in a year exist, right ? Well, easy, it is 3600 times more than hourly data. 🙂

    #195999

    yes that is obvious, but if the backtests are able to set a proper stoploss even with frequency of 1 day, then how come it has data from this year january of tick data?
    so my question is rather, is it a processing issue or a databank issue?
    the latter can’t be the case if stoploss are hit in backtests on tickdata even though the test is only done for hourly/daily timescheme.

    #196008

    That is a good one ! But I think this is the answer :

    It does not really require tick data in order to simulate that your order gets filled. It also would not be far off when no real tick data is available, because we can assume that your order gets filled anyway. Or better : that it would have filled back at the time anyway. Only with more exorbitant amounts (like 100M+) and/or with very low volatility instruments, the filling may happen slowly (if at all). But this is not really a Trailing Stop issue, is it ? Or maybe you even weren’t talking about Trailing Stop.

     

    #196012

    I am talking about trailing stop loss. But if I run a backtest and you don’t have tickdata which data are you using to say a trailing stop loss was hit? Depending on low and high of a bar and if you say the stoploss is within the range it is counted as a hit?

Viewing 15 posts - 1 through 15 (of 18 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login