Creating an Expert Advisor

Back to topics list  | 1 2 To post a new topic, please log in or register
avatar
10
Barno_07 2016.05.23 16:52 #
 
GumRai:
WHRoeder has already told you. Don't return EMPTY_VALUE from a boolean function. It is the same as returning True.
extern double lot = 0.1;
extern double tp = 20;
extern double sl = 0;

extern string s = "candle at which u want to place the order";
extern int opentime = 8;   // opening trade candle
extern string r = "candle at which u want to close the order if not stopped out or tp";
extern int cltime = 9;     // if not closed by sl or tp
int magic = 1344; // no. to define a trade made bu EA
extern double g = 5;   // place BSO/SSO by no of pips
bool check = true;    // checking condition


int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      bool t = checktime();
      
      if(OrdersTotal() == 0)
      {
         if(t == true && check == true)
         {
            openbuystop();
            opensellstop();
            check = false;
            
         }
         else if (t == false)
         {
            closebuy();
            closesell();
            deletepending();
            check = true;
         }
      }
      else if (OrdersTotal() == 1)
      {
         deletepending();
      }
     
    
  }


void openbuystop()
{
   if (OrderSend(Symbol(),OP_BUYSTOP,lot,High[1]+g*Point,3,High[1],High[1]+g*Point+tp*Point,NULL,magic,0,NULL)== true)
   {
      Alert("Order Sent");
   }
   else
   fun_err(GetLastError());
}

void opensellstop()
{
    if (OrderSend(Symbol(),OP_SELLSTOP,lot,Low[1]-g*Point,3,Low[1],Low[1]-g*Point-tp*Point,NULL,magic,0,NULL)== true)
   {
      Alert("Order Sent");
   }
   else
   fun_err(GetLastError());
}

void closebuy()
{
   for(int j=1;j<=OrdersTotal();j++)
   {
      OrderSelect(j-1,SELECT_BY_POS,MODE_TRADES);
      
      if(OrderType() == OP_BUY &&  OrderMagicNumber() == magic)
      {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,NULL);
         PlaySound("news.wav");
      }
   }
}

void closesell()
{
   for(int j=1;j<=OrdersTotal();j++)
   {
      OrderSelect(j-1,SELECT_BY_POS,MODE_TRADES);
      
      if(OrderType() == OP_SELL &&  OrderMagicNumber() == magic)
      {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,NULL);
         PlaySound("news.wav");
      }
   }
}

int fun_err(int Error)
{
   switch(Error)
   {
      case 1: Alert("Server Busy. Trying Once Again...");
      Sleep(3000);
      return(1);
      
      case 123: Alert("Price Changed. trying once Again...");
      RefreshRates();
      return(1);
      
      case 124: Alert("Waiting for new tick...");
      while(RefreshRates() == false)
      Sleep(1);
      return(1);
      
      case 137: Alert("Broker is busy..");
      Sleep(3000);
      return(1);
      
      case 146: Alert("Trading systes is busy");
      Sleep(500);
      return(1);
      
      case 2: Alert("Common Error...");
      return(0);
      
      case 133: Alert("Trading forbidden");
      return(0);
      
      case 4: Alert("Not enough money to execute op");
      return(0);
      
      
      default: Alert("Error Occured: ",Error);
      return(0);
   }
}

bool checktime()
{
   int i = 0;
   int hourtime = TimeHour(Time[i]);
   
   if(hourtime == opentime)
   {
      return(true);
   }
   else if (hourtime == cltime)
   {
      return(false);
   }
  else return(false);
}

bool deletepending()
{
   for(int w = 0; w < OrdersTotal(); w++)
   {
      OrderSelect(w,SELECT_BY_POS,MODE_TRADES);
      
      if(OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP)
      {
         OrderDelete(OrderTicket());
      }
      
   }
  
      return(false);
}

avatar
12985
WHRoeder 2016.05.23 17:30 #
 
  1. bool checktime(){
       int hourtime = TimeHour(Time[0]);
       
       if(hourtime == opentime) return true;
       if(hourtime == cltime)   return false;
       return false;
    }
    Checktime only returns true during the bar/all bars during the opentime hour. Therefor all orders will be closed the next bar after that hour. Cltime is now irrelevant.

  2. void closebuy(){
       for(int j=1;j<=OrdersTotal();j++)
    :
    void closesell(){
       for(int j=1;j<=OrdersTotal();j++)
    :
    bool deletepending(){
       for(int w = 0; w < OrdersTotal(); w++){
          OrderSelect(w,SELECT_BY_POS,MODE_TRADES);
          if(OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP){
             OrderDelete(OrderTicket());
          }
       }
       return false;
    }
    In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
  3. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Back to topics list   | 1 2  

To add comments, please log in or register