• 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

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:

    https://ninjatrader.com/support/foru...ad.php?t=19387

    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...ick_replay.htm

    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.
        https://ninjatrader.com/support/help...fill_logic.htm

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by stalt, Today, 07:52 AM
        5 responses
        14 views
        0 likes
        Last Post NinjaTrader_PatrickG  
        Started by im197, 07-30-2019, 12:29 PM
        2 responses
        10 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by 7robert, Today, 08:20 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by GARZONJ, Today, 08:06 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by MarianApalaghiei, Today, 01:26 AM
        1 response
        18 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X