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

Order tracking
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=84
Page 1 of 2
Author:  matrixebiz [ Thu Nov 24, 2011 4:51 pm ]
Post subject:  Order tracking

Hello All, just wondering what your techniques are for selecting specific orders on the same pair that have multiple orders currently open without using MagicNumbers or Comments. I was thinking of using something like OrderOpenTime() but haven't checked this Return to see what type of value it spits out and how to implement this type of checking with EA's that use the Recovery System.

All options welcome :)

Thanks
Author:  gaheitman [ Thu Nov 24, 2011 5:21 pm ]
Post subject:  Re: Order tracking

Do you need to get just the first or the last order, or will you need to find one in between? For the first or the last, you can just iterate through the orders list forwards or backwards (respectively) and just take the first match.

Doesn't OrderOpenTime() change after the order is actually filled, if you start with a pending order? I seem to remember running across that at some point. Not sure if it matters anyway, but it might change the order if you suspected the order was on when you placed them. And if I am remembering wrong, it really doesn't matter. :)

You could also keep track of the orders in an array as you place them. If you use a multi-dimensional array you can keep track of additional data you want for each order (as long as you map it to integers).

George
matrixebiz wrote:Hello All, just wondering what your techniques are for selecting specific orders on the same pair that have multiple orders currently open without using MagicNumbers or Comments. I was thinking of using something like OrderOpenTime() but haven't checked this Return to see what type of value it spits out and how to implement this type of checking with EA's that use the Recovery System.

All options welcome :)

Thanks
Author:  matrixebiz [ Thu Nov 24, 2011 5:38 pm ]
Post subject:  Re: Order tracking

gaheitman wrote:Do you need to get just the first or the last order, or will you need to find one in between? For the first or the last, you can just iterate through the orders list forwards or backwards (respectively) and just take the first match.
No, I will actually need to check every open order for the pair as I have a CloseAll function that calculates the profit of all the orders and closes them when a total profit is hit.
gaheitman wrote:Doesn't OrderOpenTime() change after the order is actually filled, if you start with a pending order? I seem to remember running across that at some point. Not sure if it matters anyway, but it might change the order if you suspected the order was on when you placed them. And if I am remembering wrong, it really doesn't matter. :)
Yes, you are correct but I'm not using pending orders so this should not affect me
gaheitman wrote:You could also keep track of the orders in an array as you place them. If you use a multi-dimensional array you can keep track of additional data you want for each order (as long as you map it to integers).
This sounds like a viable option but I'm not advanced enough in my coding abilities do do something like this unless you feel like elaborating on this with examples :)

Thank you
Author:  gaheitman [ Thu Nov 24, 2011 6:17 pm ]
Post subject:  Re: Order tracking

If you are just trying to close all the open orders when you hit a threshold of profit, something like this should work:

Code: Select all

   double target = 100; //target profit in money not pips
   double profit = 0;   //hold the profit as we go through the list

   //scan open trades
   int total=OrdersTotal();
   for(int i=0; i<total; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if (OrderSymbol() == Symbol())
            profit += OrderProfit();
     }

   //scan closed trades
   total=OrdersHistoryTotal();
   for(i=0; i<total; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         if (OrderSymbol() == Symbol())
            profit += OrderProfit();
        
     }

   if (profit >= target)
     //made it, now close all the orders
     CloseOpenOrders();  //the procedure to close all the matching open orders
   
If you aren't interested in including closed trades, just strike the second loop.

This isn't finished code, just a quick example of the looping you would need.

George
matrixebiz wrote:
gaheitman wrote:Do you need to get just the first or the last order, or will you need to find one in between? For the first or the last, you can just iterate through the orders list forwards or backwards (respectively) and just take the first match.
No, I will actually need to check every open order for the pair as I have a CloseAll function that calculates the profit of all the orders and closes them when a total profit is hit.
gaheitman wrote:Doesn't OrderOpenTime() change after the order is actually filled, if you start with a pending order? I seem to remember running across that at some point. Not sure if it matters anyway, but it might change the order if you suspected the order was on when you placed them. And if I am remembering wrong, it really doesn't matter. :)
Yes, you are correct but I'm not using pending orders so this should not affect me
gaheitman wrote:You could also keep track of the orders in an array as you place them. If you use a multi-dimensional array you can keep track of additional data you want for each order (as long as you map it to integers).
This sounds like a viable option but I'm not advanced enough in my coding abilities do do something like this unless you feel like elaborating on this with examples :)

Thank you
Author:  matrixebiz [ Thu Nov 24, 2011 6:36 pm ]
Post subject:  Re: Order tracking

gaheitman wrote:If you are just trying to close all the open orders when you hit a threshold of profit, something like this should work:

Code: Select all

   double target = 100; //target profit in money not pips
   double profit = 0;   //hold the profit as we go through the list

   //scan open trades
   int total=OrdersTotal();
   for(int i=0; i<total; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if (OrderSymbol() == Symbol())
            profit += OrderProfit();
     }

   //scan closed trades
   total=OrdersHistoryTotal();
   for(i=0; i<total; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         if (OrderSymbol() == Symbol())
            profit += OrderProfit();
        
     }

   if (profit >= target)
     //made it, now close all the orders
     CloseOpenOrders();  //the procedure to close all the matching open orders
   
If you aren't interested in including closed trades, just strike the second loop.

This isn't finished code, just a quick example of the looping you would need.

George
Ok, that seems easy enough but it's an older EA of mine so I'll have to scan though it to see if that is all I was trying to do with the multiple orders. It uses a Recovery system in it as well, so not sure if that was also the reason I was using MagicNumbers to also keep track of those orders also. I'll get back to you on that. Thank you for the code
Author:  matrixebiz [ Thu Nov 24, 2011 6:49 pm ]
Post subject:  Re: Order tracking

Hi, looks like I was using the extra magic numbers to find out if the first order was in DD so many pips hence needing a recovery order then if profit was still not hit, needed to check if the second order is now also in DD by so many pips to place another recovery order, etc.

Code: Select all

void OrderNeedsRecovery()
{
int totalorders = OrdersTotal();
for(int i=totalorders-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
{
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()
       && OrderOpenPrice() - Point*ReEntryLinePips > Bid) RecoveryInProgress = true;
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber+1 && OrderSymbol() == Symbol()
       && OrderOpenPrice() - Point*ReEntryLinePips > Bid) RecoveryInProgress1 = true;
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber+2 && OrderSymbol() == Symbol()
       && OrderOpenPrice() - Point*ReEntryLinePips > Bid) RecoveryInProgress2 = true;
                      
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()
       && OrderOpenPrice() + Point*ReEntryLinePips < Ask) RecoveryInProgress = true;
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber+1 && OrderSymbol() == Symbol()
       && OrderOpenPrice() + Point*ReEntryLinePips < Ask) RecoveryInProgress1 = true;
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber+2 && OrderSymbol() == Symbol()
       && OrderOpenPrice() + Point*ReEntryLinePips < Ask) RecoveryInProgress2 = true;

}}
return;
}
Author:  gaheitman [ Thu Nov 24, 2011 7:08 pm ]
Post subject:  Re: Order tracking

The code to go through them all and close them would still be appropriate. I also finally took a look into MPTM, and it has the ability to monitor a basket of trades (here it would be a basket of the same currency) and close them all if they meet a predefined profit. Certainly worth a look. http://www.stevehopwoodforex.com/phpBB3 ... ?f=21&t=64
matrixebiz wrote:Hi, looks like I was using the extra magic numbers to find out if the first order was in DD so many pips hence needing a recovery order then if profit was still not hit, needed to check if the second order is now also in DD by so many pips to place another recovery order, etc.

Code: Select all

void OrderNeedsRecovery()
{
int totalorders = OrdersTotal();
for(int i=totalorders-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
{
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()
       && OrderOpenPrice() - Point*ReEntryLinePips > Bid) RecoveryInProgress = true;
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber+1 && OrderSymbol() == Symbol()
       && OrderOpenPrice() - Point*ReEntryLinePips > Bid) RecoveryInProgress1 = true;
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber+2 && OrderSymbol() == Symbol()
       && OrderOpenPrice() - Point*ReEntryLinePips > Bid) RecoveryInProgress2 = true;
                      
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()
       && OrderOpenPrice() + Point*ReEntryLinePips < Ask) RecoveryInProgress = true;
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber+1 && OrderSymbol() == Symbol()
       && OrderOpenPrice() + Point*ReEntryLinePips < Ask) RecoveryInProgress1 = true;
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber+2 && OrderSymbol() == Symbol()
       && OrderOpenPrice() + Point*ReEntryLinePips < Ask) RecoveryInProgress2 = true;

}}
return;
}
Author:  matrixebiz [ Thu Nov 24, 2011 7:23 pm ]
Post subject:  Re: Order tracking

gaheitman wrote:The code to go through them all and close them would still be appropriate.
... but the problem here is that if you see in the recovery code, I'm not actually looking for it to check the profit and close them all at this moment in time, I need it to check specific orders to see if an order is at a certain amount of DrawDown so I can tell the EA to open another order. So basically what I need for example is for the EA to check on lets say the third order I've opened and see how many pips it is in the negative and if it is -200pips then the EA will determine if I will open another order. This is why I was using MagicNumber + 2 on the third order opened so I could check on that order specifically.
Sorry if this is getting confusing.
Author:  gaheitman [ Thu Nov 24, 2011 7:49 pm ]
Post subject:  Re: Order tracking

I'm reading the code out of context, but I thought that this was a test to see if we need to enter a new trade. For example, in this test:

Code: Select all

if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber+2 && OrderSymbol() == Symbol()
       && OrderOpenPrice() - Point*ReEntryLinePips > Bid) RecoveryInProgress2 = true;
the code is checking to see if the order we entered with MagicNumber+2 has gone far enough against us that we need to enter another recovery trade. I assumed this code was followed by a test to see that if RecoveryInProgress2 = true, enter recovery trade #3.

Another way of looking at the test is to just examine the first trade. If it goes against us ReEntryLinePips, enter a trade. If it then goes against us ReEntryLinePips*2, enter a trade. You want to stop when the number of open trades exceeds the max number of recovery trades you are willing to enter + 1. Your code just has to keep track of the first ticket number and re-select it after you enter a trade. Or, if you are just trading one currency, one direction at a time, you can just select the first trade in the list with OrderSelect(0, SELECT_BY_POS)
matrixebiz wrote:
gaheitman wrote:The code to go through them all and close them would still be appropriate.
... but the problem here is that if you see in the recovery code, I'm not actually looking for it to check the profit and close them all at this moment in time, I need it to check specific orders to see if an order is at a certain amount of DrawDown so I can tell the EA to open another order. So basically what I need for example is for the EA to check on lets say the third order I've opened and see how many pips it is in the negative and if it is -200pips then the EA will determine if I will open another order. This is why I was using MagicNumber + 2 on the third order opened so I could check on that order specifically.
Sorry if this is getting confusing.
Author:  matrixebiz [ Thu Nov 24, 2011 8:05 pm ]
Post subject:  Re: Order tracking

gaheitman wrote:I'm reading the code out of context, but I thought that this was a test to see if we need to enter a new trade. For example, in this test:

Code: Select all

if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber+2 && OrderSymbol() == Symbol()
       && OrderOpenPrice() - Point*ReEntryLinePips > Bid) RecoveryInProgress2 = true;
the code is checking to see if the order we entered with MagicNumber+2 has gone far enough against us that we need to enter another recovery trade. I assumed this code was followed by a test to see that if RecoveryInProgress2 = true, enter recovery trade #3.
I wrote the code so you are correct in what I'm trying to do here :) You are reading it correctly :) but need to get rid of associating MagicNumbers to the orders.
gaheitman wrote:Another way of looking at the test is to just examine the first trade. If it goes against us ReEntryLinePips, enter a trade. If it then goes against us ReEntryLinePips*2, enter a trade. You want to stop when the number of open trades exceeds the max number of recovery trades you are willing to enter + 1. Your code just has to keep track of the first ticket number and re-select it after you enter a trade. Or, if you are just trading one currency, one direction at a time, you can just select the first trade in the list with OrderSelect(0, SELECT_BY_POS)
... Ok, now to the solution of getting rid of MagicNumber tracking :) So you are saying that I should track by ticket number (which I have no experience with) instead of MagicNumber +2? if so, how do I check for the ticket number for the second recovery order if I'm also trading different pairs on the same account? So the ticket number of a second pair recovery order may be 10 if I am trading multiple pairs, correct?
All times are UTC Page 1 of 2