cBot when TP hit open 2 Stop Order Buy/Sell....

Created at 08 Mar 2017, 07:00
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!
mparama's avatar

mparama

Joined 11.10.2016

cBot when TP hit open 2 Stop Order Buy/Sell....
08 Mar 2017, 07:00


Please I need Help to finish the Bot.

The 1st excutemarketorder is trigged then when the TakeProfit is hit 2 StopOrders are open Buy/Sell.

When 1 StopOrder is Hit the other is cancelled ....and then the cBot should start again opening 2 StopOrders Buy/Sell when the TP is hit....and so again.

The problem is that when the TP of the 1st MarketOrder is hit the bot open 2 StopOrder....then when one of the StopOrder is Hit the Bot crash and stop with the error  "Crashed in OnTick with NullReferenceException".

Any suggestions would be really appreciate.

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

namespace cAlgo
{
    [Robot("Trend_Stop_OCO_NP_TP", TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Trend_Stop_OCO_NP_TP : Robot
    {
        [Parameter("Order Size", DefaultValue = 1000)]
        public int OrderSize { get; set; }


        [Parameter("SL (pips)", DefaultValue = 20)]
        public int SL { get; set; }

        [Parameter("TP (pips)", DefaultValue = 8)]
        public double TP { get; set; }

        [Parameter("Diff su current Open/Close", DefaultValue = 0.2, MinValue = 0.1, MaxValue = 30)]
        public double diffopenclosePips { get; set; }

        [Parameter("Pips away", DefaultValue = 4, MaxValue = 100, MinValue = 1)]
        public double PipsAway { get; set; }

        public double NetProfit;

        public int Count;

        string _Label = "Trend_Stop_OCO_NP_TP";

        protected override void OnStart()
        {

            //Positions.Closed += OnPositionClosed;
            //Positions.Opened += OnPositionOpened;

        }

        protected override void OnTick()
        {

            double currentOpen = MarketSeries.Open.Last(0);
            double currentClose = MarketSeries.Close.Last(0);

//------------------------------------------------------------------------------------------------------------------
            var diffOpenCloseBuy = (currentOpen - currentClose) < (diffopenclosePips * Symbol.PipSize) * -1;
            var diffOpenCloseSell = (currentOpen - currentClose) > diffopenclosePips * Symbol.PipSize;
//----------------------------------------------------------------------------------------------------------------

            double buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;
            double sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;

            var positionSell = Positions.Find(_Label, Symbol, TradeType.Sell);
            var positionBuy = Positions.Find(_Label, Symbol, TradeType.Buy);


            var totalOrders = PendingOrders.Count;

//----------------------------------------------------------------------------------------------------------------

            if (totalOrders == 0 && positionBuy == null && positionSell == null && currentOpen < Symbol.Bid && diffOpenCloseBuy)
            {
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, OrderSize, _Label, SL, TP);
                }
            }

            else if (totalOrders == 0 && positionBuy == null && positionSell == null && currentOpen > Symbol.Ask && diffOpenCloseSell)
            {
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, OrderSize, _Label, SL, TP);
                }

            }
            //-----------------------SELL------------- -------------------------------------------------------------------


            else
            {
                if (positionBuy != null && Symbol.Bid >= LastResult.Position.TakeProfit || positionSell != null && Symbol.Ask <= LastResult.Position.TakeProfit)
                {
                    {
                        PlaceStopOrder(TradeType.Sell, Symbol, OrderSize, sellOrderTargetPrice, _Label, SL, TP);
                        PlaceStopOrder(TradeType.Buy, Symbol, OrderSize, buyOrderTargetPrice, _Label, SL, TP);
                    }
                }



            }

//---------------------------------------------------------------------------------------------------------------------------------           
            foreach (var order in PendingOrders)
            {
                if (totalOrders > 0)
                {
                    if (positionBuy != null || positionSell != null)
                    {
                        CancelPendingOrder(order);
                    }
                }
            }

        }

//-------------------------------------------------------------------------------------------


    }

}


 


@mparama