Reverse Postion @ TP

Created at 21 Jul 2019, 00:25
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!
BI

bitmext

Joined 12.06.2019

Reverse Postion @ TP
21 Jul 2019, 00:25


Hello , wish you well. 

 

 

First I would like to thank you for your amazing platform, Second I would like to have a C-algo that reverses my positions size 1:1 @ take profit. So If I had a buy order, it would close it and then open one with the same size in the opposite direction. My coding skills are very limited.

 

Any help would be appreciated, Thanks.


@bitmext
Replies

PanagiotisCharalampous
22 Jul 2019, 10:00

Hi bitmext,

See an example below

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

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

        void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            if (obj.Reason == PositionCloseReason.TakeProfit)
            {
                if (obj.Position.TradeType == TradeType.Buy)
                    ExecuteMarketOrder(TradeType.Sell, Symbol.Name, obj.Position.VolumeInUnits);
                else
                    ExecuteMarketOrder(TradeType.Buy, Symbol.Name, obj.Position.VolumeInUnits);

            }
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

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

Best Regards,

Panagiotis


@PanagiotisCharalampous