Replies

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

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
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