My first attempt at an EA - Can I get some help with the logic

 

Hi

 New to coding but not new to FX or EAs !!

 I am wanting to try an ea that works like discribed - I feel my logic is right, but its not taking the right trades - basically it ignores the SMA indicator, and every bar is printing in the journal that their is a buy and a sell signal

 Which should be impossible

 Can someone suggest a better way to code this for a newbie like me??

 

THANKS

 


void CheckSignal()

{

   double SMA = iMA( NULL, 0, SMA_Period, SMA_Shift, SMA_Method, SMA_Price, 1);

   

   string MAFIB="false";string MAFIS="false";

   

   int type = -1; 

// ver 2.2

   // Need momentum into the SMA - have a look at getting candles to match roll of ball - that is two candles back starts signal, next candle needs to cross sma 
      in our direction and last candle to confirm

   // A cross from below (Long signal) can be defined as - a candle opening below the SMA, and closing above it.  

   // For a valid BUY signal to be triggered, the Bot need to look back the candle before the cross and the candle after the cross, If these three candles are 
      bullish - Take BUY on open of next candle.

   

  if ((Open[2] < SMA  && Close[2] > SMA))MAFIB="true";

    {

  Print ("SMA says BUY");

 }

    

   if ((Open[3] < Close[3] ) && (Open[2] < Close[2] ) && (Open[1] < Close[1]) && (Close[1] < Bid) && MAFIB=="true")

      {  

      Print("BUY signal: SMA=", SMA, "; Signal Bar Bid=", Bid);

      type = OP_BUY;

   } 

Appreciate the help

 

Nufty 

 
I would suggest you to use the MACD Sample EA as a starting point. simply replace the MACD trading conditions with yours.
 
Nufty:

Hi

 New to coding but not new to FX or EAs !!

 I am wanting to try an ea that works like discribed - I feel my logic is right, but its not taking the right trades - basically it ignores the SMA indicator, and every bar is printing in the journal that their is a buy and a sell signal


This is your problem,  it's a question of  {}  braces

  if ((Open[2] < SMA  && Close[2] > SMA))   MAFIB="true";   //  if the test is true just set MAFIB = "true"

    {                                                       //   the code in braces will always happen

  Print ("SMA says BUY");

 }

 

Also,  don't use strings here,  use bool . . .

string MAFIB="false";string MAFIS="false";

// instead do this

bool MAFIB = false, MAFIS = false;

 then this code changes  . . .

if ((Open[3] < Close[3] ) && (Open[2] < Close[2] ) && (Open[1] < Close[1]) && (Close[1] < Bid) && MAFIB )   // && MAFIB  is the same as && MAFIB == true 
 

Thanks RaptorUK

 

I will add this and see how it runs

 

Cheers

 

Nufty