How to get the history deal closed reason in Tick Method

Created at 06 Jan 2021, 13:47
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!
AP

apon.ru.bd

Joined 31.12.2020

How to get the history deal closed reason in Tick Method
06 Jan 2021, 13:47


I need last closed order reason, is it hit TP or SL? Also need Stoploss And Takeprofit.

I can found on PositionsOnClosed(PositionClosedEventArgs args) var reason=args.Reason;

 

But I need from History. Please help me.


@apon.ru.bd
Replies

firemyst
07 Jan 2021, 08:40 ( Updated at: 07 Jan 2021, 08:42 )

Sample code to get such information:

 

Position p1 = args.Position;

//Now get the historical trade stuff.
HistoricalTrade ht = History.FindLast(p1.Label, p1.SymbolName, p1.TradeType);

if (ht != null)
{
    double cp = ht.ClosingPrice;
    Print("Position \"{0} {1}\" closed for reason {2} with {3} profit. Entry Price {4}, Closing Price {5}, StopLoss {6}", p1.Id, p1.Label, args.Reason, String.Format("{0:$#,###.00}", p1.GrossProfit), p1.EntryPrice, cp, p1.StopLoss);
}

The only other thing you'll have to do is narrow down the position(s) returned if you have multiple.

For example:

if (p1.SymbolName == Symbol.Name && p1.Label == thePositionLabel)
            { ... }


@firemyst

apon.ru.bd
07 Jan 2021, 16:48 ( Updated at: 07 Jan 2021, 17:09 )

RE:

firemyst said:

Sample code to get such information:

 

Position p1 = args.Position;

//Now get the historical trade stuff.
HistoricalTrade ht = History.FindLast(p1.Label, p1.SymbolName, p1.TradeType);

if (ht != null)
{
    double cp = ht.ClosingPrice;
    Print("Position \"{0} {1}\" closed for reason {2} with {3} profit. Entry Price {4}, Closing Price {5}, StopLoss {6}", p1.Id, p1.Label, args.Reason, String.Format("{0:$#,###.00}", p1.GrossProfit), p1.EntryPrice, cp, p1.StopLoss);
}

The only other thing you'll have to do is narrow down the position(s) returned if you have multiple.

For example:

if (p1.SymbolName == Symbol.Name && p1.Label == thePositionLabel)
            { ... }

Thanks firemyst  for your quick reply. But it not satisfy my requirement. I need to check closing reason in tick method, not from positionClosing method.

please see the below code

protected override void OnTick()
        {
            var posList = Positions.Where(p => p.SymbolName == Symbol.Name && p.Label == myLabel)
            var penList = PendingOrders.Where(p.SymbolName == Symbol.Name && p.Label == myLabel);

            var cntPosition = posList.Count();

            var cntPending = penList.Count();

         if (cntPosition == 0 && cntPending == 0 && Server.Time between my time setting)
            {

                HistoricalTrade hist = History.FindLast(myLabel,Symbol.Name);
                if (hist != null)
                {
                    //need closing reason,SL,TP here
                 }

           }
            .........................

     }


@apon.ru.bd

PanagiotisCharalampous
07 Jan 2021, 17:26

Hi apon.ru.bd,

Unfortunately this information is not available in the HistoricalTrade class.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

firemyst
08 Jan 2021, 02:46

RE:

PanagiotisCharalampous said:

Hi apon.ru.bd,

Unfortunately this information is not available in the HistoricalTrade class.

Best Regards,

Panagiotis 

Join us on Telegram

Just to clarify, the "reason" is available, but the actual stoploss/take-profit value isn't, correct @Panagiotis?

 

@apon.ru.bd, you said, "I need to check closing reason in tick method, not from positionClosing method."... so put the code in the OnTick method rather than the closing one.

That's a lot of extra CPU time wasted though checking the Historical Trade information on every tick.

Also, there is a way to get the stoploss value when a position is closed. Every time OnTick is called, save the current stoploss value in a global class variable; then in the PositionsClosed method check the value of it (as it should have the latest)

Rough code sample:

private double _lastSLValue = -1;

OnTickMethod( ... )
{
     //whatever

    //check to make sure you have a position
    //you may also need to check if you have an SL if you don't always assign one
    if (p != null)
        _lastSLValue = p.StopLoss.GetValueOrDefault();

    //whatever
}

PositionsClosedMethod ( ... )
{
   //do whatever you need to with _lastSLValue

   // more stuff

   //reset the value if you need to
    _lastSLValue = -1;
}

 


@firemyst

PanagiotisCharalampous
08 Jan 2021, 08:29

Hi firemyst,

He is looking for the reason in the HistoricalTrade class.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

firemyst
08 Jan 2021, 08:47

RE:

PanagiotisCharalampous said:

Hi firemyst,

He is looking for the reason in the HistoricalTrade class.

Best Regards,

Panagiotis 

Join us on Telegram

Ah. Good point. :-)


@firemyst

apon.ru.bd
09 Jan 2021, 05:33

RE:

firemyst said:

PanagiotisCharalampous said:

Hi apon.ru.bd,

Unfortunately this information is not available in the HistoricalTrade class.

Best Regards,

Panagiotis 

Join us on Telegram

Just to clarify, the "reason" is available, but the actual stoploss/take-profit value isn't, correct @Panagiotis?

 

@apon.ru.bd, you said, "I need to check closing reason in tick method, not from positionClosing method."... so put the code in the OnTick method rather than the closing one.

That's a lot of extra CPU time wasted though checking the Historical Trade information on every tick.

Also, there is a way to get the stoploss value when a position is closed. Every time OnTick is called, save the current stoploss value in a global class variable; then in the PositionsClosed method check the value of it (as it should have the latest)

Rough code sample:

private double _lastSLValue = -1;

OnTickMethod( ... )
{
     //whatever

    //check to make sure you have a position
    //you may also need to check if you have an SL if you don't always assign one
    if (p != null)
        _lastSLValue = p.StopLoss.GetValueOrDefault();

    //whatever
}

PositionsClosedMethod ( ... )
{
   //do whatever you need to with _lastSLValue

   // more stuff

   //reset the value if you need to
    _lastSLValue = -1;
}

 

Good point, last closed reson, sl, tp can be save as global variable on PostionClosedMethod and retrieve OnTick event. 

But global variable last assigned value will be lost if terminal is off or terminal shutdown other than my valid trading hour. VPS is fine for your point.


@apon.ru.bd

apon.ru.bd
09 Jan 2021, 05:45

RE:

PanagiotisCharalampous said:

Hi apon.ru.bd,

Unfortunately this information is not available in the HistoricalTrade class.

Best Regards,

Panagiotis 

Join us on Telegram

Good findings. In mql4 and mql5 it's very easy.

As ClosingPrice, Pips, etc are found, Hopefully Spotware Systems will add these properties Reason, StopLoss, TakeProfit in HistoricalTrade class as early as possible.

Thanks


@apon.ru.bd