Bogus TakeProfit and StopLoss while position is being reversed

Created at 27 Aug 2014, 21:45
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!
AlexanderRC's avatar

AlexanderRC

Joined 04.02.2014

Bogus TakeProfit and StopLoss while position is being reversed
27 Aug 2014, 21:45


Consider the following code

using System;
using cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TpSlRaceCondition : Robot
    {
        protected override void OnStart()
        {
            Timer.Start(TimeSpan.FromMilliseconds(10));
        }

        protected override void OnTimer()
        {
            foreach (Position p in Positions)
            {
                if (p.TradeType == TradeType.Buy)
                {
                    if (p.TakeProfit != null && p.StopLoss != null && p.TakeProfit < p.StopLoss)
                    {
                        Print("Buy: TP < SL");
                    }
                }
                else
                {
                    if (p.TakeProfit != null && p.StopLoss != null && p.StopLoss < p.TakeProfit)
                    {
                        Print("Sell: SL < TP");
                    }
                }
            }
        }


    }
}

Run this cBot, open a position manually and click reverse position button. You will get the output from one of the Print statements.

This demonstrates that after the TradeType has been changed, TP and SL are not unset at the same time but at some later moment.


@AlexanderRC