stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Can an EA read last trade from terminal history?
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=1596
Page 1 of 1
Author:  forextrader-radioman [ Sat Mar 09, 2013 6:28 pm ]
Post subject:  Can an EA read last trade from terminal history?

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
Author:  dietcoke [ Sat Mar 09, 2013 7:35 pm ]
Post subject:  Re: Can an EA read last trade from terminal history?

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
Author:  fxprotrader [ Sun Mar 10, 2013 12:42 pm ]
Post subject:  Re: Can an EA read last trade from terminal history?

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; 
    }
 }
Author:  dietcoke [ Sun Mar 10, 2013 1:00 pm ]
Post subject:  Re: Can an EA read last trade from terminal history?

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()
Author:  forextrader-radioman [ Sun Mar 10, 2013 3:36 pm ]
Post subject:  Re: Can an EA read last trade from terminal history?

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
Author:  fxprotrader [ Tue Mar 12, 2013 3:21 am ]
Post subject:  Re: Can an EA read last trade from terminal history?

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;
}
  
Author:  forextrader-radioman [ Tue Mar 12, 2013 8:00 pm ]
Post subject:  Re: Can an EA read last trade from terminal history?

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 :)
All times are UTC Page 1 of 1