MT4 Coding Help Needed, Please see the snippets
Posted: Mon Apr 08, 2019 7:52 am
Hi, I've been writing a multicurrency indicator in mql4 and I'm facing a challenge currently. the main error I get is a "divide by zero" but I'm having a hard time figuring out its source, please see the code below.
First the code that works;
1. This function receives currency pair arrays which have data from all time frames and also determines negative or positive correlation
2. This function compares if up or down is stronger and sends the info to the main currency strength buffers "c_mft_str_main"
3. The modification, I had to factor in pairs that are negatively correlated so I wrote this function which brought all the problems
4. The first function is modified as this
5. I have another function that receives all this data but it creates a divide by zero error,which is ridiculous, please help!
First the code that works;
1. This function receives currency pair arrays which have data from all time frames and also determines negative or positive correlation
Code: Select all
void CSMTFStrenght(bool one_isneg,
bool two_isneg,
bool three_isneg,
bool four_isneg,
bool five_isneg,
bool six_isneg,
bool seven_isneg,
double &c_mtf_str_main[],
double &c_mtf_str_up[],
double &c_mtf_str_down[],
double &one_up[],
double &two_up[],
double &three_up[],
double &four_up[],
double &five_up[],
double &six_up[],
double &seven_up[],
double &one_down[],
double &two_down[],
double &three_down[],
double &four_down[],
double &five_down[],
double &six_down[],
double &seven_down[],
double &mtf_str_main_pre[],
int main_limit,
int shift)
{
//--- tally all timeframes
for (int i = 0; i <= 8; i++)
{
c_mtf_str_up[i] = one_up[i] + two_up[i] + three_up[i] + four_up[i] + five_up[i] + six_up[i] + seven_up[i];
c_mtf_str_down[i] = one_down[i] + two_down[i] + three_down[i] + four_down[i] + five_down[i] + six_down[i] + seven_down[i];
}
//--- compare and send highest to main mtf strenght buffer
for (int i = 0; i <= 8; i++)
CSMTFCompare(c_mtf_str_main, c_mtf_str_up, c_mtf_str_down, i, mtf_str_main_pre, main_limit, shift);
}
2. This function compares if up or down is stronger and sends the info to the main currency strength buffers "c_mft_str_main"
Code: Select all
void CSMTFCompare(double &out_buffer[],
double &a[],
double &b[],
int index,
double &out_buffer_pre[],
int main_limit,
int shift)
{
if (a[index] > b[index])
{
out_buffer[index] = a[index];
} // up wins
else if (a[index] < b[index])
{
out_buffer[index] = b[index];
} // down wins
else if (a[index] == b[index])
{
out_buffer[index] = a[index];
} // even
out_buffer[index] = EMA(main_limit, out_buffer[index], main_limit - 1, CurrenyStrPeriod, shift);
out_buffer_pre[index] = EMA(main_limit, out_buffer[index], main_limit - 1, CurrenyStrPeriod, shift - 2);
}
3. The modification, I had to factor in pairs that are negatively correlated so I wrote this function which brought all the problems
Code: Select all
double NegPairMTF(double &up[], double &down[], int index, bool isneg = false, bool isup = false, bool isdown = false)
{
double strenght = 0.0;
if (isneg == true)
{
if (up[index] > down[index])
{
strenght = down[index];
}
if (up[index] < down[index])
{
strenght = up[index];
}
if (up[index] == down[index])
{
strenght = up[index];
}
}
else
{
if (isup == true && isdown == false)
{
strenght = up[index];
}
if (isup == false && isdown == true)
{
strenght = down[index];
}
}
return strenght;
}
4. The first function is modified as this
Code: Select all
void CSMTFStrenght(bool one_isneg,
bool two_isneg,
bool three_isneg,
bool four_isneg,
bool five_isneg,
bool six_isneg,
bool seven_isneg,
double &c_mtf_str_main[],
double &c_mtf_str_up[],
double &c_mtf_str_down[],
double &one_up[],
double &two_up[],
double &three_up[],
double &four_up[],
double &five_up[],
double &six_up[],
double &seven_up[],
double &one_down[],
double &two_down[],
double &three_down[],
double &four_down[],
double &five_down[],
double &six_down[],
double &seven_down[],
double &mtf_str_main_pre[],
int main_limit,
int shift)
{
//--- tally all timeframes
for (int i = 0; i <= 8; i++){
c_mtf_str_up[i]=NegPairMTF(one_up,one_down,i,one_isneg,true,false) +
NegPairMTF(two_up,two_down,i,two_isneg,true,false) +
NegPairMTF(three_up,three_down,i,three_isneg,true,false) +
NegPairMTF(four_up,four_down,i,four_isneg,true,false) +
NegPairMTF(five_up,five_down,i,five_isneg,true,false) +
NegPairMTF(six_up,six_down,i,six_isneg,true,false) +
NegPairMTF(seven_up,seven_down,i,seven_isneg,true,false);
c_mtf_str_down[i]=NegPairMTF(one_up,one_down,i,one_isneg,false,true) +
NegPairMTF(two_up,two_down,i,two_isneg,false,true) +
NegPairMTF(three_up,three_down,i,three_isneg,false,true) +
NegPairMTF(four_up,four_down,i,four_isneg,false,true) +
NegPairMTF(five_up,five_down,i,five_isneg,false,true) +
NegPairMTF(six_up,six_down,i,six_isneg,false,true) +
NegPairMTF(seven_up,seven_down,i,seven_isneg,false,true);
}
//--- compaire and send highest to main mtf strenght buffer
for (int i = 0; i <= 8; i++)
CSMTFCompare(c_mtf_str_main, c_mtf_str_up, c_mtf_str_down, i, mtf_str_main_pre, main_limit, shift);
}
5. I have another function that receives all this data but it creates a divide by zero error,which is ridiculous, please help!