HYROLEAN

hyrVWAP.mq4

Jan 23rd, 2026 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | Money | 0 0
  1. // hyrVWAP.mq4 ( 出来高加重平均価格 )
  2. //  Copyright 2026 hyrolean
  3. //===========================================================================
  4. //----- properties -----
  5. #property copyright "2026, (c)hyrolean"
  6. #property link "https://pastebin.com/f2q02Pxd"
  7. #property version "1.00"
  8. #property strict
  9.  
  10. #property indicator_chart_window
  11. #property indicator_buffers 1
  12. #property indicator_color1  clrOrange
  13.  
  14. //----- parameter -----
  15. input int VWAPPeriod = 20 ;           // VWAP period
  16.  
  17. //----- buffer -----
  18. double VWAPBuffer[];
  19.  
  20. //---------------------------------------------------------------------------
  21. int OnInit()
  22. {
  23.    // number of indicator buffer
  24.    IndicatorBuffers(1); // VWAPBuffer
  25.    // setup the drawing style of primary buffer as line
  26.    SetIndexStyle(0,DRAW_LINE);
  27.    // setup buffer
  28.    SetIndexBuffer(0, VWAPBuffer);
  29.    // setup the indicator short name
  30.    IndicatorShortName("VWAP("+string(VWAPPeriod)+")");
  31.    return(INIT_SUCCEEDED);
  32. }
  33. //---------------------------------------------------------------------------
  34. int OnCalculate(const int rates_total,
  35.                 const int prev_calculated,
  36.                 const datetime &time[],
  37.                 const double &open[],
  38.                 const double &high[],
  39.                 const double &low[],
  40.                 const double &close[],
  41.                 const long& tick_volume[],
  42.                 const long& volume[],
  43.                 const int& spread[])
  44. {
  45.   if(rates_total<VWAPPeriod) // バーが少なすぎる
  46.     return(rates_total);
  47.  
  48.   int start=rates_total-prev_calculated-1;
  49.   if(rates_total-start<VWAPPeriod) {
  50.     start = rates_total-VWAPPeriod ;
  51.   }
  52.  
  53.   for(int i=start ; i >= 0 ; i--) {
  54.     double sumPriceVolume = 0;
  55.     double sumVolume = 0;
  56.     for(int j=VWAPPeriod-1 ; j>=0 ; j--) {
  57.       int pos = i+j ;
  58.       double price = (high[pos] + low[pos] + close[pos]) / 3.0; // 典型価格
  59.       double vol = (double)tick_volume[pos];
  60.       sumPriceVolume += price * vol ; // 典型価格 * ティックボリューム
  61.       sumVolume += vol ;
  62.     }
  63.     if(sumVolume > 0)
  64.        VWAPBuffer[i] = sumPriceVolume / sumVolume;
  65.     else
  66.        VWAPBuffer[i] = (high[i] + low[i] + close[i]) / 3.0;
  67.   }
  68.  
  69.   return(rates_total);
  70. }
  71. //---------------------------------------------------------------------------
  72.  
Tags: Mt4
Advertisement
Add Comment
Please, Sign In to add comment