cbot crashed on bar

Created at 01 Oct 2014, 12:25
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
JA

jagtrading

Joined 01.10.2014

cbot crashed on bar
01 Oct 2014, 12:25


Hi guys,

I have recently started messing about with calgo and this is my first program. I got null exception object is not referenced or something along those lines an error. could someone tell me what is wrong with my code thank you for help.

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RSIbollinger : Robot
    {

        [Parameter("Position Label", DefaultValue = "My Label")]
        public string MyLabel { get; set; }

        [Parameter("ATRperiod", DefaultValue = 14.0)]
        public int ATRperiod { get; set; }

        [Parameter("ATRmultiple_stp", DefaultValue = 2.0)]
        public int ATRmultiple_stp { get; set; }

        [Parameter("ATRmultiple_tkp", DefaultValue = 0.0)]
        public int ATRmultiple_tkp { get; set; }

        [Parameter("ATREMAperiod", DefaultValue = 0.0)]
        public int ATREMAperiod { get; set; }

        [Parameter("RSIperiod", DefaultValue = 14.0)]
        public int RSIperiod { get; set; }

        [Parameter("RSIbollingerperiod", DefaultValue = 100.0)]
        public int RSIbollingerperiod { get; set; }

        [Parameter("RSIbollingerdeviation", DefaultValue = 1.0)]
        public int RSIbollingerdeviation { get; set; }

        [Parameter("MACDsignal", DefaultValue = 12.0)]
        public int MACDsignal { get; set; }

        [Parameter("MACDFastma", DefaultValue = 26.0)]
        public int MACDFastma { get; set; }

        [Parameter("MACDslowma", DefaultValue = 12.0)]
        public int MACDslowma { get; set; }

        [Parameter("MACDbollingerperiod", DefaultValue = 100.0)]
        public int MACDbollingerperiod { get; set; }

        [Parameter("MACDbollingerdeviation", DefaultValue = 1.0)]
        public int MACDbollingerdeviation { get; set; }

        [Parameter("StochasticK", DefaultValue = 100.0)]
        public int StochasticK { get; set; }

        [Parameter("StochasticSlowing", DefaultValue = 5.0)]
        public int StochasticSlowing { get; set; }

        [Parameter("RSIcandleclose", DefaultValue = 0)]
        public int RSIcandleclose { get; set; }

        [Parameter("Slippage", DefaultValue = 0)]
        public int Slippage { get; set; }

        [Parameter("Margin", DefaultValue = 0)]
        public int p_Margin { get; set; }

        private AverageTrueRange i_ATR;
        private ExponentialMovingAverage i_ATREMA;
        private RelativeStrengthIndex i_RSI;
        private BollingerBands i_RSIBOLL;
        private MacdHistogram i_MACD;
        private BollingerBands i_MACDBOLL;
        private StochasticOscillator i_Stoch;
        private int p_volume;

        // margin = volume * leverage (leverage = 1:10)



        protected override void OnStart()
        {
            i_ATR = Indicators.AverageTrueRange(ATRperiod, MovingAverageType.Simple);
            i_ATREMA = Indicators.ExponentialMovingAverage(MarketSeries.Close, ATREMAperiod);
            i_RSI = Indicators.RelativeStrengthIndex(MarketSeries.Close, RSIperiod);
            i_RSIBOLL = Indicators.BollingerBands(i_RSI.Result, RSIbollingerperiod, RSIbollingerdeviation, MovingAverageType.Simple);
            i_MACD = Indicators.MacdHistogram(MACDslowma, MACDFastma, MACDsignal);
            i_MACDBOLL = Indicators.BollingerBands(i_MACD.Signal, MACDbollingerperiod, MACDbollingerdeviation, MovingAverageType.Simple);
            i_Stoch = Indicators.StochasticOscillator(StochasticK, StochasticSlowing, 1, MovingAverageType.Simple);

            p_volume = (p_Margin / Account.Leverage) * 100000;
        }

        protected override void OnBar()
        {
            var ATRstp = (i_ATR.Result.LastValue * ATRmultiple_stp);
            var ATRtkp = (i_ATR.Result.LastValue * ATRmultiple_tkp);
            var position = Positions.Find(MyLabel);


            if (Positions.Count == 1)
            {
                ModifyPosition(position, ATRstp, ATRtkp);
                Print("New stoploss is {0}, New take profit is {1}", position.StopLoss, position.TakeProfit);
            }

            else if (i_RSI.Result.HasCrossedAbove(i_RSIBOLL.Bottom.LastValue, RSIcandleclose) && i_Stoch.PercentK.LastValue <= 20 && i_MACD.Signal.LastValue < i_MACDBOLL.Bottom.LastValue)
            {

                ExecuteMarketOrder(TradeType.Buy, Symbol, p_volume, MyLabel, ATRstp, ATRtkp);
                Print("Long at {0}, Stoploss = {1}, Takeprofit = {2}", position.EntryPrice, position.StopLoss, position.TakeProfit);
            }
            else if (i_RSI.Result.HasCrossedBelow(i_RSIBOLL.Top.LastValue, RSIcandleclose) && i_Stoch.PercentK.LastValue >= 80 && i_MACD.Signal.LastValue > i_MACDBOLL.Top.LastValue)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, p_volume, MyLabel, ATRstp, ATRtkp);
                Print("Short at {0}, Stoploss = {1}, Takeprofit = {2}", position.EntryPrice, position.StopLoss, position.TakeProfit);

            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 


@jagtrading
Replies

Spotware
02 Oct 2014, 10:10

var position = Positions.Find(MyLabel);

You need to check position variable for null before accessing it:

if (position != null)
{
   ...
}

 


@Spotware