• Get access

I have open orders of currency pairs A, B, and C. Is there a way to get the expert to recognize that there is no open order of currency pair D?

To add comments, please log in or register
Nicholas
37
Nicholas  
Hi, say I have open orders of currency pairs A, B, and C. Is there a way to get the expert to recognize that there is no open order of currency pair D, and given the right conditions, to then open one? Or to see that there already is an trade open for currency par A and not open one? I'm wanting to have this expert running on 4 different currency pairs but I only want one open trade at a time per currency pair. How do I do this? I've been thinking about it a while and I'm stuck.

thanks.
Matt
77
Matt  

There are a few ways to do it. The easiest might be to use the code in the moving average sample from MQ that came with MT4. What it does is calculate current orders in this function:

//+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; //---- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA) { if(OrderType()==OP_BUY) buys++; if(OrderType()==OP_SELL) sells++; } } //---- return orders volume if(buys>0) return(buys); else return(-sells); }

And then in the start function, it only will seek to open an order if it doesn't already have one open:

if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); else CheckForClose();

Those are at least good pieces to start with. You could adjust it so instead of counting orders, you could just return true/false if there are any orders, and then make that function call a condition of the EA's open order test. Pretty similar to this one, very slightly different.

Edi Dimitrovski
289
Edi Dimitrovski  
I've made that with a little more code:

void GbpUsdBuy() {
  bool
    result,
    GbpUsdopen;
  double
    GbpUsdBid=MarketInfo("GBPUSD",MODE_BID),
    GbpUsdAsk=MarketInfo("GBPUSD",MODE_ASK),
    GbpUsdPoint=MarketInfo("GBPUSD",MODE_POINT);
 for(int GbpUsd=0;GbpUsd<OrdersTotal();GbpUsd++) {
  if(OrderSelect(GbpUsd,SELECT_BY_POS,MODE_TRADES)==false) break;
  if(OrderMagicNumber()!=Number || OrderSymbol()!="GBPUSD") continue;
  if(OrderType()==OP_BUY) GbpUsdopen=1;}
  if(GbpUsdopen==0)
  {result=OrderSend("GBPUSD",OP_BUY,lot(),GbpUsdAsk,3,GbpUsdBid-GbpUsdPoint*SL,GbpUsdBid+GbpUsdPoint*TP,"",Number,0,OrangeRed);
  if(result!=TRUE) Print("LastError = ",GetLastError());return;if(result==135) RefreshRates();}}

void GbpUsdSell() {
  bool
    result,
    GbpUsdopen;
  double
    GbpUsdBid=MarketInfo("GBPUSD",MODE_BID),
    GbpUsdAsk=MarketInfo("GBPUSD",MODE_ASK),
    GbpUsdPoint=MarketInfo("GBPUSD",MODE_POINT);
 for(int GbpUsd=0;GbpUsd<OrdersTotal();GbpUsd++) {
  if(OrderSelect(GbpUsd,SELECT_BY_POS,MODE_TRADES)==false) break;
  if(OrderMagicNumber()!=Number || OrderSymbol()!="GBPUSD") continue;
  if(OrderType()==OP_SELL) GbpUsdopen=1;}
  if(GbpUsdopen==0)
  {result=OrderSend("GBPUSD",OP_SELL,lot(),GbpUsdBid,3,GbpUsdAsk+GbpUsdPoint*SL,GbpUsdAsk-GbpUsdPoint*TP,"",Number,0,OrangeRed);
  if(result!=TRUE) Print("LastError = ",GetLastError());return;if(result==135) RefreshRates();}}


void EurUsdBuy() {
  bool
    result,
    EurUsdopen;
  double
    EurUsdBid=MarketInfo("EURUSD",MODE_BID),
    EurUsdAsk=MarketInfo("EURUSD",MODE_ASK),
    EurUsdPoint=MarketInfo("EURUSD",MODE_POINT);
 for(int EurUsd=0;EurUsd<OrdersTotal();EurUsd++) {
  if(OrderSelect(EurUsd,SELECT_BY_POS,MODE_TRADES)==false) break;
  if(OrderMagicNumber()!=Number || OrderSymbol()!="EURUSD") continue;
  if(OrderType()==OP_BUY) EurUsdopen=1;}
  if(EurUsdopen==0)
  {result=OrderSend("EURUSD",OP_BUY,lot(),EurUsdAsk,3,EurUsdBid-EurUsdPoint*SL,EurUsdBid+EurUsdPoint*TP,"",Number,0,OrangeRed);
  if(result!=TRUE) Print("LastError = ",GetLastError());return;if(result==135) RefreshRates();}}

void EurUsdSell() {
 bool
   result,
   EurUsdopen;
 double
   EurUsdBid=MarketInfo("EURUSD",MODE_BID),
   EurUsdAsk=MarketInfo("EURUSD",MODE_ASK),
   EurUsdPoint=MarketInfo("EURUSD",MODE_POINT);
 for(int EurUsd=0;EurUsd<OrdersTotal();EurUsd++) {
  if(OrderSelect(EurUsd,SELECT_BY_POS,MODE_TRADES)==false) break;
  if(OrderMagicNumber()!=Number || OrderSymbol()!="EURUSD") continue;
  if(OrderType()==OP_SELL) EurUsdopen=1;}
  if(EurUsdopen==0)
  {result=OrderSend("EURUSD",OP_SELL,lot(),EurUsdBid,3,EurUsdAsk+EurUsdPoint*SL,EurUsdAsk-EurUsdPoint*TP,"",Number,0,OrangeRed);
  if(result!=TRUE) Print("LastError = ",GetLastError());return;if(result==135) RefreshRates();}}
Nicholas
37
Nicholas  
thanks for the advice.

I ended up getting the output I was looking for with just this:

 for(i=0;i<total;i++)
      {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         {
         if(OrderSymbol()==Symbol())
            {
            sym++;
            }
         if(OrderSymbol()!=Symbol())
            {
            tok++;
            }
         }
      }

then if sym<1, I open an order up for the currency pair.
To add comments, please log in or register