Flexi_bars_swing_v2

Download script
Author: Zen_Leow
Flexi_bars_swing_v2

The Flexi_bars_swing_v2 script is a specialized technical analysis tool designed to identify and visualize potential trend reversals or "swing points" on a trading chart. It simplifies the complex process of finding significant market peaks (highs) and valleys (lows) by automatically plotting visual cues—specifically green and red arrows—to alert the trader of these movements.

Core Logic and Functionality

The script operates by analyzing the relationship between a candle's price action and its immediate neighbors. It looks for specific structural patterns to determine if a price point qualifies as a "Swing Bar."

1. Identifying Swing Points:

  • Upward Swings (Buy Signals): The script scans for bars where the low and high prices are lower than those of the preceding candles. If a candle establishes a new local minimum relative to its left side and is subsequently followed by higher price action on its right side, it identifies an "Up Swing."
  • Downward Swings (Sell Signals): Conversely, the script looks for bars where the high and low prices are greater than those of the preceding candles. If a candle establishes a local maximum and is followed by lower price action, it identifies a "Down Swing."

2. Dynamic Positioning and Timing: A key feature of this script is its flexibility in how it displays these signals based on user preferences:

  • Verification Logic: It includes logic to check the immediate surrounding candles (both to the left and right) to confirm that a swing point is valid before placing an arrow.
  • Offset Adjustments: Depending on your settings, the script can choose to place the signal arrow exactly where the swing occurred or "offset" it based on how many candles it takes for the price action to confirm the move. This ensures that signals are not just based on raw price, but on confirmed momentum.
  • Execution Modes: The script provides options to wait for a candle to fully close before confirming a signal, which helps in filtering out "noise" or temporary price spikes that might disappear before the market settles.

Visual Representation

The output is clean and designed for quick readability:

  • Green Arrows: Represent identified upward swing points, suggesting potential areas of support or bullish momentum.
  • Red Arrows: Represent identified downward swing points, suggesting potential areas of resistance or bearish momentum.

In summary, this tool automates the tedious task of manually identifying structural market turns. It filters through price history to highlight significant "swings," providing traders with a clearer visual map of where the market might be shifting direction.

Miscellaneous
Implements a curve of type %1
9 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