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)]
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<double> AverageGain;
private Series<double> 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
{