OCO Bot help Changing Code

Created at 11 Apr 2019, 21:30
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!
SH

Shaktale

Joined 10.04.2019

OCO Bot help Changing Code
11 Apr 2019, 21:30


Hi all,

I want to modify this existing code so I can imput decimals in the Stop Loss and Take Profit, like 23.7 pips.

I am sure is really simple to modify the code but I have no clue.

Hope someone can have a look.

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 OCO : Robot
    {
        [Parameter("Order #1 Type")]
        public string _ORType1 { get; set; }
        [Parameter("Order #1 Price", DefaultValue = 1.0)]
        public double _Price1 { get; set; }
        [Parameter("Order #1 Size", DefaultValue = 10000)]
        public int _Size1 { get; set; }
        [Parameter("Order #1 SL", DefaultValue = 20)]
        public int _SL1 { get; set; }
        [Parameter("Order #1 TP", DefaultValue = 40)]
        public int _TP1 { get; set; }

        //---

        [Parameter("Order #2 Type")]
        public string _ORType2 { get; set; }
        [Parameter("Order #2 Price", DefaultValue = 1.0)]
        public double _Price2 { get; set; }
        [Parameter("Order #2 Size", DefaultValue = 10000)]
        public int _Size2 { get; set; }
        [Parameter("Order #2 SL", DefaultValue = 20)]
        public int _SL2 { get; set; }
        [Parameter("Order #1 TP", DefaultValue = 40)]
        public int _TP2 { get; set; }

        string _Label = "mylabel";

        protected string GetLabel(string _TType)
        {
            Random rn = new Random(10);
            int x = rn.Next(100, 10000);
            _Label = _TType + x.ToString();

            var _CLD = History.FindLast(_Label);
            var _CLO = Positions.Find(_Label);
            while (_CLD != null || _CLO != null)
            {
                x++;
                _Label = _TType + x.ToString();
                _CLD = History.FindLast(_Label);
                _CLO = Positions.Find(_Label);
                //Theres a duplicated Label, finding another one
            }
            return _Label;
        }

        protected override void OnStart()
        {
            _Label = GetLabel(Symbol.Code);

            if (_ORType1 == "sell" || _ORType1 == "Sell" || _ORType1 == "SELL")
            {
                if (_Price1 > MarketSeries.Close.LastValue)
                {
                    PlaceLimitOrder(TradeType.Sell, Symbol, _Size1, _Price1, _Label, _SL1, _TP1);
                }
                else
                {
                    PlaceStopOrder(TradeType.Sell, Symbol, _Size1, _Price1, _Label, _SL1, _TP1);
                }
            }
            else if (_ORType1 == "buy" || _ORType1 == "Buy" || _ORType1 == "BUY")
            {
                if (_Price1 > MarketSeries.Close.LastValue)
                {
                    PlaceStopOrder(TradeType.Buy, Symbol, _Size1, _Price1, _Label, _SL1, _TP1);
                }
                else
                {
                    PlaceLimitOrder(TradeType.Buy, Symbol, _Size1, _Price1, _Label, _SL1, _TP1);
                }

            }
            else
            {
                Print("Parameter not recognized, bot will stop");
                Stop();
            }

            if (_ORType2 == "sell" || _ORType2 == "Sell" || _ORType2 == "SELL")
            {
                if (_Price2 > MarketSeries.Close.LastValue)
                {
                    PlaceLimitOrder(TradeType.Sell, Symbol, _Size2, _Price2, _Label, _SL2, _TP2);
                }
                else
                {
                    PlaceStopOrder(TradeType.Sell, Symbol, _Size2, _Price2, _Label, _SL2, _TP2);
                }
            }
            else if (_ORType2 == "buy" || _ORType2 == "Buy" || _ORType2 == "BUY")
            {
                if (_Price2 > MarketSeries.Close.LastValue)
                {
                    PlaceStopOrder(TradeType.Buy, Symbol, _Size2, _Price2, _Label, _SL2, _TP2);
                }
                else
                {
                    PlaceLimitOrder(TradeType.Buy, Symbol, _Size2, _Price2, _Label, _SL2, _TP2);
                }

            }
            else
            {
                Print("Parameter not recognized, bot will stop");
                Stop();
            }


        }

        protected override void OnTick()
        {
            Position _GetPos = Positions.Find(_Label);
            if (_GetPos != null)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.Label == _Label)
                    {
                        CancelPendingOrder(order);
                    }
                }
            }
        }

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

 


@Shaktale
Replies

Waxy
12 Apr 2019, 22:35

Hello,

I'm the one who made this code long ago, I will update it, see the code below.
 

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
        }
    }
}

 


@Waxy

Shaktale
13 Apr 2019, 19:59

Thats Amazing!

Thank you so much for creating such a great bot and thank you for the update.

I really appreciate it.


@Shaktale