//+------------------------------------------------------------------+
//|                                                         GMMA.mq4 |
//+------------------------------------------------------------------+
// This is an indicator; install into ...(MT4)/experts/indicators folder

#property copyright "Copyright (c) 2013, David Louisson"
#property link      "http://www.forexfactory.com/hanover"

/*+------------------------------------------------------------------+
Disclaimer and License Agreement
//+------------------------------------------------------------------+

1. The 'VENDOR' (David Louisson, of New Zealand) grants non-exclusive, non-transferable, non-revocable
License to the 'PURCHASER' to use the software and materials for the permitted purpose, for an indefinite period
of time. The Purchaser further agrees that the software and materials are for his/her own personal use only.

2. The Purchaser may not assign nor sublicense the License, nor adapt nor modify the code. The Purchaser may
not reverse engineer, disassemble, or otherwise endeavor to obtain the source code from the object code.

3. The Purchaser is limited to the number of installations as controlled by the software. If the Purchaser wishes
to use the software over this limit, the Vendor reserves the right to charge an additional License/registration fee.

4. The Purchaser must not alter, remove or obscure any trade mark or copyright symbol or legend or other
proprietary mark on the software and the materials.

5. The Purchaser is not permitted to sell, charge, mortgage or otherwise encumber the software and materials in any way.

6. The Purchaser acknowledges that the Vendor gives no guarantee as to the accuracy or completeness of the software
and the materials, or that they are free from error.

7. To the fullest extent permitted by law, the Vendor expressly disclaims all implied warranties and conditions as to
merchantability, fitness or purpose of the software and materials.

8. The Purchaser indemnifies and holds harmless the Vendor and promises to keep the Vendor indemnified against any loss,
claim, action, settlement, award, judgment, expense or damage of whatsoever kind or nature and howsoever arising that
the Vendor might suffer as a result of any inaccuracy of the software and/or the materials including any authorized or
unauthorized use of the software and/or the materials by the Purchaser. The Purchaser further indemnifies and holds
harmless the Vendor for any trading losses, both realized and floating, incurred by the Purchaser.

9. The terms of this Agreement constitute the entire terms of this Agreement and all understandings, prior
representations, arrangements or commitments that are not contained in this Agreement have no effect whatsoever and
do not bind the parties.

//+------------------------------------------------------------------+*/

#property indicator_chart_window

#include <hanover --- function header (np).mqh>

#property  indicator_buffers 8

extern int        LookbackBars               = 500;
extern string     MAperiods                  = "30,35,40,45,50,55,60";
// 3,5,8,10,12
// 30,35,40,45,50,55,60
// 75,90,105,120,135,150,165,180
// 195,210,225,240,255,270,285,300
extern int        MAmode                     = MODE_EMA;
extern int        MAprice                    = PRICE_CLOSE;
extern color      MAcolor                    = DodgerBlue;
extern int        MAwidth                    = 1;
extern int        MAstyle                    = 0;

string     ccy, sym, IndiName, diag_string;
int        dig, tf, tmf, vis, wno, RefreshEveryXMins, bar, nMA, MA[8];
double     spr, pnt, tickval, bidp, askp, minlot, lswap, sswap, NewSL;
datetime   prev_time;
bool       FirstTime;

double     ind_buffer0[], ind_buffer1[], ind_buffer2[], ind_buffer3[], ind_buffer4[], ind_buffer5[], ind_buffer6[], ind_buffer7[];

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  IndiName = NumberToStr(GetUniqueInt(),"'GMMA-'Z6'-'");
  IndicatorShortName(IndiName);

  nMA = StrToIntegerArray(MAperiods,MA);
  
  if (nMA >= 1)   { SetIndexBuffer(0,ind_buffer0);  SetIndexStyle(0,DRAW_LINE,MAstyle,MAwidth,MAcolor);  SetIndexDrawBegin(0,0); }
  if (nMA >= 2)   { SetIndexBuffer(1,ind_buffer1);  SetIndexStyle(1,DRAW_LINE,MAstyle,MAwidth,MAcolor);  SetIndexDrawBegin(1,0); }
  if (nMA >= 3)   { SetIndexBuffer(2,ind_buffer2);  SetIndexStyle(2,DRAW_LINE,MAstyle,MAwidth,MAcolor);  SetIndexDrawBegin(2,0); }
  if (nMA >= 4)   { SetIndexBuffer(3,ind_buffer3);  SetIndexStyle(3,DRAW_LINE,MAstyle,MAwidth,MAcolor);  SetIndexDrawBegin(3,0); }
  if (nMA >= 5)   { SetIndexBuffer(4,ind_buffer4);  SetIndexStyle(4,DRAW_LINE,MAstyle,MAwidth,MAcolor);  SetIndexDrawBegin(4,0); }
  if (nMA >= 6)   { SetIndexBuffer(5,ind_buffer5);  SetIndexStyle(5,DRAW_LINE,MAstyle,MAwidth,MAcolor);  SetIndexDrawBegin(5,0); }
  if (nMA >= 7)   { SetIndexBuffer(6,ind_buffer6);  SetIndexStyle(6,DRAW_LINE,MAstyle,MAwidth,MAcolor);  SetIndexDrawBegin(6,0); }
  if (nMA >= 8)   { SetIndexBuffer(7,ind_buffer7);  SetIndexStyle(7,DRAW_LINE,MAstyle,MAwidth,MAcolor);  SetIndexDrawBegin(7,0); }

  sym     = Symbol();
  ccy     = Symbol();
  tmf     = Period();
  pnt     = MarketInfo(ccy,MODE_POINT);
  dig     = MarketInfo(ccy,MODE_DIGITS);
  spr     = MarketInfo(ccy,MODE_SPREAD);
  tickval = MarketInfo(ccy,MODE_TICKVALUE);
  if (dig == 3 || dig == 5) {
    pnt     *= 10;
    spr     /= 10;
    tickval *= 10;
  } 
  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  del_obj();
  plot_obj();
  return(0);
}

//+------------------------------------------------------------------+
void del_obj()  {
//+------------------------------------------------------------------+
  int k=0;
  while (k<ObjectsTotal())   {
    string objname = ObjectName(k);
    if (StringSubstr(objname,0,StringLen(IndiName)) == IndiName)  
      ObjectDelete(objname);
    else
      k++;
  }    
  return(0);
}

//+------------------------------------------------------------------+
void plot_obj()   {
//+------------------------------------------------------------------+
  for (int i=0; i<=LookbackBars; i++)  {
    ind_buffer0[i] = EMPTY_VALUE;     if (nMA >= 1)    ind_buffer0[i] = iMA(Symbol(),Period(),MA[0],0,MAmode,MAprice,i);
    ind_buffer1[i] = EMPTY_VALUE;     if (nMA >= 2)    ind_buffer1[i] = iMA(Symbol(),Period(),MA[1],0,MAmode,MAprice,i);
    ind_buffer2[i] = EMPTY_VALUE;     if (nMA >= 3)    ind_buffer2[i] = iMA(Symbol(),Period(),MA[2],0,MAmode,MAprice,i);
    ind_buffer3[i] = EMPTY_VALUE;     if (nMA >= 4)    ind_buffer3[i] = iMA(Symbol(),Period(),MA[3],0,MAmode,MAprice,i);
    ind_buffer4[i] = EMPTY_VALUE;     if (nMA >= 5)    ind_buffer4[i] = iMA(Symbol(),Period(),MA[4],0,MAmode,MAprice,i);
    ind_buffer5[i] = EMPTY_VALUE;     if (nMA >= 6)    ind_buffer5[i] = iMA(Symbol(),Period(),MA[5],0,MAmode,MAprice,i);
    ind_buffer6[i] = EMPTY_VALUE;     if (nMA >= 7)    ind_buffer6[i] = iMA(Symbol(),Period(),MA[6],0,MAmode,MAprice,i);
    ind_buffer7[i] = EMPTY_VALUE;     if (nMA >= 8)    ind_buffer7[i] = iMA(Symbol(),Period(),MA[7],0,MAmode,MAprice,i);
  }
  return(0);
}

//+------------------------------------------------------------------+
#include <hanover --- extensible functions (np).mqh>