Counter Trade free

by modarkat in category Other at 27/05/2014
Description

cBot that automatically opens a counter trade as soon as an original trade is opened.

        private const string Label = "CounterTrade";

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

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

 

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 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 CounterTrade : Robot
    {
        private const string Label = "CounterTrade";

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

        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);
            }
        }
    }
}
Comments

aysos75 - July 17, 2014 @ 11:50

Il y a une erreur dans le code :

remplacer 

originalPosition.Label != Label

par

originalPosition.Label == Label

aysos75 - July 17, 2014 @ 11:53

Sorry there is no error

davidp13 - March 25, 2015 @ 13:51

Hi, how can I add this code to a robot that opens trades onBar. I have tried to add it, but it opens not counter trades. Thanks

Gwave - March 21, 2018 @ 17:14

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

3.33