Stop the BOT when reaching the Total Profit or Loss

Created at 21 Aug 2020, 11:35
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!
RA

raphael.mca@hotmail.com

Joined 16.04.2017

Stop the BOT when reaching the Total Profit or Loss
21 Aug 2020, 11:35


Good night business friends ...

I looked for a reference in the forum but I didn't find it, what I'm trying to implement is a function in my Robot that would work as follows.

Assign 2 parameters to the Robot

Profit by section

Loss by section

Suppose I set the profit to $2 and the loss to -$1

This means that as soon as the Robot makes a total profit of $2 (including operations already closed and those opened as well), it closes all orders and stops running.

However, if he is left with a total loss of -$1 (including operations already closed and those open as well), he closes all orders and stops running.

This calculation must be done with each new section, that is ...

If I turn on the Bot, it starts the calculation until it turns off.

If I call again, it restarts the calculation.

I don't know if I explained myself well, but if anyone has any doubts, I explain a little better, anyway, if there is someone who can help me create this function, I am very grateful ...

hugs


@raphael.mca@hotmail.com
Replies

firemyst
26 Aug 2020, 14:24

This is relatively easy.

See code example below. You'll obviously have to make adjustments for it to fit in your code.

 

//you need to set this up as a parameter
StopBotWhenLossesFallBelow


double _runningTotalsGainLoss;
string _positionLabel;

        private void Positions_Closed(PositionClosedEventArgs args)
        {
            //Only run this method if it's this instance that closed.
            Position p1 = args.Position;

            if (p1.SymbolName == Symbol.Name && p1.Label == _positionLabel)
            {
                    //Now get the historical trade stuff.
                    HistoricalTrade ht = History.FindLast(p1.Label, p1.SymbolName, p1.TradeType);

                        //Running Totals
                        _runningTotalsGainLoss += p1.NetProfit;
                        Print("PC20: Running total for \"{0}\": {1}", p1.Label, String.Format("{0:$#,###.00}", _runningTotalsGainLoss));
                        if (_runningTotalsGainLoss < StopBotWhenLossesFallBelow)
                        {
                            Print("WARNING! Running total {0} has fallen below StopBotWhenLossesFallBelow {1} threshold! Stopping bot for \"{2}\"!", String.Format("{0:$#,###.00}", _runningTotalsGainLoss), String.Format("{0:$#,###.00}", StopBotWhenLossesFallBelow), p1.Label);
							Stop();
                        }
            }

        }

 


@firemyst