Problem with drawing output of custom indicator
Problem with drawing output of custom indicator
08 Dec 2020, 15:53
Hi,
I am struggling with relative simple problem.
I can't get drawings of my indicator to indicator panel. I'm getting nan value or " Crashed in Calculate with NullReferenceException".
I check debugger output and output results are OK.
This is my script:
Any clue ? Thanks in advance.
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
public class ADXbyPem : Indicator
{
[Parameter("ADX Period", Group = "ADX", DefaultValue = 20, MinValue = 6, Step = 1, MaxValue = 100)]
public int Adx_per { get; set; }
[Parameter("ADX MA Period", Group = "ADX", DefaultValue = 20, MinValue = 6, Step = 1, MaxValue = 100)]
public int Adx_ma_per { get; set; }
[Output("adx_hist", LineColor = "Red", PlotType = PlotType.DiscontinuousLine, Thickness = 1)]
public IndicatorDataSeries adx_ma_n { get; set; }
public IndicatorDataSeries adx_hist;
private DirectionalMovementSystem adx;
private SimpleMovingAverage ma_adx;
private IndicatorDataSeries adx_h;
protected override void Initialize()
{
adx_h = CreateDataSeries();
adx = Indicators.DirectionalMovementSystem (Adx_per);
ma_adx = Indicators.SimpleMovingAverage(adx_h, Adx_ma_per);
}
public override void Calculate(int index)
{
adx_hist[index] = adx.DIMinus[index]- adx.DIPlus[index];
adx_h [index] = adx_hist[index];
adx_ma_n[index] = ma_adx.Result[index];
Print(" Adx hist:" + adx_ma_n.LastValue);
}
}
}
Replies
fx.pro
10 Dec 2020, 21:14
Hi,
Thanks for reply. I posted working final version.
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
public class ADXbyPem : Indicator
{
[Parameter("ADX Period", Group = "ADX", DefaultValue = 7, MinValue = 6, Step = 1, MaxValue = 21)]
public int Adx_per { get; set; }
[Parameter("ADX MA Period", Group = "ADX", DefaultValue = 14, MinValue = 6, Step = 1, MaxValue = 36)]
public int Adx_ma_per { get; set; }
[Parameter("Treshold skew", Group = "ADX", DefaultValue = 30, MinValue = -30, MaxValue = 70, Step = 10)]
public int Adx_skew { get; set; }
[Parameter("ADX Looback", Group = "ADX", DefaultValue = 130, MinValue = 10, Step = 10, MaxValue = 360)]
public int Adx_lback { get; set; }
public MovingAverage ma_adx;
public DirectionalMovementSystem adx;
public IndicatorDataSeries adx_h;
public bool BUYcond;
public bool SELLcond;
[Output("ADX", LineColor = "gold", PlotType = PlotType.Histogram)]
public IndicatorDataSeries Adx_ma_n { get; set; }
protected override void Initialize()
{
adx_h = CreateDataSeries();
adx = Indicators.DirectionalMovementSystem(Adx_per);
ma_adx = Indicators.MovingAverage(adx_h, Adx_ma_per, MovingAverageType.Exponential);
}
public override void Calculate(int index)
{
IndicatorArea.DrawHorizontalLine("Zero line", 0, Color.White, 1, LineStyle.Solid);
IndicatorArea.DrawHorizontalLine("Treshold Level Buy", -30 - Adx_skew, Color.Green, 1, LineStyle.Solid);
IndicatorArea.DrawHorizontalLine("Treshold Level Sell", 30 + Adx_skew, Color.Red, 1, LineStyle.Solid);
if (index >= Adx_ma_per && index >= Adx_per)
{
adx_h[index] = adx.DIPlus[index] - adx.DIMinus[index];
var MaAdxHL = ma_adx.Result.Maximum(Adx_lback);
var MaAdxLL = ma_adx.Result.Minimum(Adx_lback);
Adx_ma_n[index] = ((ma_adx.Result[index] - MaAdxLL) / (MaAdxHL - MaAdxLL) * 100 - 50) * 2;
}
BUYcond = Adx_ma_n.LastValue < (-30 - Adx_skew);
SELLcond = Adx_ma_n.LastValue > (30 + Adx_skew);
}
}
}
@fx.pro
fx.pro
10 Dec 2020, 21:26
( Updated at: 21 Dec 2023, 09:22 )
Passing values to cbot
PanagiotisCharalampous said:
Hi fx.pro,
The problem is here
adx_hist[index] = adx.DIMinus[index] - adx.DIPlus[index];
You are not initializing adx_hist anywhere.
Best Regards,
Panagiotis
I posted working version , but I have problem in importing values of this indicator to my cbot. This is part of my cbot :
private StochbyPem stoch;
private ADXbyPem adx;
protected override void OnStart()
{
adx = Indicators.GetIndicator<ADXbyPem>(Adx_per, Adx_ma_per, Adx_skew, Adx_lb);
stoch = Indicators.GetIndicator<StochbyPem>(Period_stoch, kPeriods_D, kSlowing_K, stoch_open_skew, stoch_close_skew);
}
protected override void OnBar()
{
Print(adx.BUYcond);
Print(adx.adx_h);
Print(adx.Adx_ma_n.LastValue);
As You see first print causes always false (but indicator show properly values),
second naN
third : rest of errors.
Indicator is registered properly and i checked almost all posibilties for accesing to values of this indicator (Last, Lastvalue etc).
The same proplem is with my second custom indiator (stoch).
Can You give a little hint.
Thanks,
Pem
@fx.pro
PanagiotisCharalampous
11 Dec 2020, 08:52
Hi fx.pro,
Please post the complete cBot code and parameters so that we can check.
Best Regards,
Panagiotis
@PanagiotisCharalampous
fx.pro
11 Dec 2020, 16:02
RE:
PanagiotisCharalampous said:
Hi fx.pro,
Please post the complete cBot code and parameters so that we can check.
Best Regards,
Panagiotis
I removed others indicators and I left only Adx for clear view a problem, indicator and cbot, please:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = false, AccessRights = AccessRights.None, ScalePrecision = 0)]
public class ADXbyPem : Indicator
{
[Parameter("ADX Period", Group = "ADX", DefaultValue = 7, MinValue = 6, Step = 1, MaxValue = 21)]
public int Adx_per { get; set; }
[Parameter("ADX MA Period", Group = "ADX", DefaultValue = 14, MinValue = 6, Step = 1, MaxValue = 36)]
public int Adx_ma_per { get; set; }
[Parameter("Treshold skew", Group = "ADX", DefaultValue = 30, MinValue = -30, MaxValue = 70, Step = 10)]
public int Adx_skew { get; set; }
[Parameter("ADX Looback", Group = "ADX", DefaultValue = 130, MinValue = 10, Step = 10, MaxValue = 360)]
public int Adx_lback { get; set; }
public MovingAverage ma_adx;
public DirectionalMovementSystem adx;
public IndicatorDataSeries adx_h;
public bool BUYcond;
public bool SELLcond;
[Output("ADX", LineColor = "gold", PlotType = PlotType.Histogram)]
public IndicatorDataSeries Adx_ma_n { get; set; }
protected override void Initialize()
{
adx_h = CreateDataSeries();
adx = Indicators.DirectionalMovementSystem(Adx_per);
ma_adx = Indicators.MovingAverage(adx_h, Adx_ma_per, MovingAverageType.Exponential);
}
public override void Calculate(int index)
{
IndicatorArea.DrawHorizontalLine("Zero line", 0, Color.White, 1, LineStyle.Solid);
IndicatorArea.DrawHorizontalLine("BUY Level ", -30 - Adx_skew, Color.Green, 1, LineStyle.Solid);
IndicatorArea.DrawHorizontalLine("SELL Level", 30 + Adx_skew, Color.Red, 1, LineStyle.Solid);
if (index >= Adx_ma_per && index >= Adx_per)
{
adx_h[index] = adx.DIPlus[index] - adx.DIMinus[index];
var MaAdxHL = ma_adx.Result.Maximum(Adx_lback);
var MaAdxLL = ma_adx.Result.Minimum(Adx_lback);
Adx_ma_n[index] = ((ma_adx.Result.LastValue - MaAdxLL) / (MaAdxHL - MaAdxLL) * 100 - 50) * 2;
}
BUYcond = Adx_ma_n.LastValue < (-30 - Adx_skew);
SELLcond = Adx_ma_n.LastValue > (30 + Adx_skew);
}
}
}
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.API.Requests;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class FXExplorerV2 : Robot
{
private const string MyLabel = "FX ExplorerV2";
[Parameter("Start trading (hour) 1", DefaultValue = 7)]
public int StartHour { get; set; }
[Parameter("Stop trading (hour) 1", DefaultValue = 23)]
public int StopHour { get; set; }
[Parameter("Trading hours", DefaultValue = true)]
public bool condition_tradeHours { get; set; }
[Parameter("Include Trailing Stop", DefaultValue = true)]
public bool hasTrailingStop { get; set; }
[Parameter("Trailing Stop Trigger (pips)", DefaultValue = 0)]
public int TrailingStopTrigger { get; set; }
[Parameter("Trailing Stop Step (pips)", DefaultValue = 5)]
public int TrailingStopStep { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Volume { get; set; }
[Parameter("Period", Group = "STOCH", DefaultValue = 15)]
public int Period_stoch { get; set; }
[Parameter("%K", Group = "STOCH", DefaultValue = 3)]
public int kPeriods_D { get; set; }
[Parameter("%D", Group = "STOCH", DefaultValue = 3)]
public int kSlowing_K { get; set; }
[Parameter("Skew OPEN", Group = "STOCH", DefaultValue = 0, MinValue = -15, MaxValue = 15, Step = 1)]
public int stoch_open_skew { get; set; }
[Parameter("Skew CLOSE", Group = "STOCH", DefaultValue = 0, MinValue = -15, MaxValue = 15, Step = 1)]
public int stoch_close_skew { get; set; }
[Parameter("ADX Period", Group = "ADX", DefaultValue = 7, MinValue = 6, Step = 1, MaxValue = 21)]
public int Adx_per { get; set; }
[Parameter("ADX MA Period", Group = "ADX", DefaultValue = 14, MinValue = 6, Step = 1, MaxValue = 36)]
public int Adx_ma_per { get; set; }
[Parameter("Treshold skew", Group = "ADX", DefaultValue = 0, MinValue = -30, MaxValue = 70, Step = 10)]
public int Adx_skew { get; set; }
[Parameter("ADX Looback", Group = "ADX", DefaultValue = 130, MinValue = 10, Step = 10, MaxValue = 360)]
public int Adx_lb { get; set; }
[Parameter("MA Type", Group = "MACD", DefaultValue = MovingAverageType.Exponential)]
public MovingAverageType MAType { get; set; }
[Parameter("Long Cycle", Group = "MACD", DefaultValue = 26)]
public int Mcd_long { get; set; }
[Parameter("Short Cycle", Group = "MACD", DefaultValue = 12)]
public int Mcd_short { get; set; }
[Parameter("Signal Periods", Group = "MACD", DefaultValue = 9)]
public int Mcd_per { get; set; }
//private StochbyPem stoch;
private ADXbyPem adx;
//private StochRSIbyPem srsi;
protected override void OnStart()
{
//srsi = Indicators.GetIndicator<StochRSIbyPem>(20, 20, 20, 20, 20);
adx = Indicators.GetIndicator<ADXbyPem>(Adx_per, Adx_ma_per, Adx_skew, Adx_lb);
//stoch = Indicators.GetIndicator<StochbyPem>(Period_stoch, kPeriods_D, kSlowing_K, stoch_open_skew, stoch_close_skew);
//mcd = Indicators.MacdCrossOver(mcd_long, mcd_short, mcd_per);
}
protected override void OnBar()
{
bool cond_tradeHours = (Server.Time.Hour > StartHour) && (Server.Time.Hour < StopHour);
Print(adx.BUYcond);
Print(adx.adx_h.Last(2));
Print(adx.Adx_ma_n.LastValue);
/*
if (stoch.BUYsig && cond_tradeHours)
{
Close(TradeType.Sell);
Open(TradeType.Buy);
}
// && adx_cond() > adx_trsh)
else if (stoch.SELLsig && cond_tradeHours)
{
Close(TradeType.Buy);
Open(TradeType.Sell);
}
*/
}
private void Close(TradeType tradeType)
{
foreach (var position in Positions.FindAll("FX Explorer", SymbolName, tradeType))
ClosePosition(position);
}
private void Open(TradeType tradeType)
{
var position = Positions.Find("FX Explorer", SymbolName, tradeType);
var volumeInUnits = Symbol.QuantityToVolumeInUnits(Volume);
if (position == null)
ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "FX Explorer");
}
}
}
@fx.pro
... Deleted by UFO ...
PanagiotisCharalampous
11 Dec 2020, 16:38
( Updated at: 21 Dec 2023, 09:22 )
Hi fx.pro,
Your problem is here
You need to check if the IndicatorArea is not null before you draw objects on it.
Best Regards,
Panagiotis
@PanagiotisCharalampous
fx.pro
16 Dec 2020, 00:18
( Updated at: 21 Dec 2023, 09:22 )
RE:
PanagiotisCharalampous said:
Hi fx.pro,
Your problem is here
You need to check if the IndicatorArea is not null before you draw objects on it.
Best Regards,
Panagiotis
Hi,
now all works OK. Thank You for helping.
Best Regards,
Pem
@fx.pro
PanagiotisCharalampous
09 Dec 2020, 08:40
Hi fx.pro,
The problem is here
You are not initializing adx_hist anywhere.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous