Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Backtesting Order filled is not happening after a certain time

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

    Backtesting Order filled is not happening after a certain time

    Hello,
    I am writing a strategy for backtesting. Following is the sample code of what I am using:
    Code:
    protected override void OnBarUpdate()
    
    {[INDENT]if (BarsInProgress != 0)[/INDENT][INDENT=2]return;[/INDENT][INDENT]
    if (CurrentBars[0] < 1)[/INDENT][INDENT=2]return;[/INDENT][INDENT]
    if (Position.MarketPosition == MarketPosition.Long) {[/INDENT][INDENT=2]Print("Inside MarketPosition " + Position.MarketPosition);[/INDENT][INDENT=2]//Condition to trigger sell goes here[/INDENT][INDENT]}[/INDENT]
     [INDENT]else {[/INDENT][INDENT=2]Print("Inside MarketPosition " + Position.MarketPosition);
    
    if (/*condition to buy goes here*/) {[/INDENT][INDENT=3]ContSize = x;
    
    Print("Long condition met");
    
    EnterLong(ContSize, "Long");[/INDENT][INDENT=2]
    }[/INDENT][INDENT]
    }[/INDENT]
     
    
    }
    
    
    
    protected override void OnOrderUpdate(Order order, double limitPrice, double stPrice, int quantity,
    
    int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment) {[INDENT]Print("Order " + order.Name + " is updated with state " + orderState + " Quantity " + quantity + " Error Code  " + error);[/INDENT]
     
    
    }
    I see the following in the script output:

    Code:
    Inside MarketPosition Flat
    
    Inside MarketPosition Flat
    
    Inside MarketPosition Flat
    
    Inside MarketPosition Flat
    
    Long condition met
    
    Order Long is updated with state Submitted Quantity 21 Error Code  NoError
    
    Order Long is updated with state Accepted Quantity 21 Error Code  NoError
    
    Order Long is updated with state Working Quantity 21 Error Code  NoError
    
    Order Long is updated with state Filled Quantity 21 Error Code  NoError
    
    Execution Order Name  Long and Size 21 and Price  2293.51 and state Filled
    
    Execution order name Long Filled Quantity 21
    
    Setting stop order for stopPrice 2247.6398
    
    Order StopLong is updated with state Submitted Quantity 21 Error Code  NoError
    
    Order StopLong is updated with state Accepted Quantity 21 Error Code  NoError
    
    Order StopLong is updated with state Working Quantity 21 Error Code  NoError
    
    Inside MarketPosition Long
    
    Inside MarketPosition Long
    
    Inside MarketPosition Flat
    
    Long condition met
    
    Order Long is updated with state Submitted Quantity 39 Error Code  NoError
    
    Order Long is updated with state Accepted Quantity 39 Error Code  NoError
    
    Order Long is updated with state Working Quantity 39 Error Code  NoError
    
    Order Long is updated with state Filled Quantity 39 Error Code  NoError
    
    Execution Order Name  Long and Size 39 and Price  1270.51 and state Filled
    
    Execution order name Long Filled Quantity 39
    
    Setting stop order for stopPrice 1245.0998
    
    Order StopLong is updated with state Submitted Quantity 39 Error Code  NoError
    
    Order StopLong is updated with state Accepted Quantity 39 Error Code  NoError
    
    Order StopLong is updated with state Working Quantity 39 Error Code  NoError
    
    Inside MarketPosition Long
    
    Order ExitLong is updated with state Submitted Quantity 39 Error Code  NoError
    
    Order ExitLong is updated with state Accepted Quantity 39 Error Code  NoError
    
    Order ExitLong is updated with state Working Quantity 39 Error Code  NoError
    
    Order ExitLong is updated with state CancelPending Quantity 39 Error Code  NoError
    
    Order ExitLong is updated with state CancelSubmitted Quantity 39 Error Code  NoError
    
    Order ExitLong is updated with state Cancelled Quantity 39 Error Code  NoError
    
    ExitLong Cancelled
    
    Order StopLong is updated with state Filled Quantity 39 Error Code  NoError
    
    Execution Order Name  StopLong and Size 39 and Price  1245.1 and state Filled
    
    StopLong is executed with Quantity 39
    
    Inside MarketPosition Flat
    
    [B]Long condition met[/B]
    
    Order Long is updated with state Submitted Quantity 39 Error Code  NoError
    
    Order Long is updated with state Accepted Quantity 39 Error Code  NoError
    
    [B]Order Long is updated with state Working Quantity 39 Error Code  NoError[/B]
    
    [B]Inside MarketPosition Flat[/B]
    
    [B]Long condition met [/B]
    
    Inside MarketPosition Flat
    
    Inside MarketPosition Flat
    
    [B]Long condition met [/B]
    
    .
    
    .
    
    Inside MarketPosition Flat
    
    [B]Long condition met [/B]
    
    .
    .
    As you can see for the last Order Long there is no "Filled" statement and because of that MarketPosition is Flat next time OnBarUpdate() is called which is expected behavior if the order is not "Filled". No next time onwards whenever Long Condition is met orders never get submitted.

    Interestingly this stops working after a certain time frame. For example I wanted to run the backtest from 1999 - 2020

    On the first few run it was giving me the results from 1999 - 2012 and when I changed the time frame and I was able to run from 2013 - 2020 and see the results.

    After a while, it started giving me results only from 1999 - 2003 and if I change the time frame to 2003 - 2020 I was able to see the results for this time period successfully.

    Why does it stop at random times?

    I would like to optimize the strategy from 1999 - 2020 but because I have to break it into more than one time period the optimized parameters are different for both the time period, which is obvious. How do I fix this problem?

    Thanks for your help.

    Yogesh

    #2
    Hello Yogesh, thanks for writing in.

    You must first make sure that your PC is not running out of memory. An optimization from 1999-2020 would most likely fill up over 64 gigabytes of RAM in an optimization. Watch the RAM in Windows Task Manager as the optimization runs and if the memory fills up all the way, the strategy will need to be tested in chunks to prevent memory overflow.

    Kind regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks, Chris for the response. The maximum RAM usage went up to 1050 MB. I am running around 400 combinations on a daily chart, so it doesn't look like it is a memory issue. I also tried without optimization just backtesting with the default parameter it is the same issue.

      Is there an option to disable Chart creation as part of optimization if the memory is indeed an issue?

      Comment


        #4
        Hello Yogesh, thanks for your reply.

        There is no way to disable to a chart view of the strategy analyzer. If you test the SampleMACrossover strategy do you get the same problem? I am getting trades for the entire time period on that default strategy.

        Best regards,
        -ChrisL
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Yes, it is working now, it is not consistent though, sometimes it does for the entire time period and sometimes it doesn't. Now after I tried SampleMACrossover I tried my own strategy and it worked.

          One thing that I noticed is as I change the value of a parameter the time period for which the strategy runs changes, and this is not for optimization just for the backtesting. Also what I am noticing is the time period are always aligned,

          what I mean is if a value "X1" for a parameter "Y" gives the result for

          1999 - 2003 then even if I run from 2003 - 2020 it only gives the result for 2003. But if I run from 2004 - 2020 then I get the result for the entire time.

          If I change the value to "X2" for the same parameter "Y" then it gives the result only for

          1999 - 2002 and if I run again from 2002 - 2020 I get the result for the entire time period.

          If I change the value to "X3" for the same parameter "Y" then it gives the result for the entire time period of 1999 - 2020.

          As I mentioned that I don't think it is a problem with RAM, because I am backtesting on the daily bar and the test finishes up in a second or two.

          I think there is some technical glitch in the software which is triggering this issue, can you please provide remote support to debug this issue. I really really appreciate your Chris.

          Thanks,
          Yogesh
          Last edited by yogesh_forum; 04-22-2021, 07:15 AM.

          Comment


            #6
            Hello, thanks for your reply.

            It is against company policy to look at code over remote support. You need to reduce the strategy that you are working on down to more basic components to find the source of the issue. If the SampleMACrossover strategy is working and your custom strategy is not, then it means there is a bug in your strategy. NinjaScript consultants may also be hired from the NinjaTrader Ecosystem for assistance with debugging:
            https://ninjatraderecosystem.com/sea...mming-services

            Kind regards.
            -ChrisL

            The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Thanks, Chris and make sense. Yes, I have reduced the strategy to only one parameter and that's how I was able to identify the above behavior. I understand there might be a bug in the strategy and that's where I will need help. That parameter is the percentage that I want to optimize for stop loss which I am using in SetStopLoss method, if the strategy was failing at the execution of that method I would have understood but if fails in filling the order I don't see any bug in the code.
              Last edited by yogesh_forum; 04-22-2021, 11:37 AM.

              Comment


                #8
                Hello, thanks for your reply.

                We will not be able to debug or review any custom strategies. A NinjaScript consultant from the NinjaTrader Ecosystem would need to be hired to look at the custom strategy.

                Best regards.
                Chris L.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by CortexZenUSA, Today, 12:53 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by CortexZenUSA, Today, 12:46 AM
                0 responses
                1 view
                0 likes
                Last Post CortexZenUSA  
                Started by usazencortex, Today, 12:43 AM
                0 responses
                5 views
                0 likes
                Last Post usazencortex  
                Started by sidlercom80, 10-28-2023, 08:49 AM
                168 responses
                2,266 views
                0 likes
                Last Post sidlercom80  
                Started by Barry Milan, Yesterday, 10:35 PM
                3 responses
                13 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X