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

mixed OnBarClose & OnEveryTick while backesting

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

    mixed OnBarClose & OnEveryTick while backesting

    Hello,
    I need to make a code to backtest pairs of Entry & Exit in a specific way, and I cannot find a solution for it.

    Namely:
    * enter at bar close (if the price reaches P), then
    * exit at P+2pts, but intrabar, at whatever tick the price reaches P+2pts

    I am using two bar series of the same data, a time series of 1-minute, and a tick-by-tick one. The second is used to Enter/Exit at any tick of the bar forming, not just at the close of the bar.

    While in State.Historical, the entry at the close of the bar can be achieved by setting

    Calculate=Calculate.OnBarClose
    and
    EnterLong(1/*2nd bar series*/,quantity,name) when BarsInProgress == 0 , i.e. time series of 1-minute

    Then, I switch to
    Calculate=Calculate.OnEachTick
    to check at every subsequent tick if price becomes P+2pts, to ExitLong(...)

    The thing is, it may happen that at those ticks forming the bar, the price to reach again P and an EnterLong(...) to be triggered, before the close of the bar.
    I need all EnterLong(...) to be submitted at the close of the bar only. Again, I am in State.Historical at all times.

    Any suggestion will be much appreciated, thank you.

    #2
    Hello. Thanks for the post.

    We do not recommend switching your calculation mode while your strategy is running as it is prone to error in your strategy.

    If you want to calculate your exit condition intrabar, but only enter on the close of the bar you will have to use a special technique that is described here in this reference sample:



    You can use IsFirstTickOfBar to know when the first tick comes in on a new bar to check for the entry condition.

    Ex:

    Code:
    protected override void OnBarUpdate()
    		{
    			if(CurrentBars[0] < 1) return;
    			if(BarsInProgress == 0)
    			{
    				if(IsFirstTickOfBar)
    				{
    					Print("FirstTickOfBar");
    				}
    			}
    			
    			if(BarsInProgress == 1)
    			{
    				Print("Tick Series");
    			}
    		}
    You will want to turn on Tick Replay to get the correct sequence of ticks in Historical mode. For more information on Tick Replay see this help guide link on developing for Tick Replay.


    https://ninjatrader.com/support/help...ttickofbar.htm - IsFirstTickOfBar

    Please let us know if you have any questions.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hello Chris,
      Thank you for your solution. It applies to real-time though, while I'm looking for historical ...

      Best,
      Adrian

      Comment


        #4
        Hello. Thanks for the reply.

        This will work in historical mode as well. You have a 1 tick series to fill orders and you have tick replay on, this is the closest simulation to real time you can get.

        Please review the historical backfill algorithm used in NinjaTrader to see how your orders will be filled historically using the 1 Tick data series.


        Please let us know if you have any questions.
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by MarianApalaghiei, Today, 10:49 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by love2code2trade, Yesterday, 01:45 PM
        4 responses
        28 views
        0 likes
        Last Post love2code2trade  
        Started by funk10101, Today, 09:43 PM
        0 responses
        8 views
        0 likes
        Last Post funk10101  
        Started by pkefal, 04-11-2024, 07:39 AM
        11 responses
        37 views
        0 likes
        Last Post jeronymite  
        Started by bill2023, Yesterday, 08:51 AM
        8 responses
        46 views
        0 likes
        Last Post bill2023  
        Working...
        X