Help
Help
06 Jul 2018, 14:30
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 MASD : Robot { [Parameter("Source")] public DataSeries Source { get; set; } [Parameter("MA_short_value", DefaultValue = 5)] public int MA_5 { get; set; } [Parameter("MA_long_value", DefaultValue = 12)] public int MA_12 { get; set; } [Parameter("Stop Loss", DefaultValue = 15)] public double SL { get; set; } [Parameter("Take Profit", DefaultValue = 30)] public double TP { get; set; } [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)] public double Quantity { get; set; } private SimpleMovingAverage _ma12; private SimpleMovingAverage _ma5; private StandardDeviation _SD; private Position longPosition; private Position shortPosition; protected override void OnStart() { _ma12 = Indicators.SimpleMovingAverage(Source, MA_12); _ma5 = Indicators.SimpleMovingAverage(Source, MA_5); _SD = Indicators.StandardDeviation(MarketSeries.Close, 10, MovingAverageType.Simple); } protected override void OnTick() { longPosition = Positions.Find("OrdineVendere", Symbol, TradeType.Buy); shortPosition = Positions.Find("OrdineComprare", Symbol, TradeType.Sell); var previous_ma5 = _ma5.Result.Last(1); var previous_ma12 = _ma12.Result.Last(1); var current_ma5 = _ma5.Result.Last(0); var current_ma12 = _ma12.Result.Last(0); var current_SD = _SD.Result.Last(0); var previous_SD = _SD.Result.Last(1); if (previous_ma5 < previous_ma12 && current_ma5 >= current_ma12) { // if (_SD.Result.Last(0) > _SD.Result.Last(1)) ExecuteMarketOrder(TradeType.Buy, Symbol, Quantity, "OrdineComprare", SL, TP); } if (previous_ma5 > previous_ma12 && current_ma5 <= current_ma12) { if (previous_SD < current_SD) ExecuteMarketOrder(TradeType.Sell, Symbol, Quantity, "OrdineVendere", SL, TP); } if (current_SD < previous_SD) { var position2 = Positions.Find("OrdineComprare"); ClosePosition(position2); } if (current_SD < previous_SD) { var position1 = Positions.Find("OrdineVendere"); ClosePosition(position1); } } protected override void OnStop() { } } }
Hi,
I have just started programming and I have no idea where is the problem. Thank you for any help.
Replies
quesnayquadrato
06 Jul 2018, 15:36
I can't understand if there is a problem in the code or in the idea, beacause the backtest doesn't start.
@quesnayquadrato
PanagiotisCharalampous
06 Jul 2018, 15:58
Hi quesnayquadrato,
In the backtesting log you will see the following message
01/04/2011 00:05:00.000 | Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.
This means that you are trying to read variables that are null. In your code, the exception is thrown in the following parts
if (current_SD < previous_SD) { var position2 = Positions.Find("OrdineComprare"); ClosePosition(position2); } if (current_SD < previous_SD) { var position1 = Positions.Find("OrdineVendere"); ClosePosition(position1); }
Before closing a position you should always check if it is null. Modify your code as follows
if (current_SD < previous_SD) { var position2 = Positions.Find("OrdineComprare"); if (position2 != null) ClosePosition(position2); } if (current_SD < previous_SD) { var position1 = Positions.Find("OrdineVendere"); if (position1 != null) ClosePosition(position1); }
Let me know if this helps,
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
06 Jul 2018, 15:25
Hi quesnayquadrato,
Thanks for posting in our forum. Could you please explain to us what is the problem?
Best Regards,
Panagiotis
@PanagiotisCharalampous