Drawing lines from robots and indicators
Drawing lines from robots and indicators
27 Mar 2018, 11:57
As the title says robots and indicators from tis previous post [/forum/whats-new/913]
However all explanations are for indicator. Could someone please help me do the same for ROBOT
I would like any example that impliment a robot version to this code then plot vertical lines. Thanks a lot
[Indicator("Name", IsOverlay = true, AutoRescale = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class Renko : Indicator
Replies
PanagiotisCharalampous
27 Mar 2018, 12:09
Hi ceakuk,
See below the first example as a robot
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 NewcBot : Robot { [Parameter(DefaultValue = 30, MinValue = 1)] public int Period { get; set; } protected override void OnStart() { // Put your initialization logic here } protected override void OnTick() { RedrawLines(); } private void RedrawLines() { int count = MarketSeries.Close.Count; int maxIndex1 = FindNextLocalExtremum(MarketSeries.High, count - 1, true); int maxIndex2 = FindNextLocalExtremum(MarketSeries.High, maxIndex1 - Period, true); int minIndex1 = FindNextLocalExtremum(MarketSeries.Low, count - 1, false); int minIndex2 = FindNextLocalExtremum(MarketSeries.Low, minIndex1 - Period, false); int startIndex = Math.Min(maxIndex2, minIndex2) - 100; int endIndex = count + 100; DrawTrendLine("high", startIndex, endIndex, maxIndex1, MarketSeries.High[maxIndex1], maxIndex2, MarketSeries.High[maxIndex2]); DrawTrendLine("low", startIndex, endIndex, minIndex1, MarketSeries.Low[minIndex1], minIndex2, MarketSeries.Low[minIndex2]); } private void DrawTrendLine(string lineName, int startIndex, int endIndex, int index1, double value1, int index2, double value2) { double gradient = (value2 - value1) / (index2 - index1); double startValue = value1 + (startIndex - index1) * gradient; double endValue = value1 + (endIndex - index1) * gradient; ChartObjects.DrawLine(lineName, startIndex, startValue, endIndex, endValue, Colors.Gray); ChartObjects.DrawLine(lineName + "_red", index1, value1, index2, value2, Colors.Red); } private int FindNextLocalExtremum(DataSeries series, int maxIndex, bool findMax) { for (int index = maxIndex; index >= 0; index--) { if (IsLocalExtremum(series, index, findMax)) { return index; } } return 0; } private bool IsLocalExtremum(DataSeries series, int index, bool findMax) { int end = Math.Min(index + Period, series.Count - 1); int start = Math.Max(index - Period, 0); double value = series[index]; for (int i = start; i < end; i++) { if (findMax && value < series[i]) return false; if (!findMax && value > series[i]) return false; } return true; } protected override void OnStop() { // Put your deinitialization logic here } } }
Let me know if this helps.
Best Regards,
Panagiotis
@PanagiotisCharalampous
ceakuk
27 Mar 2018, 12:52
RE:
Panagiotis Charalampous said:
Thank you very much for always answering promptly. It works with live data. is it possible to draw on back testing?
Sorry I had another question about indicator output
if possible could you please tell me why the indicator output MUST to be in the form
[Output("Close", Color = Colors.DimGray, Thickness = 1, PlotType = PlotType.Points)] public IndicatorDataSeries Close { get; set; }
I just want to an output of a single double.
I try the one below and doesnt work when I use a robot to fetch the output value
[Output("Close")] public double Close { get; set; }
Thanks a lot
@ceakuk
PanagiotisCharalampous
27 Mar 2018, 14:43
Hi ceakuk,
It is currently not possible to draw objects during backtesting. This will be added in a future release.
Regarding the output issue, could you please share the cBot and the Indicator so that we can advise you what is wrong with the code?
Best Regards,
Panagiotis
@PanagiotisCharalampous
ceakuk
27 Mar 2018, 12:03
RE:
if possible could you please tell me why the indicator output has to be in the form
I try the one below and doesnt work when I use a robot to fetch the output value
@ceakuk