Something wrong with Positions.Find funtion

Created at 31 Jul 2018, 18:48
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!
MI

MinMin

Joined 23.07.2018

Something wrong with Positions.Find funtion
31 Jul 2018, 18:48


protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
            var RSI = rsi.Result.Last(1);
if ((RSI > 65 && shortPosition == null)
            {
                Print("1");
                Print(longPosition);
                Print("bien long");
                Print(shortPosition);
                Print("bien short");
                if (longPosition != null)
                  {
                    Print("2");
                    ClosePosition(longPosition);
                    Print("3");
                   }
                Print("4");
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label);
                Print(shortPosition);
                Print("End test 1");
            }

here is log show:

31/07/2018 21:48:56.417 | New cBot, EURGBP, m1 | 1
31/07/2018 21:48:56.417 | New cBot, EURGBP, m1 | null
31/07/2018 21:48:56.417 | New cBot, EURGBP, m1 | bien long
31/07/2018 21:48:56.432 | New cBot, EURGBP, m1 | null
31/07/2018 21:48:56.432 | New cBot, EURGBP, m1 | bien short
31/07/2018 21:48:56.432 | New cBot, EURGBP, m1 | 4
31/07/2018 21:48:56.432 | New cBot, EURGBP, m1 | Executing Market Order to Sell 6000 EURGBP
31/07/2018 21:48:57.229 | New cBot, EURGBP, m1 | → Executing Market Order to Sell 6000 EURGBP SUCCEEDED
31/07/2018 21:48:57.229 | New cBot, EURGBP, m1 | null
31/07/2018 21:48:57.245 | New cBot, EURGBP, m1 | End test 1
31/07/2018 21:48:57.245 | New cBot, EURGBP, m1 | 1
31/07/2018 21:48:57.245 | New cBot, EURGBP, m1 | null
31/07/2018 21:48:57.245 | New cBot, EURGBP, m1 | bien long
31/07/2018 21:48:57.245 | New cBot, EURGBP, m1 | null
31/07/2018 21:48:57.245 | New cBot, EURGBP, m1 | bien short
31/07/2018 21:48:57.261 | New cBot, EURGBP, m1 | 4
31/07/2018 21:48:57.261 | New cBot, EURGBP, m1 | Executing Market Order to Sell 6000 EURGBP
31/07/2018 21:48:57.932 | New cBot, EURGBP, m1 | → Executing Market Order to Sell 6000 EURGBP SUCCEEDED
31/07/2018 21:48:57.948 | New cBot, EURGBP, m1 | null
31/07/2018 21:48:57.948 | New cBot, EURGBP, m1 | End test 1
31/07/2018 21:48:57.948 | New cBot, EURGBP, m1 | Crashed in Positions.Opened with NullReferenceException: Object reference not set to an instance of an object.
 

I don't understand why the order executing already but after that the  Positions.Find(label, Symbol, TradeType.Sell) still have Null value and make Cbot crashed. Please help!

Thanks so much,

 

 


@MinMin
Replies

PanagiotisCharalampous
01 Aug 2018, 10:31

Hi vancong140293,

If the method returns null, it means there is no Position with such label for that Symbol.

Best Regards,

Panagiotis


@PanagiotisCharalampous

MinMin
01 Aug 2018, 10:47

RE:

Panagiotis Charalampous said:

Hi vancong140293,

If the method returns null, it means there is no Position with such label for that Symbol.

Best Regards,

Panagioti

Hi,

You see there is a Position have been created before:

31/07/2018 21:48:56.432 | New cBot, EURGBP, m1 | Executing Market Order to Sell 6000 EURGBP
31/07/2018 21:48:57.229 | New cBot, EURGBP, m1 | → Executing Market Order to Sell 6000 EURGBP SUCCEEDED

I think Positions.Find must find to this position which just have been created?

 

Thanks,


@MinMin

PanagiotisCharalampous
01 Aug 2018, 14:15

Hi vancong140293,

Could you share the full cBot code so that I can run in on my pc and reproduce? Maybe there is something in the rest of the code that we are missing.

Best Regards,

Panagiotis


@PanagiotisCharalampous

MinMin
01 Aug 2018, 15:17

RE:
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 SampleTrendcBot : Robot
    {

        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter("Open")]
        public DataSeries SourceSeries { get; set; }
//Chose Open only
        [Parameter("Close")]
        public DataSeries SourceSeriest { get; set; }

        [Parameter("PeriodsAR", DefaultValue = 50)]
        public int PeriodsAR { get; set; }
        [Parameter("PeriodsRSI", DefaultValue = 14)]
        public int PeriodsRSI { get; set; }

        [Parameter("Slow Periods", DefaultValue = 1)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 1)]
        public int FastPeriods { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        private MovingAverage openMa;
        private MovingAverage closeMa;
        private const string label = "Sample Trend cBot";
        private AverageTrueRange AverageTR;
        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            openMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            closeMa = Indicators.MovingAverage(SourceSeriest, SlowPeriods, MAType);
            AverageTR = Indicators.AverageTrueRange(PeriodsAR, MAType);
            rsi = Indicators.RelativeStrengthIndex(SourceSeriest, PeriodsRSI);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
            var SoLenh = Positions.Count(x => x.SymbolCode == Symbol.Code);
            var currentOpenMa = openMa.Result.Last(0);
            var currentCloseMa = closeMa.Result.Last(0);
            var previousOpenMa = openMa.Result.Last(1);
            var previousCloseMa = closeMa.Result.Last(1);
            var previousOpenMa1 = openMa.Result.Last(2);
            var previousCloseMa1 = closeMa.Result.Last(2);
            var KhoangCach = AverageTR.Result.Last(1);
            var RSI = rsi.Result.Last(1);

            if ((RSI > 65 && ((previousOpenMa1 < previousCloseMa1 && previousOpenMa < previousCloseMa && currentOpenMa > previousCloseMa && currentCloseMa < previousOpenMa && ((previousCloseMa1 - previousOpenMa1) > (previousCloseMa - previousOpenMa)) && Math.Abs(currentCloseMa - currentOpenMa) > KhoangCach && Math.Abs(previousCloseMa - previousOpenMa) < (0.5 * KhoangCach)) || (Math.Abs(currentCloseMa - currentOpenMa) > KhoangCach && Math.Abs(previousCloseMa1 - previousOpenMa1) > KhoangCach && Math.Abs(previousCloseMa - previousOpenMa) < (0.1 * KhoangCach) && previousOpenMa1 < previousCloseMa1 && currentCloseMa < currentOpenMa))) && shortPosition == null)
            {
                Print("1");
                Print(longPosition);
                Print("bien long");
                Print(shortPosition);
                Print("bien short");
                if (longPosition != null)
                {
                    Print("2");
                    ClosePosition(longPosition);
                    Print("3");
                }
                Print("4");
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolumeInUnits, label);
                Print(shortPosition);
                Print("End test 1");
            }
            else if ((RSI < 35 && ((previousOpenMa1 > previousCloseMa1 && previousOpenMa > previousCloseMa && currentOpenMa < previousCloseMa && currentCloseMa > previousOpenMa && (Math.Abs(previousCloseMa1 - previousOpenMa1) > Math.Abs(previousCloseMa - previousOpenMa)) && Math.Abs(currentCloseMa - currentOpenMa) > KhoangCach && Math.Abs(previousCloseMa - previousOpenMa) < (0.5 * KhoangCach)) || (Math.Abs(currentCloseMa - currentOpenMa) > KhoangCach && Math.Abs(previousCloseMa1 - previousOpenMa1) > KhoangCach && Math.Abs(previousOpenMa - previousCloseMa) < (0.1 * KhoangCach) && previousOpenMa1 > previousCloseMa1 && currentCloseMa > currentOpenMa))) && longPosition == null)
            {
                Print("a");
                Print(longPosition);
                Print("bien longa");
                Print(shortPosition);
                Print("bien shorta");
                if (shortPosition != null)
                {
                    Print("test b");
                    ClosePosition(shortPosition);
                    Print("test c");
                }
                Print("test d");
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolumeInUnits, label);
                Print(shortPosition);
                Print("End Test 2");
            }
            if (RSI < 30 && shortPosition != null)
            {
                Print("test a1");
                ClosePosition(shortPosition);
                Print("test a2");
            }
            else if (RSI > 70 && longPosition != null)
            {
                Print("test b1");
                ClosePosition(longPosition);
                Print("test b2");
            }
            foreach (var position in Positions)
            {
                if (position.NetProfit < -4)
                {
                    Print("C1");
                    ClosePosition(position);
                }
            }
        }

        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }
    }
}

It run perfectly creat orders and close them but after few hours maybe more, this problem happened and it stop. I hope you can help!

Thanks

 


@MinMin