Any Help With This Simple Crossover EA?

 

Trying to get this EA to work... new to MQL5. Basically, I'm trading on simple MA crossovers based on risk to account balance (USDEUR on 1 HR time frame). Any help is appreciated.... thanks!

//+------------------------------------------------------------------+
//|                                                    ThaCarter.mq5 |
//|                                                John Wayne        |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "John Wayne"
#property version   "1.00"
#include <Trade\Trade.mqh>

int lasttrade = 2; //set later to 0 if sell, 1 if buy, 2 if none
CTrade trade;

extern double Lots = 0.0;//Fixed lotsize, 0.0: using AutoLot based on risk%
extern double MaxLots = 50.0;//50
extern double RiskWill = 10;//

bool posopen;//boolean for position open

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(bool& posopen, int& lasttrade)
  {
  
   int arrsize = 10;
   double SSMA[];
   double LSMA[];
  
   int sh = iMA("EURUSD",PERIOD_H1,5,0,MODE_SMA,PRICE_TYPICAL);//handle for short period SMA
   int lh = iMA("EURUSD",PERIOD_H1,15,0,MODE_SMA,PRICE_TYPICAL);//handle for long period SMA
   CopyBuffer(sh, 0, 0, arrsize, SSMA);//copy last 10 values of short period to SSMA
   ArraySetAsSeries(SSMA, true);
   CopyBuffer(lh, 0, 0, arrsize, LSMA);//copy last 10 values of long period to LSMA
   ArraySetAsSeries(LSMA, true);
  

  
   double askp = SymbolInfoDouble("EURUSD",SYMBOL_ASK);//ask price
   double bidp = SymbolInfoDouble("EURUSD",SYMBOL_BID);//bid price
  
   //Is a position open?
  
  
   if (PositionsTotal()<1){
   posopen = false;
   }
   else
   {
   posopen = true;
   }
  
   //No positions, so look for a buy once cross-over happens
   if (posopen=false){
  
   double longsum = 0; //used for sum of long positions
   double shortsum = 0; //used for sum of short positions
  
   //loop through size of array (except for current value) to find totals of arrays
   int arraysize = ArraySize(SSMA);
  
   for (int counter = 1; counter == arraysize; counter++)
   {
   longsum = longsum + LSMA[counter];
   shortsum = shortsum + SSMA[counter];
   }
  
   //Which total is larger?
   if (shortsum>longsum){//if shortsum is greater, it may be a precursor to a sell if current SSMA is less than LSMA
  
   if (SSMA[0]<LSMA[0]){
  
   SellExecute(RiskWill);
   lasttrade = 0;
   }
   }
  
   if (shortsum<longsum){//if shortsum is less, it may be a precursor to a buy if current LSMA is less than SSMA
  
   if (SSMA[0]>LSMA[0]){
  
   BuyExecute(RiskWill);
   lasttrade = 1;
   }
   }
  
   }
   //Positions are open, close on cross-over
   if(posopen = true){
  
   if (lasttrade == 0){//last trade was a sell, so short must cross long high or other criteria to be specified later
  
   if (SSMA[0]>LSMA[0]){
   PositionSelect("EURUSD");//forces closure
   int i=PositionsTotal();
   while (i>=1)
     {
      if (trade.PositionClose(PositionGetSymbol(i))) i--;
     }
  
   }
   }
   if (lasttrade == 1){//last trade was a buy, so short must cross long low or other criteria to be specified later
  
   if (LSMA[0]>SSMA[0]){
   PositionSelect("EURUSD");//forces closure
   int i=PositionsTotal();
   while (i>=1)
     {
      if (trade.PositionClose(PositionGetSymbol(i))) i--;
     }
  
   }
   }
   }
  
  }
//+------------------------------------------------------------------+


//SELLEURUSD
void SellExecute(double RiskWill)
{
double totallots = CalcLotsVolume(RiskWill);
trade.PositionOpen("EURUSD",ORDER_TYPE_BUY,totallots,SymbolInfoDouble("EURUSD",SYMBOL_BID),0,0);
}

//BUYEURUSD
void BuyExecute(double RiskWill)
{
double totallots = CalcLotsVolume(RiskWill);
trade.PositionOpen("EURUSD",ORDER_TYPE_BUY,totallots,SymbolInfoDouble("EURUSD",SYMBOL_ASK),0,0);
}

//---------- CalcLotsVolume ---------------------------
double CalcLotsVolume(double RiskPercent)
{
   double SendLots;//The actual Lotsize that will be sent with the order
   double MaxLot;//Broker's Maximum permitted amount of a lot.
   double MinLot;//Broker's Step for changing lots, e.g 0.01.
   double LotStep;//Broker's Minimum permitted amount of a lot.
   if (Lots > 0.0) SendLots = NormalizeLot(Lots);//The manual LotSize, bounded by broker's specs
   else {//Calculate AutoLot:
      MaxLot = SymbolInfoDouble("EURUSD", SYMBOL_VOLUME_MAX);
      MinLot = SymbolInfoDouble("EURUSD", SYMBOL_VOLUME_MIN);
      LotStep = SymbolInfoDouble("EURUSD", SYMBOL_VOLUME_STEP);
      double MargInit;
      SendLots = NormalizeDouble(MathFloor(AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 1000000 ), 2);
      if (SendLots < MinLot) SendLots = MinLot;//Broker's absolute minimum Lot
      if (SendLots > MaxLot) SendLots = MaxLot;//Broker's absolute maximum Lot
   }
   if (SendLots > MaxLots) SendLots = MaxLots;//Never exceed our manualy set limit
   return (SendLots);
}
//-----------------------------------------------------
//---------- NormalizeLot -----------------------------
double NormalizeLot(double Lots)
{
   double NormalizedLot;//The final LotSize, bounded by broker's specs
   double InverseLotStep;
   double LotStep = SymbolInfoDouble("EURUSD", SYMBOL_VOLUME_STEP);
   double MinLot  = SymbolInfoDouble("EURUSD", SYMBOL_VOLUME_MIN);
   double MaxLot  = SymbolInfoDouble("EURUSD", SYMBOL_VOLUME_MAX);
   if (MinLot  == 0.0) MinLot = 0.1;//In case MarketInfo returns no info
   if (MaxLot  == 0.0) MaxLot = 5;  //In case MarketInfo returns no info
   if (LotStep == 0.0) InverseLotStep = 1 / MinLot;//In case MarketInfo returns no info
   else InverseLotStep = 1 / LotStep;  
   NormalizedLot = MathFloor(Lots * InverseLotStep) / InverseLotStep;  
   if (NormalizedLot < MinLot) NormalizedLot = MinLot;//Broker's absolute minimum Lot
   if (NormalizedLot > MaxLot) NormalizedLot = MaxLot;//Broker's absolute maximum Lot
   return (NormalizedLot);
}
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Account Properties
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Account Properties - Documentation on MQL5
 
if you can post the codes with the src button. it makes reading your codes easier.
 
void OnTick(bool& posopen, int& lasttrade) 
Looks like them parameters isn't workin too well
 

I edited your first post and removed the second one.

 

There seems to be a lot wrong with your code. For example you can replace extern with input to make variable changeable by user. I suggest you read Automated Trading Language Documentation It will help you here.

Good luck!

 
//SELLEURUSD
void SellExecute(double RiskWill)
{
double totallots = CalcLotsVolume(RiskWill);
trade.PositionOpen("EURUSD",ORDER_TYPE_BUY,totallots,SymbolInfoDouble("EURUSD",SYMBOL_BID),0,0);
}

ORDER_TYPE_BUY
ORDER_TYPE_SELL
 
if (posopen=false){
Is that correct?
 
Icarus:
Is that correct?

Certainly not, it should be :

if (posopen==false){
Reason: