Flexi_bars_swing_v2

Author: Zen_Leow
Flexi_bars_swing_v2
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
Flexi_bars_swing_v2
//+------------------------------------------------------------------+
//|                                          Flexi_bars_swing_v2.mq4 |
//|                                                         Zen_Leow |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Zen_Leow"
#property link      ""

#property indicator_chart_window

// The number of buffers for calculation, up to 8
#property indicator_buffers 3

// The color for displaying arrows
#property indicator_color1 Green       // Long signal
#property indicator_color2 Red         // Short signal

// Width of the arrows
#property indicator_width1 2  // Long signal arrow
#property indicator_width2 2  // Short signal arrow

extern int ArrowDistance = 3;
extern bool WaitForCandleClose = false;
extern bool ArrowOnTriggerBar = false;

// Buffers for the calculations
double Up_Arrow_Buffer[];    // Long buffer for display
double Down_Arrow_Buffer[];   // Short buffer for display
double Right_Side_Buffer[];
int RightSideCandleNum = 0;
int SignalIndex = 0;
int ArrowOffset = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   if (WaitForCandleClose)
   {
      SignalIndex = 1;
   }
   else
   {
      SignalIndex = 0;
   }
   // SetIndexStyle  - sets the new type, style, width and color for a given indicator line
   // SetIndexBuffer - binds the array variable declared at a global level to the custom indicator pre-defined buffer
   // SetIndexArrow  - sets an arrow symbol for indicators line of the DRAW_ARROW type.
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexBuffer(0, Up_Arrow_Buffer);
   SetIndexArrow(0, 233); // Up arrow
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexBuffer(1, Down_Arrow_Buffer);
   SetIndexArrow(1, 234); // Down arrow
   SetIndexStyle(2, DRAW_NONE);
   SetIndexBuffer(2, Right_Side_Buffer);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int i;                           // Bar index       
   int Counted_bars;                // Number of counted bars
   //--------------------------------------------------------------------   
   Counted_bars=IndicatorCounted(); // Number of counted bars   
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   if (i == 0)
   {
      i = 5;  // the newest signal bar is suppose to be at index 2 or 3
   }
   while(i>SignalIndex)            // Loop for uncounted bars but always recount the newest 3 bars
   {      
      if (isUpSwingBar(i))
      {
         if (ArrowOnTriggerBar)
         {
            ArrowOffset = RightSideCandleNum;
         }
         else
         {
            ArrowOffset = 0;
         }
         if (RightSideCandleNum == 1 || (RightSideCandleNum == 2 && i > SignalIndex+1))
         {
            Up_Arrow_Buffer[i-ArrowOffset] = Low[i-ArrowOffset] - (ArrowDistance * Point);
            Down_Arrow_Buffer[i-ArrowOffset] = EMPTY_VALUE;
            Right_Side_Buffer[i] = RightSideCandleNum;
         }
      }
      else if (isDownSwingBar(i))
      {
         if (ArrowOnTriggerBar)
         {
            ArrowOffset = RightSideCandleNum;
         }
         else
         {
            ArrowOffset = 0;
         }
         if (RightSideCandleNum == 1 || (RightSideCandleNum == 2 && i > SignalIndex+1))
         {
            Down_Arrow_Buffer[i-ArrowOffset] = High[i-ArrowOffset] + (ArrowDistance * Point);
            Up_Arrow_Buffer[i-ArrowOffset] = EMPTY_VALUE;
            Right_Side_Buffer[i] = RightSideCandleNum;
         }
      }
      else
      {
         if (ArrowOnTriggerBar)
         {
            ArrowOffset = Right_Side_Buffer[i];
         }
         else
         {
            ArrowOffset = 0;
         }
         Up_Arrow_Buffer[i-ArrowOffset] = EMPTY_VALUE;
         Down_Arrow_Buffer[i-ArrowOffset] = EMPTY_VALUE;
         Right_Side_Buffer[i] = EMPTY_VALUE;
        // RightSideCandleNum = 0;
      }
      i--;
   }
//----
   return(0);
}

bool isUpSwingBar(int i)
{
   if (UpSwingLeftOk(i) && UpSwingRightOk(i))
   {
      return (true);
   }
   return (false);
}

bool UpSwingLeftOk(int i)
{
   if (Low[i] < Low[i+1])
   {
      if (High[i] < High[i+1])
      {
         return (true);
      }
      else
      {
         if (Low[i] < Low[i+2] && High[i] < High[i+2])
         {
            return (true);
         }
      }
   }
   return (false);
}

bool UpSwingRightOk(int i)
{
   if (Low[i] < Low[i-1])
   {
      if (High[i] < High[i-1])
      {
         RightSideCandleNum = 1;
         return (true);
      }
      else
      {
         if (Low[i] < Low[i-2] && High[i] < High[i-2])
         {
            RightSideCandleNum = 2;
            return (true);
         }
      }
   }
   return (false);
}

bool isDownSwingBar(int i)
{
   if (DownSwingLeftOk(i) && DownSwingRightOk(i))
   {
      return (true);
   }
   return (false);
}

bool DownSwingLeftOk(int i)
{
   if (High[i] > High[i+1])
   {
      if (Low[i] > Low[i+1])
      {
         return (true);
      }
      else
      {
         if (Low[i] > Low[i+2] && High[i] > High[i+2])
         {
            return (true);
         }
      }
   }
   return (false);
}

bool DownSwingRightOk(int i)
{
   if (High[i] > High[i-1])
   {
      if (Low[i] > Low[i-1])
      {
         RightSideCandleNum = 1;
         return (true);
      }
      else
      {
         if (Low[i] > Low[i-2] && High[i] > High[i-2])
         {
            RightSideCandleNum = 2;
            return (true);
         }
      }
   }
   return (false);
}
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---