Can some one have a look at my code and give me a hint where I am going wrong

 

I have been working on this for too many hours now... some would say a waste of time, as I still cant make it work :)

 

There is something wrong in my Algorithm but I am struggling to see where.

 

I am new to this and I think my nested "If statements" are clumsy, but I still think they should work.

 

Any suggestions so I may stop tearing my hair out??

 

Cheers

 

Nufty

 

 

//+------------------------------------------------------------------+
void CheckSignal()
{
 //Trading decision.
 // The idea is to have a long term EMA as a filter for long and short trades - use 200, if Price Action above, only take longs - if below only take shorts
 // Then the EA waits for a EMA cross of Fast and Slow
 // It sets TP and SL based on Fibs
   
    int type = -1;
    int i;
    double takeLong;
    double takeShort;
    double stopLong;
    double stopShort;
    bool MAFIB= false;
    bool MAFIS = false; 
    

   double MA14_prv1 = iMA(NULL, 0, Fast_MA, 0, Fast_Mode, PRICE_CLOSE,2);          
   double MA14_prv2 = iMA(NULL, 0, Fast_MA, 0, Fast_Mode, PRICE_CLOSE,2);         
   double MA14_now = iMA(NULL, 0, Fast_MA, 0, Fast_Mode, PRICE_CLOSE, 1);             
   double MA50_prv1 = iMA(NULL, 0, Slow_MA, 0, Slow_Mode, PRICE_CLOSE, 2);        
   double MA50_prv2 = iMA(NULL, 0, Slow_MA, 0, Slow_Mode, PRICE_CLOSE,2);        
   double MA50_now = iMA(NULL, 0, Slow_MA, 0, Slow_Mode, PRICE_CLOSE, 1);            
   double Trend_MA = iMA(NULL, 0, Trend_MA, 0, Trend_Mode, PRICE_CLOSE,1);            
   
    // Setting Point B as the swing LOW and using open as other point of Fib and Point A as the high
         lowerChannel = Low[ iLowest( NULL, 0, MODE_LOW, BarSwing, 0)];
         upperChannel = High[ iHighest( NULL, 0, MODE_HIGH, BarSwing, 0)];
    
   // ver "far too many" :)  
   // A BUY signal 
   // Added print as a debug - but it wont cycle through

  if((MA14_now > Trend_MA)) MAFIB=true;
 {
  Print ("Step 1 of Fib BUY");
   }
   
   if(MA14_prv1 > MA50_prv1) 
   {
   Print ("Step 2 OK");
   }
   
     if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2))
   {
   Print ("Step 3 OK");
   }
   
    if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2) && (MA14_now > MA50_now))
   {
   Print ("Step 4 OK");
   }
   
    if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2) && (MA14_now > MA50_now) && (Close[1] < Bid))
   {
   Print ("Step 5 OK");
   }
   
    if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2) && (MA14_now > MA50_now) && (Close[1] < Bid) && MAFIB == true)
   {
   Print ("Should BUY Something!!!!");
   Print("BUY signal: Fib=", MA14_now, "; Signal Bar Bid=", Bid);
      type = OP_BUY;
   }
 
Nufty:

I have been working on this for too many hours now... some would say a waste of time, as I still cant make it work :)

 There is something wrong in my Algorithm but I am struggling to see where.

 I am new to this and I think my nested "If statements" are clumsy, but I still think they should work.


You don't actually say what is not working . . .  you also need to show/explain what the values of some variables are,  for example: Fast_Mode, Slow_Mode, Trend_Mode, etc.
 
//+------------------------------------------------------------------+
void CheckSignal()
{
 //Trading decision.
 // The idea is to have a long term EMA as a filter for long and short trades - use 200, if Price Action above, only take longs - if below only take shorts
 // Then the EA waits for a EMA cross of Fast and Slow
 // It sets TP and SL based on Fibs
   
    int type = -1;
    int i;
    double takeLong;
    double takeShort;
    double stopLong;
    double stopShort;
    bool MAFIB= false;
    bool MAFIS = false; 
    

   double MA14_prv1 = iMA(NULL, 0, Fast_MA, 0, Fast_Mode, PRICE_CLOSE,2);          
   double MA14_prv2 = iMA(NULL, 0, Fast_MA, 0, Fast_Mode, PRICE_CLOSE,2);         
   double MA14_now = iMA(NULL, 0, Fast_MA, 0, Fast_Mode, PRICE_CLOSE, 1);             
   double MA50_prv1 = iMA(NULL, 0, Slow_MA, 0, Slow_Mode, PRICE_CLOSE, 2);        
   double MA50_prv2 = iMA(NULL, 0, Slow_MA, 0, Slow_Mode, PRICE_CLOSE,2);        
   double MA50_now = iMA(NULL, 0, Slow_MA, 0, Slow_Mode, PRICE_CLOSE, 1);            
   double Trend_MA = iMA(NULL, 0, Trend_MA, 0, Trend_Mode, PRICE_CLOSE,1);            
   
    // Setting Point B as the swing LOW and using open as other point of Fib and Point A as the high
         lowerChannel = Low[ iLowest( NULL, 0, MODE_LOW, BarSwing, 0)];
         upperChannel = High[ iHighest( NULL, 0, MODE_HIGH, BarSwing, 0)];
    
   // ver "far too many" :)  
   // A BUY signal 
   // Added print as a debug - but it wont cycle through

  if((MA14_now > Trend_MA)) MAFIB=true;
 {
  Print ("Step 1 of Fib BUY"); ALWAYS EXECUTED
   }
   
   if(MA14_prv1 > MA50_prv1) 
   {
   Print ("Step 2 OK");
   }
   
     if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2))  unnecessary parenthesis, there are many others
   {
   Print ("Step 3 OK");
   }
   
    if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2) && (MA14_now > MA50_now))
   {
   Print ("Step 4 OK");
   }
   
    if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2) && (MA14_now > MA50_now) && (Close[1] < Bid))
   {
   Print ("Step 5 OK");
   }
   
    if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2) && (MA14_now > MA50_now) && (Close[1] < Bid) && MAFIB == true)
   {
   Print ("Should BUY Something!!!!");
   Print("BUY signal: Fib=", MA14_now, "; Signal Bar Bid=", Bid);
      type = OP_BUY;
   }
I did not check the logical only syntax.
 
   double MA14_prv1 = iMA(NULL, 0, Fast_MA, 0, Fast_Mode, PRICE_CLOSE,2);          
   double MA14_prv2 = iMA(NULL, 0, Fast_MA, 0, Fast_Mode, PRICE_CLOSE,2);     // both same value

 and

   double MA50_prv1 = iMA(NULL, 0, Slow_MA, 0, Slow_Mode, PRICE_CLOSE, 2);        
   double MA50_prv2 = iMA(NULL, 0, Slow_MA, 0, Slow_Mode, PRICE_CLOSE,2);   //also both same value

so makes

if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2))

 never true  no ("Step 3 OK")

...also

if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2) && (MA14_now > MA50_now))   //never true
if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2) && (MA14_now > MA50_now) && (Close[1] < Bid))   // never true

 

if((MA14_prv1 > MA50_prv1) && (MA14_prv2 < MA50_prv2) && (MA14_now > MA50_now) && (Close[1] < Bid) && MAFIB == true)  //never true

 

 

 never true   no ("Step 4 OK") no ("Step 5 OK")no .......

 

Absolutely sensational assistance

 

THANK YOU angevoyageur  and especially deVries

 

I have been changing little things all over this code and in my frustrations I obviously miseed these values, and I do think the extra set of eyes has helped!!

 

Let me make changes

 

Really appreciate the assistance

 

PS  I will post the entire EA as an attachment as well next time I have issues, it may help expliain the variables etc 

 

I am back again - this coding caper isnt as easy as I had hoped :)

 

Another 6 or so hours googling and reading and nothing has pinpointed what my problem is.

 

I will attach the entire EA for reference to all my variables.  

 

My issues at the moment are:

1.  The EA only takes long trades - sets incorrect SL and TP (I think my fib calcs are correct, do not sure why)

2. The EA Ignores the TREND MA. - I have a filter about the price action needed to be above the MA_Trend but the EA ignores it and executes BUYS on the other two MA crossing.

 

I bet there is more wrong with it, but these are the first two issues I have.

 

What I have been googling and reading, and therefore my thinking - I think I need to add a 'return' in the code once the rend filter is found to be false, maybe this will loop it to wait for this to become Truse before it moves to the next line of code

 

I added one - and it didnt do anything :(

 

Also.  I am perplexed as to why SELLS arent executed.  Maybe my use of Bools is confusing the code (and me!!)

 

:)

 

Once I get this working, with a HELL OF A LOT of assistance, I want to add a filter where it wont take trades under a certain number of pips -  simple reasoning is the spread will take a winner to a loser if it takes trades where the fib channel set up is too small.

 

Again, I appreciate any assistance / help.

Cheers

 

Nufty

Files:
 
      if( ArraySize(m_arrActiveTickets) < MaxTrades)
         if( Time[1] > m_orderCloseTime)
            CheckSignal();
What do you want that happens here ??
Reason: