give the magic number of the latest closed trade on History

 
hi guys Im trying to get the magic number of my most recent Take Profit trade on history with this code

int magicnumberclose()

SHMagicNumberClosed = 0 ;
for(int i=OrdersTotal()-1;i>= 0 ;i--)
{
   if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)==true)
      {             
      if(OrderSymbol()==Symbol() && OrderSymbol()==_Symbol)   
          SHMagicNumberClosed = OrderMagicNumber();
      }
}
but instead of giving me the most recent. It is giving me the earliest magic number. 
Extract profit down to the last pip
Extract profit down to the last pip
  • www.mql5.com
The article describes an attempt to combine theory with practice in the algorithmic trading field. Most of discussions concerning the creation of Trading Systems is connected with the use of historic bars and various indicators applied thereon. This is the most well covered field and thus we will not consider it. Bars represent a very artificial entity; therefore we will work with something closer to proto-data, namely the price ticks.
 
mark692:
hi guys Im trying to get the magic number of my most recent Take Profit trade on history with this code

but instead of giving me the most recent. It is giving me the earliest magic number. 
int magicnumberclose()

SHMagicNumberClosed = 0 ;
for(int i=OrdersHistoryTotal()-1;i>= 0 ;i--)
{
   if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)==true)
      {             
      if(OrderProfit()>0 && OrderSymbol()==Symbol() )   
          SHMagicNumberClosed = OrderMagicNumber();
      }
}
 
Mehmet Bastem:

Sorry Mehmet.

  1. Your code continues selecting all orders. It does not stop once it finds one.

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

  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.04.21)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020.06.08)

  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.11.01)

    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:

Sorry Mehmet.

  1. Your code continues selecting all orders. It does not stop once it finds one.

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

  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.04.21)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020.06.08)

  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.11.01)

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

hmm this is confusing 

 
Mehmet Bastem:

hi sir Mehmet sometimes it gives the right magic number sometime its not

 
mark692:

hi sir Mehmet sometimes it gives the right magic number sometime its not

If there is no magic number, it is certain that it will return zero. You just look for orders with magic number that close with profit in EUR/USD (example) pair and it will close first in your code. If there are 10 orders to be taken with profit, you will get the magic number of the 10th order.


int magicnumberclose()

SHMagicNumberClosed = 0 ;
for(int i=OrdersHistoryTotal()-1;i>= 0 ;i--)
{
   if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)==true)
      {             
      if(OrderProfit()>0 && OrderSymbol()==Symbol() )
          {   
          SHMagicNumberClosed = OrderMagicNumber();
break;
}
      }
}
 
William Roeder:

Sorry Mehmet.

  1. Your code continues selecting all orders. It does not stop once it finds one.

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

  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.04.21)
              Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020.06.08)

  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.11.01)

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

Sorry, no one cares about your ignorant answers, as your goal in this forum is just to score points. Please do not reply to my answers. You are not helping anyone, you are just mocking and writing confusing things that have no scientific value.

 
Mehmet Bastem:

If there is no magic number, it is certain that it will return zero. You just look for orders with magic number that close with profit in EUR/USD (example) pair and it will close first in your code. If there are 10 orders to be taken with profit, you will get the magic number of the 10th order.


Oh i see it only gives the magic number of trade if its in profit.I think its not giving me the magic number sometimes because it closes with no profit. hmm what will i add on the code to give me also the last closed with or without profit. Thank you so much Sir Mehmet
 
mark692:
Oh i see it only gives the magic number of trade if its in profit.I think its not giving me the magic number sometimes because it closes with no profit. hmm what will i add on the code to give me also the last closed with or without profit. Thank you so much Sir Mehmet
int magicnumberclose()

SHMagicNumberClosed = 0 ;
for(int i=OrdersHistoryTotal()-1;i>= 0 ;i--)
{
   if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)==true)
      {             
      if(OrderProfit()=>0 && OrderSymbol()==Symbol() )
          {   
          SHMagicNumberClosed = OrderMagicNumber();
break;
}
      }
}