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

OnEachTick

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

    OnEachTick

    I normally run all my strategies on OnBarUpdate and want to learn more about OnEachTick. For example, I want identify an inside bar using OnBarUpdate. Once I identify this inside bar, i want to transition to OnEachTick so that I can instantaneously identify when the next bar breaks above the inside bar to enter long. My question is what do I reference once I transition to OnEachTick? Normally for OnBarUpdate, i just reference the previous bar high, low, open or close. For OnEachTick, I assume the code would be "if Current tick price is greater than High[1] then enter long"? Can i reference High[1] once I am OnEachTick or does High[1] only work when OnBarUpdate?

    If you can provide a ninjascript snip of code that would go under OnEachTick for this specific example that would be very helpful. Thanks

    #2
    Hello algospoke,

    Your understanding is correct, to check if the price breaks the high you could compare Close[0] greater than High[1], Close[0] represents the last tick when using OnEachTick. To do what you are asking you can run the script OnEachTick and use IsFirstTickOfBar to simulate OnBarClose events. Once your entry condition becomes true you can toggle a variable which is used in the OnEachTick portion of your code.

    You can see a sample of using IsFirstTickOfbar here: https://ninjatrader.com/support/help...ttickofbar.htm

    Your logic would end up looking similar to:
    Code:
    private bool checkIntrabar = false;
    protected override void OnBarUpdate()
    {​
        if (IsFirstTickOfBar)
       {
          if(yourEntryConditions)
          { 
              checkIntrabar = true; 
          }
       } else if(checkIntrabar == true)
       {
          //do your intrabar check
          if(yourIntrabarCondition)
          {
              checkIntrabar = false; 
          }
       }
    }
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse,

      Thanks for the response. That cleared up a lot for me and I was able to get the strategy coded up and working in market replay. My next question is how do I backtest this strategy since its Calculate.OnEachTick? I have created the strategy to enter long once it breaks above the inside bar's high and have it exiting on the next bar. When I open strategy analyzer and add this strategy, I get 0 trades even though I know it should execute trades based on the market replay. Are you unable to run backtesting when using OnEachTick? Do i need to subscribe to level II data to get it to work?

      Thanks

      Comment


        #4
        Hello algospoke,

        Using historical data the strategy is always processed OnBarClose. The best way to test OnEachTick strategies would be in realtime or using the playback connection. You can additionally use TickReplay however that will add considerable load and processing time to the backtest or optimizations which is something to keep in mind if you are trying to test larger datasets.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Jesse,

          Ok that makes sense. Got another question. Let's say I want the chart background to change gold tint whenever checkIntraBar is true and then return back to the default color once checkIntraBar returns back to false. I am only looking to have the chart background behind the current bar be gold whereas the rest of the chart can maintain the default chart background. It will help the user identify that we are now in OnEachTick calculation for the current bar and we could possibly have a long entry coming up. Once we enter long then i want to return the entire chart back to default color. Below is my code. How can I add this to my code?

          HTML Code:
          #region Using declarations
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.ComponentModel.DataAnnotations;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Windows;
          using System.Windows.Input;
          using System.Windows.Media;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Gui;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Gui.SuperDom;
          using NinjaTrader.Gui.Tools;
          using NinjaTrader.Data;
          using NinjaTrader.NinjaScript;
          using NinjaTrader.Core.FloatingPoint;
          using NinjaTrader.NinjaScript.Indicators;
          using NinjaTrader.NinjaScript.DrawingTools;
          #endregion
          
          //This namespace holds Strategies in this folder and is required. Do not change it.
          namespace NinjaTrader.NinjaScript.Strategies
          {
              public class InsideBarByTick : Strategy
              {
                  private bool CheckIntraBar;
                  protected override void OnStateChange()
                  {
                      if (State == State.SetDefaults)
                      {
                          Description                                    = @"Enter the description for your new custom Strategy here.";
                          Name                                        = "InsideBarByTick";
                          Calculate                                    = Calculate.OnEachTick;
                          EntriesPerDirection                            = 1;
                          EntryHandling                                = EntryHandling.AllEntries;
                          IsExitOnSessionCloseStrategy                = true;
                          ExitOnSessionCloseSeconds                    = 30;
                          IsFillLimitOnTouch                            = false;
                          MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                          OrderFillResolution                            = OrderFillResolution.Standard;
                          Slippage                                    = 0;
                          StartBehavior                                = StartBehavior.WaitUntilFlat;
                          TimeInForce                                    = TimeInForce.Gtc;
                          TraceOrders                                    = false;
                          RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                          StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                          BarsRequiredToTrade                            = 20;
                          // Disable this property for performance gains in Strategy Analyzer optimizations
                          // See the Help Guide for additional information
                          IsInstantiatedOnEachOptimizationIteration    = true;
                          CheckIntraBar = false;
                      }
                      else if (State == State.Configure)
                      {
          //                SetProfitTarget(CalculationMode.Ticks,4);
          //                SetStopLoss(CalculationMode.Ticks,4);
                      }
                  }
          
                  protected override void OnBarUpdate()
                  {
                      if(CurrentBar<BarsRequiredToTrade)
                          return;
                      
                      if(IsFirstTickOfBar)
                      {
                          if(Position.MarketPosition == MarketPosition.Flat)
                          {
                              CheckIntraBar = false;
                          }
                          
                          else if(Position.MarketPosition == MarketPosition.Long)
                          {
                              ExitLong();
                              CheckIntraBar = false;
                          }
                          
                          if(High[2]>High[1] && Low[2]<Low[1])
                          {
                              CheckIntraBar = true;
                          }
                      }
                      
                      else if(CheckIntraBar == true)
                      {
                          if(Close[0]>High[1])
                          {
                              EnterLong();
                          }
                      }
                      
                      Print(Time[0].ToString() + "| CheckIntraBar  " + CheckIntraBar);
                  }
              }
          }
          
          ​
          Thanks

          Comment


            #6
            Hello algospoke,

            To change the current bars background you can use BackBrush:



            You can call that when your condition is true to color the current bar.

            To make sure the previous bar is not colored you can make a condition like the following to reset previous bars.

            Code:
            if(CurrentBar > 0)
            {
               BackBrushes[1] = null;
            }
            JesseNinjaTrader Customer Service

            Comment


              #7
              Awesome Jesse. This was exactly what I was looking for. Appreciate all your help on this!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by rt61968, 02-17-2018, 05:24 PM
              3 responses
              2,733 views
              0 likes
              Last Post MasterEtrad3  
              Started by nuobo, Today, 07:43 PM
              0 responses
              2 views
              0 likes
              Last Post nuobo
              by nuobo
               
              Started by ETFVoyageur, Today, 02:04 PM
              3 responses
              21 views
              0 likes
              Last Post ETFVoyageur  
              Started by cre8able, Today, 06:18 PM
              0 responses
              6 views
              0 likes
              Last Post cre8able  
              Started by ETFVoyageur, Today, 06:05 PM
              0 responses
              7 views
              0 likes
              Last Post ETFVoyageur  
              Working...
              X