Indicator help

 

Hi,


I am developing an indicator based on another indicator (channelProfit).

What I am trying to do is detect when the channelProfit changes its signal (its buffer 6), and signal it in my indicator with an arrow (separate wondow bottom row).

In addition, I am trying to see what's the situation on another timeframe (top row).

When the bottom row matches the top row, a circle should appear between those lines.


Unfortunately, the indicator is rather eratic:

1. When initialized, it does not function every time;

2. When it does come up, the results are not correct (for instance: using H1 timeframe for low row and comparisonPeriod =1, i.e. H4 for upper row; opening 2 charts on the screen for checkup - H1, H4 reveals that the indicator shows erroneous results).


Can anyone help?


Here is the code:


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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

//| _TrendArrows3.mq4 |

//| Copyright © 2009,Zeev Ben-Nahum |

//| http://www.amadot.co.il/ |

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

#property copyright "Copyright © 2009, ZBN"

#property link "http://www.amadot.co.il/"


//---- properties

#property indicator_separate_window

#property indicator_minimum -5.0

#property indicator_maximum 5.0

#property indicator_buffers 5

#property indicator_color1 Blue

#property indicator_color2 Red

#property indicator_color3 Blue

#property indicator_color4 Red

#property indicator_color5 Yellow


//----- externs

extern double eChannelProfitRisk = 2.0;

extern int comparisonPeriod = 1;


//---- buffers

double zBuffer0[]; // current period UP

double zBuffer1[]; // current period DOWN

double zBuffer2[]; // compare period UP

double zBuffer3[]; // compare period DOWN

double zBuffer4[]; // compre=current buffer


//---- statics

static bool _initFinished = false; // flag for setting window's name

static int _currentBarTime = 0; //time of current Bars

static int _comparisonBarTime = 0; //time of comparison Bars

static bool _isCurrentBar; // is the script already ran in current period's time

static bool _isComparisonBar; // is the script already ran in comparison period's time

static int _currentArrow = 0;

static int _comparisonArrow = 0;

static int _comparisonPeriod = -1; // comparison period in minutes

static string _currentPeriodName; // name of current period

static string _comparisonPeriodName; // name of comparison period


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

//| Custom indicator initialization function |

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

int init()

{

int period[]={1,5,15,30,60,240,1440,10080,43200};

string periodString[]={"M1","M5","M15","M30","H1","H4","D1","W1","MN1"};

///// [initating buffers] /////

SetIndexBuffer(0, zBuffer0);

SetIndexBuffer(1, zBuffer1);

SetIndexBuffer(2, zBuffer2);

SetIndexBuffer(3, zBuffer3);

SetIndexBuffer(4, zBuffer4);

SetIndexStyle(0, DRAW_ARROW);

SetIndexStyle(1, DRAW_ARROW);

SetIndexStyle(2, DRAW_ARROW);

SetIndexStyle(3, DRAW_ARROW);

SetIndexStyle(4, DRAW_ARROW);

SetIndexArrow(0, 233);

SetIndexArrow(1, 234);

SetIndexArrow(2, 233);

SetIndexArrow(3, 234);

SetIndexArrow(4, 161);

//// [timeframe] ////

int j, max = 9, min = 0;

if (comparisonPeriod > 0)

max -= comparisonPeriod;

else

min += (-1*comparisonPeriod);

for (j=min; j<max && _comparisonPeriod == -1;j++)

{

if(Period() == period[j])

{

_currentPeriodName = periodString[j];

_comparisonPeriod = period[j+comparisonPeriod];

_comparisonPeriodName = periodString[j+comparisonPeriod];

Print("Comparison period: " + _comparisonPeriodName);

}

}

//// [filling the buffers for 1st time] ////

int currentArrow, comparisonArrow;

for(int i=1; i<=Bars; i++) // please mind that we don't update CURRENT BAR (i've changed from 0 to 1) //

{

currentArrow = iCustom(NULL, Period(), "channelprofit", eChannelProfitRisk,0, 6, i);

comparisonArrow = iCustom(NULL, _comparisonPeriod, "channelprofit", eChannelProfitRisk,0, 6, i);

signalAssignment(i, currentArrow, comparisonArrow);

}

//// [saving last arrows (for the start function)] ////

_currentArrow = currentArrow;

_comparisonArrow = comparisonArrow;

//// [saving current and comparison's bar times] ////

_currentBarTime = iTime(NULL,0,0);

_comparisonBarTime = iTime(NULL,_comparisonPeriod,0);

return(0);

}

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

//| Custom indicator deinitialization function |

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

int deinit()

{

return(0);

}


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

//| Custom indicator iteration function |

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

int start()

{

///////////////////////////// TIME CHECKING //////////////////////////

int currentBarTime = iTime(NULL,0,0);

int comparisonBarTime = iTime(NULL,_comparisonPeriod,0);

/// checking if the script already ran in this bar

if (currentBarTime == _currentBarTime)

{

_isCurrentBar = true;

}

else

{

_isCurrentBar = false;

_currentBarTime = currentBarTime;

}

/// checking if the script already ran in comparison's period bar

if (comparisonBarTime == _comparisonBarTime)

{

_isComparisonBar = true;

}

else

{

_isComparisonBar = false;

_comparisonBarTime = comparisonBarTime;

}

///////////////////////////// UPDATING WINDOW NAME //////////////////////////

/// if running for the first time, saving window index ///

if(_initFinished == false)

{

string windowName = "Trend Arrows 3 ("+_currentPeriodName+","+_comparisonPeriodName+")";

IndicatorShortName(windowName);

_initFinished = true;

}

///////////////////////////// UPDATING BARS //////////////////////////

/********************************************************

Bar update algorithm:

--------------------------------

Start status - this function runs for the 1st time after all buffers were updated except for the current bar

1. we have 2 static integers -

one saves current period's time (_currentArrow)

second saves comparison period's time (_comparisonArrow)

2. if a bar was added in current period's => updating (_currentArrow) and setting (updateBuffers) flag to TRUE

3. if a bar was added in comparison period's => updating (_comparisonArrow) and setting (updateBuffers) flag to TRUE

4. if (updateBuffers) is TRUE => updating buffers.


********************************************************/

// updating buffers //

bool updateBuffers = false;

// if running for the 1st time this bar

if (_isCurrentBar == false)

{

_currentArrow = iCustom(NULL, Period(), "channelprofit", eChannelProfitRisk,0, 6, 1);

updateBuffers = true;

_isCurrentBar = true;

}

// if running for the 1st time in comparison period's bar

if (_isComparisonBar == false)

{

_comparisonArrow = iCustom(NULL, _comparisonPeriod, "channelprofit", eChannelProfitRisk,0, 6, 1);

updateBuffers = true;

_isComparisonBar = true;

}

// if there's a need to update buffers

if (updateBuffers)

signalAssignment(1, _currentArrow, _comparisonArrow);

return(1);

}

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

// signal elaboration

int signalAssignment(int position, int currentArrow, int comparisonArrow)

{

// current is blue

if (currentArrow == 1)

{

zBuffer0[position] = -2;

zBuffer1[position] = 10;

}

else

{

zBuffer1[position] = -2;

zBuffer0[position] = -10;

}

// comparison is blue

if (comparisonArrow == 1)

{

zBuffer2[position] = 2;

zBuffer3[position] = 10;

}

else

{

zBuffer3[position] = 2;

zBuffer2[position] = 10;

}

if (comparisonArrow == currentArrow )

{

zBuffer4[position] = 0;

}

else

{

zBuffer4[position] = 10;

}

}

//+++++++++++++++++++++++++++++++++++++


Thanks,


Zeev


Files:
Reason: