Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

trigonometry - How to Get Angle Between Two Prices

I am trying to calculate the angle between two prices on a chart. Essentially I would like to calculate the angle between a moving average price between two different previous days. I understand that I can draw a line on a chart by angle, but the angle between these points are different at two different zooms (naturally). This comes down to the inconsistent x co-ordinate. I have shared a couple images below for better understanding:

Zoom level 1

Zoom level 2

So as you can see the trend by angle remains constant, but the MA moves as you zoon (as the x-coord shifts). Now, I have tried with the below code using simple trigonomtery, but I don't think this is correct.

int getTrendSignal()
   {
      int dayCount1 = 1;
      int dayCount2 = dayCount1 + 29;
      double maPrice1, maPrice2;
      string mySymbol = Symbol();
      double opposite;
      int adjacent = 30;
      const double tangentFactor = 2891.53;
      double totalMAAngle = 0;
      
      for(int d = 1; d <= numberMonths; d++){
         //get prices
         maPrice1 = iMA(mySymbol, PERIOD_D1, 60, 0, MODE_EMA, PRICE_CLOSE, dayCount1);
         maPrice2 = iMA(mySymbol, PERIOD_D1, 60, 0, MODE_EMA, PRICE_CLOSE, dayCount1);
         
         if(maPrice1 > maPrice2){
            opposite = maPrice1 - maPrice2;
            
            totalMAAngle += MathArctan(adjacent / (opposite * tangentFactor));
         }
         if(maPrice1 < maPrice2){
            opposite = maPrice2 - maPrice1;
            
            totalMAAngle += 180 - MathArctan(adjacent / (opposite * tangentFactor));
         }
         
         //increment day count
         dayCount1 += 30;
         dayCount2 += 30;
      }
      
      totalMAAngle /=  numberMonths;
      printf("MA Angle: " + (string)(totalMAAngle));
      
      if(totalMAAngle>0 && totalMAAngle<=maxBullTrendAngle){ return 1;}
      if(totalMAAngle>=minBearTrendAngle && totalMAAngle<180){ return 0;}
      
      return -1;
   }

So I am not sure if I am taking the correct approach because if using trigonometry would require consistent units of measure. One length is a price differential and another is a time differential...

If anyone knows how to solve this, that would be helpful!

question from:https://stackoverflow.com/questions/65873284/how-to-get-angle-between-two-prices

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...