Using the output of an indicator in a bot
05 Sep 2015, 06:40
I've got a customer indicator that I've referenced. Now I need to access it's values.
I was hoping to get a dataseries so I could start to use functions like hascrossedabove with the symbol marketseries.
The indicator has 3 outputs (It's a multi time frame one), How do i go about doing this:
I've defined the indicator in my onstart:
multi = Indicators.GetIndicator<EMAMTF>(14, EMATimeframe1, EMATimeframe2, EMATimeframe3);
Then in my ontick I've tried to use:
Print(multi.EMA1.Result.Lastvalue);
But it doesn't work.
by the way the indicator is this standard EMAMTF from this website:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
public class EMAMTF : Indicator
{
[Parameter(DefaultValue = 14)]
public int Periods { get; set; }
[Parameter("EMA Timeframe1", DefaultValue = "Minute15")]
public TimeFrame EMATimeframe1 { get; set; }
[Parameter("EMA Timeframe2", DefaultValue = "Hour")]
public TimeFrame EMATimeframe2 { get; set; }
[Parameter("EMA Timeframe3", DefaultValue = "Hour4")]
public TimeFrame EMATimeframe3 { get; set; }
[Output("EMA1", Color = Colors.Blue)]
public IndicatorDataSeries EMA1 { get; set; }
[Output("EMA2", Color = Colors.Red)]
public IndicatorDataSeries EMA2 { get; set; }
[Output("EMA3", Color = Colors.Yellow)]
public IndicatorDataSeries EMA3 { get; set; }
private MarketSeries series1;
private MarketSeries series2;
private MarketSeries series3;
private ExponentialMovingAverage Ema1;
private ExponentialMovingAverage Ema2;
private ExponentialMovingAverage Ema3;
protected override void Initialize()
{
series1 = MarketData.GetSeries(EMATimeframe1);
series2 = MarketData.GetSeries(EMATimeframe2);
series3 = MarketData.GetSeries(EMATimeframe3);
Ema1 = Indicators.ExponentialMovingAverage(series1.Close, Periods);
Ema2 = Indicators.ExponentialMovingAverage(series2.Close, Periods);
Ema3 = Indicators.ExponentialMovingAverage(series3.Close, Periods);
}
public override void Calculate(int index)
{
var index1 = GetIndexByDate(series1, MarketSeries.OpenTime[index]);
if (index1 != -1)
{
EMA1[index] = Ema1.Result[index1];
}
var index2 = GetIndexByDate(series2, MarketSeries.OpenTime[index]);
if (index2 != -1)
{
EMA2[index] = Ema2.Result[index2];
}
var index3 = GetIndexByDate(series3, MarketSeries.OpenTime[index]);
if (index3 != -1)
{
EMA3[index] = Ema3.Result[index3];
}
}
private int GetIndexByDate(MarketSeries series, DateTime time)
{
for (int i = series.Close.Count - 1; i > 0; i--)
{
if (time == series.OpenTime[i])
return i;
}
return -1;
}
}
}
Replies
ClickAlgo
05 Sep 2015, 14:32
Also try and make the Ema1 private field public and access this value instead.
@ClickAlgo
cloesd
06 Sep 2015, 03:45
RE:
Paul_Hayes said:
try this and let me know if it works, I have had some issues with indicator objects loading lazily, they have a reference, but no values.
double x =multi.EMA1.Result.Lastvalue;
Print(x);
Error CS1061: 'cAlgo.API.IndicatorDataSeries' does not contain a definition for 'Result' and no extension method 'Result' accepting a first argument of type 'cAlgo.API.IndicatorDataSeries' could be found (are you missing a using directive or an assembly reference?)
This is what I get.
I've set everthing to public in the indicator aswell, still same issue:
public ExponentialMovingAverage Ema1;
public ExponentialMovingAverage Ema2;
public ExponentialMovingAverage Ema3;
@cloesd
ClickAlgo
06 Sep 2015, 08:08
Did you try and run the robot on a weekend when the markets were close?
If so then there are no data-feeds coming into cTrader, instead on a weekend you need to test your functionality with a back-test and log the results.
Aso from your robot you do not need to declare the result..
x = multi.EMA1.LastValue; Print(x);
@ClickAlgo

ClickAlgo
05 Sep 2015, 13:50
try this and let me know if it works, I have had some issues with indicator objects loading lazily, they have a reference, but no values.
double x =multi.EMA1.Result.Lastvalue;
Print(x);
@ClickAlgo