NexusFi: Find Your Edge


Home Menu

 





NonLinearRegression.mq4 Transform to Easy Language


Discussion in EasyLanguage Programming

Updated
    1. trending_up 1,519 views
    2. thumb_up 0 thanks given
    3. group 1 followers
    1. forum 1 posts
    2. attach_file 1 attachments




 
Search this Thread
  #1 (permalink)
cowbearcar
Hong Kong, China
 
Posts: 14 since Apr 2017
Thanks Given: 0
Thanks Received: 1

 
Code
//+------------------------------------------------------------------+
//|                                          NonLinearRegression.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Green
#property indicator_color2 Orange
#property indicator_color3 Orange
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2

//
//
//
//
//

extern int  NLRLength = 20;
extern int  NLRPrice  = PRICE_CLOSE;

//
//
//
//
//

double nlrBuffer[];
double ndaBuffer[];
double ndbBuffer[];
double trnBuffer[];


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int init()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,nlrBuffer);
   SetIndexBuffer(1,ndaBuffer);
   SetIndexBuffer(2,ndbBuffer);
   SetIndexBuffer(3,trnBuffer);
   
   //
   //
   //
   //
   //
   
   NLRLength = MathMax(MathMin(NLRLength,500),2);
   return(0);
}
int deinit() { return(0); }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int start()
{
   int    counted_bars=IndicatorCounted();
   int    limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);

   //
   //
   //
   //
   //

   if (trnBuffer[limit] == -1) CleanPoint(limit,ndaBuffer,ndbBuffer);
      for(int i=limit; i>=0; i--)
      {
         nlrBuffer[i] = NLR(NLRLength, NLRPrice, i, 0);
         ndaBuffer[i] = EMPTY_VALUE;
         ndbBuffer[i] = EMPTY_VALUE;
         trnBuffer[i] = trnBuffer[i+1];
      
         //
         //
         //
         //
         //
            
         if (nlrBuffer[i] > nlrBuffer[i+1]) trnBuffer[i] =  1;
         if (nlrBuffer[i] < nlrBuffer[i+1]) trnBuffer[i] = -1;
            if (trnBuffer[i] == -1) PlotPoint(i,ndaBuffer,ndbBuffer,nlrBuffer);
      }
   return(0);
}

  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

double YValue[];
double XValue[];
double XM,YM,XM2;
int    arraySize=-1;

//
//
//
//
//

void setArrays(int Length)
{
   arraySize = Length;
      ArrayResize(YValue,Length);
      ArrayResize(XValue,Length);
}

//
//
//
//
//

double NLR(int Length,int AppliedPrice,int shift,int desiredBar)
{
   Length = MathMin(Length,Bars-shift);
      if (Length<1) return(EMPTY_VALUE);
      if (arraySize!=Length) setArrays(Length);
   
   //
   //
   //
   //
   //
   
   double AvgX = 0.0;
   double AvgY = 0.0;

   for(int i=0;i<Length;i++)
      {
         XValue[i] = i;
         YValue[i] = iMA(NULL,0,1,0,MODE_SMA,AppliedPrice,shift+i);
            AvgX  += XValue[i];
            AvgY  += YValue[i];
      }
      AvgX /= Length;
      AvgY /= Length;

      //
      //
      //
      //
      //
      
      double SXX   = 0;
      double SXY   = 0;
      double SYY   = 0;
      double SXX2  = 0;
      double SX2X2 = 0;
      double SYX2  = 0;

      for(i=0;i<Length;i++)
      {
         XM    = XValue[i] - AvgX; 
         YM    = YValue[i] - AvgY;
         XM2   = XValue[i] * XValue[i] - AvgX*AvgX;
         SXX   += XM*XM;
         SXY   += XM*YM;
         SYY   += YM*YM;
         SXX2  += XM*XM2;
         SX2X2 += XM2*XM2;
         SYX2  += YM*XM2;
      }         

      //
      //
      //
      //
      //
      
      double tmp;
      double ACoeff=0;
      double BCoeff=0;
      double CCoeff=0;

      tmp = SXX*SX2X2 - SXX2*SXX2;
      if (tmp!=0) {
         BCoeff = ( SXY*SX2X2 - SYX2*SXX2 ) / tmp;
         CCoeff = ( SXX*SYX2  - SXX2*SXY )  / tmp;
      }
      ACoeff = AvgY   - BCoeff*AvgX       - CCoeff*AvgX*AvgX;
      tmp    = ACoeff + BCoeff*desiredBar + CCoeff*desiredBar*desiredBar;

   //
   //
   //
   //
   //
   
   return(tmp);      
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

//
//
//
//
//

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (first[i+1] == EMPTY_VALUE)
      {
      if (first[i+2] == EMPTY_VALUE) {
          first[i]    = from[i];
          first[i+1]  = from[i+1];
          second[i]   = EMPTY_VALUE;
         }
      else {
          second[i]   = from[i];
          second[i+1] = from[i+1];
          first[i]    = EMPTY_VALUE;
         }
      }
   else
      {
         first[i]   = from[i];
         second[i]  = EMPTY_VALUE;
      }
}
I got problem on transform mq4 into algo version.
Error prompt: Array make fail to change old value etc.

How to transform those type of mq4?

Besides, I also want to some extra MA into Algo version.
Such as LWMA, LSMA,

From the above indicator, it only use SMA 20 to make new non-repaint trend
I need further test on others MA method even for HMA etc.

The attached photo show how it work on EURUSD Hour chart.
This is worth to transform into Algo version.

Thank you.


Attached Thumbnails
Click image for larger version

Name:	20180720_EURUSD_hour.png
Views:	300
Size:	45.0 KB
ID:	252147  
Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Warsh Rate Hike at 40%, Iran June 15 Expires Tonight at …
Prediction Markets & Event Contracts
Second Night of US Strikes Crushes Iran June 15 to 3.6% …
Prediction Markets & Event Contracts
Fabrication or Framework? Irans Denied MOU Explains the …
Prediction Markets & Event Contracts
Bookmap Global Plus Lifetime + Lifetime Addons For Sale
Platforms and Indicators
June 15 Peace Odds Surge From 3.6% to 12.25% After Trump …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
13 thanks
Darmok and Jalad at Tanagra
3 thanks
Big Mike in Ecuador
1 thanks
30 Sessions
1 thanks




Last Updated on July 21, 2018


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts