Implementing Portfolio Stop Loss

Created at 05 Sep 2021, 16:40
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!
HA

hao.han

Joined 22.06.2020

Implementing Portfolio Stop Loss
05 Sep 2021, 16:40


Hi, I was someone could help me with some suggestions on how to solve this problem:

High level issue: I have a portfolio of trading cBots running (so 2 or more). How can I halt all trading if account equity falls below X, akin to an "account/portfolio stop loss", until instructed otherwise.

Elaboration: I do not necessarily care exactly how the effect of the stop loss achieved. For example, there may be a variable in a cBot that acts like a switch to say if trades can/cannot be executed. Or something that automatically switches off all cBots (after first closing all positions). But most importantly, simply "closing all positions" is not sufficient as I want to prevent all cBots from opening further trades until instructed otherwise (which can be either manual or by code - such as "wait Y hours, then allow trades again").

I read briefly in another forum about potentially using "named pipes", but my programming skills are not amazing, and this seems like quite a difficult way to achieve the desired effect.

Any thoughts/suggestions would be greatly appreciated (an example would be amazing also).

Thank you


@hao.han
Replies

firemyst
06 Sep 2021, 16:01

RE:

hao.han said:

Hi, I was someone could help me with some suggestions on how to solve this problem:

High level issue: I have a portfolio of trading cBots running (so 2 or more). How can I halt all trading if account equity falls below X, akin to an "account/portfolio stop loss", until instructed otherwise.

Elaboration: I do not necessarily care exactly how the effect of the stop loss achieved. For example, there may be a variable in a cBot that acts like a switch to say if trades can/cannot be executed. Or something that automatically switches off all cBots (after first closing all positions). But most importantly, simply "closing all positions" is not sufficient as I want to prevent all cBots from opening further trades until instructed otherwise (which can be either manual or by code - such as "wait Y hours, then allow trades again").

I read briefly in another forum about potentially using "named pipes", but my programming skills are not amazing, and this seems like quite a difficult way to achieve the desired effect.

Any thoughts/suggestions would be greatly appreciated (an example would be amazing also).

Thank you

IN each bot, in the OnTick method, just check the equity of your account:

 

//Get your position if any
Position p = Positions.Find(someLabel, Symbol.Name);

//Checks the equity of the account
if (Account.Equity <= someValue)
{
    //If we have a position open, close it
    if (p != null)
        p.Close();

    Stop(); //Stop this bot instance
}
else
{
    //keep going and do whatever
}

 


@firemyst

hao.han
06 Sep 2021, 22:58

RE: RE:

firemyst said:

hao.han said:

Hi, I was someone could help me with some suggestions on how to solve this problem:

High level issue: I have a portfolio of trading cBots running (so 2 or more). How can I halt all trading if account equity falls below X, akin to an "account/portfolio stop loss", until instructed otherwise.

Elaboration: I do not necessarily care exactly how the effect of the stop loss achieved. For example, there may be a variable in a cBot that acts like a switch to say if trades can/cannot be executed. Or something that automatically switches off all cBots (after first closing all positions). But most importantly, simply "closing all positions" is not sufficient as I want to prevent all cBots from opening further trades until instructed otherwise (which can be either manual or by code - such as "wait Y hours, then allow trades again").

I read briefly in another forum about potentially using "named pipes", but my programming skills are not amazing, and this seems like quite a difficult way to achieve the desired effect.

Any thoughts/suggestions would be greatly appreciated (an example would be amazing also).

Thank you

IN each bot, in the OnTick method, just check the equity of your account:

 

//Get your position if any
Position p = Positions.Find(someLabel, Symbol.Name);

//Checks the equity of the account
if (Account.Equity <= someValue)
{
    //If we have a position open, close it
    if (p != null)
        p.Close();

    Stop(); //Stop this bot instance
}
else
{
    //keep going and do whatever
}

 

 

Hi firemyst,

Thanks a lot for your reply. I had thought of this idea and my only concern was the synchronicity because different symbols will be experiencing ticks at different times. At extreme times, a FX symbol may have no ticks for many seconds or even minute+. During such periods, an "on tick" event would not be triggered - thus leading to some cbots closing and some not. But having thought about it a bit more, perhaps this is not such a huge problem if some cbots are shut down slightly behind others. thanks again!

 


@hao.han

firemyst
07 Sep 2021, 03:07 ( Updated at: 07 Sep 2021, 03:28 )

RE: RE: RE:

hao.han said:

 

Hi firemyst,

Thanks a lot for your reply. I had thought of this idea and my only concern was the synchronicity because different symbols will be experiencing ticks at different times. At extreme times, a FX symbol may have no ticks for many seconds or even minute+. During such periods, an "on tick" event would not be triggered - thus leading to some cbots closing and some not. But having thought about it a bit more, perhaps this is not such a huge problem if some cbots are shut down slightly behind others. thanks again!

 

 

Okay. So what? :-)

instead of having each bot instance close its own position, in all onTick methods, close ALL positions regardsless.

//Checks the equity of the account
if (Account.Equity <= someValue)
{
    //If we have any positions open, close them
    foreach (Position p in Positions)
        p.Close();

    Stop(); //Stop this bot instance
}
else
{
    //keep going and do whatever
}

//////////////////////////
//Another example: if you want, keep the positions in profit going with a TSL 
//so you can keep building up your equity

//Checks the equity of the account
if (Account.Equity <= someValue)
{
    //If we have any positions open, close them
    foreach (Position p in Positions)
    {
        //but for those already in profit, set a TSL so we can keep reaping pay day
        if (p.Pips > 5)
        {
            //Move to at least break even before doing so if our 
            //current SL isn't already past the break even point
            if (p.TradeType == TradeType.Buy && p.StopLoss.GetValueOrDefault() < p.EntryPrice)
                p.ModifyStopLossPrice(p.EntryPrice);
            else if (p.TradeType == TradeType.Sell && p.StopLoss.GetValueOrDefault() > p.EntryPrice)
                p.ModifyStopLossPrice(p.EntryPrice);

            p.ModifyTrailingStop(true);
        }
        else
            p.Close();
    }
    Stop(); //Stop this bot instance
}
else
{
    //keep going and do whatever
}

//////// blah blah blah :-)

 


@firemyst