If tick activity at a price exceeds the Threshold (double) , it is for marking.
In other word, indicator motitors tick velocity and marks price level which doubled the tick size,
Logically, below code should work, but I am facing the code overly takes time, marking oldest bars only. And not marking correctly.
I am not not sure whether my approach is correct... Any advise or suggestion for correction?
.
AddPlot(new Stroke(Brushes.Yellow, 2), PlotStyle.Hash, "DPOC_Line");
protected override void OnMarketData(MarketDataEventArgs e)
{
if (Bars.Count < LookBack)
return;
if (e.MarketDataType == MarketDataType.Last && e.Price != 0)
{
double price = e.Price;
DateTime currentTime = DateTime.Now;
if ((currentTime - lastResetTime).TotalSeconds >= NumberOfSeconds)
{
lastResetTime = currentTime;
if (tickHistory.Count >= LookBack)
tickHistory.Dequeue();
int totalTickCount = tickVelocityMap.Values.Sum();
tickHistory.Enqueue(Math.Max(totalTickCount, 1)); // Prevent zero values
if (totalTickCount > Threshold)
Print(" Tick History Updated -> New Count: " + tickHistory.Count);
}
if (!tickVelocityMap.ContainsKey(price))
{
tickVelocityMap[price] = 1;
}
else if (tickVelocityMap[price] < Threshold * 2) limit unnecessary growth
{
tickVelocityMap[price]++;
}
if (tickVelocityMap[price] % 50 == 0)
Print(" Tick Recorded at Price: " + price + " | Current Tick Count: " + tickVelocityMap[price]);
}
}
protected override void OnBarUpdate()
{
if (CurrentBars[0] < LookBack)
return;
markedPriceLevels.Clear(); // Reset markers for current bar
double avgTickSpeed = Math.Max(tickHistory.Average() / (LookBack * 2), 1);
double calculatedThreshold = avgTickSpeed * (Threshold / 10);
if (Math.Abs(calculatedThreshold - avgTickSpeed) > 5)
{
Print(" Debug: avgTickSpeed Calculation -> " + avgTickSpeed);
Print(" Debug: Adjusted Threshold Value -> " + calculatedThreshold);
}
foreach (var kvp in tickVelocityMap)
{
if (kvp.Value > calculatedThreshold)
{
markedPriceLevels.Add(kvp.Key);
Print(" Marked Price: " + kvp.Key + " | Tick Speed: " + kvp.Value + " (Threshold: " + calculatedThreshold + ")");
}
}
if (markedPriceLevels.Count > 0)
{
foreach (double price in markedPriceLevels)
{
Draw.Text(this, "Marker_" + CurrentBars[0] + "_" + price, "✦", 0, price, Brushes.Yellow);
}
}
}
[Browsable(false)]
[XmlIgnore]
public Series<double> DPOC_Line
{
get { return Values[0]; }
}

Comment