rolling window with linear decreasing weights

Back to topics list To post a new topic, please log in or register
avatar
15
master_dude 2015.07.24 09:10
 

good day to you all!

attached code is of a 10 period rolling window with linear descreasing weights. this is my first and very rudimentary step towards implementing an adaptive feature into EAs so that parameter values adapt to changing market conditions with weight on more recent data.

however, it is not working properly. i initially wanted to use the switch() operator to set the parameters values (Vertical_Shift_Up/Down) based on the value of MA_Direction. but switch() only takes integer values. instead i decided to code these many lines of if() statements. but it only sets the value for Vertical_Shift_Up/Down at the onset of the EA and they never change.

anyone who can see what I have done wrong? the overall function itself is embedded in code that runs once pr new candle, and thus the aim is for the parameters Vertical_Shift_Up/Down to update values once pr candle.

the variables, MA_Direction andVertical_Shift_Up/Down have been initialized globally. the MA_Direction part of code is working fine...

void Linear_Decreasing_Window()
{
   double MA_Period1=iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,1) - iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,2);
   double MA_Period2=iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,2) - iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,3);
   double MA_Period3=iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,3) - iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,4);
   double MA_Period4=iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,4) - iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,5);
   double MA_Period5=iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,5) - iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,6);
   double MA_Period6=iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,6) - iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,7);
   double MA_Period7=iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,7) - iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,8);
   double MA_Period8=iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,8) - iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,9);
   double MA_Period9=iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,9) - iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,10);
   double MA_Period10=iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,10) - iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To,11);
      
   double Weight_Period1 = 0.19;
   double Weight_Period2 = 0.17;
   double Weight_Period3 = 0.15;
   double Weight_Period4 = 0.13;
   double Weight_Period5 = 0.11;
   double Weight_Period6 = 0.09;
   double Weight_Period7 = 0.07;
   double Weight_Period8 = 0.05;
   double Weight_Period9 = 0.03;
   double Weight_Period10 = 0.01;

   MA_Direction = (  
                        Weight_Period1*MA_Period1 +
                        Weight_Period2*MA_Period2 +
                        Weight_Period3*MA_Period3 +
                        Weight_Period4*MA_Period4 +
                        Weight_Period5*MA_Period5 +
                        Weight_Period6*MA_Period6 +
                        Weight_Period7*MA_Period7 +
                        Weight_Period8*MA_Period8 +
                        Weight_Period9*MA_Period9 +
                        Weight_Period10*MA_Period10
                   )    *1000;
      
   double X1=0.5,X2=0.4,X3=0.3,X4=0.2,X5=0.1,X6=0.05,X7=0.04,X8=0.03,X9=0.02,
   X10=0,X11=-0.02,X12=-0.03,X13=-0.04,X14=-0.05,X15=-0.1,X16=-0.2,X17=0.3,X18=-0.4,X19=-0.5;
      
   if(MA_Direction > X1) Vertical_Shift_Down = -20;
   if(X1 >= MA_Direction && MA_Direction > X2) Vertical_Shift_Down = -40;
   if(X2 >= MA_Direction && MA_Direction > X3) Vertical_Shift_Down = -60;
   if(X3 >= MA_Direction && MA_Direction > X4) Vertical_Shift_Down = -80;
   if(X4 >= MA_Direction && MA_Direction > X5) Vertical_Shift_Down = -100;
   if(X5 >= MA_Direction && MA_Direction > X6) Vertical_Shift_Down = -120;
   if(X6 >= MA_Direction && MA_Direction > X7) Vertical_Shift_Down = -140;
   if(X7 >= MA_Direction && MA_Direction > X8) Vertical_Shift_Down = -160;
   if(X8 >= MA_Direction && MA_Direction > X9) Vertical_Shift_Down = -180;
   if(X9 >= MA_Direction && MA_Direction > X10) Vertical_Shift_Down = -200;
   if(X10 >= MA_Direction && MA_Direction > X11) Vertical_Shift_Down = -220;
   if(X11 >= MA_Direction && MA_Direction > X12) Vertical_Shift_Down = -240;
   if(X12 >= MA_Direction && MA_Direction > X13) Vertical_Shift_Down = -260;
   if(X13 >= MA_Direction && MA_Direction > X14) Vertical_Shift_Down = -280;
   if(X14 >= MA_Direction && MA_Direction > X15) Vertical_Shift_Down = -300;
   if(X15 >= MA_Direction && MA_Direction > X16) Vertical_Shift_Down = -320;
   if(X16 >= MA_Direction && MA_Direction > X17) Vertical_Shift_Down = -340;
   if(X17 >= MA_Direction && MA_Direction > X18) Vertical_Shift_Down = -360;
   if(X18 >= MA_Direction && MA_Direction > X19) Vertical_Shift_Down = -380;
   if(X19 >= MA_Direction) Vertical_Shift_Down = 400;
      
   double Y1=0.5,Y2=0.4,Y3=0.3,Y4=0.2,Y5=0.1,Y6=0.05,Y7=0.04,Y8=0.03,Y9=0.02,
   Y10=0,Y11=-0.02,Y12=-0.03,Y13=-0.04,Y14=-0.05,Y15=-0.1,Y16=-0.2,Y17=0.3,Y18=-0.4,Y19=-0.5;
      
   if(MA_Direction > Y1) Vertical_Shift_Up = 20;
   if(Y1 >= MA_Direction && MA_Direction > Y2) Vertical_Shift_Up = 40;
   if(Y2 >= MA_Direction && MA_Direction > Y3) Vertical_Shift_Up = 60;
   if(Y3 >= MA_Direction && MA_Direction > Y4) Vertical_Shift_Up = 80;
   if(Y4 >= MA_Direction && MA_Direction > Y5) Vertical_Shift_Up = 100;
   if(Y5 >= MA_Direction && MA_Direction > Y6) Vertical_Shift_Up = 120;
   if(Y6 >= MA_Direction && MA_Direction > Y7) Vertical_Shift_Up = 140;
   if(Y7 >= MA_Direction && MA_Direction > Y8) Vertical_Shift_Up = 160;
   if(Y8 >= MA_Direction && MA_Direction > Y9) Vertical_Shift_Up = 180;
   if(Y9 >= MA_Direction && MA_Direction > Y10) Vertical_Shift_Up = 200;
   if(Y10 >= MA_Direction && MA_Direction > Y11) Vertical_Shift_Up = 220;
   if(Y11 >= MA_Direction && MA_Direction > Y12) Vertical_Shift_Up = 240;
   if(Y12 >= MA_Direction && MA_Direction > Y13) Vertical_Shift_Up = 260;
   if(Y13 >= MA_Direction && MA_Direction > Y14) Vertical_Shift_Up = 280;
   if(Y14 >= MA_Direction && MA_Direction > Y15) Vertical_Shift_Up = 300;
   if(Y15 >= MA_Direction && MA_Direction > Y16) Vertical_Shift_Up = 320;
   if(Y16 >= MA_Direction && MA_Direction > Y17) Vertical_Shift_Up = 340;
   if(Y17 >= MA_Direction && MA_Direction > Y18) Vertical_Shift_Up = 360;
   if(Y18 >= MA_Direction && MA_Direction > Y19) Vertical_Shift_Up = 380;
   if(Y19 >= MA_Direction) Vertical_Shift_Down = 400;

}
avatar
12985
WHRoeder 2015.07.24 15:08 #
 

master_dude:

however, it is not working properly.

at the onset of the EA and they never change.

  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. We can't see where you call your function. There are no mind readers here.
  3. Learn about arrays:
    void Linear_Decreasing_Window()
    {
       static double Weights[]{0.19, 0.17, 0.15, 0.13, 0.11, 
                               0.09, 0.07, 0.05, 0.03, 0.01};
       MA_Direction = 0;
       for(int i=0; i < ArraySize(Weights); ++i){
          double ma1  = iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To, i+1);
          double ma2  = iMA(NULL,0,2,MA_Shift,MA_Method,MA_Applied_To, i+2);
          MA_Direction += Weights[i]* (ma1 - ma2);
       }
    
       static double X[]={ +0.5, +0.4, +0.3, +0.2, +0.1, +0.05, +0.04, +0.03, +0.02, 
                           +0,
                          -0.02, -0.03, -0.04, -0.05, -0.1, -0.2, 0.3, -0.4, -0.5};
       static int down[]={-20, -40, -60, -80, -100, -120, -140, -160, -180, -200,
                          -220, -240, -260, -280, -300, -320, -340, -360, -380, 400}
       for(int i=0; i < ArraySize(X); ++i)
          if(X[i] >= MA_Direction) Vertical_Shift_Down = down[i];
    
       static double Y[]={ 0.5, 0.4, 0.3, 0.2, 0.1, 0.05, 0.04, 0.03, 0.02, 0,
                          -0.02, -0.03, -0.04, -0.05, -0.1, -0.2, 0.3, -0.4, -0.5}
       static int up[]={20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260,
                        280, 300, 320, 340, 360, 380, 400}
       for(int i=0; i < ArraySize(Y); ++i)
          if(Y[i] >= MA_Direction) Vertical_Shift_Up = up[i];
    }
    Questionable down[i] could be simplified to -20*(i+1).
avatar
15
master_dude 2015.07.25 12:24 #
 
WHRoeder:

master_dude:

however, it is not working properly.

at the onset of the EA and they never change.

  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. We can't see where you call your function. There are no mind readers here.
  3. Learn about arrays:Questionable down[i] could be simplified to -20*i.
thanks again WHRoeder. this was extremely helpful. the code you wrote gave me the chance to learn a lot about arrays and how to use for loops better. truly appreciated!
Back to topics list  

To add comments, please log in or register