Category Other  Published on 28/01/2021

Order Cancels Order (OCO)

Description

Similar to my previous OSO Bot, the OCO Bot handle 2 order, if any of those orders is executed the remaining order will be canceled.

Type Buy or Sell and the prices and the Bot will take care if it's limit or stop order by itself.

If you find any issue send an email to waxavi@outlook.com

-01/27/2020: I have updated this bot to use the latest parameter features


using System.Linq;
using cAlgo.API;
 
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class OrderCancelsOrder : Robot
    {
        //---ORDER 1

        [Parameter("Type", DefaultValue = TradeType.Buy, Group = "Order #1")]
        public TradeType InputOrderType1 { get; set; }

        [Parameter("Price", DefaultValue = 1.0, Group = "Order #1")]
        public double InputOrderPrice1 { get; set; }

        [Parameter("Size", DefaultValue = 10000, Group = "Order #1")]
        public double InputOrderSize1 { get; set; }

        [Parameter("SL", DefaultValue = 20, Group = "Order #1")]
        public double InputStopLoss1 { get; set; }

        [Parameter("TP", DefaultValue = 40, Group = "Order #1")]
        public double InputTakeProfit1 { get; set; }
 
        //---ORDER 2

        [Parameter("Type", DefaultValue = TradeType.Sell, Group = "Order #2")]
        public TradeType InputOrderType2 { get; set; }

        [Parameter("Price", DefaultValue = 1.0, Group = "Order #2")]
        public double InputOrderPrice2 { get; set; }

        [Parameter("Size", DefaultValue = 10000, Group = "Order #2")]
        public double InputOrderSize2 { get; set; }

        [Parameter("SL", DefaultValue = 20, Group = "Order #2")]
        public double InputStopLoss2 { get; set; }

        [Parameter("TP", DefaultValue = 40, Group = "Order #2")]
        public double InputTakeProfit2 { get; set; }

        private string _label;
 
        protected override void OnStart()
        {
            _label = SymbolName + Server.Time.Ticks;

            if (InputOrderType1 == TradeType.Sell)
            {
                if (InputOrderPrice1 > Bars.ClosePrices.LastValue)
                    PlaceLimitOrder(TradeType.Sell, SymbolName, InputOrderSize1, InputOrderPrice1, _label, InputStopLoss1, InputTakeProfit1);
                else
                    PlaceStopOrder(TradeType.Sell, SymbolName, InputOrderSize1, InputOrderPrice1, _label, InputStopLoss1, InputTakeProfit1);
            }
            else
            {
                if (InputOrderPrice1 > Bars.ClosePrices.LastValue)
                    PlaceStopOrder(TradeType.Buy, SymbolName, InputOrderSize1, InputOrderPrice1, _label, InputStopLoss1, InputTakeProfit1);
                else
                    PlaceLimitOrder(TradeType.Buy, SymbolName, InputOrderSize1, InputOrderPrice1, _label, InputStopLoss1, InputTakeProfit1);
            }

            if (InputOrderType2 == TradeType.Sell)
            {
                if (InputOrderPrice2 > Bars.ClosePrices.LastValue)
                    PlaceLimitOrder(TradeType.Sell, SymbolName, InputOrderSize2, InputOrderPrice2, _label, InputStopLoss2, InputTakeProfit2);
                else
                    PlaceStopOrder(TradeType.Sell, SymbolName, InputOrderSize2, InputOrderPrice2, _label, InputStopLoss2, InputTakeProfit2);
            }
            else
            {
                if (InputOrderPrice2 > Bars.ClosePrices.LastValue)
                    PlaceStopOrder(TradeType.Buy, SymbolName, InputOrderSize2, InputOrderPrice2, _label, InputStopLoss2, InputTakeProfit2);
                else
                    PlaceLimitOrder(TradeType.Buy, SymbolName, InputOrderSize2, InputOrderPrice2, _label, InputStopLoss2, InputTakeProfit2);
            }

            PendingOrders.Filled += PendingOrders_Filled;
        }

        private void PendingOrders_Filled(PendingOrderFilledEventArgs obj)
        {
            if (obj.PendingOrder.Label != _label)
                return;

            foreach (var order in PendingOrders.Where(x => x.Label == _label))
                order.Cancel();

            Stop();
        }
    }
}

Waxy's avatar
Waxy

Joined on 12.05.2015

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: OCO.algo
  • Rating: 5
  • Installs: 4107
  • Modified: 13/10/2021 09:55
Comments
Log in to add a comment.
CA
Cam_maC · 5 years ago

Hi All - I updated this a little bit - I added 'Cancel orders on stop and bot stop on OCO' functionality

Source: https://raw.githubusercontent.com/camAtGitHub/cTrader-Stuff/master/Order_Cancels_Order.algo

 

SH
Shaktale · 5 years ago

Thank you so much!

Waxy's avatar
Waxy · 5 years ago

Hello, I've updated it to work with decimals.

SH
Shaktale · 5 years ago

how can I input decimals in the stop loss and take profit, like 20.3 pips?