If you can provide a ninjascript snip of code that would go under OnEachTick for this specific example that would be very helpful. Thanks
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
OnEachTick
Collapse
X
-
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. ThanksTags: None
-
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; } } }
-
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
-
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.
Comment
-
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?
ThanksHTML 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); } } }
Comment
-
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; }
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Yesterday, 05:17 AM
|
0 responses
56 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
133 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
73 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
45 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
49 views
0 likes
|
Last Post
|

Comment