| / | Forum |
|
Huckleberry
2010.03.12 00:49
Hello to all -----------------------------------------------------------------+ //| Opening and Closeing.mq4 | //| Copyright © 2010, Ben banta | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, Ben banta" //+------------------------------------------------------+ //| Closing My Way | //+------------------------------------------------------+ double ATR; double Slippage = 3; double StopLoss_Buy; double StopLoss_Sell; double Lots=0.4; double MagicNumber; int ticket; int order; //----------------- Closing Sell --------------- int start() { for(int index = OrdersTotal() -1; index >= 0; index--) { if (OrderSelect(index, SELECT_BY_TICKET) // existing orders && OrderMagicNumber() == MagicNumber // with my MN && OrderSymbol() == Symbol()) // with my symbol { if(OrderType() == OP_SELL) order = OrderTicket(); if(OrderTicket() == OrderOpenPrice() + (ATR*2)) // ATR*2 above Sell. Hard Stop { OrderClose(OrderTicket(),OrderLots(),3,Blue); Print("Sell order closed with Hard Stop"); } else { Print("Error closing Sell Hard Stop", GetLastError()); } //---------- Closeing Buy ------------- if(OrderType() == OP_BUY) order = OrderTicket(); if(OrderTicket() == OrderOpenPrice() - (ATR*2)) // ATR*2 below Buy. Hard Stop { OrderClose(OrderTicket(),OrderLots(),3,Red); Print("Buy order closed with Hard Stop"); } else { Print("Error closing Buy Hard Stop", GetLastError()); return(0); } } } //------------ Opening Buy ----------------- if(OrdersTotal() < 1) // Checking for any working orders { if (Ask > High[iHighest(NULL,0,MODE_HIGH,20, 1)]) // When this condition is true, // with no working orders { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0," ",0,0,Blue); // Buy! Useing these parameters if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print ("Buy Order Was a Big Success :" , OrderOpenPrice()); } else Print ("Error Opening Buy Order :" , GetLastError()); return(0); } //--------------- Opening Sell ---------------- if (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)]) //When this condition is true, //with no working orders { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0," ",0,0,Red); //Sell! Useing these parameters if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print ("Sell Order Was a Big Success :" , OrderOpenPrice()); } else Print ("Error Opening Sell Order :" ,GetLastError()); return(0); } return(0); } } |
|
Orders Management - It's Simple The article deals with various ways of how to control open positions and pending orders. It is devoted to simplifying of writing Expert Advisors. |
|
Ickyrus
2010.03.12 02:44
-----------------------------------------------------------------+ //| Opening and Closeing.mq4 | //| Copyright © 2010, Ben banta | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, Ben banta" //+------------------------------------------------------+ //| Closing My Way | //+------------------------------------------------------+ double ATR; double Slippage = 3; double StopLoss_Buy; double StopLoss_Sell; double Lots=0.4; double MagicNumber; int ticket; int order; //----------------- Closing Sell --------------- int start() { for(int index = OrdersTotal() -1; index >= 0; index--) { if ( OrderSelect(index, SELECT_BY_TICKET) // existing orders && OrderMagicNumber() == MagicNumber // with my MN && OrderSymbol() == Symbol() // with my symbol ) { if(OrderType() == OP_SELL) order = OrderTicket(); if(OrderTicket() == OrderOpenPrice() + (ATR*2)) // ATR*2 above Sell. Hard Stop { OrderClose(OrderTicket(),OrderLots(),3,Blue); Print("Sell order closed with Hard Stop"); } else { Print("Error closing Sell Hard Stop", GetLastError()); } //ENDif (OrderTicket() == OrderOpenPrice() + (ATR*2)) //---------- Closeing Buy ------------- if(OrderType() == OP_BUY) order = OrderTicket(); if(OrderTicket() == OrderOpenPrice() - (ATR*2)) // ATR*2 below Buy. Hard Stop { OrderClose(OrderTicket(),OrderLots(),3,Red); Print("Buy order closed with Hard Stop"); } else { Print("Error closing Buy Hard Stop", GetLastError()); return(0); }//ENDif (OrderTicket() == OrderOpenPrice() - (ATR*2)) }//ENDIf ( OrderSelect(index, SELECT_BY_TICKET) etc }// END for loop //------------ Opening Buy ----------------- if(OrdersTotal() < 1) // Checking for any working orders { if (Ask > High[iHighest(NULL,0,MODE_HIGH,20, 1)]) // When this condition is true, // with no working orders { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0," ",0,0,Blue); // Buy! Useing these parameters if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print ("Buy Order Was a Big Success :" , OrderOpenPrice()); } else Print ("Error Opening Buy Order :" , GetLastError()); //ENDif (ticket>0) return(0); }//ENDif (Ask > High[iHighest(NU... etc //--------------- Opening Sell ---------------- if (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)]) //When this condition is true, //with no working orders { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0," ",0,0,Red); //Sell! Useing these parameters if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print ("Sell Order Was a Big Success :" , OrderOpenPrice()); }//ENDif (ticket>0) else Print ("Error Opening Sell Order :" ,GetLastError()); //ENDif (OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) return(0); }//ENDif (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)]) return(0); }//ENDif (OrdersTotal() < 1) }//ENDint start()Should make you code blocks easier to read |
|
WHRoeder
2010.03.12 03:08
if(OrderTicket() == OrderOpenPrice() - (ATR*2))Order ticket is a very large integer (probably in the billions,) the key to the order. OOP - 2ATR is a price. The two will NEVER be equal if you meant OrderClosePrice() == OOP-2ATR real numbers will almost never compare equals. use instead: if( OrderClosePrice() - OrderOpenPrice()-ATR*2 <= 0)//... OrderSelect(index, SELECT_BY_TICKET) // existing orders && OrderMagicNumber() == MagicNumber // with my MN && OrderSymbol() == Symbol()Your orderSend isn't setting a magic number |
|
Huckleberry
2010.03.12 05:25
Cheers |
|
Huckleberry
2010.03.12 06:20
WHRoeder wrote >>
Order ticket is a very large integer (probably in the billions,) the key to the order. OOP - 2ATR is a price. The two will NEVER be equal if you meant OrderClosePrice() == OOP-2ATR real numbers will almost never compare equals. use instead: Your orderSend isn't setting a magic number Thank you for looking in and giving the suggestions. But I must say I did not understand the OOP -2ATR right away. OOP = OrderOpenPrice. That went right over my head at first. Your explaination is absolutely correct. I was caught up on the == sign. The OrderClosePrice() might never equal OrderOpenPrice() minus the (ATR*2). Thank you for catching that. Let me see if I understand your suggestion. I'll restate it here. if(OrderClosePrice()-OOP()-ATR*2 <= 0) This looks like it reads (I'll use arbitary figures here) OCP = 1.4361, OOP = 1.4321, ATR = 20. So the expression would look like: if (1.4321 - 1.4361 - (20*2) <= 0), I'm sorry, but I do not understand. Maybe I can use the same figures to rearrange the expression, to show what I meant. But this did not work either when I poked them into the program. The new arrangement looks like this. if (1.4361 >= 1.4321 + (20*2)). Where the OCP >=OOP + ATR*2, control passes to: OrderClose (OrderTicket(), OrderLots(), etc. Your other observation was that the OrderSend is not setting a magic number. Could I just take this part out of the program: && OrderMagicNumber == MagicNumber, Thanks again, and if you can see other improvements, I would appriciate. Cheers |
|
Ais
2010.03.12 10:40
OrderClose ( OrderTicket (), OrderLots (), OrderClosePrice (), 0 ) ; |
|
Huckleberry
2010.03.12 18:48
Hello Ais
Thank you for your response. Hope you are doing well. The suggestion was inserted this morning. Sorry to say the compilier does not like what I have coded. I am not sure what statement in the program is incorrect. The suggestions I believe are fine. I will repost the program and flag the area I believe maybe the problem. I'll also state the strategy for closeing. The sell position will be stopped out above the order open price by Atr*2. Such as, OOP is 1.4321, Atr =20, and OCP = 1.4361. So I coded the expression as, if(OrderClosePrice >= OrderOpenPrice(), - (ATR*2)) // condition is met, control passes to.... OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(),Blue) ; Note: There is no StopLoss(), or TakeProfit() in the OrderSend statement, if that has any relevance. Thanks again for everyones help Cheers double ATR; double Slippage = 3; double StopLoss_Buy; double StopLoss_Sell; double Lots=0.4; double MagicNumber; int ticket; int order; //----------------- Closing Sell --------------- int start() { for(int index = OrdersTotal() -1; index >= 0; index--) { if (OrderSelect(index, SELECT_BY_TICKET) // existing orders //&& OrderMagicNumber() == MagicNumber // with my MN && OrderSymbol() == Symbol()) // with my symbol { if(OrderType() == OP_SELL) order = OrderTicket(); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< if(OrderClosePrice() >= OrderOpenPrice() + (ATR*2)) // condition to be met, // control then passes to... { OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Blue); // OrderClose //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Print("Sell order closed with Hard Stop"); } else { Print("Error closing Sell Hard Stop", GetLastError()); } //---------- Closeing Buy ------------- if(OrderType() == OP_BUY) order = OrderTicket(); if(OrderClosePrice() <= OrderOpenPrice() - (ATR*2)) // ATR*2 below Buy. Hard Stop { OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Red); Print("Buy order closed with Hard Stop"); } else { Print("Error closing Buy Hard Stop", GetLastError()); return(0); } } } //------------ Opening Buy ----------------- if(OrdersTotal() < 1) // Checking for any working orders { if (Ask > High[iHighest(NULL,0,MODE_HIGH,20, 1)]) // When this condition is true, // with no working orders { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0," ",0,0,Blue); // Buy! Useing these parameters if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print ("Buy Order Was a Big Success :" , OrderOpenPrice()); } else Print ("Error Opening Buy Order :" , GetLastError()); return(0); } //--------------- Opening Sell ---------------- if (Bid < Low[iLowest(NULL,0,MODE_LOW,20,1)]) //When this condition is true, //with no working orders { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0," ",0,0,Red); //Sell! Useing these parameters if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print ("Sell Order Was a Big Success :" , OrderOpenPrice()); } else Print ("Error Opening Sell Order :" ,GetLastError()); return(0); } return(0); } } |
|
Ais
2010.03.12 19:24
Hello Huckleberry, |
|
Ickyrus
2010.03.12 19:50
Why do "order = OrderTicket(); " and not use thit value in the orderclose() function? |
|
Huckleberry
2010.03.12 21:40
Cheers |
|
Huckleberry
2010.03.12 21:46
Ickyrus wrote >>
Why do "order = OrderTicket(); " and not use thit value in the orderclose() function? Thanks for your observation and suggestion. Your time is valuable. Your reasoning makes perfect sense. I am not any more certain as you as it comes to selected order. I'll give your suggestion a try. Thanks again Cheers |