Good start but code needs cleaning! - (Small Indicator)

 

Please note: I am very novice at MQL! Anyways, basically I am trying to create an indicator that displays text on the current chart based on conditions calculated from a different timeframe, i.e. in my case if previous close on Weekly chart was below 10 SMA then display "Sell" on current chart (whatever timeframe).

I know that you have to 'calculate' the MA first, send it to buffer then call it, but I am very confused as to how to do it. Here is my code so far:

#property copyright "©2011 by Splexin @ www.liveforex.biz"
#property link "http://www.liveforex.biz"
#property indicator_chart_window
#property indicator_buffers 2

// exported variables


// local variables
int ObjCount = 0;  // count of all objects created on the chart, allows creation of objects with unique names
int current = 0; // variable points to current bar

string TextObj3 = "";
double Buffer3[];
string TextObj6 = "";
double Buffer6[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
    if (true) ObjectsDeleteAll();      // clear the chart
    IndicatorShortName("G7 Weekly Direction Signal");
    IndicatorDigits(0);
    IndicatorBuffers(2);
    
    SetIndexBuffer(0, Buffer3);
    
    ObjCount += 1;
    TextObj3 = "IChart_" + ObjCount;
    ObjectCreate(TextObj3, OBJ_LABEL, 0, 0, 0);
    ObjectSet(TextObj3, OBJPROP_CORNER, 0);
    ObjectSet(TextObj3, OBJPROP_XDISTANCE, 150);
    ObjectSet(TextObj3, OBJPROP_YDISTANCE, 15);
    
    SetIndexBuffer(1, Buffer6);
    
    ObjCount += 1;
    TextObj6 = "IChart_" + ObjCount;
    ObjectCreate(TextObj6, OBJ_LABEL, 0, 0, 0);
    ObjectSet(TextObj6, OBJPROP_CORNER, 0);
    ObjectSet(TextObj6, OBJPROP_XDISTANCE, 150);
    ObjectSet(TextObj6, OBJPROP_YDISTANCE, 15);
    return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
    if (true) ObjectsDeleteAll();
    
    
    return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator start function                                  |
//+------------------------------------------------------------------+
int start()
{
    OnEveryTick1();
    //~~~~~~~~~~
    for(int i=ObjectsTotal()-1; i>=0; i--){
        if(ObjectFind("IChart_"+i)>-1){
            if(ObjectDescription("IChart_"+i)=="Label"){
                ObjectDelete("IChart_"+i);
            }
        }
    }
    //~~~~~~~~~~
    return(0);
}

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

void OnEveryTick1()
{
    
    int i;
    int counted_bars = IndicatorCounted();
    if(counted_bars < 0) return(-1);
    if(counted_bars > 0) counted_bars--;
    i = Bars - counted_bars;
    // main calculation loop
    while (i >= 0)
    {
        current = i;
        TechnicalAnalysis2x2();
        TechnicalAnalysis2x7();
        
        i--;
    }
}

void TechnicalAnalysis2x2()
{
    if ((Close[1] < iMA(NULL, PERIOD_W1,10,0,MODE_SMA,PRICE_CLOSE,0))
    {
        ChartText3();
        
    }
}

void ChartText3()
{
    Buffer3[current]= NULL;
    ObjectSetText(TextObj3, "Sell Rallies", 22, "Arial", Red);
    
}

void TechnicalAnalysis2x7()
{
    if ((Close[1] > iMA(NULL, PERIOD_W1,10,0,MODE_SMA,PRICE_CLOSE,0))
    {
        ChartText6();
        
    }
}

void ChartText6()
{
    Buffer6[current]= NULL;
    ObjectSetText(TextObj6, "Buy Dips", 22, "Arial", Green);
    
}

Please especially pay attention to the "if (Close[1] >" lines....I am pretty sure those are the cause of my unbalanced parenthesis error.....

 
peepingtom:

Please note: I am very novice at MQL! Anyways, basically I am trying to create an indicator that displays text on the current chart based on conditions calculated from a different timeframe, i.e. in my case if previous close on Weekly chart was below 10 SMA then display "Sell" on current chart (whatever timeframe).

I know that you have to 'calculate' the MA first, send it to buffer then call it, but I am very confused as to how to do it. Here is my code so far:

Please especially pay attention to the "if (Close[1] >" lines....I am pretty sure those are the cause of my unbalanced parenthesis error.....

Well give a man a fish and you feed him for a day ...

#property copyright "©2011 by Splexin @ www.liveforex.biz"
#property link "http://www.liveforex.biz"
#property indicator_chart_window
#property indicator_buffers 2

// exported variables


// local variables
int ObjCount = 0;  // count of all objects created on the chart, allows creation of objects with unique names
int current = 0; // variable points to current bar

string TextObj3 = "";
double Buffer3[];
string TextObj6 = "";
double Buffer6[];

int init(){
    if (true) ObjectsDeleteAll();      // clear the chart
    IndicatorShortName("G7 Weekly Direction Signal");
    IndicatorDigits(0);
    IndicatorBuffers(2);
    
    SetIndexBuffer(0, Buffer3);
    
    ObjCount += 1;
    TextObj3 = "IChart_" + ObjCount;
    ObjectCreate(TextObj3, OBJ_LABEL, 0, 0, 0);
    ObjectSet(TextObj3, OBJPROP_CORNER, 0);
    ObjectSet(TextObj3, OBJPROP_XDISTANCE, 150);
    ObjectSet(TextObj3, OBJPROP_YDISTANCE, 15);
    
    SetIndexBuffer(1, Buffer6);
    
    ObjCount += 1;
    TextObj6 = "IChart_" + ObjCount;
    ObjectCreate(TextObj6, OBJ_LABEL, 0, 0, 0);
    ObjectSet(TextObj6, OBJPROP_CORNER, 0);
    ObjectSet(TextObj6, OBJPROP_XDISTANCE, 150);
    ObjectSet(TextObj6, OBJPROP_YDISTANCE, 15);
    return(0);
}

int deinit(){
    if( true ) // what is the point of this test?
      ObjectsDeleteAll();
    
    return(0);
}

int start(){
    OnEveryTick1();

    for(int i=ObjectsTotal()-1; i>=0; i--){
        if(ObjectFind("IChart_"+i)>-1){
            if(ObjectDescription("IChart_"+i)=="Label"){
                ObjectDelete("IChart_"+i);
            }
        }
    }

    return(0);
}

void OnEveryTick1(){
    
    int counted_bars = IndicatorCounted();
    if( counted_bars < 0 )
      return(-1);
      
    if( counted_bars > 0 )
      counted_bars--;
    
    int i = Bars - counted_bars;
    
    while(i >= 0){          // main calculation loop

        current = i;
        TechnicalAnalysis2x2();
        TechnicalAnalysis2x7();
        
        i--;
    }
}


void TechnicalAnalysis2x2(){
    if( Close[1] < iMA(NULL, PERIOD_W1,10,0,MODE_SMA,PRICE_CLOSE,0) ){
        ChartText3();
    }
}



void ChartText3(){
    Buffer3[current]= NULL;
    ObjectSetText(TextObj3, "Sell Rallies", 22, "Arial", Red);
}



void TechnicalAnalysis2x7(){
    if( Close[1] > iMA(NULL, PERIOD_W1,10,0,MODE_SMA,PRICE_CLOSE,0) ){
        ChartText6();
    }
}

void ChartText6(){
    Buffer6[current]= NULL;
    ObjectSetText(TextObj6, "Buy Dips", 22, "Arial", Green);
}

So, how did I fix your code? (so it complies at least)

The answer when you don't have a debugger is "binary search".

Comment out half the code and see if the problem is there or not. If it is not fixed try the other half. If it is fixed then halve the half until you find the function or line responsible.

 
Thank you very much!!! Works perfectly :)
Reason: