How to reference indicator inside Robot
How to reference indicator inside Robot
01 Dec 2015, 22:35
Hi,
I have searched long for a simple example to referencing an indicator inside a robot that initializes the indicator and then prints the value of the indicator on tick.
Please assist in helping me fix this code so that other people can benefit from this example.
Code for Indicator
using System; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using System.Linq; using System.Collections.Generic; namespace cAlgo.Indicators { [Indicator(IsOverlay = false, ScalePrecision = 5, AccessRights = AccessRights.FullAccess)] public class ATR : Indicator { [Parameter("Search Symbol")] public string tradePair { get; set; } [Parameter("Periods", DefaultValue = 20)] public int Period { get; set; } [Output("Main")] //RESULT IS WHAT GETS PASSED TO ROBOT WHEN ROBOT ENQUIRES ABOUT SYMBOL (ssymbol) public IndicatorDataSeries Result { get; set; } private ExponentialMovingAverage _ema; //ExponentialMovingAverage private IndicatorDataSeries _tempBuffer; public Symbol symbol; public MarketSeries mktSeries; public TimeFrame tf = TimeFrame.Hour; protected override void Initialize() { symbol = MarketData.GetSymbol(tradePair); mktSeries = MarketData.GetSeries(symbol, tf); _tempBuffer = CreateDataSeries(); _ema = Indicators.ExponentialMovingAverage(_tempBuffer, Period); //Indicators.ExponentialMovingAverage(_tempBuffer, Period); } public override void Calculate(int index) { //Calculate the output for the Symbol asked for by the User // and return double high = mktSeries.High[index]; double low = mktSeries.Low[index]; if (index == 0) { _tempBuffer[index] = (high - low); //* symbol.PipSize } else { double prevClose = mktSeries.Close[index - 1]; _tempBuffer[index] = (Math.Max(high, prevClose) - Math.Min(low, prevClose)); //* symbol.PipSize } Result[index] = _ema.Result[index]; } } }
Code for the robot that initializes two separate indicators on start and then calls the value onTick()
using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; using cAlgo.Indicators; using System; using System.Linq; using System.Collections.Generic; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)] public class NEWBOT : Robot { [Parameter(DefaultValue = "EURJPY")] public string tradepair { get; set; } public ATR ATRIndicator; public ATR ATRIndicator2; protected override void OnStart() { //Initialise the tradePair for two tradePairs ATRIndicator = Indicators.GetIndicator<ATR>("AUDUSD", 20); ATRIndicator2 = Indicators.GetIndicator<ATR>("EURUSD", 20); } protected override void OnTick() { Symbol s = MarketData.GetSymbol("AUDUSD"); Symbol s1 = MarketData.GetSymbol("EURUSD"); Print("SYMBOL {0} has Value of = {1}", "AUDUSD", Math.Round(ATRIndicator.Result.LastValue, s.Digits)); Print("SYMBOL {0} has Value of = {1}", "EURUSD", Math.Round(ATRIndicator2.Result.LastValue, s1.Digits)); } } }
So please assist in explaining why the code returns NaN onTick()
Replies
jhtrader
02 Dec 2015, 20:32
Your materials need to be better
Dear CAlgo Team,
The example I am trying to demonstrate is not for trading purposes. The material you have in Referencing Custom Indicators does not show any example where a robot reads the information from an indicator. This question is directly asks how to use the API to access information from an indicator inside a robot. There is no trading logic it is a simple "hello world" type example.
In order to build use of your platform I urge you to document many more examples so that people new to the platform can use the API better. I am trying to help bridge gaps in the knowledge base by framing simple searchable examples that I am sure will provide useful examples to the community, I am deliberately making them general so that they are not in any way specific to any strategy, just a simple use case on how to use the API.
Please consider that without users actively able to use the API the good work you are doing will amount to little.
Daedalusx086
05 Dec 2015, 02:02
( Updated at: 21 Dec 2023, 09:20 )
RE: Your materials need to be better
jhtrader said:
Dear CAlgo Team,
The example I am trying to demonstrate is not for trading purposes. The material you have in Referencing Custom Indicators does not show any example where a robot reads the information from an indicator. This question is directly asks how to use the API to access information from an indicator inside a robot. There is no trading logic it is a simple "hello world" type example.
In order to build use of your platform I urge you to document many more examples so that people new to the platform can use the API better. I am trying to help bridge gaps in the knowledge base by framing simple searchable examples that I am sure will provide useful examples to the community, I am deliberately making them general so that they are not in any way specific to any strategy, just a simple use case on how to use the API.
Please consider that without users actively able to use the API the good work you are doing will amount to little.
Hey JHTrader,
The NaN you are getting arises from the disparity between the rates of growth in the indexes on different timeframes. Your results are there, just not displaying in terms of the chart time frame - see below pic:
As the indexes grow at different rates (15 min at 4 times that of the 1 hour time frame), you need to convert the index of the chart time frame you want to get the result for to terms of the referenced time frame.
The following code shows the changes in the indicator that are required to get the referenced time frame to show in terms of the chart time frame (plus I updated the NaN check, just couldn't help myself):
public override void Calculate(int index) { //Calculate the output for the Symbol asked for by the User // and return // Convert index of current chart timeframe to the index of the timeframe being referenced int i_symbolindex = mktSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]); // Use this converted index to get values in relation to chart indexes double high = mktSeries.High[i_symbolindex]; double low = mktSeries.Low[i_symbolindex]; // Moved for error check var prevClose = mktSeries.Close[i_symbolindex - 1]; // Modified check for robustness if (double.IsNaN(prevClose)) { _tempBuffer[index] = (high - low); //* symbol.PipSize } else { _tempBuffer[index] = (Math.Max(high, prevClose) - Math.Min(low, prevClose)); //* symbol.PipSize } Result[index] = _ema.Result[index]; }
Cheers.
@Daedalusx086
boris_k
22 Jul 2023, 11:02
( Updated at: 22 Jul 2023, 11:03 )
RE: How to reference indicator inside Robot
Spotware said:
Dear Trader,
Please have a look at the Referencing Custom Indicators section of our API Programmers Guides.
The links are broken. Update them, please
@boris_k
PanagiotisChar
24 Jul 2023, 06:07
RE: RE: How to reference indicator inside Robot
boris_k said:
Spotware said:
Dear Trader,
Please have a look at the Referencing Custom Indicators section of our API Programmers Guides.
The links are broken. Update them, please
Hi,
The post is eight years old. You can find everything you need in the link below
https://help.ctrader.com/ctrader-automate/custom-indicators/
Need help? Join us on Telegram
@PanagiotisChar
Spotware
02 Dec 2015, 17:47
Dear Trader,
Please have a look at the Referencing Custom Indicators section of our API Programmers Guides.
In addition, we would like to inform you that we do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also can contact one of our Partners or post a job in Development Jobs section for further coding assistance.
@Spotware