Capture current trade type

 

Hi all,

I'm trying to generate the current open trade type but with the following code but the result is always 0 even when the current trade is a SELL.

I've adapted the same function to collect the last (history) type trade and it worked fine.

For this function i've changed the OrdersTotal for OrdersHistoryTotal and MODE_TRADES for MODE_HISTORY.

Any thoughts? 

 int CurrentOrderType2()
  {
   datetime TicketTime = 0;
   int LastOT;

   for(int ix = 1; ix < OrdersTotal(); ix++)
     {
      if(OrderSelect(ix, SELECT_BY_POS, MODE_TRADES)
         && OrderMagicNumber() == MagicNumber
         && OrderSymbol() == Symbol())
        {
         if(OrderOpenTime() > TicketTime)
           {
            LastOT = OrderType();
           }
        }
      else
         Print("OrderSelect returned the error of ",GetLastError());
     }
//Print("Last closed order: ",TicketN,"  ",TicketTime,"  ",LastOT);   //For testing
   return(LastOT); // Buy==0, Sell==1, Others==2 through 5
  }

thank you in advance!

Andrés

 
Pinto André:

Hi all,

I'm trying to generate the current open trade type but with the following code but the result is always 0 even when the current trade is a SELL.

I've adapted the same function to collect the last (history) type trade and it worked fine.

For this function i've changed the OrdersTotal for OrdersHistoryTotal and MODE_TRADES for MODE_HISTORY.

Any thoughts? 

thank you in advance!

Andrés

I think you need to find last ticket number first..

after that you can find last order type with a ticket number

and + 

 int LastOT;

LastOT should be -1, due to zero means OP_BUY

 
Pinto André:

Hi all,

I'm trying to generate the current open trade type but with the following code but the result is always 0 even when the current trade is a SELL.

I've adapted the same function to collect the last (history) type trade and it worked fine.

For this function i've changed the OrdersTotal for OrdersHistoryTotal and MODE_TRADES for MODE_HISTORY.

Any thoughts? 

thank you in advance!

Andrés

What would you start from order 1... why not starting from order "0". orders positions starts from "0"

for(int ix = 0; ix < OrdersTotal(); ix++)
 
Pinto André: Any thoughts? 
         if(OrderOpenTime() > TicketTime)
           {
            LastOT = OrderType();
           }

  1. You don't update TicketTime, so you are not getting the latest.

  2. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017)

  3. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  4. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - <TICKET>
    Rollover - <TICKET>

    >R/O - 1,000 EUR/USD @0.52

    #<ticket>  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)
 
William Roeder #:
  1. You don't update TicketTime, so you are not getting the latest.

  2. Do not assume history has only closed orders.
              OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017)

  3. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  4. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
              "balance" orders in account history - Day Trading Techniques - MQL4 programming forum (2017)

    Broker History
    FXCM
    Commission - <TICKET>
    Rollover - <TICKET>

    >R/O - 1,000 EUR/USD @0.52

    #<ticket>  N/A
    OANDA
    Balance update
    Financing (Swap: One entry for all open orders.)

Hi William,

for point 1 I'm not following you. TicketTime is always zero in order to trigger the if settence when a order is open and I'm not getting any value at all. 

The idea is to store the result into an array and use it later (I have this part covered), the issue is that i'm not getting the correct value when the trade happens.

for point 2 and 3, thank you it's a good tip. I could be a work around. Nonetheless for my knowledge I'm will pursue and try to fix the current code. 

Thank you.

 
Ahmet Metin Yilmaz #:

I think you need to find last ticket number first..

after that you can find last order type with a ticket number

and + 

LastOT should be -1, due to zero means OP_BUY

Thank you Ahmet.

I will check it out how to call last ticket number.

And thank you for the good tip on LastOT.

 
Pinto André #: for point 1 I'm not following you. TicketTime is always zero in order to trigger the if settence when a order is open and I'm not getting any value at all.

When in doubt, think. If it is always zero to trigger, then what is the point of having the if and variable at all?

 
Pinto André #:

Thank you Ahmet.

I will check it out how to call last ticket number.

And thank you for the good tip on LastOT.

last ticket nr. is the biggest nr.

 
Ahmet Metin Yilmaz #:

last ticket nr. is the biggest nr.

Not necessarily.

A pending order triggering could be the last trade opened, but it may not have the biggest number.

The biggest number may be the remainder from a partial close order, but it may not be the last trade opened.

 
Keith Watford #:

Not necessarily.

A pending order triggering could be the last trade opened, but it may not have the biggest number.

The biggest number may be the remainder from a partial close order, but it may not be the last trade opened.

right

may be it helps to find  https://www.mql5.com/en/forum/109180