Page 1 of 1

Function Collection (MQ4)

PostPosted: Sat Oct 22, 2016 6:10 am
by Apprentice
Here i will, u can post the functions that you can use as part of your own implementation.

MathTanh

PostPosted: Sat Oct 22, 2016 6:11 am
by Apprentice
Code: Select all
double MathTanh(double x)
{
   double exp;
   double returnNum;
   if(x>0)
     {
       exp=MathExp(-2*x);
       returnNum= (1-exp)/(1+exp);
       return (returnNum);
     }
   else
     {
       exp=MathExp(2*x);
       returnNum=(exp-1)/(1+exp);
       return (returnNum);
     }
}

Moving all elements up one place in an array

PostPosted: Tue Dec 25, 2018 12:56 am
by skinner36
Moving array elements up one value to insert a new value at the beginning of the array can be a bit tedious to code. If the array has been switched around so that element 0 is the right most element then an extra degree of frustration is added to the issue.

This is how I solved the problem for my current project.

Let’s assume we have a dynamic array of 100 structures elements and the rightmost element is 0.
Code: Select all
struct TestStruct
{
   int     value1;
   double value2;
   string value3;
};

TestStruct Test[]

ArrayResize(Test, 100);      // Give it a size of 100

ArraySetAsSeries(Test, true); // Turn the array around so the element 0 is the far right one


Now, your program has been working as you intended but the array is now full of data and you want to add the new element to the beginning of the array at element 0 (farthest right element) and move all of the other element values up one and drop the old 100th element off.

The easiest way to do this that I have found is as follows.

Code: Select all
    // Set the array to read from left to right as a normal array does
    ArraySetAsSeries(Test, false);

     // Add a new empty element to the end
    ArrayResize(Test, 101);

    // Set the array to read from right to left similar to the bars on a chart
    ArraySetAsSeries(Test, true);

    // Now the array has been turned around delete the last element   
    ArrayResize(Test, 100);


Done

Cheers,

John