Historical trade - Crashed in OnTick with NullReferenceException:

Created at 05 Oct 2022, 07: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!
JE

jeelom111

Joined 05.10.2022

Historical trade - Crashed in OnTick with NullReferenceException:
05 Oct 2022, 07:25


Hi,

My code crash when try to pull historical trade data

 

This is the issue - HistoricalTrade ht = History.FindLast("RobotID1"); 

When I tried to use the variable     ht.       for anything it will crash.

please help.

Thanks.

 

This is my code.

 

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

namespace cAlgo.Robots
{
    //Set time zone to Eastern Standard Time EP9-Best time to trade
    [Robot(TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.None)]
    public class EP12NNFXBot : Robot
    {
        //Create Parameters EP10-Functions and Parameters
        [Parameter("Risk %", DefaultValue = 0.02)]
        public double RiskPct { get; set; }
        
           [Parameter("Stop Loss", Group = "Protection", DefaultValue = 40)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", Group = "Protection", DefaultValue = 40)]
        public int TakeProfit { get; set; }
        
        [Parameter("Tradeamount: ", Group = "Protection", DefaultValue = 40)]
        public int TradeAmount { get; set; }


         [Parameter("Robot Label", DefaultValue = "buy eurusd")]
        public string RobotID1 { get; set; }

        //Create indicator variables EP5-ATR
                public TDFInnfxabbu tdfi;
                public double GrossProfit { get; }
                public double VolumeInUnits { get; }
                public double NetProfit { get; }

        private Random random = new Random();
        

        protected override void OnStart()
        {
        
       // Open(TradeType.Buy, "RobotID1",1000);
       Open(GetRandomTradeType(), "RobotID1",1000);
        HistoricalTrade ht = History.FindLast("RobotID1");

        }

        protected override void OnTick()
        {
            // Put your core logic here EP7-MACD and EP8-Custom Indicators
            //var TradeAmounts = TradeAmount;
            HistoricalTrade ht = History.FindLast("RobotID1");


            //Check Entry Signal
            if  (ht.NetProfit > 0 & Account.Equity < 400)

            {
                //Open(GetRandomTradeType(), "RobotID1",1000);
                Open(GetRandomTradeType(), "RobotID1",1000);
                
                
            }
            else if ( Account.Equity < 400) //ht.NetProfit < 0 &
            {
                Open(ht.TradeType, "RobotID1",ht.VolumeInUnits);
                //Open(GetRandomTradeType(), "RobotID1",1000);
            }

            //Check Exit Indicator
            //if (Account.Balance < MaxEquity & (History.Last().TradeType == TradeType.Sell))
            {
                //Open(TradeType.Sell, "RobotID1",1000);
                
            }
            //else if (Account.Balance > MaxEquity & (History.Last().TradeType == TradeType.Sell))
            {
               //Open(TradeType.Sell, "RobotID1", 1000);
               //();
            }

        }
        //Function for opening a new trade - EP10-Functions and Parameters
        private void Open(TradeType tradeType, string Label, double tradeamount)
        {

            var position = Positions.Find(Label, SymbolName);
            //var tradeamount = 1000;
           
           
            if (position == null & Server.Time.Hour == 16 & Server.Time.Minute == 29)
                ExecuteMarketOrder(tradeType, SymbolName, tradeamount, Label, StopLoss, TakeProfit);
        }

        //Function for closing trades - EP10-Functions and Parameters
        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
            }
        private void Close(TradeType tradeType, string Label)
        {
            foreach (var position in Positions.FindAll(Label, SymbolName, tradeType))
                ClosePosition(position);
                
                
        }
        
    }
}
 


@jeelom111
Replies

PanagiotisChar
05 Oct 2022, 08:24

Hi jeelom111,

If you debug your code, you will find out exactly where your code is crashing. It is probably crashing because ht is null. You should be checking if the trade exists before using it.

Aieden Technologies

Need Help? Join us on Telegram

 


@PanagiotisChar

jeelom111
06 Oct 2022, 08:32

RE:

PanagiotisChar said:

Hi jeelom111,

If you debug your code, you will find out exactly where your code is crashing. It is probably crashing because ht is null. You should be checking if the trade exists before using it.

Aieden Technologies

Need Help? Join us on Telegram

 

Hi, I tried to debug it, I have changed the code so that there is a position opened and closed on start so there is history but the FindLast still doesn't work.

can you help me show an example of a working code for this function? there isn't one available on the Ctrader website.

 

 

HistoricalTrade ht = History.FindLast("RobotID1");+

Print(ht.NetProfit)


@jeelom111