Can't make it trade

 

Hello, I am in forex for a couple of months and i decided try to write a simple EA. After reading many EA codes from this forum, i still can't make it trade. I have not codded the strategy, because it is useless sinc it does not want to trade whit this simple code.

Please, help me. Where is my mistake?

//+------------------------------------------------------------------+
//|                                                     SmartBot.mq4 |
//|                                      Copyright © 2011, LaMeRSoFt |
//|                                                lamersoft@mail.bg |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, LaMeRSoFt"
#property link      "lamersoft@mail.bg"
#include <WinUser32.mqh>

extern double Risk = 1;
//----Initalization variables
bool InitedProperly = false; //Shows  ifthe sytem wasinted properly
double Lots;
string Symb;
double MinLot; //the minimmu lot we can trde
double MaxLot; //Maximum lot we can trade
double Step;
double OneLot;
int Ticket;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
string AccountType;
string RunningMode;
//----
   Comment("Initializing Expert advisor...");
   InitedProperly = false; //If eevrything goes ok, than wew il say that iint is ok
   //Let's see if we are connectetd  ot heinteernt at first place
   if(!IsConnected()) 
      MessageBox("Well, you are not connected to server (or worst - the Internet). Connect and try again.", "Initialization error", IDOK|MB_ICONWARNING);
   else if(!IsExpertEnabled()) //-First we will check if EA is allowed
      MessageBox("Expert advisors(EAs) are not allowed. I can\'t do my job", "Initialization error", IDOK|MB_ICONWARNING);
   else if(!IsTradeAllowed()) //-First we will check if trading is allowed
      MessageBox("Trades are not allowed. I can\'t do my job", "Initialization error", IDOK|MB_ICONWARNING);
   else
   { 
      if(IsDemo()) //Determine acount type
         AccountType = "DEMO";
      else 
         AccountType = "LIVE";
      
      if(IsTesting())   //Determine tunning mode
         RunningMode = "TEST";
      else
         RunningMode = "NORMAL";
         
      Symb = OrderSymbol();         //Get the symbol we are trading
      MinLot = MarketInfo(Symb,MODE_MINLOT); //Get  the minimum trading lot
      MaxLot = MarketInfo(Symb,MODE_MAXLOT);//Gett the maximum trading lot 
      Step   = MarketInfo(Symb,MODE_LOTSTEP);
      OneLot = MarketInfo(Symb,MODE_MARGINREQUIRED);
      Ticket = 0;
      
      MessageBox("Your system is inited. You are trading in "+AccountType+" account in "+RunningMode+" mode with "+Symb+" pair."+"\n"+"Expert advisor is now runnig"+"Step: "+Step,"Completed",IDOK|MB_ICONWARNING);   
      InitedProperly = true;
   }
//----
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()

{if(InitedProperly == true)  Comment("Running...",Step);
//----

Ticket = BuyTrade();
CloseBuyTrade(Ticket);
Print("Buy: ",Ticket);
//----

 return(0); }
//+------------------------------------------------------------------+
//===================================================================+
//+------------------------------------------------------------------+
//|Calculate Lots out from risk percent                              |
//+------------------------------------------------------------------+
double Lots()
{
double l;
Print(Step);
   if (Risk > 100)
      l = MaxLot;                          
   if (Risk == 0)
      l=MinLot;                                               
   l = MathFloor(AccountFreeMargin()*Risk/100/OneLot/Step)*Step;
   if (l < MinLot)
      l=MinLot;                        
   if (l*OneLot > AccountFreeMargin()) 
   {       
      MessageBox("You do not have enought money to trade", IDOK|MB_ICONWARNING);                                  
      return(0);                          
   }
   else
      return(l);
}
//+------------------------------------------------------------------+
//===================================================================+
int BuyTrade()
{
int t;

   Lots();
   t = OrderSend(Symb,OP_BUY, Lots,Ask,2,0,0,"",1,0,Green);
   if(Ticket<0)
   {
      Print("OrderSend failed with error #",GetLastError());
      return(0);
   }
   return(t);
}

int SellTrade()
{
   Lots();
}

int CloseBuyTrade(int t)
{
   OrderClose(t,Lots,Bid,3,Violet);
}

int CloseSellTrade()
{
}
 

Your function lot does return the value, but you are not storing it. And you should (have to) use different names for variables.

CHANGE:

double Lots; to: //double Lots;
Lots();      to: //Lots();

Or delete that two lines

t = OrderSend(Symb,OP_BUY, Lots,Ask,2,0,0,"",1,0,Green); to: t = OrderSend(Symb,OP_BUY, Lots(),Ask,2,0,0,"",1,0,Green);

i have not checked your Lots() function but with these two errors no trade could get executed. You might also check the log, there you can see why an order doesn't get executed.

 
zzuegg:

Your function lot does return the value, but you are not storing it. And you should (have to) use different names for variables.

i have not checked your Lots() function but with these two errors no trade could get executed. You might also check the log, there you can see why an order doesn't get executed.


Тhank you very much. This is very stupid mistake. I will fix it. But now i have another problem. It always says "Division by zero". I made some tests and saw that the variable Step, that must be inited in the beginning equals zero after initialization. How can I fix that?

Symb = OrderSymbol();         //Get the symbol we are trading
      MinLot = MarketInfo(Symb,MODE_MINLOT); //Get  the minimum trading lot
      MaxLot = MarketInfo(Symb,MODE_MAXLOT);//Gett the maximum trading lot 
      Step   = MarketInfo(Symb,MODE_LOTSTEP);
      OneLot = MarketInfo(Symb,MODE_MARGINREQUIRED);
      Ticket = 0;
This is the init code. And Step equals zero.
 
Symb = OrderSymbol();         //Get the symbol we are trading
      MinLot = MarketInfo(Symb,MODE_MINLOT); //Get  the minimum trading lot

You can't use OrderSymbol() until you've done an orderSelect

You want the chart symbol, use Symbol()

or simply use MarketInfor(NULL ...)

Reason: