Closing a Counter Trade Automatically

Created at 21 Mar 2018
Gwave's avatar

Gwave

Joined 26.10.2014

Status

Open


Budget

20.00 GBP


Payment Method

Direct Payment

Job Description

Hi guys.

How can i modify CounterTrade to automatically Close " it's trade " when the " Original position " closes. Please any help would be much appreciated. 

Many Thanks

This is the cbot

https://ctdn.com/algos/cbots/show/475

Comments
Log in to add a comment.
event's avatar
event · 6 years ago

         private const string Label = "CounterTrade";

        protected override void OnStart()
        {
            Positions.Opened += OnPositionsOpened;
            Positions.Closed += OnPositionsClosed;
        }

        void OnPositionsClosed(PositionClosedEventArgs args)
        {
            var originalPosition = args.Position;
            if (originalPosition.Label != Label)
            {
                var tradeType = originalPosition.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;
                var position = Positions.Find(Label, Symbol, tradeType);
                if (position != null)
                {
                    ClosePosition(position);
                }
            }
        }

        void OnPositionsOpened(PositionOpenedEventArgs args)
        {
            var originalPosition = args.Position;
            if (originalPosition.Label != Label)
            {
                var tradeType = originalPosition.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;
                ExecuteMarketOrder(tradeType, Symbol, originalPosition.Volume, Label);
            }
        }