Remove the need for enabling tick replay on the data series in order for the indicator to work

This commit is contained in:
moshferatu 2024-01-23 12:02:17 -08:00
parent 3c339ecb56
commit 27a94141a2

View File

@ -28,8 +28,11 @@ namespace NinjaTrader.NinjaScript.Indicators
[CategoryOrder("Plots", 2)] [CategoryOrder("Plots", 2)]
public class OrderFlowOscillator : Indicator public class OrderFlowOscillator : Indicator
{ {
private double TradesAtBid = 0; private const int PrimaryBars = 0;
private double TradesAtAsk = 0; private const int TickBars = 1;
private int Buys = 0;
private int Sells = 0;
private Series<double> AverageGain; private Series<double> AverageGain;
private Series<double> AverageLoss; private Series<double> AverageLoss;
@ -40,7 +43,7 @@ namespace NinjaTrader.NinjaScript.Indicators
{ {
Description = @"Credit to @jamestsliu for the idea, though the result is different."; Description = @"Credit to @jamestsliu for the idea, though the result is different.";
Name = "Order Flow Oscillator"; Name = "Order Flow Oscillator";
Calculate = Calculate.OnEachTick; Calculate = Calculate.OnBarClose;
IsOverlay = false; IsOverlay = false;
DisplayInDataBox = true; DisplayInDataBox = true;
DrawOnPricePanel = true; DrawOnPricePanel = true;
@ -60,6 +63,9 @@ namespace NinjaTrader.NinjaScript.Indicators
{ {
AddLine(UpperLevelStroke, UpperLevel, "Upper Level"); AddLine(UpperLevelStroke, UpperLevel, "Upper Level");
AddLine(LowerLevelStroke, LowerLevel, "Lower Level"); AddLine(LowerLevelStroke, LowerLevel, "Lower Level");
AddDataSeries(Instrument.FullName, new BarsPeriod {
BarsPeriodType = BarsPeriodType.Tick, Value = 1 }, Bars.TradingHours.Name);
} }
else if (State == State.DataLoaded) else if (State == State.DataLoaded)
{ {
@ -70,11 +76,23 @@ namespace NinjaTrader.NinjaScript.Indicators
protected override void OnBarUpdate() 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) if (CurrentBar < 2)
return; return;
// Order Flow Imbalance // Order Flow Imbalance
double OFI = TradesAtAsk - TradesAtBid; double OFI = Buys - Sells;
if (CurrentBar < Period) if (CurrentBar < Period)
{ {
@ -92,24 +110,30 @@ namespace NinjaTrader.NinjaScript.Indicators
double OFI_RSI = 100 - (100 / (1 + RS)); double OFI_RSI = 100 - (100 / (1 + RS));
Oscillator[0] = OFI_RSI; Oscillator[0] = OFI_RSI;
Sells = 0;
Buys = 0;
} }
/*
The following method only works with tick replay enabled on the data series:
protected override void OnMarketData(MarketDataEventArgs marketDataUpdate) protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
{ {
if (IsFirstTickOfBar) if (IsFirstTickOfBar)
{ {
TradesAtBid = 0; Sells = 0;
TradesAtAsk = 0; Buys = 0;
} }
if (marketDataUpdate.MarketDataType == MarketDataType.Last) if (marketDataUpdate.MarketDataType == MarketDataType.Last)
{ {
if (marketDataUpdate.Price <= marketDataUpdate.Bid) if (marketDataUpdate.Price <= marketDataUpdate.Bid)
TradesAtBid += 1; Sells += 1;
else if (marketDataUpdate.Price >= marketDataUpdate.Ask) else if (marketDataUpdate.Price >= marketDataUpdate.Ask)
TradesAtAsk += 1; Buys += 1;
} }
} }
*/
public override string DisplayName public override string DisplayName
{ {