Help required
22 Jan 2016, 14:56
Hi,
I am a newbie coder and I am struggling to do the following as of now on a custom indicator -
Set the value of certain variables in my code to 0 at the start of a new bar.
Can someone please let me know/post code as to how this can be done .
Since I intend to use data based on market depth volumes on a 1 min chart and the void MarketDepthUpdated() method code runs as long as there is a change in any layers of the market depth - I would like the indicator to change certain variables to 0 so that the calculations start fresh on the very open of a new bar as opposed to the arrival of the first tick after the completion of the previous minute.
I understand that there is an OnBar method for cBots but not for indicators. Would have been very handy if the OnBar method was available for indicators too :)
I have read through the forums regarding Calculate and OnTick methods.When exactly is a new bar formed as far as indicator calculations are concerned ? - is it based on a new tick or based on time?
To clarify by 'based on time' I meant-
Example : for a 1 min chart - is the new bar formed at 10:00:00
or
does it only form when the next tick arrives ,say at 10:00:05 i.e. 5 seconds later.
Below is the code I have done so far -
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class mdb10 : Indicator
{
[Output("rbuyhist5", Color = Colors.Green, PlotType = PlotType.Histogram, Thickness = 1)]
public IndicatorDataSeries Buy5Bias { get; set; }
[Output("rsellhist5", Color = Colors.Red, PlotType = PlotType.Histogram, Thickness = 1)]
public IndicatorDataSeries Sell5Bias { get; set; }
[Output("rhistdelta5", Color = Colors.White, Thickness = 1)]
public IndicatorDataSeries DeltaFive { get; set; }
public MarketDepth _marketDepth;
public double tobA0 = 0;
public double tobA1 = 0;
public double tobA2 = 0;
public double tobA3 = 0;
public double tobA4 = 0;
public double tobB0 = 0;
public double tobB1 = 0;
public double tobB2 = 0;
public double tobB3 = 0;
public double tobB4 = 0;
public double tobA5Sum = 0;
public double tobB5Sum = 0;
public double rbuyhist5 = 0;
public double rsellhist5 = 0;
public double rhistdelta5 = 0;
public double ratiosell5 = 0;
public double ratiobuy5 = 0;
protected override void Initialize()
{
// Get Market Depth
_marketDepth = MarketData.GetMarketDepth(Symbol);
// subscribe to event Updated
_marketDepth.Updated += MarketDepthUpdated;
}
void MarketDepthUpdated()
{
foreach (var entry in _marketDepth.AskEntries)
{
tobA0 = _marketDepth.AskEntries[0].Volume;
tobA1 = _marketDepth.AskEntries[1].Volume;
tobA2 = _marketDepth.AskEntries[2].Volume;
tobA3 = _marketDepth.AskEntries[3].Volume;
tobA4 = _marketDepth.AskEntries[4].Volume;
}
foreach (var entry in _marketDepth.BidEntries)
{
tobB0 = _marketDepth.BidEntries[0].Volume;
tobB1 = _marketDepth.BidEntries[1].Volume;
tobB2 = _marketDepth.BidEntries[2].Volume;
tobB3 = _marketDepth.BidEntries[3].Volume;
tobB4 = _marketDepth.BidEntries[4].Volume;
}
tobA5Sum = tobA0 + tobA1 + tobA2 + tobA3 + tobA4;
tobB5Sum = tobB0 + tobB1 + tobB2 + tobB3 + tobB4;
if (tobB5Sum > tobA5Sum)
{
ratiobuy5 = tobB5Sum / tobA5Sum;
if (ratiobuy5 >= 1)
{
rbuyhist5 += ratiobuy5;
rhistdelta5 = (rbuyhist5 - rsellhist5);
}
}
if (tobA5Sum > tobB5Sum)
{
ratiosell5 = tobA5Sum / tobB5Sum;
if (ratiosell5 >= 1)
{
rsellhist5 += ratiosell5;
rhistdelta5 = (rbuyhist5 - rsellhist5);
}
}
}
// Trying to set the values of the following variables to 0 at the start of the new bar
// --> rbuyhist5, rsellhist5 and rhistdelta5.
public override void Calculate(int index)
{
Buy5Bias[index] = rbuyhist5;
Sell5Bias[index] = -(rsellhist5);
DeltaFive[index] = rhistdelta5;
}
}
}
Any help would be highly appreciated
Cheers

whis.gg
23 Jan 2016, 10:59
Hi!
What about something like that. It's just pseudo-code but you should get the idea. :)
private bool isNewCandle = true; private TimeSeries openTime; private TimeSpan timeFrame = MarketSeries.TimeFrame.ToTimeSpan(); // This method doesn't exit. You need to write one. public override void Calculate(int index) { if (isNewCandle) { SetValuesToZero(); // Sets your values to zero. openTime = MarketSeries.OpenTime.LastValue; isNewCandle = false; } if (Time > openTime + timeFrame) { isNewCandle = true; } }@whis.gg