Download MetaTrader 5

Help. closing all orders

To add comments, please log in or register
Register as a seller and start making profit!
tafadzwa
117
tafadzwa 2016.02.04 11:36 
///+------------------------------------------------------------------+

This is what i want to do.( if the Bid crosses a level  (A)  up and then comes back to level (A) close all orders.  if the Bid crosses a level (B) down and then comes back to level (B) close all orders. My code ( for level (B)) is not working .See my unfinished ea.

Pardon my coding. im still new to it. Thanks a lot.

//|                                                          ppp.mq4 |
//|                                                             EA 1 |
//|                                               xpotato6@gmail.com |
//+------------------------------------------------------------------+
#property copyright "EA 1"
#property link      "xpotato6@gmail.com"
#property version   "1.00"
#property strict

#include <stdlib.mqh>
#include <stderror.mqh>

int total=OrdersTotal(),
lst,
cnt,
q1,
ct,
lc,
tt,
m=0,
lp=0,
ticket; 

double
qq,xx, 
ln,ln_2,ln_3,
Line,max,min,
LEQT,
Lot=0.02,
Lots=0.01;
string A=Symbol();

void OnTick()
{



//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//DECLARE THE LINE                                                              xxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
if(OrdersTotal()==0 && High[0]==Low[0])
 { 
   LEQT=AccountEquity();
   Line=Open[0];  
 }

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//OPENING OF BUY ORDER 1st                                                      xxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
if(OrdersTotal()==0 && Bid==Line+(1*Point))
 {
  Alert("OPEN BUY HERE");
  ticket= OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"0",0,0,Green);
  cnt++;lst=1;
if(ticket>0)
 {
  if( OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
  Print("Buy is opened at price :",OrderOpenPrice(),"lst",lst);
  }
 else Print("Shit there is error:",GetLastError());
 return;
 }  
 
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//OPENING OF SELL ORDER 1st                                                     xxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx           
if(OrdersTotal()==0 && Bid==Line-1*Point)
 {
  Alert("OPEN SELL HERE");
  ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"0",0,0,Red);
  cnt++;
  lst=2;
if(ticket>0)
 {
  if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
  Print("Sell is Opened at price",OrderOpenPrice(),"lst",lst);
 }
else Print("Shti there is error:",GetLastError());
return;
 }

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//OPENING OF BUY ORDER 2nd                                                      xxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx    
if(( lst==2 || lst==4)&& Bid>Line)
 {
  Alert("OPEN BUY HERE");
  ticket= OrderSend(Symbol(),OP_BUY,Lot,Ask,3,0,0,"0",0,0,Blue);
  cnt++;
if(ticket>0)
 {
  if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
  Print("Order BUY sucessful @:",OrderOpenPrice(),"lst",lst);
  lst=3;
 }
else Print("Shit Buy cant Open:",GetLastError());
return;
 }   
    
if((lst==1 || lst ==3) && OrdersTotal()>0 && Bid<Line)
 {
  Alert("OPEN SELL HERE");
  ticket=OrderSend(Symbol(),OP_SELL,Lot,Bid,3,0,0,"0",0,0,Pink);cnt++;
  if(ticket>0)
 {
  if(OrderSelect(SELECT_BY_TICKET,MODE_TRADES))
  Print("Sell Opened @:",OrderOpenPrice(),"lst",lst);
  lst=4;
 }
 }
 
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//CURB THE LOSSES stage 1                                                       xxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

if(max<Bid)   //This seems to be working.
max=Bid;

if(max>Bid && Bid==Line+(30*Point)) // level(A) is **Line+(30*Point)**
Close_all_by();

//???????????????????????????????????????????????????????// Problem is here
if(min>Bid)                                              //
min=Bid;                                                 //  
                                                         //  THIS PART HERE IS NOT WORKING
if(Bid>min && Bid==Line-(30*Point))   // level(B) is **Line-(30*Point)**                   
Close_all_by();                                          //
//???????????????????????????????????????????????????????//
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//THE FUNCTION FOR CLOSING OF ORDERS                                        xxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void Close_all_by()
  {
   for(int x=OrdersTotal()-1;x>=0;x--)
     {
      if(!OrderSelect(x,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderType()==OP_BUY || OrderType()==OP_SELL)
        {
         if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrRed)){ Alert(ErrorDescription(GetLastError())); }
         else Alert("All Closed");
        }
     }
   }
puiucristian
24
puiucristian 2016.02.04 20:58  
tafadzwa:

This is what i want to do.( if the Bid crosses a level  (A)  up and then comes back to level (A) close all orders.  if the Bid crosses a level (B) down and then comes back to level (B) close all orders. My code ( for level (B)) is not working .See my unfinished ea.

Pardon my coding. im still new to it. Thanks a lot.

you shouldn't use == when comparing prices. maybe the price "jumps" over that value and it will never close, because it's not the same value

 it's better to use <= or >= like Bid <= line, that way you are sure it will close if the price is equal or lower, that way if it jumps over the line, it will still close

To add comments, please log in or register