Can an EA read last trade from terminal history?

Post Reply
User avatar
forextrader-radioman
Trader
Posts: 156
Joined: Sun Nov 27, 2011 11:11 am
Location: Hamburg/Germany

Can an EA read last trade from terminal history?

Post by forextrader-radioman »

Hello coders :)

One question: is it possiple for an EA to read the last trade from terminal history?

For example: Just in this moment there is a trade closed: it was a buy EURUSD 1.3550 and TP was 1.3800 with 0.5 lot. Use this data to create a new pending order (with same settings).

all of you a nice weekend,
Dietmar
„Reality is merely an illusion, albeit a very persistent one.” (Albert Einstein) "Realität ist lediglich eine Illusion, allerdings eine sehr hartnäckige."
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Can an EA read last trade from terminal history?

Post by dietcoke »

forextrader-radioman wrote:Hello coders :)

One question: is it possiple for an EA to read the last trade from terminal history?

For example: Just in this moment there is a trade closed: it was a buy EURUSD 1.3550 and TP was 1.3800 with 0.5 lot. Use this data to create a new pending order (with same settings).

all of you a nice weekend,
Dietmar
Not that i know i'm afraid

You would have to know the ticket no of all open trades (in an array or saved as gv's) and then poll the open trades to see if one has disappared from the open list
fxprotrader
Trader
Posts: 26
Joined: Sat Jun 16, 2012 11:26 pm

Re: Can an EA read last trade from terminal history?

Post by fxprotrader »

dietcoke wrote:
forextrader-radioman wrote:Hello coders :)

One question: is it possiple for an EA to read the last trade from terminal history?

For example: Just in this moment there is a trade closed: it was a buy EURUSD 1.3550 and TP was 1.3800 with 0.5 lot. Use this data to create a new pending order (with same settings).

all of you a nice weekend,
Dietmar
Not that i know i'm afraid

You would have to know the ticket no of all open trades (in an array or saved as gv's) and then poll the open trades to see if one has disappared from the open list
dietcoke-
Maybe I misunderstand the request but as I understand it here is a bit of code that will do just that. I have been using a modified version of it for quite a while to find when last trade closed and works quite well.

Taken from an article here:
http://forum.mql4.com/10899

Code: Select all

string last;
for(int i=OrdersHistoryTotal()-1,i>=0,i--)
 {
   OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
    {
       //for buy order
       if(OrderType()==OP_BUY && OrderProfit()>0) last="profit";
       if(OrderType()==OP_BUY && OrderProfit()<0) last="loss";
       break; 
    }
 }
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Can an EA read last trade from terminal history?

Post by dietcoke »

fxprotrader wrote:
dietcoke-
Maybe I misunderstand the request but as I understand it here is a bit of code that will do just that. I have been using a modified version of it for quite a while to find when last trade closed and works quite well.

Taken from an article here:
http://forum.mql4.com/10899

Code: Select all

string last;
for(int i=OrdersHistoryTotal()-1,i>=0,i--)
 {
   OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
    {
       //for buy order
       if(OrderType()==OP_BUY && OrderProfit()>0) last="profit";
       if(OrderType()==OP_BUY && OrderProfit()<0) last="loss";
       break; 
    }
 }



But you don't want to be constantly trawling the order history to see if a tp has just been hit. History can get quite big after a time depending on trading frequency

you still have to track the open trades so you can intelligently decide when its a good idea to check the history. and if you do that you will know the ticket number you should be looking for to get the OrderCloseTime()
User avatar
forextrader-radioman
Trader
Posts: 156
Joined: Sun Nov 27, 2011 11:11 am
Location: Hamburg/Germany

Re: Can an EA read last trade from terminal history?

Post by forextrader-radioman »

hmmm, thank you for your thoughts ...

for my need it is ok to replace just the actual closed trade. EA has to monitor constantly and if any TP is given: replace that trade as a pending order. Thats all ... after that it is standing by until a new "TP event" is given ... would that be possible with the code?

(damn, we have winter again here in Hamburg/Germany ... 15cm new snow over the last night ... 3 days before we had spring temperatures and sunshine)

, Dietmar
„Reality is merely an illusion, albeit a very persistent one.” (Albert Einstein) "Realität ist lediglich eine Illusion, allerdings eine sehr hartnäckige."
fxprotrader
Trader
Posts: 26
Joined: Sat Jun 16, 2012 11:26 pm

Re: Can an EA read last trade from terminal history?

Post by fxprotrader »

forextrader-radioman wrote:hmmm, thank you for your thoughts ...

for my need it is ok to replace just the actual closed trade. EA has to monitor constantly and if any TP is given: replace that trade as a pending order. Thats all ... after that it is standing by until a new "TP event" is given ... would that be possible with the code?

(damn, we have winter again here in Hamburg/Germany ... 15cm new snow over the last night ... 3 days before we had spring temperatures and sunshine)

, Dietmar
Here is a portion of the atual code to determine last closed trade and whether the trade was loss, profit or BE and how long ago it closed.

There is not a problem with running it on EachTick. If you are concerend then run it only once per bar.

Code: Select all

for(pos=OrdersHistoryTotal(); pos >= 0; pos--){
 if(OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY) && OrderMagicNumber()==MagicNumber && OrderSymbol()== Symbol()){
 
if(OrderType()==OP_SELL){
 OP=OrderProfit();
 lastord="S";
 BarsSinceLastClose=iBarShift(NULL,0,OrderCloseTime());
if(OrderProfit()<=0){
  lastlossbarsS =  (Time[0]-OrderCloseTime())/TimeDivisor;
  lastS="loss";
  }
   else if(OrderProfit()>0){
  lastlossbarsS =  (Time[0]-OrderCloseTime())/TimeDivisor;
  lastS="profit";  

 }
 else{
  lastS="BE";  
  lastlossbarsS =  (Time[0]-OrderCloseTime())/TimeDivisor;
}
 break;
}
  
User avatar
forextrader-radioman
Trader
Posts: 156
Joined: Sun Nov 27, 2011 11:11 am
Location: Hamburg/Germany

Re: Can an EA read last trade from terminal history?

Post by forextrader-radioman »

fxprotrader wrote: Here is a portion of the atual code to determine last closed trade and whether the trade was loss, profit or BE and how long ago it closed. [...]
Thank you :)
„Reality is merely an illusion, albeit a very persistent one.” (Albert Einstein) "Realität ist lediglich eine Illusion, allerdings eine sehr hartnäckige."
Post Reply

Return to “Coders Hangout”