Order Cancels Order (OCO)

by Xavier R in category Other at 04/08/2015
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

Download
1562 downloads
How to install
Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
using cAlgo.API;
 
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class OCO : Robot
    {
        //---ORDER 1

        [Parameter("Order #1 Type")]
        public string InputOrderType1 { get; set; }

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

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

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

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

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

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

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

        [Parameter("Order #1 TP", DefaultValue = 40)]
        public double InputTakeProfit2 { get; set; }
 
        private string _label;
 
        protected override void OnStart()
        {
            _label = string.Format("{0}{1}{2}", Symbol.Code, TimeFrame, Server.Time.Ticks);
            PendingOrders.Filled += PendingOrders_Filled;

            if (InputOrderType1.ToLower() == "sell")
            {
                if (InputOrderPrice1 > MarketSeries.Close.LastValue)
                    PlaceLimitOrder(TradeType.Sell, Symbol, InputOrderSize1, InputOrderPrice1, _label, InputStopLoss1, InputTakeProfit1);
                else
                    PlaceStopOrder(TradeType.Sell, Symbol, InputOrderSize1, InputOrderPrice1, _label, InputStopLoss1, InputTakeProfit1);
            }
            else if (InputOrderType1.ToLower() == "buy")
            {
                if (InputOrderPrice1 > MarketSeries.Close.LastValue)
                    PlaceStopOrder(TradeType.Buy, Symbol, InputOrderSize1, InputOrderPrice1, _label, InputStopLoss1, InputTakeProfit1);
                else
                    PlaceLimitOrder(TradeType.Buy, Symbol, InputOrderSize1, InputOrderPrice1, _label, InputStopLoss1, InputTakeProfit1);
            }
            else
            {
                Print("Parameter not recognized, bot will stop");
                Stop();
            }
 
            if (InputOrderType2.ToLower() == "sell")
            {
                if (InputOrderPrice2 > MarketSeries.Close.LastValue)
                    PlaceLimitOrder(TradeType.Sell, Symbol, InputOrderSize2, InputOrderPrice2, _label, InputStopLoss2, InputTakeProfit2);
                else
                    PlaceStopOrder(TradeType.Sell, Symbol, InputOrderSize2, InputOrderPrice2, _label, InputStopLoss2, InputTakeProfit2);
            }
            else if (InputOrderType2.ToLower() == "buy")
            {
                if (InputOrderPrice2 > MarketSeries.Close.LastValue)
                    PlaceStopOrder(TradeType.Buy, Symbol, InputOrderSize2, InputOrderPrice2, _label, InputStopLoss2, InputTakeProfit2);
                else
                    PlaceLimitOrder(TradeType.Buy, Symbol, InputOrderSize2, InputOrderPrice2, _label, InputStopLoss2, InputTakeProfit2);
            }
            else
            {
                Print("Parameter not recognized, bot will stop");
                Stop();
            }
        }

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

            foreach (PendingOrder order in PendingOrders)
            {
                if (order.Label == _label)
                {
                    CancelPendingOrder(order);
                }
            }
        }

        protected override void OnTick()
        {

        }
 
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}
Comments

Shaktale - April 11, 2019 @ 21:18

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

Xavier R - April 12, 2019 @ 22:45

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

Shaktale - April 13, 2019 @ 20:01

Thank you so much!

0