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

Order Select Question?
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=1296
Page 1 of 1
Author:  ewinner [ Mon Dec 10, 2012 3:48 pm ]
Post subject:  Order Select Question?

Hi Guys,

Im having a bit of trouble with some code and going round and round in circles!!!! any help much appreciated before i go totally mad!!

What im trying to do, is check to see if there is an open order already, before it sends another order at the same price (yes, im working on a grid trader!!)

The code i have come up with so far finds the last order OrderOpenPrice ok, but i need it to find an order if it exists with the same price, NOT just the last one placed - make any sense??

Thanks for any help
Mark

Code: Select all


bool DoesBuyTradeExist()
{
   
   int TicketNo = 0;
   double BuyOrderPrice=0;
   
   if (OrdersTotal() == 0) return(false);
   
   for (int cc = OrdersTotal() - 1; cc >= 0 ; cc--)
   {
      if (!OrderSelect(cc,SELECT_BY_POS,MODE_TRADES)) continue;
      if (OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() != Symbol()) continue;

      
      if (OrderType() == OP_BUY)      
      {
      TicketNo = OrderTicket();
      BuyOrderPrice = OrderOpenPrice();
      }
      
      return(true);         
      
   }//for (int cc = OrdersTotal() - 1; cc >= 0 ; cc--)

   return(false);

}//End bool DoesBuyTradeExist()


Author:  dietcoke [ Mon Dec 10, 2012 7:47 pm ]
Post subject:  Re: Order Select Question?

ewinner wrote:Hi Guys,

Im having a bit of trouble with some code and going round and round in circles!!!! any help much appreciated before i go totally mad!!

What im trying to do, is check to see if there is an open order already, before it sends another order at the same price (yes, im working on a grid trader!!)

The code i have come up with so far finds the last order OrderOpenPrice ok, but i need it to find an order if it exists with the same price, NOT just the last one placed - make any sense??

Thanks for any help
Mark

Code: Select all


bool DoesBuyTradeExist()
{
   
   int TicketNo = 0;
   double BuyOrderPrice=0;
   
   if (OrdersTotal() == 0) return(false);
   
   for (int cc = OrdersTotal() - 1; cc >= 0 ; cc--)
   {
      if (!OrderSelect(cc,SELECT_BY_POS,MODE_TRADES)) continue;
      if (OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() != Symbol()) continue;

      
      if (OrderType() == OP_BUY)      
      {
      TicketNo = OrderTicket();
      BuyOrderPrice = OrderOpenPrice();
      }
      
      return(true);         
      
   }//for (int cc = OrdersTotal() - 1; cc >= 0 ; cc--)

   return(false);

}//End bool DoesBuyTradeExist()


The same price as what?? Your code does not mention any price except the last order price.

The code you have posted might tell you the price of the last buy trade but you are relying on the order history being in the right order (not always true in Empty4).

If you are programming grid system, I presume you are either placing trades a fixed distance apart or using an algorithm of some sort to determine where the next trade is to be placed.

Your function needs to know what price to compare the order open price with.

Perhaps your function rather than returning true/or false should be a double and return the last orderopenprice or zero if there is none. You can then calculate where the next order should be based on that price and see if the bid has reached that level yet.

I would also suggest it is dangerous to try and compare an open order with a price as if the there is a single digit difference in the two decimals the result will be false. Comparing supposedly identical double values is fraught with that sort of difficulty
Author:  ewinner [ Mon Dec 10, 2012 8:09 pm ]
Post subject:  Re: Order Select Question?

Hi Dietcoke,

Thanks for your reply. I think im getting myself a little confused here, as to how to go about this...!

Heres an example of what im trying to do:

Bid gets to 1.30000
BUYSTOP order sent at 1.30100
BUYSTOP order fills
Bid retraces to 1.29900
Bid moves back to 1.30000
BUYSTOP order sent AGAIN at 1.30100

Its at the last stage where it opens another order at 1.30100 <<< This is what im trying to stop >>> Before it sends the order, i want it to check all open orders, if an order already exists at that price it doesn't send the order. That is why i was trying to use the last OpenOrderPrice, but your right about the order history not being in order, ive found that out today!!! sure is Empty4!

Thanks for your tips, ill give it a rethink tonight

Cheers
Mark
Author:  SteveHopwood [ Mon Dec 10, 2012 10:31 pm ]
Post subject:  Re: Order Select Question?

Welcome to my world Mark. I spend most of my life going round in the sort of circles you have been running today. :lol: Or is that :o

I would solve the problem by using a grid of lines on the chart, with properties that can alter as they are passed and which will tell the ea whether to send a trade or not.

So, taking buys as an example, I would define some constants at the start of my code:
#define buyline0 "Buy line 0"
#define buyline1 "Buy line 1"
.
.
.
.
.
#define buyline10 "Buy line 10"

or however many buy lines my grid required.

Then I would say to myself, "Ok, if a line is passed, the ea will send a trade and delete the line so it will not be traded again."

Then I have this function:

Code: Select all

void DrawHorizontalLine(string name, double price, color col, int style, int width)
{
   
   ObjectDelete(name);
   
   ObjectCreate(name, OBJ_HLINE, 0, TimeCurrent(), price);
   ObjectSet(name, OBJPROP_COLOR, col);
   ObjectSet(name, OBJPROP_STYLE, style);
   ObjectSet(name, OBJPROP_WIDTH, width);
   

}//void DrawLine(string name, double price, color col)
I use this to set up my grid of lines. Suppose I want to set up a grid of 5 trades, 10 pips apart, starting at 1.30000. The calls to the function are:

DrawHorizontalLine(buyline0, 1.30000, green, STYLE_SOLID, 0)
DrawHorizontalLine(buyline1, 1.30100, green, STYLE_SOLID, 0)
DrawHorizontalLine(buyline2, 1.30200, green, STYLE_SOLID, 0)
etc

Then, when the market crosses buyline(x) and the trade send is successful (whole can of worms there but I assume you are on top of that) use ObjectDelete(buyline0) to delete the line so it cannot be traded twice.

Ok, so some detail missed out of this but I assume you can work out a few thingies for yourself.

Also, not saying that mine is necessarily the best approach. It is the one I would take.

:D
Author:  ewinner [ Mon Dec 10, 2012 10:38 pm ]
Post subject:  Re: Order Select Question?

Thanks Steve, thats a really cool approach...... ill have another look at it tomorrow when ive cooled down :-)

Cheers
Mark
Author:  f-trader [ Fri Dec 14, 2012 12:42 pm ]
Post subject:  Re: Order Select Question?

Another way to find an already existing order at the price-level:

Code: Select all

/**
* Return true if there exists an order of this type at exactly this price.
*/
bool isOrder(int type, double price, int magic) {
   int cnt, num;
   int total = OrdersTotal();
   for (cnt = 0; cnt < total; cnt++) {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() == magic && (type == -1 || OrderType() == type)) {
         if (isEqualPrice(OrderOpenPrice(), price)) {
            num++;
         }
      }
   }
   return (num > 0);
}

/**
* Compare two prices.
* The floating point precision of Empty4 can make two seemingly identical values
* still differ from each other. This function will compare two prices after
* rounding them to the precision that is used for prices (value of Digits).
*/
bool isEqualPrice(double a, double b) {
   return(NormalizeDouble(a, Digits) == NormalizeDouble(b, Digits));
}
Since slippage can result in a price different from the grid-level you want to compare,
you should not compare prices like it is done in the code above but
use buffer-distance.
Something like

Code: Select all

if  (grid-price <= OrderOpenPrice() + buffer-distance  && grid-price >= OrderOpenPrice() - buffer-distance )
    num++;
The code-snippets are from common_functions.mqh by 7bit
which is available for free here:
https://sites.google.com/site/prof7bit/common_functions

I use it in all of my EAs.
Author:  ewinner [ Fri Dec 14, 2012 4:25 pm ]
Post subject:  Re: Order Select Question?

Thanks f-trader, really useful info, new there had to be a way, just hadn't found it!!!

Your a star! Much appreciated. Have a Great Christmas

Mark
All times are UTC Page 1 of 1