Executing market orders within class methods

Created at 30 Jun 2019, 21:03
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!
eivaremir's avatar

eivaremir

Joined 08.03.2019

Executing market orders within class methods
30 Jun 2019, 21:03


Im trying to execute market order from the class method

Initialize()

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
public enum TradeDirection
{
    Buy,
    Sell
}
public enum Reduction
{
    True,
    False
}
public enum VolumeIncrementMode
{
    Exponential,
    Lineal,
    Recursive,
    LinealRecursive
}

namespace cAlgo.Robots
{

    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HedgingSystemv2 : Robot
    {
        public class Cycle : HedgingSystemv2
        {
            public HedgingSystemv2 TradeHandler = new cAlgo.Robots.HedgingSystemv2();
            public string ID;
            public TradeResult[] CyclePositions = new TradeResult[1000];
            public double GD;
            public int LevelInPips;
            public double UsedVolume;
            public double BasePrice;
            public VolumeIncrementMode VolumeIncmtMode;
            public TradeType CycleType;
            public double TP;
            public string Sym;
            /*public Cycle(string ID, double UsedVolume)
            {
                //Print("Initializing Cycle...");
                this.ID = ID;
                this.UsedVolume = UsedVolume;
                //Print("Cycle Initialized");
            }*/

            public Cycle(string ID, double UsedVolume, TradeType type, VolumeIncrementMode mode, string Sym)
            {
                //Print("Initializing Cycle...");
                this.ID = ID;
                this.UsedVolume = UsedVolume;
                this.CycleType = type;
                this.VolumeIncmtMode = mode;
                this.Sym = Sym;


                //Print("Cycle Initialized");

            }
            public void Initialize()
            {
                this.CyclePositions[0] = this.TradeHandler.ExecuteMarketOrder(TradeType.Buy, SymbolName, 1000, "0");
            }
            public void CheckNewLevel()
            {

            }
            public void NewTP()
            {

            }
            public void Reset()
            {
            }
        }
        // LEVEL PARAMS
        [Parameter("Distancia de Compras", Group = "Levels", MinValue = 0, DefaultValue = 250)]
        public int pDistanceBuy { get; set; }
        [Parameter("Distancia de Ventas", Group = "Levels", MinValue = 0, DefaultValue = 250)]
        public int pDistanceSell { get; set; }

        // TP PARAMS
        [Parameter("Tomar Ganancias", Group = "Take Profit", MinValue = 0, DefaultValue = 25)]
        public int pTakeProfit { get; set; }
        [Parameter("Posicionamiento %", Group = "Take Profit", MinValue = -200, MaxValue = 100, DefaultValue = 0)]
        public int pTakeProfitPosition { get; set; }
        [Parameter("Reducción", Group = "Take Profit")]
        public Reduction pTakeProfitReduction { get; set; }
        [Parameter("Razon de Reducción", Group = "Take Profit", MinValue = 0, MaxValue = 100, DefaultValue = 16)]
        public int pTakeProfitReductionRatio { get; set; }

        // VOLUME PARAMS
        [Parameter("Volumen inicial", Group = "Volume", MinValue = 0.01, DefaultValue = 0.01)]
        public double pVolumeStart { get; set; }
        [Parameter("Modo de incremento", Group = "Volume")]
        public VolumeIncrementMode pVolumeIncrement { get; set; }
        [Parameter("Constante de incremento", Group = "Volume", MinValue = 1.0, DefaultValue = 2.0)]
        public double pVolumeIncrementConstant { get; set; }


        // GLOBAL VARIABLES
        Cycle[] Cycles = new Cycle[1000];

        bool NewBuyCycle = true;
        bool NewSellCycle = true;
        string ThiscBotLabel;
        int LCCI = 0;
        protected override void OnStart()
        {

            ThiscBotLabel = GetType().Name;
            Print("Starting {0} in {1}", ThiscBotLabel, Symbol.Name);
            Print("Minimum Volume: {0}", Symbol.VolumeInUnitsMin);
            Positions.Closed += PositionsOnClosed;
        }


        protected override void OnTick()
        {
            if (NewBuyCycle)
            {
                Cycles[LCCI] = new Cycle(LCCI.ToString() + "|" + ThiscBotLabel, pVolumeStart, TradeType.Buy, pVolumeIncrement, SymbolName);
                Print("ID: {0}", Cycles[0].ID);
                Print("USED VOLUME: {0}", Cycles[0].UsedVolume);
                Print("Type: {0}", Enum.GetName(typeof(TradeType), Cycles[0].CycleType));
                Print("Volume Mode: {0}", Enum.GetName(typeof(VolumeIncrementMode), Cycles[0].VolumeIncmtMode));
                Cycles[LCCI].Initialize();
                NewBuyCycle = !NewBuyCycle;
            }
            if (NewSellCycle)
            {
            }

        }

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
        }
        protected override void OnStop()
        {
            if (this.IsBacktesting)
            {
                foreach (var position in Positions)
                {

                    ClosePosition(position);
                }
            }
        }
    }

}

But i recieve the following error in runtime:

28/06/2019 00:00:00.422 | Crashed in OnTick with NullReferenceException: Object reference not set to an instance of an object.

Any idea?


@eivaremir