MT4 Coding Help Needed, Please see the snippets

everything that doesn't belong elsewhere cometh here

Moderator: moderators

User avatar
paulcarissimo
rank: <50 posts
rank: <50 posts
Posts: 7
Joined: Mon Mar 25, 2019 11:28 pm
Reputation: 1
Location: Kampala, Uganda
Gender: Male

MT4 Coding Help Needed, Please see the snippets

Postby paulcarissimo » 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

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!
"Learning is Like Rowing Upstream; Not to Advance is to Fall Back"
学習は上流漕ぎのようなものです。前進しないことは後退することです

Please add www.kreslik.com to your ad blocker white list.
Thank you for your support.

User avatar
TheRumpledOne
rank: 10000+ posts
rank: 10000+ posts
Posts: 13425
Joined: Sun May 14, 2006 9:31 pm
Reputation: 1831
Location: Oregon
Real name: Avery T. Horton, Jr.
Gender: None specified
Contact:

Re: MT4 Coding Help Needed, Please see the snippets

Postby TheRumpledOne » Mon Apr 08, 2019 3:35 pm

Upload the indicator in entirety and perhaps I can figure out the error.

Also, look in the journal, I think it will tell you what line caused the error.

I don't even see a divide in your code!!
IT'S NOT WHAT YOU TRADE, IT'S HOW YOU TRADE IT!

Please do NOT PM me with trading or coding questions, post them in a thread.

User avatar
paulcarissimo
rank: <50 posts
rank: <50 posts
Posts: 7
Joined: Mon Mar 25, 2019 11:28 pm
Reputation: 1
Location: Kampala, Uganda
Gender: Male

Re: MT4 Coding Help Needed, Please see the snippets

Postby paulcarissimo » Mon Apr 08, 2019 9:48 pm

TheRumpledOne wrote:Upload the indicator in entirety and perhaps I can figure out the error.

Also, look in the journal, I think it will tell you what line caused the error.

I don't even see a divide in your code!!


This is the code where the zero divide occurs, this code receives the output of "CSMTFStrenght" and expresses it as a percentage

Code: Select all

string CSSort(double current_Str, double pre_Str, double strenght_up, double strenght_down)
{
    string retString      = "";
    double total_strenght = strenght_up + strenght_down;
    double _up            = strenght_up / total_strenght * 100;
    double _down          = strenght_down / total_strenght * 100;
    if (strenght_up > strenght_down)
        retString = DoubleToString(_up, 1) ;//+ PME(current_Str, pre_Str);                 
    if (strenght_up < strenght_down)
        retString = DoubleToString(_down, 1);// + PME(current_Str, pre_Str);             
    if (strenght_up == strenght_down)
        retString =DoubleToString(_down, 1);// "EVEN";
    return retString;
}


However, the problem is not the function above, but this one below;

Code: Select all

 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);
                       
                 
           }



For proprietary reasons, I cant upload the full code [-X , its not the code, but the algorithm.
"Learning is Like Rowing Upstream; Not to Advance is to Fall Back"
学習は上流漕ぎのようなものです。前進しないことは後退することです

User avatar
paulcarissimo
rank: <50 posts
rank: <50 posts
Posts: 7
Joined: Mon Mar 25, 2019 11:28 pm
Reputation: 1
Location: Kampala, Uganda
Gender: Male

Re: MT4 Coding Help Needed, Please see the snippets

Postby paulcarissimo » Mon Apr 08, 2019 10:43 pm

Okay, I think I figured it out, however Id like to see your solution anyway.
Thanks for your time TRO!
"Learning is Like Rowing Upstream; Not to Advance is to Fall Back"
学習は上流漕ぎのようなものです。前進しないことは後退することです

User avatar
TheRumpledOne
rank: 10000+ posts
rank: 10000+ posts
Posts: 13425
Joined: Sun May 14, 2006 9:31 pm
Reputation: 1831
Location: Oregon
Real name: Avery T. Horton, Jr.
Gender: None specified
Contact:

Re: MT4 Coding Help Needed, Please see the snippets

Postby TheRumpledOne » Tue Apr 09, 2019 5:58 pm

paulcarissimo wrote:Okay, I think I figured it out, however Id like to see your solution anyway.
Thanks for your time TRO!



Proper coding is:

IF DENOMINATOR <> 0 THEN DIVIDE ELSE *ERROR DENOMINATOR IS ZERO*

Failure to code in this way can crash critical systems while you are asleep causing you to be awakened by a frantic caller.
IT'S NOT WHAT YOU TRADE, IT'S HOW YOU TRADE IT!



Please do NOT PM me with trading or coding questions, post them in a thread.

Please add www.kreslik.com to your ad blocker white list.
Thank you for your support.

User avatar
paulcarissimo
rank: <50 posts
rank: <50 posts
Posts: 7
Joined: Mon Mar 25, 2019 11:28 pm
Reputation: 1
Location: Kampala, Uganda
Gender: Male

Re: MT4 Coding Help Needed, Please see the snippets

Postby paulcarissimo » Tue Apr 09, 2019 8:12 pm

TheRumpledOne wrote:
paulcarissimo wrote:Okay, I think I figured it out, however Id like to see your solution anyway.
Thanks for your time TRO!



Proper coding is:

IF DENOMINATOR <> 0 THEN DIVIDE ELSE *ERROR DENOMINATOR IS ZERO*

Failure to code in this way can crash critical systems while you are asleep causing you to be awakened by a frantic caller.


I'm aware of this workaround, I would have used it and not asked for help but the point is that the functions never return a zero value. This only happened after using the "NegPairMTF" function. In any case , thanks for your help TRO :)
"Learning is Like Rowing Upstream; Not to Advance is to Fall Back"
学習は上流漕ぎのようなものです。前進しないことは後退することです

User avatar
TheRumpledOne
rank: 10000+ posts
rank: 10000+ posts
Posts: 13425
Joined: Sun May 14, 2006 9:31 pm
Reputation: 1831
Location: Oregon
Real name: Avery T. Horton, Jr.
Gender: None specified
Contact:

Re: MT4 Coding Help Needed, Please see the snippets

Postby TheRumpledOne » Wed Apr 10, 2019 6:03 pm

paulcarissimo wrote:
TheRumpledOne wrote:
paulcarissimo wrote:Okay, I think I figured it out, however Id like to see your solution anyway.
Thanks for your time TRO!



Proper coding is:

IF DENOMINATOR <> 0 THEN DIVIDE ELSE *ERROR DENOMINATOR IS ZERO*

Failure to code in this way can crash critical systems while you are asleep causing you to be awakened by a frantic caller.


I'm aware of this workaround, I would have used it and not asked for help but the point is that the functions never return a zero value. This only happened after using the "NegPairMTF" function. In any case , thanks for your help TRO :)


NEVER SAY NEVER WHEN CODING!!

You ALWAYS test. The alternative is to get a frantic call that the system crashed when you are asleep.
IT'S NOT WHAT YOU TRADE, IT'S HOW YOU TRADE IT!



Please do NOT PM me with trading or coding questions, post them in a thread.

User avatar
paulcarissimo
rank: <50 posts
rank: <50 posts
Posts: 7
Joined: Mon Mar 25, 2019 11:28 pm
Reputation: 1
Location: Kampala, Uganda
Gender: Male

Re: MT4 Coding Help Needed, Please see the snippets

Postby paulcarissimo » Thu Apr 11, 2019 5:10 pm

TheRumpledOne wrote:
paulcarissimo wrote:
TheRumpledOne wrote:

Proper coding is:

IF DENOMINATOR <> 0 THEN DIVIDE ELSE *ERROR DENOMINATOR IS ZERO*

Failure to code in this way can crash critical systems while you are asleep causing you to be awakened by a frantic caller.


I'm aware of this workaround, I would have used it and not asked for help but the point is that the functions never return a zero value. This only happened after using the "NegPairMTF" function. In any case , thanks for your help TRO :)


NEVER SAY NEVER WHEN CODING!!

You ALWAYS test. The alternative is to get a frantic call that the system crashed when you are asleep.


LOL TRO, I get the point , that's a very big font size :mrgreen:
"Learning is Like Rowing Upstream; Not to Advance is to Fall Back"
学習は上流漕ぎのようなものです。前進しないことは後退することです

Please add www.kreslik.com to your ad blocker white list.
Thank you for your support.


Return to “general”