Trying to create a generic trading EA to an Indicator

 

First, this is a simple admission: I am feeling like I'm crap at this coding 'Milarkey' at the moment, but I am not giving up!!

The following is a mash-up of code which I have stitched together whilst I am learning. What I am trying to achieve is link the Indicator to the EA so it will trade according to the Indicator signals.

Within the Indicator (cleverly called "Indicator") I have added a third buffer called "ExtMapBuffer3" In this, I want to get the indicator to set 1 for a Buy and 2 for a sell

Within the EA (fiendishly called "Indicator Trader"), I am using iCustom to pull the information into the EA using the following "int iFeed=iCustom(NULL,0,"Indicator",3,0);"

Needless to say, the EA is not trading and, in accordance with the rules of seniority in my home, I am kicking a toy cat and the wife is kicking me!! So please, before she calls the Neighbours to join in the violence and to engender a short lived feeling of equality in my home, can anyone help!?

This is the relevant source code for the EA

   // Feed from indicator_buffer
   
   double iFeed=iCustom(NULL,0,"Indicator",3,0);
  
///--------------------------------------------------------------5a-----
      //Trade Entry Criteria
      
   if( iFeed == 1)                                 //Buy_Trigger                         
       {
        Opn_B=true;                                // Criterion for opening Buy
        Cls_S=true;                                // Criterion for closing Sell                                                  
       }                                
   if( iFeed == 2)                                 //Sell_Trigger
       {
         Opn_S=true;                               // Criterion for opening Sell
         Cls_B=true;                               //Criterion for closing Buy
       }
//--------------------------------------------------------------- 6 --

And this is the revised code for the Indicator which sees to be working fine.

//| Indicator.mq4

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Blue                
#property indicator_color2 Red                 
#define IName              "Indicator"

//-------------------------------
// Input parameters
//-------------------------------
extern bool CalculateOnBarClose    = false;

//-------------------------------
// Buffers
//-------------------------------
double ExtMapBuffer1[];                         
double ExtMapBuffer2[];
double ExtMapBuffer3[];                         

//-------------------------------
// Internal variables
//-------------------------------

double last_signal = 0;
double last_action = OP_BUY;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
    SetIndexStyle(0, DRAW_ARROW, STYLE_DOT, 1);
    SetIndexArrow(0, 233);
    SetIndexBuffer(0, ExtMapBuffer1);
    SetIndexStyle(1, DRAW_ARROW, STYLE_DOT, 1);
    SetIndexArrow(1, 234);
    SetIndexBuffer(1, ExtMapBuffer2);
    SetIndexBuffer(2, ExtMapBuffer3);   
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
    return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
    // Start, limit, etc..
    int start = 0;
    int limit;
    int counted_bars = IndicatorCounted();

    // nothing else to do?
    if(counted_bars < 0) 
        return(-1);

    // do not check repeated bars
    limit = Bars - 1 - counted_bars;
    
    // Check if ignore bar 0
    if(CalculateOnBarClose == true) start = 1;
    
    // Check the signal foreach bar from past to present
    for(int i = limit; i >= start; i--)
    {
        // Grab all fractals
        double upper_fractal_5b = upper_fractal_5b(i, true);
        double upper_fractal_7b = upper_fractal_7b(i, true);
        double lower_fractal_5b = lower_fractal_5b(i, true);
        double lower_fractal_7b = lower_fractal_7b(i, true);
            
        // Long 5bar reversal
        if(lower_fractal_5b != 0 && lower_fractal_5b != last_signal && last_action == OP_SELL)
        {
            ExtMapBuffer1[i] = lower_fractal_5b;
            last_signal = lower_fractal_5b;
            last_action = OP_BUY;
            ExtMapBuffer3[i]=1;
        } else 
        
        // Long 7bar reversal
        if(lower_fractal_7b != 0&& lower_fractal_7b != last_signal && last_action == OP_SELL)
        {
            ExtMapBuffer1[i] = lower_fractal_7b;
            last_signal = lower_fractal_7b;
            last_action = OP_BUY;
            ExtMapBuffer3[i]=1;
        } else 
         
        // Short 5bar reversal
        if(upper_fractal_5b != 0 && upper_fractal_5b != last_signal && last_action == OP_BUY)
        {
            ExtMapBuffer2[i] = upper_fractal_5b;
            last_signal = upper_fractal_5b;
            last_action = OP_SELL;
            ExtMapBuffer3[i]=2;
        } else 
        
        // Short 7bar reversal
        if(upper_fractal_7b != 0&& upper_fractal_7b != last_signal && last_action == OP_BUY)
        {
            ExtMapBuffer2[i] = upper_fractal_7b;
            last_signal = upper_fractal_7b;
            last_action = OP_SELL;
            ExtMapBuffer3[i]=2;
        }
        // Is in only a slow zigzag signal?
        // ExtMapBuffer2[i+2] = fr_t;
    
    }
    return(0);
}


Guys, please help. The Wife has just told me she is taking up Mixed Martial Arts!!



 
There have been plenty of threads recently (last 4 weeks) about iCustom . . . search for them, find them and read them. Or are you looking for someone to write your code for you ? if you aren't you need to be a little more specific about what you are having problems with . . .
 
  1. double ExtMapBuffer2[];
    int ExtMapBuffer3[];
    :
    SetIndexBuffer(2, ExtMapBuffer3);

    Buffers must be a double, bool SetIndexBuffer( int index, double array[])


  2. Detailed explanation of iCustom - MQL4 forum


  3. Guys, please help. The Wife has just told me she is taking up Mixed Martial Arts!!
    You've already lost.
 
#property indicator_buffers 2
If you are using 3 buffers then you better tell the system you are using 3 not 2!
 

Quite why so many people call the indicator buffers ExternalMapBufferN

baffles me. It's just a name. Why not use something relevant?

LowerFractalBuffer

UpperFractalBuffer

BuySellOutput


for example.

 

Guys, I have shown the revised code at the head of this post. Thanks for your help with this as always.

The Indicator seems to be working fine but I am still not trading. I have highlighted where I think the problem is but I have no idea what the problem is or how to fix it. Can someone point me in the right direction as I have spent so many hours just staring at this code and am going slowly insane!

 
Where are your debugging Print statements ? do you know you are getting the correct values from the Indicator ? do you know your Indicator is "indicating" a Buy or a Sell ? what do you know ? what don't you know ? what do you need to know ? how will you find out what you need to know ?
 

Willforth:

I have highlighted where I think the problem is but I have no idea what the problem is or how to fix it.

Is this voodoo debugging? " I can feel it using the force".

Debugging is not guesswork, at least it is educated guesswork. You might assume it is something or other then test it to see if the guess was right.

In that for loop you do not actually set ExtMapBuffer to anything if the tests are not true. I would be happier if at the top of the loop you set it to 0, for example, then let a condition change it to either 1 or 2.

If the indicator is working you should see the 1 or 2 values changing in the indicator readout. Then you need to check those values get to the EA with comments or print statements. Once you have established that, you are on a strong footing.

 
Hmmm, I have much to learn. Thanks Guys. I feel some more research is very much needed.
 
Willforth:

First, this is a simple admission: I am feeling like I'm crap at this coding 'Milarkey' at the moment, but I am not giving up!!


Do not give up! I am going through the same frustrations, but I WILL persevere.

 

Am currently dancing round the room and sending a disparaging email to the Wife for mocking me! IT WORKS!!!!!!!!! Indicator was generating the right data but iCustom was wrong.....

I took your advice and found a way to check the output of the Indicator....the Indicator was fine....it took me most of my day but I found the problem....

In the Indicator...

SetIndexBuffer(2, ExtMapBuffer3);

In the EA....

double iFeed=iCustom(NULL,0,"Indicator",3,0);

Wrong Buffer!!!!

I am now in the process of seeing if the EA adds to the Bank Balance or subtracts (Demo...of course).

Thanks for all your help and support Guys

Reason: