How to get the angle of a moving average? - page 2

 
Could you please post some code with context as my code continues to return an angle of 0?  For example, find angle of SMA 50 over 10 periods.
 
jretzloff:
Could you please post some code with context as my code continues to return an angle of 0? For example, find angle of SMA 50 over 10 periods.
:) why do you not post your code continues to return an angle of 0 for other to help you ?
 
DxdCn:
jretzloff:
Could you please post some code with context as my code continues to return an angle of 0? For example, find angle of SMA 50 over 10 periods.
:) why do you not post your code continues to return an angle of 0 for other to help you ?

Basically, because it is full of crap i have been trying to get it to work with. ..full of print statements, etc.. It is a total hack to try and test computation for possible use in visualization later. Anyway, here it is:

//+------------------------------------------------------------------+
//|                                                  Angle of MA.mq4 |
//|                                Copyright © 2007,                 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007"
#property link      ""
 
#property indicator_separate_window
#property indicator_minimum -60.0
#property indicator_maximum 60.0
#property indicator_buffers 1
#property indicator_color1 Lime
#property indicator_width1 3
/*#property indicator_color2 Red
#property indicator_width2 3
*/
 
extern int MAPeriod = 50;
extern int SignalPeriod = 10;
 
double posAngle[], negAngle[];
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
 
   IndicatorBuffers(1);
   
   SetIndexBuffer(1, posAngle);
   //SetIndexStyle(1, DRAW_ARROW);
   SetIndexStyle(1, DRAW_HISTOGRAM);
   //SetIndexArrow(1, 110);
   SetIndexLabel(1, "Positive Angle");
   SetIndexEmptyValue(1, 0.0);
   
   /*SetIndexBuffer(2, negAngle);
   //SetIndexStyle(2, DRAW_ARROW);
   SetIndexStyle(2, DRAW_HISTOGRAM);
   //SetIndexArrow(2, 110);
   SetIndexLabel(2, "Negative Angle");
   SetIndexEmptyValue(2, 0.0);*/
   
   ArrayInitialize(posAngle, 0.0);
   //ArrayInitialize(negAngle, 0.0);
 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0) 
       return(-1);
   if(counted_bars > 0) 
       counted_bars--;
   int limit = Bars - counted_bars; 
   
   double angle = 0.0;
   double price1 = 0.0, price2 = 0.0;
//----
   for(int x = 0; x < limit; x++) 
   {
      //if (x >= MAPeriods) 
      //{
         angle = 0.0;
         price1 = iMA(Symbol(),0,MAPeriod,0, MODE_SMA,PRICE_CLOSE,0);
         price2 = iMA(Symbol(),0,MAPeriod,SignalPeriod, MODE_SMA,PRICE_CLOSE,0);
         double test = (SignalPeriod-0.0)/WindowBarsPerChart();
         //Print("test: ", test);
         Print("Price1/2 ", price1, "/", price2, " angle->", angle);
         Print("price1-price2: ", price1-price2);
         //Print("WindowPriceMin(): ", WindowPriceMin());
         //Print("WindowPriceMax(): ", WindowPriceMax());
         //Print("WindowPriceMax()- WindowPriceMin(): ", WindowPriceMax()- WindowPriceMin());
         //Print("WindowBarsPerChart(): ", WindowBarsPerChart());
         //Print("SignalPeriod: ", SignalPeriod);
         //Print("(SignalPeriod-0)/WindowBarsPerChart()): ", (SignalPeriod-0.0)/WindowBarsPerChart());
         
         
         if (price1-price2 > 0)
            angle = MathArctan(MathTan(((price1-price2)/(WindowPriceMax()- WindowPriceMin()))/((SignalPeriod-0.0)/WindowBarsPerChart())))*180/3.14; 
         else
            angle = 0.0;
            
         Print("Angle > 0: ", angle>0.0);
         Print("Angle < 0: ", angle<0.0);
         
         if (angle > 0.0)
         {
            Print("+++++++++++++++++++ ANGLE +++++++++++++++++++");
            posAngle[x] = angle;
            //negAngle[x] = 0.0;
         }
         else if ( angle < 0.0)
         {
            Print("------------------- ANGLE -------------------");
            //negAngle[x] = angle;
            posAngle[x] = 0.0;  
         }
         else // some error occurred
         {
            Print("******************* ANGLE *******************");
            posAngle[x] = 0.0;
            //negAngle[x] = 0.0;
         }
         
      /*}
      else
      {
         posAngle[x] = 0.0;
         negAngle[x] = 0.0;
      }*/
      Print("posAngle[x]: ", posAngle[x]);
      Print("negAngle[x]: ", negAngle[x]);
      Print("Angle [", TimeToStr(Time[x], TIME_DATE|TIME_MINUTES), "] ---------------------------------------------> ", angle);
   }
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

NO need so many cdes!
In you cade:
MathArctan(MathTan(((price1-price2)/(WindowPriceMax()- WindowPriceMin()))/((SignalPeriod-0. 0)/WindowBarsPerChart())))*180/3.14;


What is the meaning of "SignalPeriod" and why?

You know,generally, an angle is the realtion betweent a line and X axes, that line is defined by two points.
In your calculation , price2 and price1 are two values at same X coordinate,

In my formula, use (delt Y) / (delt X) to calculate angle :
MathArctan(MathTan(
((price1-price2)/(WindowPriceMax()- WindowPriceMin())) // is delt Y
/
((shift2-shift1)/WindowBarsPerChart()) // is delt X
))
*180/3.14

 

what would be the values for shift1 and shift2? or where are they calculated from? i know this has been my problem but don't understand how to apply with the moving average.

 
here, an angle is the realtion betweent a line and X axes,
a line is defined by two points.
(price1,shift1), (price2,shift2) are that two points' coordinate. shift same as x in you code.
---------------------------------------------------
other words, if you need to calculate the angle of any two lines, you need 3 or 4 points ( two line need 3 or 4 points to define) , and need more knowlesge of trigonometric functions.
from you code , I guess you want to calculate the angle of any two lines (like two lines of MACD), not the angle of one line and X axes.
So you need 3 or 4 points , should review more knowlesge of trigonometric functions, maybe law of cosines.
--------------------------
Or , 1st , calculate each angle of one of lines and X axes, 2nd, their difference is the angle of that two lines .
 

thank you for your response, i am only trying to calculate the angle of a single line, i.e. a moving average and the x-axis. i do know enough about trig to perform the calculations, just not with what is available through MT.

very simply, i would like to calculate the current angle of the MA at shift 0 with the second reference point being the MA at SignalPeriod or MA at ? bars earlier. the other reference point would be the intersect of the shift 0 y and the SignalPeriod x.

 
If so, price2 should change to :
price2 = iMA(Symbol(),0,MAPeriod,0, MODE_SMA,PRICE_CLOSE,SignalPeriod);

X coordinate (SignalPeriod) should be last parameter of iMA(....) function, not 4th parather. (4th parather: ma_shift is another meaning, not use it except you know what it is !!!!)
Now Ok, try again!
 
  Whats Wrong with this code ? ? ? 
  I am trying to 4 angles but I keep getting a divide 0 error ? 
  Thanks, 
  KK
  
 
  VectorPer = 16;
 
  HighStartPoint       = iHigh(Symbol(),0,VectorPer);
  PreviousBarHigh      = iHigh(Symbol(),0,SIGNALCANDLE+1);
  HighestPoint         = High[iHighest(Symbol(),0,MODE_HIGH,VectorPer,SIGNALCANDLE+1)];
  HighestAngle         = MathArctan(MathTan(((HighStartPoint-HighestPoint)/(WindowPriceMax()- WindowPriceMin()))/((VectorPer-SIGNALCANDLE+1)/WindowBarsPerChart())))*180/3.14;
  PreviousHighBarAngle = MathArctan(MathTan(((HighStartPoint-PreviousBarHigh)/(WindowPriceMax()- WindowPriceMin()))/((VectorPer-SIGNALCANDLE+1)/WindowBarsPerChart())))*180/3.14;
 
  pHighStartPoint       = iHigh(Symbol(),0,VectorPer+1);
  pPreviousBarHigh      = iHigh(Symbol(),0,SIGNALCANDLE+2);
  pHighestPoint         = High[iHighest(Symbol(),0,MODE_HIGH,VectorPer+1,SIGNALCANDLE+2)];
  pHighestAngle         = MathArctan(MathTan(((pHighStartPoint-pHighestPoint)/(WindowPriceMax()- WindowPriceMin()))/((VectorPer+1-SIGNALCANDLE+2)/WindowBarsPerChart())))*180/3.14;
  pPreviousHighBarAngle = MathArctan(MathTan(((pHighStartPoint-pPreviousBarHigh)/(WindowPriceMax()- WindowPriceMin()))/((VectorPer+1-SIGNALCANDLE+2)/WindowBarsPerChart())))*180/3.14;
  
  LowStartPoint        = iLow(Symbol(),0,VectorPer);
  PreviousBarLow       = iLow(Symbol(),0,SIGNALCANDLE);
  LowestPoint          = Low[iLowest(Symbol(),0,MODE_LOW,VectorPer,SIGNALCANDLE+1)];
  LowestAngle          = MathArctan(MathTan(((LowStartPoint-LowestPoint)/(WindowPriceMax()- WindowPriceMin()))/((VectorPer-SIGNALCANDLE+1)/WindowBarsPerChart())))*180/3.14;
  PreviousLowBarAngle  = MathArctan(MathTan(((LowStartPoint-PreviousBarLow)/(WindowPriceMax()- WindowPriceMin()))/((VectorPer-SIGNALCANDLE+1)/WindowBarsPerChart())))*180/3.14;
 
  pLowStartPoint        = iLow(Symbol(),0,VectorPer+1);
  pPreviousBarLow       = iLow(Symbol(),0,SIGNALCANDLE+2);
  pLowestPoint          = Low[iLowest(Symbol(),0,MODE_LOW,VectorPer,SIGNALCANDLE+2)];
  pLowestAngle          = MathArctan(MathTan(((pLowStartPoint-pLowestPoint)/(WindowPriceMax()- WindowPriceMin()))/((VectorPer+1-SIGNALCANDLE+2)/WindowBarsPerChart())))*180/3.1415;
  pPreviousLowBarAngle  = MathArctan(MathTan(((pLowStartPoint-pPreviousBarLow)/(WindowPriceMax()- WindowPriceMin()))/((VectorPer+1-SIGNALCANDLE+2)/WindowBarsPerChart())))*180/3.1415;
 
Reason: