Indicator Results Not Translating to Cbot
Indicator Results Not Translating to Cbot
19 Aug 2020, 10:12
Hi,
Would anyone be able to assist with the below.
My indicator works fine and I can plot it on a chart and access the values. But when I declare the indicator into a test bot, then try to retrieve a value, it only ever returns one value.
CBOT:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Test2 : Robot
{
[Parameter(DefaultValue = 3, MinValue = 1)]
public int HLPeriods { get; set; }
private Test ATBHL;
protected override void OnStart()
{
// Put your initialization logic here
ATBHL = Indicators.GetIndicator<Test>(HLPeriods);
}
protected override void OnTick()
{
// Put your core logic here
}
protected override void OnBar()
{
// Put your core logic here
Print("ATBHL.High.Last(0) = ", ATBHL.High.Last(0));
Print("ATBHL.High.Last(1) = ", ATBHL.High.Last(1));
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
INDICATOR:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Test : Indicator
{
[Parameter(DefaultValue = 3, MinValue = 1)]
public int HLPeriods { get; set; }
[Output("High", PlotType = PlotType.Points, LineColor = "Green", Thickness = 10)]
public IndicatorDataSeries High { get; set; }
[Output("Low", PlotType = PlotType.Points, LineColor = "Red", Thickness = 10)]
public IndicatorDataSeries Low { get; set; }
public double t1 = 0, t2 = 0, t3 = 0, t4 = 0;
protected override void Initialize()
{
// Initialize and create nested indicators
}
public override void Calculate(int index)
{
// Calculate value at specified index
// Result[index] = ...
int e = 1;
int f = 1;
for (int a = 0; a <= HLPeriods; a++)
{
if ((Bars.HighPrices[index] > Bars.HighPrices[index + e]) && (Bars.HighPrices[index] > Bars.HighPrices[index - e]))
{
e++;
if (e == HLPeriods + 1)
{
t2 = t1;
t1 = Bars.HighPrices[index];
}
}
else
{
a = HLPeriods;
t1 = t1;
}
}
for (int c = 0; c <= HLPeriods; c++)
{
if ((Bars.LowPrices[index] < Bars.LowPrices[index + f]) && (Bars.LowPrices[index] < Bars.LowPrices[index - f]))
{
f++;
if (f == HLPeriods + 1)
{
t4 = t3;
t3 = Bars.LowPrices[index];
}
}
else
{
c = HLPeriods;
t3 = t3;
}
}
High[index] = t1;
Low[index] = t3;
}
}
}
I have attached a photo below. Clearly the indicator is working on the chart and the values change, but the CBOT is not able to retrieve them.
Replies
mlcilia
19 Aug 2020, 10:56
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi mlcilia,
The problem lies in this condition
Bars.HighPrices[index] > Bars.HighPrices[index + e]
You are looking for a future value which will always be null for future values. Therefore the code will never enter into this part of the code. It will only work for past values and this is why you get past values when you add an indicator. But if you leave the indicator on the chart for a while, you will see that the indicator behaves the same way.
Best Regards,
Panagiotis
Thanks Panagiotis, will give this ago and hopefully it works :).
@mlcilia
PanagiotisCharalampous
19 Aug 2020, 10:39
Hi mlcilia,
The problem lies in this condition
You are looking for a future value which will always be null for future values. Therefore the code will never enter into this part of the code. It will only work for past values and this is why you get past values when you add an indicator. But if you leave the indicator on the chart for a while, you will see that the indicator behaves the same way.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous