How to check if the previous trade was profitable?

 

Hi,

I ran into a problem, which is too high for me at the moment.

The idea is that if a trade was profitable, no new trade should be opened that day. If the trade was a losing trade, a new trade should be opened (if meets the conditions of course).

To solve it, I creared a variable:

int Profitable;

By default its value is 0.

What I want to do is if the Close price was higher than the Open price (if it is a buy of course), "Probitable" should be 1. Therefore no new trade will be opened, because "Profitable=0" is a condition for opening.

I can get the Openprice easily, like:

if(ticket1>0)
{
if(OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES)) Print("Long Opened : ",OrderOpenPrice());

Lastopenprice= OrderOpenPrice();

Print ("Lastopenprice is",OrderOpenPrice());

}

In the journal, it prints it correctly.

However, I can't get the Close price for some reason:

if(OrderSelect(ticket1,SELECT_BY_POS)==true)
Print("Close price for the order ",ticket1," = ",OrderClosePrice());
else

Print("OrderSelect failed error code is",GetLastError());

It always gives me back 0.


Can you, please help me? Thanks !!

 
kebot:

Hi,

I ran into a problem, which is too high for me at the moment.

The idea is that if a trade was profitable, no new trade should be opened that day. If the trade was a losing trade, a new trade should be opened (if meets the conditions of course).

To solve it, I creared a variable:

int Profitable;

By default its value is 0.

What I want to do is if the Close price was higher than the Open price (if it is a buy of course), "Probitable" should be 1. Therefore no new trade will be opened, because "Profitable=0" is a condition for opening.

I can get the Openprice easily, like:

if(ticket1>0)
{
if(OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES)) Print("Long Opened : ",OrderOpenPrice());

Lastopenprice= OrderOpenPrice();

Print ("Lastopenprice is",OrderOpenPrice());

}

In the journal, it prints it correctly.

However, I can't get the Close price for some reason:

if(OrderSelect(ticket1,SELECT_BY_POS)==true)
Print("Close price for the order ",ticket1," = ",OrderClosePrice());
else

Print("OrderSelect failed error code is",GetLastError());

It always gives me back 0.


Can you, please help me? Thanks !!



you will find some answers when you search for OrderHistory on this side!

have luck :-)

 

Well, wake up, we are not informatics!!

Try to be user-friendly!!

I have read both https://docs.mql4.com/ and book.mql4.com, all of it.

None of them explains how we can get the order information in the different stages of the order (pending, opened, closed).

Like:

1. If u have an opened order, you can get the open price of the order like this:

if(OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES)) Print("Long Opened : ",OrderOpenPrice());


2. If you have a closed order and want to get the close price of that trade, you can get it like this:


blablabla...

Your comment was useless.

Maybe u are tired of beginners questions. Well, I can tell you that the problem is that the whole documentation is written BY informatics...

 
kebot:

Well, wake up, we are not informatics!!

Try to be user-friendly!!

I have read both https://docs.mql4.com/ and book.mql4.com, all of it.

None of them explains how we can get the order information in the different stages of the order (pending, opened, closed).

Like:

1. If u have an opened order, you can get the open price of the order like this:

if(OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES)) Print("Long Opened : ",OrderOpenPrice());


2. If you have a closed order and want to get the close price of that trade, you can get it like this:


blablabla...

Your comment was useless.

Maybe u are tired of beginners questions. Well, I can tell you that the problem is that the whole documentation is written BY informatics...


hmmm you question was this, no?

>> if a trade was profitable, no new trade should be opened

the last trade you find only in orderhistory?

OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);

 

Tx.

Let me write how I understand it in general if I am right.

If there is an opened but not closed trade, we can get the different types of the order information (price, time, etc.) by

1. Selecting the order in question with OrderSelect(.,., MODE_TRADES).

2. Then get the type of info u want for example Open price with Openorderprice()

If there is a closed order and want to get the different order information from that we do it by

1. Selecting the order with OrderSelect(.,.,MODE_HISTORY)

2. Then get the type of info u want for example order profit price with Orderprofit()

Is this right?

 
kebot:

Tx.

Let me write how I understand it in general if I am right.

If there is an opened but not closed trade, we can get the different types of the order information (price, time, etc.) by

1. Selecting the order in question with OrderSelect(.,., MODE_TRADES).

2. Then get the type of info u want for example Open price with Openorderprice()

If there is a closed order and want to get the different order information from that we do it by

1. Selecting the order with OrderSelect(.,.,MODE_HISTORY)

2. Then get the type of info u want for example order profit price with Orderprofit()

Is this right?




and dont forget to loop throug the orders if are more then one and you look for some special Pair/MagicNumber etc...

for(int i=OrdersTotal()-1; i>=0; i--) 
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol())
{
do_something();
}
}
only as example ..
 

Tx. It is true!!

I can see your formula returns the orders in descending order, first the last, and so on.

Am I right to suppose that Magic number is a unique identifier? When I trade in real life with a real broker, it is a number given by the broker's system?

When I shoot down the expert advisor and restart it the next day, the OrderHistory will return all the orders I made in the past (as long as it is found in the broker's system), is it right?

By your expertise, if there are 100s of trades per day, will it not slow down the expert advisor? Or what do you think is the biggest number of trades when you say it might be an issue?

In my case there will be only day trades. So do you think it is worth it to somehow limit the search you described to a daily basis? Because my system is not interested in what happened the day before, every day is a completely blank sheet.

I wonder if I can solve it without having to shot down the system and restart every day, unless we can set a script to reset all open order criteria to default on workdays at for example 00:00 am.

 

kebot, to determine the last order that was closed in the history pool filtered by order type, symbol and magic number you could use the following function, you could modify the function to further filter out by date if required.

//+------------------------------------------------------------------+
//| Function..: LastClosedOrderPos                                   |
//| Purpose...: Locate position number of order that was last closed |
//|             in the history pool (closed and canceled orders).    |
//| Parameters: iOrderType        - Order type, pass -1 for any type.|
//|             iOrderMagicNumber - Magic number of the expert or the|
//|                                 script, pass -1 for all orders to|
//|                                 be searched.                     |
//|             sSymbol           - Symbol/Instrument, defaults to   |
//|                                 all symbols to be searched for.  |
//| Returns...: iOrderPosition or -1 (not found).                    |
//| Sample....: int iPos=LastClosedOrderPos(OP_SELL,MagicNumberSell);|
//|             if (OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY))  |
//|                Print("Profit for last order ",OrderProfit());    |
//|             else                                                 |
//|                Print("OrderSelect error ",GetLastError());       |
//+------------------------------------------------------------------+
int LastClosedOrderPos(int iOrderType=-1, int iOrderMagicNumber=-1, string sSymbol="") {
  datetime tOrderCloseTime;
  int      i, iOrderPosition=-1, iOrdersTotal=OrdersHistoryTotal();
 
  for (i=0; i<iOrdersTotal; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (sSymbol=="" || OrderSymbol()==sSymbol) {
        if (iOrderType<0 || OrderType()==iOrderType) {
          if (iOrderMagicNumber<0 || OrderMagicNumber()==iOrderMagicNumber) {
            if (tOrderCloseTime<OrderCloseTime()) {
              tOrderCloseTime=OrderCloseTime();
              iOrderPosition=i;
            }
          }
        }
      }
    }
  }
  return(iOrderPosition);
}  
 

Tx!

We are getting closer :).

If I unnderstand this part correctly, it returns either -1 (if something is wrong) or a "position number". What is this position number exactly?

As I am not an informatics expert, if I want to get how much profit this last order made, is it like:

double A;

double Profitabletrade;

HERE COMES YOUR FUNCTION

If (OrderSelect (iOrderPosition, SELECT_BY_POS, MODE_HISTORY)) A=OrderProfit();

If A>0 Profitabletrade=1;


If this is ok, where do I put the OrderProfit line? (After which paranthesis?)

In general this is one of my biggest issues, that there are so many {s and }s, I realised that if I put the line in the wrong line, I get totally different results.

 

The function LastClosedOrderPos returns the position number of the order which was last closed matching the

filtering criteria of Symbol, OrderType and OrderMagicNumber. Orders which have been closed or pending

orders which have been cancelled are listed seperately in the OrdersHistory pool (refer to the OrderSelect()

function).


Code to handle the result of a closed trade could be as follows:

bool bProfitableTrade = false;
int  iPos = LastClosedOrderPos();
if (iPos >= 0) {
   if (OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY))
      bProfitableTrade = ((OrderProfit()+OrderCommission()+OrderSwap()) > 0);
   else
      Print("OrderSelect error ",GetLastError());
   if (bProfitableTrade) {
      // do whatever after a good trade
   }
   else {
      // do whatever after a bad trade
   }   
}
Reason: