Automatic cancellation of Pending order after TP made

Created at 28 Jul 2016, 05:10
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!
RY

ryan.mcmanus86

Joined 28.07.2016

Automatic cancellation of Pending order after TP made
28 Jul 2016, 05:10


Looking for some help/direction in the programming world for a bot that cancels pending orders on the same trading pair; once the current position meets the take profit set.

I couldn't find anything of relevance on the threads or cBots. I thought that this might have been a little more common than originally thought.

Very limited C# experience (currently in the stages of learning).

 


@ryan.mcmanus86
Replies

... Deleted by UFO ...

ryan.mcmanus86
28 Jul 2016, 12:13

RE:

lucian said:


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 ryanmcmanus86 : Robot
    {

        [Parameter("Label", DefaultValue = "ryan.mcmanus86")]
        public string Label { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 0.1)]
        public double Quantity { get; set; }

        [Parameter(" Take Profit", DefaultValue = 15)]
        public double tp { get; set; }

        [Parameter("Stop Loss", DefaultValue = 15)]
        public double Sl { get; set; }
        protected override void OnStart()
        {
            var volume = Symbol.QuantityToVolume(Quantity);
            Positions.Closed += OnPositionClosed;
            ExecuteMarketOrder(TradeType.Sell, Symbol, volume, Label, Sl, tp);
            PlaceStopOrder(TradeType.Buy, Symbol, volume, Symbol.Ask, Label);

        }

        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            var position = args.Position;

            if (position.SymbolCode == Symbol.Code && position.NetProfit > 0)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.Label == Label && order.SymbolCode == Symbol.Code)
                    {
                        CancelPendingOrder(order);
                    }
                }


            }
        }
    }
}

 

Wow that was quick... thanks so much for that lucian...

Unfortunately I cannot get it to work for me... maybe I am doing it wrong... but it seems to just open two orders for the same quantity... regardless if I have a current position or a pending position...

Any help would be greatly appreciated... I definitely didn't think I would get a reply this quick...


@ryan.mcmanus86

... Deleted by UFO ...

ryan.mcmanus86
03 Aug 2016, 05:12

Hey Lucian... I appreciate you help here...

I'm looking for...

 - if current open position hits take profit... then close all pending orders on currency pair only.

 - if current open position hits stop loss then take no action.


@ryan.mcmanus86