Last order profit code please help!!

 

I've got this function which i don't know why it doesn't  work. I want the EA to place a buy order is last closed  sell trade was a loss.

double LastS()
{

double ProS = 0;
      
 for(int i=OrdersHistoryTotal()-1; i>=0; i--)
         {
         if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == true)
         
            // match the symbol 
            if(OrderSymbol() == Symbol())
            {
              
               if(OrderType() == OP_SELL)
                  ProS +=OrderClosePrice()-OrderOpenPrice();
            }
            
            
       }
       
          return( ProS);


then if  LastS()<0 ..then buy .. i.e if last order profit is negative and order was a sell, then buy. But this does not seem to place any orders. Is there anything wrong??

 
prweza:

I've got this function which i don't know why it doesn't  work. I want the EA to place a buy order is last closed  sell trade was a loss.


then if  LastS()<0 ..then buy .. i.e if last order profit is negative and order was a sell, then buy. But this does not seem to place any orders. Is there anything wrong??


please help

 
prweza:

I've got this function which i don't know why it doesn't  work. 


Where did you "got" it?

 
prweza: Is there anything wrong??
  1. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
              Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum
 
prweza:

please help

  1. YOU didn't write this.
  2. It isn't sending BUY  orders because there is no code to SEND buy orders.
  3. READ the documentation. --> https://docs.mql4.com/
  4. READ the book. --> https://book.mql4.com/
  5. Put forth some effort and do not expect others to work for free.


MQL4 Reference
MQL4 Reference
  • docs.mql4.com
MetaQuotes Language 4 (MQL4) is a built-in language for programming trading strategies. This language is developed by MetaQuotes Software Corp. based on their long experience in the creation of online trading platforms. Using this language, you can create your own Expert Advisors that make trading management automated and are perfectly suitable...
 

ill this  will add magic number, will this work?


double LastS()
{

double ProS = 0;
      
 for(int i=OrdersHistoryTotal()-1; i>=0; i--)
         {
         if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == true)
         
            // match the symbol 
            if(OrderSymbol() == Symbol
         &&MagicNumber()==Magic number..
            {
              
               if(OrderType() == OP_SELL)
                  ProS +=OrderClosePrice()-OrderOpenPrice();
            }
            
            
       }
       
          return( ProS);

Will this work?

 
prweza:

ill this  will add magic number, will this work?


Will this work?

That isn't referencing the magic number of the ticket.

It's OrderMagicNumber()

Please see this documentation page.

https://docs.mql4.com/trading/ordermagicnumber


I'd use something more like this:

int magic = 123; //my magic number

for(int x=OrdersTotal()-1;x>=0;x--){   //scan the order basket
 if(OrderSelect(x,SELECT_BY_POS,MODE_TRADES)==true){ //select a ticket via index position X from the current trade basket (Change if you want history...)
  if(OrderSymbol()==Symbol() && OrderMagicNumber() == magic){ //does the ticket symbol match the chart? And does the ticket magic number match my defined magic number
   if(OrderType()==OP_BUY){ /* DO SOMETHING HERE FOR BUY TICKETS*/}
   if(OrderType()==OP_SELL){/* DO SOMETHING HERE FOR SELL TICKETS*/}
  }
 }
}

I didn't test that, but it should give you a good example to work from.

-Jack

OrderMagicNumber - Trade Functions - MQL4 Reference
OrderMagicNumber - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderMagicNumber - Trade Functions - MQL4 Reference
 

double last;

for(int i=OrdersHistoryTotal()-1;i>=0;i--)

 {

   OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);  //error was here

   if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)

    {

       //for buy order

       if(OrderType()==OP_BUY && OrderClosePrice()>=OrderOpenPrice()) last=1;

       if(OrderType()==OP_BUY && OrderClosePrice()<=OrderOpenPrice()) last=0;

    }

 }

What about this code?


Thanks.

 
//+------------------------------------------------------------------+
//| Get Profits                                                      |
//+------------------------------------------------------------------+
double GetProfits(ENUM_ORDER_TYPE Type, int Count)
  {
//----

   int      Cnt     = 0;

   double   Profits = 0.0;

   for(int i = OrdersHistoryTotal() - 1; i >= 0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == false)
      {
         Print("Error in history!");

         break;
      }

      if(OrderSymbol() == _Symbol && OrderMagicNumber() == MagicNum)
      {
         if(OrderType() == Type)
         {
            Cnt++;

            Profits += OrderProfit();
         }
      }

      if(Cnt == Count) break;
   }

//----
   return(Profits);
  }

Get the total profits of last xx trades

 
Dua Yong Rew:

Get the total profits of last xx trades


I just mean the profit of last  closed order.Not the sum of all closed orders.,

 
prweza:

I just mean the profit of last  closed order.Not the sum of all closed orders.,



double LastOrderClosedProfit()
{
   int      ticket      =-1;
   datetime last_time   = 0;
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)&&OrderSymbol()==_Symbol&&OrderCloseTime()>last_time)
      {
         last_time = OrderCloseTime();
         ticket = OrderTicket();
      }
   }
   if(!OrderSelect(ticket,SELECT_BY_TICKET))
   {
      Print("OrderSelectError: ",GetLastError())
      return 0.0;
   }    
   return OrderProfit();
}