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

Get exact entry time in a backtest

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

    Get exact entry time in a backtest

    I would like to get the exact entry time of my trades in a backtest on the 60 minute chart if possible. My first thought was to add a shorter time frame (1 minute) and add a print command in OnExecution() when the order is filled but that didn’t work (it always prints the entry time as the last minute in the hour).

    I did try printing execution.time in OnExecution but that only gave me the hour the trade was entered (where I'm trying to find the exact minute).

    Any help or suggestions on how to get this to work would be much appreciated. I have included a sample of what I tried below for reference.

    Code:
    //create the IOrder object
    private IOrder entryOrder = null;
    
    
    protected override void Initialize()
    {
    	CalculateOnBarClose = true;
            string intrabar = Instrument.FullName; 
    	Add(intrabar,PeriodType.Minute,1); //add extra time frame
    }
    
    OnBarUpdate
    //if buy conditions
    { entryOrder = EnterLongStop(DefaultQuantity, High[0]+2*TickSize) }
    
    protected override void OnExecution(IExecution execution)
    {
    if (entryOrder != null && entryOrder == execution.Order)
    	{
    		Print("Highs"+Highs[1][0]+"Times"+Times[1][1]);
    	}						
    }

    #2
    Hello newuser,

    Thank you for your post.

    As the backtest is performed on bars objects and has no connection to reference time from, there is no means to print a time of the execution that would not match the last bars update timestamp.

    Please let me know if you have any questions.

    Comment


      #3
      Thanks for the prompt reply. Ok, so if the backtest is performed on bars objects, is there a way to do this within the context of the multi time frame methodology? Given that I’m always entering trades at a certain price I was thinking I could create a separate variable to capture the entry price value, then check on the shorter (secondary) time frame to see which particular bar high is greater (for a long trade) than the value stored in the variable.

      Another idea I had was to look at the bar (for the primary series) where entry occurs and then print all bars on the secondary series for that primary bar. After a bit of playing around I got it working as per the following code:

      Code:
      //create variables
      private IOrder entryOrder = null;
      private bool enteredlong = false;
      
      protected override void Initialize()
      {
      	CalculateOnBarClose = true;
              string intrabar = Instrument.FullName; 
      	Add(intrabar,PeriodType.Minute,15); //add extra time frame
      }
      
      OnBarUpdate
      }	if ( //buy conditions)
      { entryOrder = EnterLongStop(DefaultQuantity, High[0]+2*TickSize) }
      
      if (enteredlong == true)
      	{
      Print(Highs[1][3]+ Highs[1][2]+ Highs[1][1]+ Highs[1][0]); //print the intrabar data when entry is triggered
      	enteredlong = false; 	//reset the variable so it doesn’t print on every iteration of OnBarUpdate
      	}	
      }
      
      protected override void OnExecution(IExecution execution)
                 {
      	if (entryOrder != null && entryOrder == execution.Order)
      		{
      		enteredlong = true;
      		}
      	}
      That allowed me to export the data into excel and identify on which intra bar entry occurred, but I figure there must be a way to cut out the intermediate steps of printing to the output window and doing extra work in excel.

      I have tried reading the online manual and playing around with the code some more but I haven’t got my head completely around the multi time frame logic just yet. If you could point me in the right direction for my idea in the first paragraph that would be much appreciated. I'm a little unclear on how to reference every secondary bar that sits inside a given primary bar, and then I want to check which of those bars is the first to exceed the value stored in my variable.

      Comment


        #4
        Hello newuser,

        Thank you for your response.

        You could add the secondary series that is smaller (such as 1 tick) then call that bars object in OnExecution() when the execution occurs to see the time relative to the last secondary series bar close. However, likely the secondary bar series will have closed at the same time as the primary that filled the order. So you may benefit from submitting the order to the secondary series for fill and then calling it's Times[1][0] in the OnExecution() for the time of the fill.

        Please let me know if you have any questions.

        Comment


          #5
          I tried submitting the order to the secondary series for fill as per your suggestion but I couldn’t get it to work. I found several problems with the code that I wrote – it didn’t trigger all the trades that it should have, and it triggered other trades that it shouldn’t have (I say that based on a comparison to the same script without the extra code for the secondary bar series).

          I suspect the problems occur due a conflict between the conditions on the primary bar series and the entry order on the secondary bar series but I’m lost on what to try next.

          So I noticed the time based conditions (which are against the primary timeframe) don’t work i.e. I start getting trades placed outside those times.

          I’m also confused as to how I keep the order alive for only 1 bar on the primary frame. I tried using

          Code:
          entryOrder = EnterLongStop(1,false,DefaultQuantity,High[0]+2*TickSize);
          but with the ‘false’ override I didn’t get any trades whatsoever. When I set the liveUntilCancelled override to ‘true’ I do get trades (but wrong results as noted above).

          I can’t see how else I can make the primary bars time criteria work with an entry order submitted to a secondary time frame? Here’s what I tried:

          Code:
          Protected override void Inititalize()
          {
          String intrabar = Instrument.FullName;
          Add(intrabar, PeriodType.Minute, 10);
          }
          
          Protected override void OnBarUpdate()
          {
          If(BarsInProgress == 0)
          {
          	If (//conditions)
          		{
          		entryOrder = EnterLongStop(1,true,DefaultQuantity,High[0]+2*TickSize);
          		}
          	}
          }
          
          Protected override void OnExecution (IExecution execution)
          	{
          	if (entryOrder != null && entryOrder == execution.Order)
          		{
          		Print(Times[1][0]);
          		}
          	}
          I did at least note that the print function in OnExecution was working as intended and the Strategy Analyzer ‘trades’ screen showed the entry time for trades on the secondary time frame, so the orders were definitely being submitted to the shorter time frame.

          Comment


            #6
            Hello newuser,

            Thank you for your post.

            I would need to review the conditions that are not working as intended or occurring outside the desired time.
            If you prefer you can send these conditions in an email to platformsupport[at]ninjatrader[dot]com with 'ATTN: Patrick H' in the subject line and a reference to this thread in the body of the email.

            I look forward to assisting you further.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by thanajo, 05-04-2021, 02:11 AM
            4 responses
            470 views
            0 likes
            Last Post tradingnasdaqprueba  
            Started by aa731, Today, 02:54 AM
            0 responses
            4 views
            0 likes
            Last Post aa731
            by aa731
             
            Started by Christopher_R, Today, 12:29 AM
            0 responses
            10 views
            0 likes
            Last Post Christopher_R  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            166 responses
            2,237 views
            0 likes
            Last Post sidlercom80  
            Started by thread, Yesterday, 11:58 PM
            0 responses
            6 views
            0 likes
            Last Post thread
            by thread
             
            Working...
            X