如何获得移动平均线的角度? - 页 2

 
我的代码继续返回0的角度,你能不能贴出一些有背景的代码? 例如,找到10个周期的SMA 50的角度。
 
jretzloff:
由于我的代码继续返回一个0的角度,您能不能发布一些有背景的代码?例如,找到10个周期的SMA50的角度。
:) 你为什么不公布 你的代码,继续返回0 的角度,让其他人来帮助你?
 
DxdCn:
jretzloff:
由于我的代码继续返回0的角度,你能不能发布一些有背景的代码?例如,找到10个周期的SMA50的角度。
:) 你为什么不公布 你的代码,让其他人来帮助你?

基本上,因为它充满了废话,我一直在试图让它工作......充满了打印语句,等等。它是一个完全的黑客,试图测试计算,以便以后可能用于可视化。总之,它在这里。

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

不需要这么多的CDS!
在你的程序中。
MathArctan(MathTan(((price1-price2)/(WindowPriceMax()- WindowPriceMin()))/((SignalPeriod-0. 0)/WindowBarsPerChart())))*180/3.14;


SignalPeriod "的含义是什么,为什么?

你知道,一般来说,角度是一条线和X轴之间的关系,这条线是由两点定义的。
在你的计算中,price2和price1是在同一个X坐标上的两个值。

在我的公式中,使用(delt Y)/(delt X)来计算角度。
MathArctan(MathTan(
((price1-price2)/(WindowPriceMax()- WindowPriceMin()))// 是 delt Y
/
((shift2-shift1)/WindowBarsPerChart())//是delt X
))
*180/3.14

 

我知道这一直是我的问题,但我不明白如何应用移动 平均线。

 
这里,一个角度是直线和X轴之间的实数,
,一条直线由两点定义。
(price1,shift1), (price2,shift2)是这两个点的坐标。shift在你的代码中与x相同。
---------------------------------------------------
换句话说,如果你需要计算任何两条线的角度,你需要3或4个点(两条线需要3或4个点来定义),并且需要更多的三角函数 知识。
从你的代码中,我猜你想要计算任何两条线的角度(如MACD的两条线),而不是一条线和X轴的角度。
所以你需要3或4个点,应该多复习三角函数的知识,也许是余弦定律。
--------------------------
或者,首先,计算其中一条线与X轴的角度,其次,它们的差值就是这两条线的角度。
 

谢谢你的回答,我只想计算一条线的角度,即移动平均线 和X轴的角度。我对三角函数有足够的了解,可以进行计算,只是没有通过MT提供的东西。

,非常简单,我想计算移位0的MA的当前角度,第二个参考点是SignalPeriod的MA或更早的MA。另一个参考点是移位0的Y和SignalPeriod X的交点。

 
如果是这样,price2应该变为 。
price2 =iMA(Symbol(),0,MAPeriod,0, MODE_SMA,PRICE_CLOSE,SignalPeriod)。

X坐标(SignalPeriod)应该是iMA(....)函数的最后一个参数,而不是第四个参数。(第4个参数:ma_shift是另一个意思,除非你知道它是什么,否则不要使用它 !!!!)
现在好了,再试一次!
 
  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;