HYROLEAN

hyrKagiZag.mq4

Jan 10th, 2026 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.75 KB | Money | 0 0
  1. // hyrKagiZag.mq4
  2. //  Copyright 2026 hyrolean
  3. //===========================================================================
  4. //----- properties -----
  5. #property copyright "2026, (c)hyrolean"
  6. #property link "https://pastebin.com/5j7EjYEp"
  7. #property version "1.12"
  8. #property strict
  9.  
  10.   // version 1.12
  11.   //  - added ArraySetAsSeries() codes to OnCalculate()
  12.  
  13.   // version 1.11
  14.   //  - bugfix on updating new extremum values among previous-current timelines
  15.  
  16.   // version 1.10
  17.   //  - supported the feature of progressive shift price multiplied by ATR
  18.  
  19. #property indicator_chart_window
  20. #property indicator_buffers 1
  21. #property indicator_color1  clrYellow
  22.  
  23. //----- parameters -----
  24. input double ShiftPrice = 100 ;      // Shift price in points
  25. input bool ATRMultiplying = true ;   // ATR shift price multiplying enabled
  26. input double ATRMultiplier = 0.025 ; // ATR shift price multiplier
  27. input int ATRPeriod = 14 ;           // ATR period
  28.  
  29. //----- buffers -----
  30. double ExtKagizagBuffer[];
  31. double ExtHighBuffer[];
  32. double ExtLowBuffer[];
  33.  
  34. //----- global -----
  35. int ExtLevel=3;
  36.  
  37. //---------------------------------------------------------------------------
  38. int OnInit()
  39. {
  40.    // number of indicator buffers
  41.    IndicatorBuffers(3); // Kagizag, High, Low
  42.    // setup the drawing style of primary buffer as section
  43.    SetIndexStyle(0,DRAW_SECTION);
  44.    // setup buffers
  45.    SetIndexBuffer(0,ExtKagizagBuffer);
  46.    SetIndexBuffer(1,ExtHighBuffer);
  47.    SetIndexBuffer(2,ExtLowBuffer);
  48.    SetIndexEmptyValue(0,0.0);
  49.    // setup the indicator short name
  50.    if(ATRMultiplying) {
  51.      IndicatorShortName("KagiZag("+string(ShiftPrice)+","
  52.        +string(ATRMultiplier)+","+string(ATRPeriod)+")");
  53.    }else {
  54.      IndicatorShortName("KagiZag("+string(ShiftPrice)+")");
  55.    }
  56.  
  57.    return(INIT_SUCCEEDED);
  58. }
  59. //---------------------------------------------------------------------------
  60. int OnCalculate(const int rates_total,
  61.                 const int prev_calculated,
  62.                 const datetime &time[],
  63.                 const double &open[],
  64.                 const double &high[],
  65.                 const double &low[],
  66.                 const double &close[],
  67.                 const long& tick_volume[],
  68.                 const long& volume[],
  69.                 const int& spread[])
  70. {
  71.   int i, pos, opos=-1, numExt, extMode=0 ;
  72.   double curLow=0.0, curHigh=0.0 ;
  73.   double deltaLow=0.0, deltaHigh=0.0 ;
  74.   double shift = ShiftPrice*Point ;
  75.   ArraySetAsSeries(high,true);
  76.   ArraySetAsSeries(low,true);
  77.   ArraySetAsSeries(open,true);
  78.   if(prev_calculated==0) { // 初期状態
  79.     InitializeAll();
  80.     pos = rates_total ;
  81.   }else {
  82.     // 近傍の有効頂点数(ExtLevelまでの)を数える
  83.     for(i=numExt=0;numExt<ExtLevel&&i<rates_total;i++) {
  84.       if(ExtKagizagBuffer[i]!=0.0)
  85.         numExt++;
  86.     }
  87.     if(!numExt) {
  88.       // 頂点ナシ(初期状態)
  89.       InitializeAll();
  90.       pos = rates_total ;
  91.     }else {
  92.       // 頂点がみつかった
  93.       pos = i-1 ;
  94.       if(ExtLowBuffer[pos]!=0.0) { // 頂点は、谷に位置する
  95.         curLow = ExtLowBuffer[pos] ;
  96.         extMode = 1 ; // 山を見つけるモード
  97.       }else { // 頂点は、山に位置する
  98.         curHigh = ExtHighBuffer[pos] ;
  99.         extMode = -1 ; // 谷を見つけるモード
  100.       }
  101.       // 頂点再列挙の事前処理として近傍の頂点データを抹消しておく
  102.       for(i=pos-1;i>=0;i--) {
  103.         ExtKagizagBuffer[i]=0.0;
  104.         ExtLowBuffer[i]=0.0;
  105.         ExtHighBuffer[i]=0.0;
  106.       }
  107.     }
  108.   }
  109.   // メイン処理
  110.   for(i=pos-1;i>=0;i--) {
  111.     if(ATRMultiplying) {
  112.       if(rates_total-i<ATRPeriod) continue;
  113.       shift = ShiftPrice * iATR(NULL,0,ATRPeriod,i) * ATRMultiplier ;
  114.     }
  115.     switch(extMode) { // 頂点検出モード(山と谷のどちらのピークであるか)
  116.     case 0: // どちらのモードでもない(初期状態)
  117.       deltaHigh = high[i]-open[pos-1] ;
  118.       deltaLow = open[pos-1]-low[i] ;
  119.       if(deltaHigh>=deltaLow) { // 上昇気味
  120.         if(deltaHigh>=shift) { // 初期価格から上にシフト
  121.           curLow = open[pos-1] ;
  122.           ExtKagizagBuffer[pos-1]=curLow;
  123.           ExtLowBuffer[pos-1]=curLow;
  124.           curHigh = high[i] ;
  125.           pos = i ;
  126.           ExtKagizagBuffer[pos] = curHigh ;
  127.           ExtHighBuffer[pos] = curHigh ;
  128.           extMode = -1 ; // 谷を見つけるモードに移行
  129.         }
  130.       }else { // 下降気味
  131.         if(deltaLow>=shift) { // 初期価格から下にシフト
  132.           curHigh = open[pos-1] ;
  133.           ExtKagizagBuffer[pos-1]=curHigh;
  134.           ExtHighBuffer[pos-1]=curHigh;
  135.           curLow = low[i] ;
  136.           pos = i ;
  137.           ExtKagizagBuffer[pos] = curLow ;
  138.           ExtLowBuffer[pos] = curLow ;
  139.           extMode = 1 ; // 山を見つけるモードに移行
  140.         }
  141.       }
  142.       break;
  143.     case 1: // 山を見つけるモード ( 谷側に価格が進行中 )
  144.       deltaHigh = high[i] - curLow ;
  145.       deltaLow = curLow - low[i] ;
  146.       if(deltaHigh>deltaLow) { // 上昇気味
  147.         if(deltaHigh>=shift) { // 谷底から上にシフト
  148.           curHigh = high[i] ;
  149.           opos = pos ;
  150.           pos = i ;
  151.           ExtKagizagBuffer[pos] = curHigh ;
  152.           ExtHighBuffer[pos] = curHigh ;
  153.           extMode = -1 ; // 谷を見つけるモードに移行
  154.           break;
  155.         }
  156.       }
  157.       if(deltaLow>=0) { // 最安値更新
  158.         curLow = low[i] ;
  159.         if(opos>=0) { // ひとつ前のポジション(山)が有効
  160.           int ih = iHighest(NULL,0,MODE_HIGH,opos-pos,pos) ;
  161.           if(curHigh<=high[ih]) { // 現在からひとつ前までの最高値が前の山より高い
  162.             // ひとつ前のポジション(山)を更新
  163.             curHigh=high[ih];
  164.             ExtKagizagBuffer[opos] = 0.0 ;
  165.             ExtHighBuffer[opos]=0.0;
  166.             opos=ih;
  167.             ExtKagizagBuffer[ih]=curHigh;
  168.             ExtHighBuffer[ih]=curHigh;
  169.           }
  170.         }
  171.         if(opos!=pos) ExtKagizagBuffer[pos]=0.0;
  172.         ExtLowBuffer[pos]=0.0;
  173.         pos=i;
  174.         ExtKagizagBuffer[pos]=curLow;
  175.         ExtLowBuffer[pos]=curLow;
  176.       }
  177.       break;
  178.     case -1: // 谷を見つけるモード ( 山側に価格が進行中 )
  179.       deltaHigh = high[i] - curHigh ;
  180.       deltaLow = curHigh - low[i] ;
  181.       if(deltaLow>deltaHigh) { // 下降気味
  182.         if(deltaLow>=shift) { // 山頂から下にシフト
  183.           curLow = low[i] ;
  184.           opos = pos ;
  185.           pos = i ;
  186.           ExtKagizagBuffer[pos] = curLow ;
  187.           ExtLowBuffer[pos] = curLow ;
  188.           extMode = 1 ; // 山を見つけるモードに移行
  189.           break;
  190.         }
  191.       }
  192.       if(deltaHigh>=0) { // 最高値更新
  193.         curHigh = high[i] ;
  194.         if(opos>=0) { // ひとつ前のポジション(谷)が有効
  195.           int il = iLowest(NULL,0,MODE_LOW,opos-pos,pos) ;
  196.           if(curLow>=low[il]) { // 現在からひとつ前までの最安値が前の谷より低い
  197.             // ひとつ前のポジション(谷)を更新
  198.             curLow=low[il];
  199.             ExtKagizagBuffer[opos] = 0.0 ;
  200.             ExtLowBuffer[opos]=0.0;
  201.             opos=il;
  202.             ExtKagizagBuffer[il]=curLow;
  203.             ExtLowBuffer[il]=curLow;
  204.           }
  205.         }
  206.         if(opos!=pos) ExtKagizagBuffer[pos]=0.0;
  207.         ExtHighBuffer[pos]=0.0;
  208.         pos=i;
  209.         ExtKagizagBuffer[pos]=curHigh;
  210.         ExtHighBuffer[pos]=curHigh;
  211.       }
  212.       break;
  213.     }
  214.   }
  215.   return(rates_total);
  216. }
  217. //---------------------------------------------------------------------------
  218. void InitializeAll()
  219. {
  220.    ArrayInitialize(ExtKagizagBuffer,0.0);
  221.    ArrayInitialize(ExtHighBuffer,0.0);
  222.    ArrayInitialize(ExtLowBuffer,0.0);
  223. }
  224. //---------------------------------------------------------------------------
  225.  
Tags: Mt4
Advertisement
Add Comment
Please, Sign In to add comment