referencing an custom Indicator
referencing an custom Indicator
25 Aug 2020, 11:56
Dear all,
I tried for hours but did not succeed, so I kindly ask for support: I try to reference another indicator within an new one. This works fine als long as the referenced indicator is a built in one. But when I try it with a custom one, even the SampleSMA example from the indicator guide, I can't get any result. And YES, I have checkboxed the custom indicator with "Manage References" within cTrader. I have no idea why it does not work - any kind ideas? Thank you very much
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 NNFXInfosonChart : Indicator
{
public MoneyFlowIndex _cci_daily;
public SampleSMA _sma;
string test;
protected override void Initialize()
{
_cci_daily = Indicators.MoneyFlowIndex(MarketData.GetBars(TimeFrame.Daily), 14);
_sma = Indicators.GetIndicator<SampleSMA>(MarketData.GetBars(TimeFrame.Daily), 14);
}
public override void Calculate(int index)
{
test = Convert.ToString(_sma.Result.LastValue);
Chart.DrawStaticText("Symbols", test, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Silver);
}
}
}
Replies
xabbu
25 Aug 2020, 12:54
Dear Panagiotis,
many thanks for picking this question up. The code is in my first post, the SampleSMA ist from cTrader 3.8 or from cTrader Automate Guides. The simple (demo) task is to display the latest value (as text, in the middle of the top screen) in the chart area. This works fine with indicators, which are build into the cTrader logic (MoneyflowIndix, CCI etc) but for me NOT with custom indicators.
Q: Can you please explain to us what do you mean that it does not work?
A: nothing is displayed in the top middle of the screen (when using CCI or another build-in indicator it works)
Does this make my question clearer..? Best regards,
SampleSMA Code:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, AccessRights = AccessRights.None)]
public class SampleSMA : Indicator
{
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter(DefaultValue = 14)]
public int Periods { get; set; }
[Output("Main", LineColor = "Turquoise")]
public IndicatorDataSeries Result { get; set; }
public override void Calculate(int index)
{
double sum = 0.0;
for (int i = index - Periods + 1; i <= index; i++)
{
sum += Source[i];
}
Result[index] = sum / Periods;
}
}
}
my test code:
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 NNFXInfosonChart : Indicator
{
public MoneyFlowIndex _cci_daily;
public SampleSMA _sma;
string test;
protected override void Initialize()
{
_cci_daily = Indicators.MoneyFlowIndex(MarketData.GetBars(TimeFrame.Daily), 14);
_sma = Indicators.GetIndicator<SampleSMA>(MarketData.GetBars(TimeFrame.Daily), 14);
}
public override void Calculate(int index)
{
test = Convert.ToString(_sma.Result.LastValue);
Chart.DrawStaticText("Symbols", test, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Silver);
}
}
}
@xabbu
PanagiotisCharalampous
25 Aug 2020, 14:27
Hi xabbu,
You are passing wrong parameters in the indicator. Here is the correct way to initialize it
_sma = Indicators.GetIndicator<SampleSMA>(MarketData.GetBars(TimeFrame.Daily).ClosePrices, 14);
Best Regards,
Panagiotis
@PanagiotisCharalampous
BernhardCS
13 May 2024, 22:23
( Updated at: 14 May 2024, 05:22 )
RE: referencing an custom Indicator
PanagiotisCharalampous said:
Hi xabbu,
You are passing wrong parameters in the indicator. Here is the correct way to initialize it
_sma = Indicators.GetIndicator<SampleSMA>(MarketData.GetBars(TimeFrame.Daily).ClosePrices, 14);
Best Regards,
Panagiotis
Hi Panagiotis,
is there a particular indicator calculation order or could we influence it, e.g. by priorities?
In the example mentioned above the custom indicator “NNFXInfosonChart” uses the values of custom indicator “SampleSMA”, but how we can make sure that SampleSMA.LastValue is belonging to current Index (Index of NNFXInfosonChart.Calculation(THIS_INDEX)) and not to the previous index when retrieved in NNFXInfosonChart.Calculation-method?
Thank you so much.
Best regards,
Bernhard
@BernhardCS
PanagiotisCharalampous
14 May 2024, 05:48
RE: RE: referencing an custom Indicator
bernhardCM said:
PanagiotisCharalampous said:
Hi xabbu,
You are passing wrong parameters in the indicator. Here is the correct way to initialize it
_sma = Indicators.GetIndicator<SampleSMA>(MarketData.GetBars(TimeFrame.Daily).ClosePrices, 14);
Best Regards,
Panagiotis
Hi Panagiotis,
is there a particular indicator calculation order or could we influence it, e.g. by priorities?
In the example mentioned above the custom indicator “NNFXInfosonChart” uses the values of custom indicator “SampleSMA”, but how we can make sure that SampleSMA.LastValue is belonging to current Index (Index of NNFXInfosonChart.Calculation(THIS_INDEX)) and not to the previous index when retrieved in NNFXInfosonChart.Calculation-method?
Thank you so much.
Best regards,
Bernhard
LastValue is always the value of the last index, since the Calculate() method is called when you call the property.
@PanagiotisCharalampous
BernhardCS
14 May 2024, 09:02
RE: RE: RE: referencing an custom Indicator
great, thanks for your prompt answer!
@BernhardCS
PanagiotisCharalampous
25 Aug 2020, 12:47
Hi xabbu,
Can you please explain to us what do you mean that it does not work? Also can you provide the indicator code so that we can reproduce what you are seeing?
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous