Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // hyrVWAP.mq4 ( 出来高加重平均価格 )
- // Copyright 2026 hyrolean
- //===========================================================================
- //----- properties -----
- #property copyright "2026, (c)hyrolean"
- #property link "https://pastebin.com/f2q02Pxd"
- #property version "1.00"
- #property strict
- #property indicator_chart_window
- #property indicator_buffers 1
- #property indicator_color1 clrOrange
- //----- parameter -----
- input int VWAPPeriod = 20 ; // VWAP period
- //----- buffer -----
- double VWAPBuffer[];
- //---------------------------------------------------------------------------
- int OnInit()
- {
- // number of indicator buffer
- IndicatorBuffers(1); // VWAPBuffer
- // setup the drawing style of primary buffer as line
- SetIndexStyle(0,DRAW_LINE);
- // setup buffer
- SetIndexBuffer(0, VWAPBuffer);
- // setup the indicator short name
- IndicatorShortName("VWAP("+string(VWAPPeriod)+")");
- return(INIT_SUCCEEDED);
- }
- //---------------------------------------------------------------------------
- int OnCalculate(const int rates_total,
- const int prev_calculated,
- const datetime &time[],
- const double &open[],
- const double &high[],
- const double &low[],
- const double &close[],
- const long& tick_volume[],
- const long& volume[],
- const int& spread[])
- {
- if(rates_total<VWAPPeriod) // バーが少なすぎる
- return(rates_total);
- int start=rates_total-prev_calculated-1;
- if(rates_total-start<VWAPPeriod) {
- start = rates_total-VWAPPeriod ;
- }
- for(int i=start ; i >= 0 ; i--) {
- double sumPriceVolume = 0;
- double sumVolume = 0;
- for(int j=VWAPPeriod-1 ; j>=0 ; j--) {
- int pos = i+j ;
- double price = (high[pos] + low[pos] + close[pos]) / 3.0; // 典型価格
- double vol = (double)tick_volume[pos];
- sumPriceVolume += price * vol ; // 典型価格 * ティックボリューム
- sumVolume += vol ;
- }
- if(sumVolume > 0)
- VWAPBuffer[i] = sumPriceVolume / sumVolume;
- else
- VWAPBuffer[i] = (high[i] + low[i] + close[i]) / 3.0;
- }
- return(rates_total);
- }
- //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment