From 27a94141a274f611b15c75c24f5dae6423678f46 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 23 Jan 2024 12:02:17 -0800 Subject: [PATCH] Remove the need for enabling tick replay on the data series in order for the indicator to work --- indicators/OrderFlowOscillator.cs | 58 ++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/indicators/OrderFlowOscillator.cs b/indicators/OrderFlowOscillator.cs index 6a16878..5323f3a 100644 --- a/indicators/OrderFlowOscillator.cs +++ b/indicators/OrderFlowOscillator.cs @@ -28,8 +28,11 @@ namespace NinjaTrader.NinjaScript.Indicators [CategoryOrder("Plots", 2)] public class OrderFlowOscillator : Indicator { - private double TradesAtBid = 0; - private double TradesAtAsk = 0; + private const int PrimaryBars = 0; + private const int TickBars = 1; + + private int Buys = 0; + private int Sells = 0; private Series AverageGain; private Series AverageLoss; @@ -40,7 +43,7 @@ namespace NinjaTrader.NinjaScript.Indicators { Description = @"Credit to @jamestsliu for the idea, though the result is different."; Name = "Order Flow Oscillator"; - Calculate = Calculate.OnEachTick; + Calculate = Calculate.OnBarClose; IsOverlay = false; DisplayInDataBox = true; DrawOnPricePanel = true; @@ -60,6 +63,9 @@ namespace NinjaTrader.NinjaScript.Indicators { AddLine(UpperLevelStroke, UpperLevel, "Upper Level"); AddLine(LowerLevelStroke, LowerLevel, "Lower Level"); + + AddDataSeries(Instrument.FullName, new BarsPeriod { + BarsPeriodType = BarsPeriodType.Tick, Value = 1 }, Bars.TradingHours.Name); } else if (State == State.DataLoaded) { @@ -70,11 +76,23 @@ namespace NinjaTrader.NinjaScript.Indicators protected override void OnBarUpdate() { + if (BarsInProgress == TickBars) + { + int currentBar = CurrentBars[1]; + if (BarsArray[1].GetClose(currentBar) <= BarsArray[1].GetBid(currentBar)) + Sells += 1; + else if (BarsArray[1].GetClose(currentBar) >= BarsArray[1].GetAsk(currentBar)) + Buys += 1; + } + + if (BarsInProgress != PrimaryBars) + return; + if (CurrentBar < 2) return; // Order Flow Imbalance - double OFI = TradesAtAsk - TradesAtBid; + double OFI = Buys - Sells; if (CurrentBar < Period) { @@ -92,24 +110,30 @@ namespace NinjaTrader.NinjaScript.Indicators double OFI_RSI = 100 - (100 / (1 + RS)); Oscillator[0] = OFI_RSI; + + Sells = 0; + Buys = 0; } - protected override void OnMarketData(MarketDataEventArgs marketDataUpdate) - { - if (IsFirstTickOfBar) + /* + The following method only works with tick replay enabled on the data series: + protected override void OnMarketData(MarketDataEventArgs marketDataUpdate) { - TradesAtBid = 0; - TradesAtAsk = 0; - } + if (IsFirstTickOfBar) + { + Sells = 0; + Buys = 0; + } - if (marketDataUpdate.MarketDataType == MarketDataType.Last) - { - if (marketDataUpdate.Price <= marketDataUpdate.Bid) - TradesAtBid += 1; - else if (marketDataUpdate.Price >= marketDataUpdate.Ask) - TradesAtAsk += 1; + if (marketDataUpdate.MarketDataType == MarketDataType.Last) + { + if (marketDataUpdate.Price <= marketDataUpdate.Bid) + Sells += 1; + else if (marketDataUpdate.Price >= marketDataUpdate.Ask) + Buys += 1; + } } - } + */ public override string DisplayName {