Navigation:Home > Content >

#GenesisArrowMatrix_1.20_2.mq4

Time: 2014-09-16 | Download file:#GenesisArrowMatrix_1.20_2.mq4

/*------------------------------------------------------------------------------------
   Description: Additional Matrix for the Genesis Trading System
                - Does not repaint
                - MTF
   Based on Genesis Matrix	
   Change log:
   1.20  Changed indicator from ASCTrend to Xaphod's ASCTrend1i; 500x faster
   1.10  Added Global Variable output
         Modified the NextHigherTF to return PERIOD_M30 for PERIOD_M15, not H1.
   1.00  2012 09 09 Converted to just arrow components
-------------------------------------------------------------------------------------*/
// Indicator properties
#property copyright "Copyright © 2012, Cody_R"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 C'0,90,255'
#property indicator_color2 Red
#property indicator_color3 C'0,90,255'
#property indicator_color4 Red
#property indicator_minimum 0
#property indicator_maximum 1.2

//#include 

// Win32 API
#import "user32.dll"
   bool     GetWindowRect(int hWnd, int& lpRect[]);
#import
#import "Kernel32.dll"
  int CreateFileA(string lpFileName,int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition,int dwFlagsAndAttributes, int hTemplateFile);
  int CloseHandle(int hObject);
#import

#define OPEN_EXISTING 3
#define FILE_SHARE_READ 1
#define GENERIC_READ 0x80000000
#define FILE_ATTRIBUTE_NORMAL 128
#define INVALID_HANDLE_VALUE 0xFFFFFFFF

// Constant definitions
#define INDICATOR_NAME "GenesisArrows"
#define INDICATOR_VERSION "1.20_2"
#define BAR_CHAR 110


#define IND_Row1      "ASCTrend1i"
#define IND_Row2      "ASCTrend1i"

#define IDX_Row1      0
#define IDX_Row2      1
//#define IDX_Row3      2
//#define IDX_Row4      3

// Indicator parameters
extern string    Indi.Version=INDICATOR_VERSION;
extern string    TimeFrame.Settings="——————————————————————————————";
extern int       TimeFrame.Period=0;        // Timeframe: 0,1,5,15,30,60,240,1440 etc. Current Timeframe=0.
extern int       TimeFrame.Auto=0;          // Automatically select higher TF. M15 and M30 -> H1. Off=0, 1st HTF=1, 2nd HTF=2
extern string    ARR.Settings="——————————————————————————————";
extern string    ARR.Label="Arr_";
extern string    HTF_ARR.Settings="——————————————————————————————";
extern int       HTF_ARR.Period=0;          // Timeframe: 0,1,5,15,30,60,240,1440 etc. Current Timeframe=0. 
extern int       HTF_ARR.Auto=1;            // Automatically select higher TF. M15 and M30 -> H1. Off=0, 1st HTF=1, 2nd HTF=2
extern string    HTF_ARR.Label="Arr_";
extern string    Alert.Settings="——————————————————————————————";
extern bool      Alert.OnBarClose=True;     // Alert only when an open bar closes
extern bool      Alert.Popup=False;         // Enable popup window & sound on alert
extern string    Alert.Sound="";            // Play sound on alert. Wav files only
extern bool      Alert.Email=False;         // Enable send email on alert
extern string    Alert.Subject="";          // Email Subject. Null string ("") will result in a preconfigured subject.
extern string    Display.Settings="——————————————————————————————";
extern color     Display.LabelColor=White;  // Color of Histogram Id labels

// Global module varables
static int       crGreen  = 1;
static int       crBlue   = 1;
static int       crLong   = 1;
static int       crRed    = -1;
static int       crShort  = -1;

// Histogram
double gadRow1Up[];
double gadRow1Dn[];
double gadRow2Up[];
double gadRow2Dn[];

double RowGap[2] = {0.9, 0.4};

// Labels
double gadGap[2];
string Row1.Label,
       Row2.Label,
       Row3.Label,
       Row4.Label;

// Globals
int    giRepaintBars;
string gsIndicatorName;
bool   gbInit;
int    NumberOfRows;

//-----------------------------------------------------------------------------
// function: init()
// Description: Custom indicator initialization function.
//-----------------------------------------------------------------------------
int init() {
  string sTFList;
  int iMaxTF;
  int i, j;
  
  NumberOfRows = indicator_buffers/2;

  // Init indicator buffers
  IndicatorBuffers(indicator_buffers);
  
  SetIndexStyle(0,DRAW_ARROW);
  SetIndexBuffer(0,gadRow1Up);
  SetIndexStyle(1,DRAW_ARROW);
  SetIndexBuffer(1,gadRow1Dn);

  SetIndexStyle(2,DRAW_ARROW);
  SetIndexBuffer(2,gadRow2Up);
  SetIndexStyle(3,DRAW_ARROW);
  SetIndexBuffer(3,gadRow2Dn);

  for ( i = 0; i < indicator_buffers; i++) {
    SetIndexLabel(i,NULL);
    SetIndexEmptyValue(i,0.0);
    SetIndexArrow(i,BAR_CHAR);
  }

  // Set HTF Arr TimeFrame
    if ( HTF_ARR.Period == 0 ) HTF_ARR.Period = Period();
    
    switch(HTF_ARR.Auto) {
    case 1:
      HTF_ARR.Period=NextHigherTF(HTF_ARR.Period);
    break;
    case 2:
      HTF_ARR.Period=NextHigherTF(NextHigherTF(HTF_ARR.Period));
    break;
    default:
      if (HTF_ARR.Period<1 || HTF_ARR.Period==Period()) {
        HTF_ARR.Period=Period();
      }
    break;
  }

   HTF_ARR.Label = HTF_ARR.Label+  TF2Str(HTF_ARR.Period);
  
  // Set Timeframe
  switch(TimeFrame.Auto) {
    case 1:
      TimeFrame.Period=NextHigherTF(TimeFrame.Period);
      giRepaintBars=TimeFrame.Period/Period()+2;
    break;
    case 2:
      TimeFrame.Period=NextHigherTF(NextHigherTF(TimeFrame.Period));
      giRepaintBars=TimeFrame.Period/Period()+2;
    break;
    default:
      if (TimeFrame.Period<1 || TimeFrame.Period==Period()) {
        TimeFrame.Period=Period();
        giRepaintBars=0;
      }
      else {
        giRepaintBars=TimeFrame.Period/Period()+2;
      }
    break;
  }

  ARR.Label = ARR.Label + TF2Str(TimeFrame.Period);

  // Set histogram positions
  for (i=0; i<=NumberOfRows; i++)
  {
    gadGap[i]    = RowGap[i]; 
  }                            

  // Check that required indicators are installed
  if (!IndicatorExists(IND_Row1+".ex4"))
    Alert(Symbol()+", "+TF2Str(Period())+", "+INDICATOR_NAME+": Error! "+TerminalPath()+"\\experts\\indicators\\"+IND_Row1+".ex4 cannot be found.");
  if (!IndicatorExists(IND_Row2+".ex4"))
    Alert(Symbol()+", "+TF2Str(Period())+", "+INDICATOR_NAME+": Error! "+TerminalPath()+"\\experts\\indicators\\"+IND_Row2+".ex4 cannot be found.");

  // Misc
  gsIndicatorName=INDICATOR_NAME+" "+INDICATOR_VERSION+"("+TF2Str(TimeFrame.Period)+"."+TF2Str(HTF_ARR.Period)+")";
  IndicatorShortName(gsIndicatorName);
  gbInit=True;
  return(0);
}


//-----------------------------------------------------------------------------
// function: deinit()
// Description: Custom indicator deinitialization function.
//-----------------------------------------------------------------------------
int deinit() {
  // Clear text objects
  for(int i=ObjectsTotal()-1; i>-1; i--)
    if (StringFind(ObjectName(i),gsIndicatorName)>=0)  ObjectDelete(ObjectName(i));
  // Clear Global Variables
  GlobalVariableDel(StringConcatenate(Symbol(), " ", INDICATOR_NAME," ", INDICATOR_VERSION, " ", TF2Str(Period() ), " ",Row1.Label ) );
  GlobalVariableDel(StringConcatenate(Symbol(), " ", INDICATOR_NAME," ", INDICATOR_VERSION, " ", TF2Str(Period() ), " ",Row2.Label ) );
  return (0);
}

//-----------------------------------------------------------------------------
// function: start()
// Description: Custom indicator iteration function.
//-----------------------------------------------------------------------------
int start() {
  int iNewBars;
  int iCountedBars;
  static datetime tCurBar;

  // Get unprocessed bars
  iCountedBars=IndicatorCounted();
  if(iCountedBars < 0) return (-1);
  if(iCountedBars>0) iCountedBars--;

  // Set bars to redraw
  if (NewBars(TimeFrame.Period)>3)
    iNewBars=Bars-1;
  else
    iNewBars=Bars-iCountedBars;
  if (iNewBars=0; i--)
  {
    // Shift index for higher time-frame
    if (TimeFrame.Period>Period() )
      j=iBarShift(Symbol(), TimeFrame.Period, Time[i]);
    else
      j=i;
    // Calc HA
    if (iHTFBar!=j)
    {
      iHTFBar=j;
      dSig=iCustom(NULL,TimeFrame.Period,IND_Row1, 2, j);
    }
    // No Data
    if (iClose(NULL,TimeFrame.Period,j)==0) {
      vdUp[i]=EMPTY_VALUE;
      vdDn[i]=EMPTY_VALUE;
    }
    // Bull signal
    else if (dSig>=0) {
      GlobalVariableSet(StringConcatenate(Symbol(), " ", INDICATOR_NAME," ", INDICATOR_VERSION, " ", TF2Str(Period() ), " ",Row1.Label ), crLong);
      vdUp[i]=gadGap[iRow];
      vdDn[i] = 0;
    }
    // Bear signal
    else {
      GlobalVariableSet(StringConcatenate(Symbol(), " ", INDICATOR_NAME," ", INDICATOR_VERSION, " ", TF2Str(Period() ), " ",Row1.Label ), crShort);
      vdDn[i]=gadGap[iRow];
      vdUp[i] = 0;
    } 
  }
  return(0);
}

int ProcessRow2(int iNewBars, double& vdUp[], double& vdDn[], int iRow)
{
  int i,j;
  double dSig;
  int iHTFBar=-1;

  for(i=iNewBars; i>=0; i--)
  {
    // Shift index for higher time-frame
    if (HTF_ARR.Period>Period() )
      j=iBarShift(Symbol(), HTF_ARR.Period, Time[i]);
    else
      j=i;
    // Calc HA
    if (iHTFBar!=j)
    {
      iHTFBar=j;
      dSig=iCustom(NULL,HTF_ARR.Period,IND_Row2, 2, j);
    }
    // No Data
    if (iClose(NULL,HTF_ARR.Period,j)==0) {
      vdUp[i]=EMPTY_VALUE;
      vdDn[i]=EMPTY_VALUE;
    }
    // Bull signal
    else if (dSig>=0) 
    {
      GlobalVariableSet(StringConcatenate(Symbol(), " ", INDICATOR_NAME," ", INDICATOR_VERSION, " ", TF2Str(Period() ), " ",Row2.Label ), crLong);
      vdUp[i]=gadGap[iRow];
      vdDn[i] = 0;
    }
    // Bear signal
    else 
    {
      GlobalVariableSet(StringConcatenate(Symbol(), " ", INDICATOR_NAME," ", INDICATOR_VERSION, " ", TF2Str(Period() ), " ",Row2.Label ), crShort);
      vdDn[i]=gadGap[iRow];
      vdUp[i] = 0;
    } 
  }
  return(0);
}

//-----------------------------------------------------------------------------
// function: Writelabel()
// Description: Write a label for a bar
//-----------------------------------------------------------------------------
int Writelabel(string sLabel,double dPrice) {
  string sObjId;
  sObjId=gsIndicatorName+"_"+sLabel;
  if(ObjectFind(sObjId) < 0)
    ObjectCreate(sObjId, OBJ_TEXT, WindowFind(gsIndicatorName), Time[0]+Period()*60*3.5, dPrice);
  ObjectSetText(sObjId, sLabel, 8, "Calibri", Display.LabelColor); //Lucida Console
  ObjectMove(sObjId,0,Time[0]+Period()*60*3.5, dPrice);
  return(0);
}


//-----------------------------------------------------------------------------
// function: IndicatorExists()
// Description: Check if an indicator exists
//-----------------------------------------------------------------------------
bool IndicatorExists(string sIndicatorName) {
  int hFile;
  string sFile;

  // Exit if dlls are disabled
  if (!IsDllsAllowed())
    return(True);

  // Try to open indicator
  sFile=TerminalPath()+"\\experts\\indicators\\"+sIndicatorName;
  hFile=CreateFileA(sFile,0,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  if (hFile==INVALID_HANDLE_VALUE) {
    return(False);
  }
  else {
    CloseHandle(hFile);
    return(True);
  }
  return(False);
}

//-----------------------------------------------------------------------------
// function: TF2Str()
// Description: Convert time-frame to a string
//-----------------------------------------------------------------------------
string TF2Str(int iPeriod) {
  switch(iPeriod) {
    case PERIOD_M1: return("M1..");
    case PERIOD_M5: return("M5..");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1: return("H1.");
    case PERIOD_H4: return("H4.");
    case PERIOD_D1: return("D1.");
    case PERIOD_W1: return("W1.");
    case PERIOD_MN1: return("MN1");
    default: return("M"+iPeriod);
  }
  return(0);
}


//-----------------------------------------------------------------------------
// function: NextHigherTF()
// Description: Select the next higher time-frame.
//              Note: M15 and M30 both select H1 as next higher TF.
//-----------------------------------------------------------------------------
int NextHigherTF(int iPeriod) {
  if (iPeriod==0) iPeriod=Period();
  switch(iPeriod) {
    case PERIOD_M1: return(PERIOD_M5);
    case PERIOD_M5: return(PERIOD_M15); 
    case PERIOD_M15: return(PERIOD_M30);
    case PERIOD_M30: return(PERIOD_H1);
    case PERIOD_H1: return(PERIOD_H4);
    case PERIOD_H4: return(PERIOD_D1);
    case PERIOD_D1: return(PERIOD_W1);
    case PERIOD_W1: return(PERIOD_MN1);
    case PERIOD_MN1: return(PERIOD_MN1);
    default: return(Period());
  }
}

//-----------------------------------------------------------------------------
// function: CheckAlert()
// Description: Check for new alerts
//-----------------------------------------------------------------------------
void CheckAlert() {
  static datetime tAlertBar;
  static int iPrevAlert=0;
  
  if (Alert.Popup || Alert.Email || Alert.Sound!="") {
    
    // Alert on the close of the current bar
    if (Alert.OnBarClose && tAlertBar0 && gadRow2Up[i]>0 )//&& gadR3Up[i]>0 && gadR4Up[i]>0)
    return(True);
  else
    return(False);
}


//-----------------------------------------------------------------------------
// function: DnSignal()
// Description: eturn true if there is a down signal
//-----------------------------------------------------------------------------
bool DnSignal(int i) {
  if (gadRow1Dn[i]>0 && gadRow2Dn[i]>0 )//&& gadR3Dn[i]>0 && gadR4Dn[i]>0)
    return(True);
  else
    return(False);
}

Recommend