IsLastBar alternative for Robots
IsLastBar alternative for Robots
27 Mar 2018, 14:11
Hi,
I have an indicator that uses IsLastBar
IsLastBar
is thare an alternative in Robots?
I'm having a robot that fetches output of indicator but im not impressed by indicator. I'm trying to migrate all my indicator codes to the robot. Unfortunately IsLastBar is only available for indicators. thanks in advance
Replies
ctid386599
27 Mar 2018, 14:35
RE:
The best would be to be able to have output which is of type double not of type IndicatorDataSeries
@ctid386599
PanagiotisCharalampous
27 Mar 2018, 14:41
Hi,
It is possible to return double values using an indicator. If you provide us with your indicator, we might be able to show you how to do this.
Best Regards,
Panagiotis
@PanagiotisCharalampous
ctid386599
27 Mar 2018, 15:00
RE:
im using this renko /algos/indicators/show/1086
using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; using System; using System.Collections.Generic; using System.Linq; namespace cAlgo { [Indicator("Renko", IsOverlay = true, AutoRescale = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class Renko : Indicator { [Parameter("Renko (Pips)", DefaultValue = 10, MinValue = 0.1, Step = 1)] public double RenkoPips { get; set; } [Parameter("Bricks To Show", DefaultValue = 100, MinValue = 1)] public int BricksToShow { get; set; } [Parameter("Zoom Level", DefaultValue = 3, MinValue = 0, MaxValue = 5, Step = 1)] public double ZoomLevel { get; set; } [Parameter("Bullish Color", DefaultValue = "SeaGreen")] public string ColorBull { get; set; } [Parameter("Bearish Color", DefaultValue = "Tomato")] public string ColorBear { get; set; } [Output("Open", Color = Colors.DimGray, Thickness = 1, PlotType = PlotType.Points)] public IndicatorDataSeries Open { get; set; } [Output("High", Color = Colors.DimGray, Thickness = 1, PlotType = PlotType.Points)] public IndicatorDataSeries High { get; set; } [Output("Low", Color = Colors.DimGray, Thickness = 1, PlotType = PlotType.Points)] public IndicatorDataSeries Low { get; set; } [Output("Close", Color = Colors.DimGray, Thickness = 1, PlotType = PlotType.Points)] public IndicatorDataSeries Close { get; set; } public class Brick { public double Open { get; set; } public double Close { get; set; } } private List<Brick> renkos = new List<Brick>(); private double closeLastValue, thickness, renkoPips, renkoLastValue; private Colors colorBull, colorBear; private bool colorError; private int lastCount; protected override void Initialize() { if (!Enum.TryParse<Colors>(ColorBull, out colorBull) || !Enum.TryParse<Colors>(ColorBear, out colorBear)) colorError = true; renkoPips = RenkoPips * Symbol.PipSize; thickness = Math.Pow(2, ZoomLevel) - (ZoomLevel > 0 ? 1 : 0); renkoLastValue = 0; } public override void Calculate(int index) { if (colorError) { ChartObjects.DrawText("Error0", "{o,o}\n/)_)\n \" \"\nOops! Incorrect colors.", StaticPosition.TopCenter, Colors.Gray); return; } if (renkoLastValue == 0) { var open = MarketSeries.Open.LastValue; renkoLastValue = open - (open % renkoPips) + renkoPips / 2; } closeLastValue = MarketSeries.Close.LastValue; while (closeLastValue >= renkoLastValue + renkoPips * 1.5) { renkoLastValue += renkoPips; renkos.Insert(0, new Brick { Open = renkoLastValue - renkoPips / 2, Close = renkoLastValue + renkoPips / 2 }); if (renkos.Count() > BricksToShow) renkos.RemoveRange(BricksToShow, renkos.Count() - BricksToShow); if (IsLastBar) UpdateHistory(index); } while (closeLastValue <= renkoLastValue - renkoPips * 1.5) { renkoLastValue -= renkoPips; renkos.Insert(0, new Brick { Open = renkoLastValue + renkoPips / 2, Close = renkoLastValue - renkoPips / 2 }); if (renkos.Count() > BricksToShow) renkos.RemoveRange(BricksToShow, renkos.Count() - BricksToShow); if (IsLastBar) UpdateHistory(index); } bool isNewBar = MarketSeries.Close.Count > lastCount; if (IsLastBar && isNewBar) { UpdateHistory(index); Open[index - BricksToShow] = double.NaN; High[index - BricksToShow] = double.NaN; Low[index - BricksToShow] = double.NaN; Close[index - BricksToShow] = double.NaN; lastCount = MarketSeries.Close.Count; } if (IsRealTime) UpdateLive(index); } private void UpdateHistory(int index) { for (int i = 0; i < BricksToShow - 1 && i < renkos.Count() - 1; i++) { var color = renkos[i].Open < renkos[i].Close ? colorBull : colorBear; ChartObjects.DrawLine(string.Format("renko.Last({0})", i + 1), index - i - 1, renkos[i].Open, index - i - 1, renkos[i].Close, color, thickness, LineStyle.Solid); Open[index - i - 1] = renkos[i].Open; High[index - i - 1] = Math.Max(renkos[i].Open, renkos[i].Close); Low[index - i - 1] = Math.Min(renkos[i].Open, renkos[i].Close); Close[index - i - 1] = renkos[i].Close; } } private void UpdateLive(int index) { double y1, y2; var top = Math.Max(renkos[0].Open, renkos[0].Close); var bottom = Math.Min(renkos[0].Open, renkos[0].Close); if (closeLastValue > top) y1 = top; else if (closeLastValue < bottom) y1 = bottom; else y1 = closeLastValue; y2 = closeLastValue; var colorLive = y1 < y2 ? colorBull : colorBear; ChartObjects.DrawLine("renko.Live", index, y1, index, y2, colorLive, thickness, LineStyle.Solid); Open[index] = y1; High[index] = y1 > y2 ? y1 : y2; Low[index] = y1 < y2 ? y1 : y2; Close[index] = y2; } } }
I want to use
private void UpdateHistory(int index) { Open = renkos[0].Open; High = Math.Max(renkos[0].Open, renkos[0].Close); Low = Math.Min(renkos[0].Open, renkos[0].Close); Close = renkos[0].Close; }
I just need the newest entries to renkos which is in [0].
Thanks
@ctid386599
PanagiotisCharalampous
27 Mar 2018, 15:15
Hi,
You can have public properties of type double in an indicator and read them from a cBot. They don't need to be output properties. See below
public int LastValue;
Then you can read these properties from cBots like below
protected override void OnStart() { _renko = Indicators.GetIndicator<Renko>(); } protected override void OnBar() { var renko = _renko.LastValue; }
Let me know if this helps.
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
27 Mar 2018, 14:16
Dear Trader,
Thanks for posting in our forum. Such property is not available since it doesn't make sense in the context of a robot. A robot is always executed on the last bar in contrast to an indicator that needs to calculate the values of all previous bars before calculating the last one. If you can become more specific to what you are trying to do then maybe we could direct you towards the correct solution.
Best Regards,
Panagiotis
@PanagiotisCharalampous