order repeat?

 
hi,

im using the following command to place an order at a preset price:



OrderSend(Symbol(),OP_SELL,Lots,91.000,1,Ask+StopLoss*Point,Ask-TakeProfit*Point,"sell",0,0,CLR_NONE); RefreshRates();

or

OrderSend(Symbol(),OP_BUY,Lots,91.000,1,Bid-StopLoss*Point,Bid+TakeProfit*Point,"buy",0,0,CLR_NONE); RefreshRates();

firstly is there somehting wrong with this code?

and more importantly how do i make a command so that the buy/sell order that has been placed on the reset price is not sent again until it has reached a stoploss or a takeprofit?

thanks inadvance

 
hi, you have to check all your open orders. firstly i suggest using a magic number for each EA.
int MagicNr=12;
bool isOrderOpen=false;
for(int i=OrdersTotal()-1;i>=0;i-){
  OrderSelect(i,SELECT_BY_POS);
  if(OrderMagicNumber()==MagicNr && OrderSymbol()==Symbol() ){
    isOrderOpen=true;
  }
}

if(!isOrderOpen){
 OrderSend(Symbol(),OP_SELL,Lots,91.000,1,Ask+StopLoss*Point,Ask-TakeProfit*Point,"sell",MagicNr,0,CLR_NONE);  
 //or
 OrderSend(Symbol(),OP_BUY,Lots,91.000,1,Bid-StopLoss*Point,Bid+TakeProfit*Point,"buy",MagicNr,0,CLR_NONE); 
}
 
nayef:

[...]

firstly is there somehting wrong with this code?

[...]

This is for instant execution at 91.000. Obviously depending on your execution criteria, there is a high chance the current price won't be exactly 91.000. You would be better using ask / bid.


If you want a pending order at 91.000, then you need a different order type.

OP_BUYLIMIT - buy limit pending position,
OP_BUYSTOP - buy stop pending position,
OP_SELLLIMIT - sell limit pending position,
OP_SELLSTOP - sell stop pending position.

You also run a risk of error 130 on your TP and SL. Best to use NormaliseDouble to make sure you've got the right number of decimals. And, maybe I'm nitty but I am always more explicit with calculations. It may work the way you've done it, but I never do it that way so don't know... I would explicitly ensure the Point is multiplying the stoploss only and not the sum of the ask and stoploss... personally, I calculate it outside the ordersend and just pass the result into ordersend.

double sl= NormalizeDouble(Ask+(StopLoss*Point),Digits);
 
thank you so much uve both been very helpful, ill try that and see how it works :)
 
i tried the:


OP_BUYLIMIT - buy limit pending position,
OP_BUYSTOP - buy stop pending position,
OP_SELLLIMIT - sell limit pending position,
OP_SELLSTOP - sell stop pending position.

the problem is it will just keep opening the sell limit for the specific number and for some reason only the sell limit works and others dont, the sell limit is first on the list however:

this is a tryout
double Lots = 0.1;
double StopLoss = 200;
double TakeProfit = 50;
int start() {

OrderSend(Symbol(),OP_SELLLIMIT,Lots,93.350,1,Ask+StopLoss*Point,Ask-TakeProfit*Point,"sell",0,0,CLR_NONE); 
OrderSend(Symbol(),OP_BUYLIMIT,Lots,93.350,1,Bid+StopLoss*Point,Bid-TakeProfit*Point,"buy",0,0,CLR_NONE); 
OrderSend(Symbol(),OP_SELLSTOP,Lots,93.350,1,Ask+StopLoss*Point,Ask-TakeProfit*Point,"sell",0,0,CLR_NONE); 
OrderSend(Symbol(),OP_BUYSTOP,Lots,93.350,1,Bid+StopLoss*Point,Bid-TakeProfit*Point,"buy",0,0,CLR_NONE); 

}
 
i dont think a magic number would be necessary as it would just add bulk to the script and ive already reached the limit on the script at 389 lines so i guess i need to minimize as much as possible. is there another way around the magic number? like "order only valid if theres no existing order" or somthing like that. cheers
 
shure.

if (OrdersTotal()==0) {
 //OPEN ORDERS
}
but i would strongly recommand to use the method with magic numbers. with the OrdersTotal() method the order will only be sent if there are no other open trades open on your account
 

then the orders total is useless for what am doing, is there a way round the magic numbers?

because my script is absolutley huge! this is just "2" parts of the script. how can i implement the magic number on this script? thanks

OrderSend(Symbol(),OP_SELLSTOP,Lots,86.000,3,86.000+StopLoss*Point,86.000-TakeProfit*Point,"sell",0,0,CLR_NONE);
OrderSend(Symbol(),OP_BUYLIMIT,Lots,86.000,3,86.000-StopLoss*Point,86.000+TakeProfit*Point,"buy",0,0,CLR_NONE);
OrderSend(Symbol(),OP_SELLLIMIT,Lots,86.000,3,86.000+StopLoss*Point,86.000-TakeProfit*Point,"sell",0,0,CLR_NONE);
OrderSend(Symbol(),OP_BUYSTOP,Lots,86.000,3,86.000-StopLoss*Point,86.000+TakeProfit*Point,"buy",0,0,CLR_NONE);


OrderSend(Symbol(),OP_SELLSTOP,Lots,86.050,3,86.050+StopLoss*Point,86.050-TakeProfit*Point,"sell",0,0,CLR_NONE);
OrderSend(Symbol(),OP_BUYLIMIT,Lots,86.050,3,86.050-StopLoss*Point,86.050+TakeProfit*Point,"buy",0,0,CLR_NONE);
OrderSend(Symbol(),OP_SELLLIMIT,Lots,86.050,3,86.050+StopLoss*Point,86.050-TakeProfit*Point,"sell",0,0,CLR_NONE);
OrderSend(Symbol(),OP_BUYSTOP,Lots,86.050,3,86.050-StopLoss*Point,86.050+TakeProfit*Point,"buy",0,0,CLR_NONE);
 
why is this uncompileable?
double Lots = 0.1;
double StopLoss = 50;
double TakeProfit = 50;
int MagicNr=12;
bool isOrderOpen=false;
int start() {

int MagicNr=12;
bool isOrderOpen=false;
for(int i=OrdersTotal()-1;i>=0;i-){
  OrderSelect(i,SELECT_BY_POS);
  if(OrderMagicNumber()==MagicNr && OrderSymbol()==Symbol() ){
    isOrderOpen=true;
  }
}

if(!isOrderOpen){
 OrderSend(Symbol(),OP_SELL,Lots,91.000,1,Ask+StopLoss*Point,Ask-TakeProfit*Point,"sell",MagicNr,0,CLR_NONE);  

 OrderSend(Symbol(),OP_BUY,Lots,91.000,1,Bid-StopLoss*Point,Bid+TakeProfit*Point,"buy",MagicNr,0,CLR_NONE); 

 OrderSend(Symbol(),OP_SELL,Lots,92.000,1,Ask+StopLoss*Point,Ask-TakeProfit*Point,"sell",MagicNr,0,CLR_NONE);  

 OrderSend(Symbol(),OP_BUY,Lots,92.000,1,Bid-StopLoss*Point,Bid+TakeProfit*Point,"buy",MagicNr,0,CLR_NONE); 
}

 return(0);
 }
and how can i add an orders total of 8 to the above? thanks
 

Look at your errors when compiling. Double click it and it will take you straight to were you are missing a "-" sign. it should be i--.


I'm not sure what you are trying to achieve but what you have posted won't work as there are 2 different prices at order execution if you want an instant market order you need to use Ask/Bid.


If you are wanting a pending order at a specific price you need a different order type as mentioned before. You would do well to include error handling to get an idea of where your code is failing.

With regards to counting orders, not sure what you want. OrdersTotal() will count everything. pending or running, so the code below will not execute if there are already pending orders.

Try this... hopefully this is what you are trying to achieve.

#EDIT: Code had error - so removed
 

Sorry, I made an error on the error handling and rectified it below..


   double Lots = 0.1;
   double StopLoss = 500;
   double TakeProfit = 500;
   double buystop_price=1.51;
   double sellstop_price=1.47;
   int MagicNr=12;
   double sl,tp,entry;
   int   _last_error;
   
   if (OrdersTotal()>0)
      {
      Alert("Open Orders");
      }
      
   if (OrdersTotal()==0)
      {
      sl=NormalizeDouble(sellstop_price+(StopLoss*Point),Digits);
      tp=NormalizeDouble(sellstop_price-(StopLoss*Point),Digits);
      entry=NormalizeDouble(sellstop_price,Digits);
      OrderSend(Symbol(),OP_SELLSTOP,Lots,entry,1,sl,tp,"Sellstop",MagicNr,0,CLR_NONE);  
      _last_error=GetLastError();
      if (_last_error>0)
         {
         Alert( "Sellstop Error - Code ",_last_error);   
         }

      sl=NormalizeDouble(buystop_price-(StopLoss*Point),Digits);
      tp=NormalizeDouble(buystop_price+(StopLoss*Point),Digits);
      entry=NormalizeDouble(buystop_price,Digits);
      OrderSend(Symbol(),OP_BUYSTOP,Lots,entry,1,sl,tp,"Buystop",MagicNr,0,CLR_NONE);  
      _last_error=GetLastError();
      if (_last_error>0)
         {
         Alert( "Buy stop Error -Code ",GetLastError());
         }


      }
 
thank u very much Viffer, my question with regards to (OrdersTotal()>0) ... this specific layout of it will only open a trade if there are no pending trades... what if i want it to open upto 8 trades and no more than 8 trades? is orderstotal even useful in this case? or is there another command? or isit even possible to do with the odd layout that ive got. thanks again