3 Bar Swing

楼主  收藏   举报   帖子创建时间:  2019-05-05 05:15 回复:0 关注量:46
3-bar-swing-indicator.png

  1. //+------------------------------------------------------------------+
  2. //|                                                  3_bar_swing.mq4 |
  3. //|                                                         Zen_Leow |
  4. //|                                                                  |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Zen_Leow"
  7. #property link      ""

  8. #property indicator_chart_window

  9. // The number of buffers for calculation, up to 8
  10. #property indicator_buffers 2

  11. // The color for displaying arrows
  12. #property indicator_color1 Green       // Long signal
  13. #property indicator_color2 Red         // Short signal

  14. // Width of the arrows
  15. #property indicator_width1 1  // Long signal arrow
  16. #property indicator_width2 1  // Short signal arrow

  17. extern int ArrowDistance = 3;

  18. // Buffers for the calculations
  19. double Up_Arrow_Buffer[];    // Long buffer for display
  20. double Down_Arrow_Buffer[];   // Short buffer for display
  21. //+------------------------------------------------------------------+
  22. //| Custom indicator initialization function                         |
  23. //+------------------------------------------------------------------+
  24. int init()
  25. {
  26.    // SetIndexStyle  - sets the new type, style, width and color for a given indicator line
  27.    // SetIndexBuffer - binds the array variable declared at a global level to the custom indicator pre-defined buffer
  28.    // SetIndexArrow  - sets an arrow symbol for indicators line of the DRAW_ARROW type.
  29.    SetIndexStyle(0, DRAW_ARROW);
  30.    SetIndexBuffer(0, Up_Arrow_Buffer);
  31.    SetIndexArrow(0, 233); // Up arrow
  32.    SetIndexStyle(1, DRAW_ARROW);
  33.    SetIndexBuffer(1, Down_Arrow_Buffer);
  34.    SetIndexArrow(1, 234); // Down arrow
  35. //----
  36.    return(0);
  37.   }
  38. //+------------------------------------------------------------------+
  39. //| Custom indicator deinitialization function                       |
  40. //+------------------------------------------------------------------+
  41. int deinit()
  42.   {
  43. //----
  44.    
  45. //----
  46.    return(0);
  47.   }
  48. //+------------------------------------------------------------------+
  49. //| Custom indicator iteration function                              |
  50. //+------------------------------------------------------------------+
  51. int start()
  52. {
  53.    int i;                           // Bar index      
  54.    int Counted_bars;                // Number of counted bars
  55.    //--------------------------------------------------------------------   
  56.    Counted_bars=IndicatorCounted(); // Number of counted bars   
  57.    i=Bars-Counted_bars-1;           // Index of the first uncounted   
  58.    if (i == 0)
  59.    {
  60.       i = 2;  // the newest signal bar is suppose to be at index 2
  61.    }
  62.    while(i>1)                      // Loop for uncounted bars     
  63.    {      
  64.       if (isUpSwingBar(i))
  65.       {
  66.          Up_Arrow_Buffer[i] = Low[i] - (ArrowDistance * Point);
  67.          Down_Arrow_Buffer[i] = EMPTY_VALUE;
  68.       }
  69.       else if (isDownSwingBar(i))
  70.       {
  71.          Down_Arrow_Buffer[i] = High[i] + (ArrowDistance * Point);
  72.          Up_Arrow_Buffer[i] = EMPTY_VALUE;
  73.       }
  74.       else
  75.       {
  76.          Up_Arrow_Buffer[i] = EMPTY_VALUE;
  77.          Down_Arrow_Buffer[i] = EMPTY_VALUE;
  78.       }
  79.       i--;
  80.    }
  81. //----
  82.    return(0);
  83. }

  84. bool isUpSwingBar(int i)
  85. {
  86.    if (Low[i] < Low[i+1] && Low[i] < Low[i-1] && High[i] < High[i+1] && High[i] < High[i-1])
  87.    {
  88.       return (true);
  89.    }
  90.    return (false);
  91. }

  92. bool isDownSwingBar(int i)
  93. {
  94.    if (Low[i] > Low[i+1] && Low[i] > Low[i-1] && High[i] > High[i+1] && High[i] > High[i-1])
  95.    {
  96.       return (true);
  97.    }
  98.    return (false);
  99. }
  100. //+------------------------------------------------------------------+
打赏