• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Account Balance tracking code is printing executions that did not occur

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Account Balance tracking code is printing executions that did not occur

    Hi all,

    To track account balance and use it in my backtest model I have put the following code at the end of my script:

    Code:
            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    
        if (execution.Order != null && execution.Order.OrderState == OrderState.Filled)
        {
            if(execution.Order.Name == "BrunsBuy")
        {
    
    
            AcctValue = (AcctValue - ((execution.Price)* execution.Quantity));
            Print(AcctValue.ToString()+" - "+(execution.OrderId)+" "+(execution.Time));    
            }
    
    
    
        }
    }
    When this runs on one instrument I get the expected results that match exactly what the strategy did:

    MTCH Output:
    4919.42 - NT-00000-1418 12/24/2019 12:00:00 PM

    AMD Output:
    4953.23 - NT-00000-1435 12/26/2019 3:00:00 PM
    4898.43 - NT-00002-1435 2/14/2020 3:00:00 PM


    When it runs on the instrument list of the above items I get the following:
    4953.23 - NT-00000-1452 12/26/2019 3:00:00 PM Order ID did not happen
    4898.43 - NT-00002-1452 2/14/2020 3:00:00 PM Order ID did not happen
    4919.42 - NT-00000-1453 12/24/2019 12:00:00 PM Order ID did not happen
    4851.66 - NT-00000-1459 12/26/2019 3:00:00 PM This Order ID occurred
    4796.86 - NT-00002-1459 2/14/2020 3:00:00 PM This Order ID occurred
    4838.84 - NT-00000-1467 12/24/2019 12:00:00 PM This Order ID occurred

    I'm just running this on the buy executions right now. Where are these random buy IDs coming from when I run this on two instruments at once? These are no where to be found in the strategy analyzer. Note how the output did not print them in chronological order either... which is weird to me.

    Alec

    #2
    Hello GreenspanAlec,

    Welcome to the forums!

    When a baskest test is run or an optimization backtest is preformed, NinjaTrader performs a "Details run" which is used to populate the Results grid. If you are comparing output from a basket test, the ID's that get printed out in the Output Window may not necessarily correspond to the same ID's listed when reviewing the strategies results.

    Demo - https://drive.google.com/file/d/1a-C...w?usp=drivesdk

    As we can see, when we switch instruments, we see a "Details run" performed and not the complete backtest. These values are then consistent when we compare ID's because the ID's were generated with that last "Details run."

    You are really running into separate functionality NinjaTrader utilizes to populate Results grids and other grids which shouldn't be confused with your actual backtests. You wouldn't see this unless you were comparing the results to the prints in your output window. I recommend moving forward by comparing the output window to individual backtests and to avoid comparing the output window of basket and optimization backtests to the results grids.

    Please also note the Strategy Analyzer was not designed to utilize a full account for portfolio management when backtesting. You can add a variable for account funds to implement this functionality, but this variable will be separate for each strategy instance. In other words, the strategy will have a different amount of "funds" when backtesting multiple instruments, because we are testing separate instances of that strategy against separate instruments.

    You could consider creating a multi time frame script which trades all of the symbols the strategy will operate with, and the funds from your strategy will be shared since you will then have one strategy that trades multiple symbols.

    Multi Time Frame and Instruments - https://ninjatrader.com/support/help...nstruments.htm

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Nate G, Yesterday, 05:27 PM
    1 response
    10 views
    0 likes
    Last Post MojoJojo  
    Started by Mindset, Yesterday, 10:35 PM
    1 response
    4 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by habibalex, Yesterday, 08:19 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_Marco  
    Started by lakersfan, Yesterday, 07:53 PM
    0 responses
    10 views
    0 likes
    Last Post lakersfan  
    Started by RobertoPaez, Yesterday, 07:33 PM
    0 responses
    6 views
    0 likes
    Last Post RobertoPaez  
    Working...
    X