5/13/62 EA Assistance

 

Hello community!

I've been studying this stuff for a few months now, needless to say I'm pretty green at mql4. I really need some help getting this code based on Rob Booker's 5/13/62 strategy to compile.

My business partner and I charted this a few weeks ago and determined that entering trades based on Booker's 5/13/62 crosses and exiting said trades on 5/13 crosses could be a relatively profitable strategy.

I've managed to hack up some other code and piece this together. Like I said something is really wrong. Was wondering if anyone could lend a minute to help a rookie out.

Kindest Regards

//--- input parameters
extern double MAFast = 5;
extern double MAMed = 13;
extern double MASlow = 62;
extern double MAMode = 1; //0 simple, 1 exponential, 2 smoothed, 3 linea weighted
extern double MinDistanceAtCross = 30.0;
extern double Lots = 0.1;
extern double Slippage = 3.0;
extern int Magic = 1618;

int _Ticket = 0;

// multiplers
double pt, mt;

// fatal error during startup
bool fatal_error = false;

/* setup state - if false, waiting for initial trade setup, if true,
   initial setup has taken place and we are waiting for a retrace
   back to touch the MASlow */
bool setup_state = false;
int setup_direction;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+

int init()
  {
  if (Bars < MASlow)
      {
      Print("chart bars less than " + MASlow);
      fatal_error = true;
      return(0);  
      }
      
   // Set 'pt' and 'mt'.
   if (Digits == 3 || Digits == 5)
      {
      // five decimal broker
      pt = Point * 10;
      mt = 10;
      }
   else
      {
      // 4 decimal broker
      pt = Point;
      mt = 1;
      }
            
   return(0);
  }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int start()
{
   double freemargin;
   double MAF1, MAF2; // fast MA values last complete bar (1) and previous complete bar (2)
   double MAM1, MAM2; // medium MA values
   double MAS1, MAS2; // slow MA values
   double MAS0;       // slow MA current (forming) bar
   double tmp;
   int ticket;
   
   if (fatal_error)
      return(0);
      
   freemargin = AccountFreeMargin();
   MAF1 = iMA(Symbol(), 0, MAFast, 0, MAMode, PRICE_CLOSE, 1);
   MAF2 = iMA(Symbol(), 0, MAFast, 0, MAMode, PRICE_CLOSE, 2);
   MAM1 = iMA(Symbol(), 0, MAMed, 0, MAMode, PRICE_CLOSE, 1);
   MAM2 = iMA(Symbol(), 0, MAMed, 0, MAMode, PRICE_CLOSE, 2);
   MAS1 = iMA(Symbol(), 0, MASlow, 0, MAMode, PRICE_CLOSE, 1);
   MAS2 = iMA(Symbol(), 0, MASlow, 0, MAMode, PRICE_CLOSE, 2);
   MAS0 = iMA(Symbol(), 0, MASlow, 0, MAMode, PRICE_CLOSE, 0);

//+------------------------------------------------------------------+

// check for open position and load globals
   orderInit(Magic);
   
   // if no trade opened by this EA/Symbol
   if (_Ticket == 0)
      {
      // buy initial setup
      if ((MAF2 < MAS2 && MAF1 > MAS1) || (MAF2 < MAM2 && MAF1 > MAM1 && MAM1 > MAS1))
         {
         // force state back to beginning
         setup_state = false;
         
         // set setup direction
         setup_direction = OP_BUY;
         tmp = NormalizeDouble((Ask - MAS1) * 10000.0, 1);
         Print("cross:1st buy cond met:ask=", Ask, ",MAS1=", MAS1, ",distance=", tmp);
            
         // second condition
         if (tmp >= MinDistanceAtCross)
            {
            Print("2nd buy cond met:waiting for slow MA re-touch");
            setup_state = true;
            }
            
         return(0);
         }
      
      // sell initial setup      
      if ((MAF2 > MAS2 && MAF1 < MAS1) || (MAF2 > MAM2 && MAF1 < MAM1 && MAM1 < MAS1))
         {
         setup_state = false;
         setup_direction = OP_SELL;
         tmp = NormalizeDouble((MAS1 - Bid) * 10000.0, 1);
         Print("cross:1st sell cond met:bid=", Bid, ",MAS1=", MAS1, ",distance=", tmp);
         
         if (tmp >= MinDistanceAtCross)
            {
            Print("2nd sell cond met:waiting for slow MA re-touch");
            setup_state = true;
            }
            
         return(0);
         }                  
         
      }
 }

 void CheckForClose()
  {
   double MAF1, MAF2; // fast MA values last complete bar (1) and previous complete bar (2)
   double MAM1, MAM2; // medium MA values      
      
//---- check order type 
      if(OrderType()==OP_BUY)
        {
         if(MAF2 > MAM2 && MAF1 < MAM1)
        }
        
      if(OrderType()==OP_SELL)
        {
         if(MAF2 < MAM2 && MAF1 > MAM1)
        }
    return(0);
  }
 
 
fxcoup:

Hello community!

I've been studying this stuff for a few months now, needless to say I'm pretty green at mql4. I really need some help getting this code based on Rob Booker's 5/13/62 strategy to compile.

My business partner and I charted this a few weeks ago and determined that entering trades based on Booker's 5/13/62 crosses and exiting said trades on 5/13 crosses could be a relatively profitable strategy.

I've managed to hack up some other code and piece this together. Like I said something is really wrong. Was wondering if anyone could lend a minute to help a rookie out.

Kindest Regards

_Ticket is always 0

If you are going to use OrderType() you must first select the order that you want the order type for . . . use OrderSelect()

 
You'd better find someone experienced to create this EA for your. There is a lot of tricky stuff around executing orders and overcoming errors and brokers limitations. However this code will manage your money.
 

@cyberfx.org

Could you elaborate a bit sir.

I don't mean to come across as ignorant but I absolutely have to figure this thing out. I've spent countless hours trying to get familiar with mql4 and I've fallen in love with the capabilities.

Like I said earlier I'm not sure all that is wrong with the EA so far but for some reason I feel like I'm pretty close to having something I can atleast begin to backtest and optimize!

@RaptorUK.... thanks for the heads up. Still trying to wrap my head around it. Will update when I get something moving.

Regards

 
fxcoup:


@RaptorUK.... thanks for the heads up. Still trying to wrap my head around it. Will update when I get something moving.

Regards

Ah, you didn't show all your code . . . so _Ticket gets set somewhere else ?
 
RaptorUK:
Ah, you didn't show all your code . . . so _Ticket gets set somewhere else ?

I thought I was declaring that in the global variables.

Anyhow, here are some changes I've made to the code . Mainly in the closing section.

Somehow I'm still throwing a 'orderInit' - function is not defined error.

Cheers

//--- input parameters
extern double MAFast = 5;
extern double MAMed = 13;
extern double MASlow = 62;
extern double MAMode = 1; //0 simple, 1 exponential, 2 smoothed, 3 linea weighted
extern double MinDistanceAtCross = 30.0;
extern double Lots = 0.1;
extern double Slippage = 3.0;
extern int Magic = 1618;
int _Ticket = 0;

// multiplers
double pt, mt;

// fatal error during startup
bool fatal_error = false;

/* setup state - if false, waiting for initial trade setup, if true,
   initial setup has taken place and we are waiting for a retrace
   back to touch the MASlow */
bool setup_state = false;
int setup_direction;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+

int init()
  {
  if (Bars < MASlow)
      {
      Print("chart bars less than " + MASlow);
      fatal_error = true;
      return(0);  
      }
      
   // Set 'pt' and 'mt'.
   if (Digits == 3 || Digits == 5)
      {
      // five decimal broker
      pt = Point * 10;
      mt = 10;
      }
   else
      {
      // 4 decimal broker
      pt = Point;
      mt = 1;
      }
            
   return(0);
  }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int start()
{
   double freemargin;
   double MAF1, MAF2; // fast MA values last complete bar (1) and previous complete bar (2)
   double MAM1, MAM2; // medium MA values
   double MAS1, MAS2; // slow MA values
   double MAS0;       // slow MA current (forming) bar
   double tmp;
   int cnt, ticket, total;
   
   if (fatal_error)
      return(0);
      
   freemargin = AccountFreeMargin();
   MAF1 = iMA(Symbol(), 0, MAFast, 0, MAMode, PRICE_CLOSE, 1);
   MAF2 = iMA(Symbol(), 0, MAFast, 0, MAMode, PRICE_CLOSE, 2);
   MAM1 = iMA(Symbol(), 0, MAMed, 0, MAMode, PRICE_CLOSE, 1);
   MAM2 = iMA(Symbol(), 0, MAMed, 0, MAMode, PRICE_CLOSE, 2);
   MAS1 = iMA(Symbol(), 0, MASlow, 0, MAMode, PRICE_CLOSE, 1);
   MAS2 = iMA(Symbol(), 0, MASlow, 0, MAMode, PRICE_CLOSE, 2);
   MAS0 = iMA(Symbol(), 0, MASlow, 0, MAMode, PRICE_CLOSE, 0);

//+------------------------------------------------------------------+

// check for open position and load globals
   orderInit(Magic);
   
   // if no trade opened by this EA/Symbol
   if (_Ticket == 0)
      {
      // buy initial setup
      if ((MAF2 < MAS2 && MAF1 > MAS1) || (MAF2 < MAM2 && MAF1 > MAM1 && MAM1 > MAS1))
         {
         // force state back to beginning
         setup_state = false;
         
         // set setup direction
         setup_direction = OP_BUY;
         tmp = NormalizeDouble((Ask - MAS1) * 10000.0, 1);
         Print("cross:1st buy cond met:ask=", Ask, ",MAS1=", MAS1, ",distance=", tmp);
            
         // second condition
         if (tmp >= MinDistanceAtCross)
            {
            Print("2nd buy cond met:waiting for slow MA re-touch");
            setup_state = true;
            }
            
         return(0);
         }
      
      // sell initial setup      
      if ((MAF2 > MAS2 && MAF1 < MAS1) || (MAF2 > MAM2 && MAF1 < MAM1 && MAM1 < MAS1))
         {
         setup_state = false;
         setup_direction = OP_SELL;
         tmp = NormalizeDouble((MAS1 - Bid) * 10000.0, 1);
         Print("cross:1st sell cond met:bid=", Bid, ",MAS1=", MAS1, ",distance=", tmp);
         
         if (tmp >= MinDistanceAtCross)
            {
            Print("2nd sell cond met:waiting for slow MA re-touch");
            setup_state = true;
            }
            
         return(0);
         }                  
         
      }
 }

 void CheckForClose()
  {
   double MAF1, MAF2; // fast MA values last complete bar (1) and previous complete bar (2)
   double MAM1, MAM2; // medium MA values 
   int cnt, ticket, total; 
           
//---- check order type 
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()==OP_BUY)
        {
         if((MAF2 > MAM2 && MAF1 < MAM1))
         OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
        }
     else   
      if(OrderType()==OP_SELL)
        {
         if((MAF2 < MAM2 && MAF1 > MAM1))
         OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
        }
    return(0);
  }
 
 
fxcoup:

1. I thought I was declaring that in the global variables.

Anyhow, here are some changes I've made to the code . Mainly in the closing section.

2. Somehow I'm still throwing a 'orderInit' - function is not defined error.

Cheers

1. You do declare it, int _Ticket = 0; but you never, ever change it . . so it will always be 0 . . . unless you have more code that you aren't showing.

2. Well you call the function, orderInit(Magic); where is this function declared ?