Download MetaTrader 5

An EA coding problem from Beginner

To add comments, please log in or register
Do you have a trading signal? Publish it in your blog as well!
JunbingX
19
JunbingX 2016.03.13 01:50 

Hi Guys,

I'm beginner trying to code my first EA, however I met some problem that may beyond my knowledge.

I'm buiding an EA to open position at predifined space. It worked out only the second trade is opened based on the predifined space. Others just open, could anybody show me where is the problem ?

111
//+------------------------------------------------------------------+
//|                                                      shunshi.mq4 |
//|                                                           Jaguar |
//|                                                        FreeWorld |
//+------------------------------------------------------------------+
#property copyright "Jaguar"
#property link      "FreeWorld"
#property version   "1.00"
#property strict
//----------------UserInputParameter---------------------------------
extern   int      GridSpace=300;
extern   double   StartPrice=1.392;
extern   double   Lots=0.1;
extern   bool     BuyUp=true;
extern   bool     SellDown=false;
//----------------GlobalVariable--------------------------------------
double   LastBuyPrice=StartPrice;
double   LastSellPrice=StartPrice;
int      BuyCount=0;
int      SellCount=0;  
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
//---BuyUP----------------------------------------------------------

   if(BuyUp)
   {
      if(Ask>(LastBuyPrice+Point*GridSpace))
      {

         OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"BuyUp",9541,0,Red);
         
            LastBuyPrice+=Point*GridSpace;
            BuyCount+=1;
          
            
         
      
      }
    }
      
      
//---SellDown
 /*  if(SellDown)
    {
      if(Bid<(LastBuyPrice-Point*GridSpace))
      {
         if(OrderSend(Symbol(),OP_SELL,Lots,Ask,3,0,0)==true)
         {
            LastSellPrice=Bid;
            SellCount+=1;
         }
      
      }
    }
   
   */ 
   
   }
 
  
//+------------------------------------------------------------------+
JunbingX
19
JunbingX 2016.03.13 10:30  
Any help ?
Carl Schreiber
6589
Carl Schreiber 2016.03.13 11:14  
I think you need to set BuyUp to false after you opened a new buy and wait for ...
JunbingX
19
JunbingX 2016.03.13 14:12  
Carl Schreiber:
I think you need to set BuyUp to false after you opened a new buy and wait for ...

Hi Carl,

 

Thank you very much for your answer.

I changed the code as follow and  it works, even i don't know why it couldn't before.

 if(BuyUp)
   {
      if(Ask>(LastBuyPrice+Point*GridSpace))
      {

         if(OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"BuyUp",9541,0,Red))
         {
            
            LastBuyPrice=Ask;
            BuyCount+=1;
          
            
         }
         else
         {
         printf("Excute Buy Error");
         }
      
      }
To add comments, please log in or register