FlagLength: Determines the length of the flag pattern.
TakeProfit1Ratio, takeProfit2Ratio, takeProfit3Ratio: Define the ratios for calculating the take-profit levels relative to the entry price.
RiskRewardRatio: Specifies the risk-reward ratio for calculating the stop-loss level relative to the entry price.
2 Flag Conditions:
BullishFlag: Checks if the current bar meets the conditions for a bullish flag pattern. It evaluates to true if the low of the current bar is lower than the low flagLength bars ago, and the close of the current bar is higher than the high flagLength bars ago.
BearishFlag: Checks if the current bar meets the conditions for a bearish flag pattern. It evaluates to true if the high of the current bar is higher than the high flagLength bars ago, and the close of the current bar is lower than the low flagLength bars ago.
3. Entry Price:
EntryPrice: Calculates the entry price based on whether a bullish or bearish flag pattern is identified. For a bullish flag, the entry price is set to the low of the current bar. For a bearish flag, the entry price is set to the high of the current bar.
4. Stop Loss:
StopLoss: Determines the stop-loss level based on the entry price and the specified riskRewardRatio. For a bullish flag, the stop-loss level is calculated by subtracting the difference between the high and low of the current bar multiplied by the riskRewardRatio from the low of the current bar. For a bearish flag, the stop-loss level is calculated similarly but added to the high of the current bar.
5. Take Profit Levels:
Three take-profit levels (takeProfit1, takeProfit2, takeProfit3) are calculated based on the entry price, stop-loss level, and specified take-profit ratios (takeProfit1Ratio, takeProfit2Ratio, takeProfit3Ratio).
6. Plotting Signals and Levels:
Bullish and bearish flag patterns are plotted using triangle shapes (shape.triangleup for
bullish and shape.triangledown for bearish) above or below the bars, respectively.
Entry, stop-loss, and take-profit levels are plotted using horizontal lines (line.new) with different colors and styles. Entry and stop-loss levels are labeled with "Entry" and "SL", respectively, while take-profit levels are labeled with "TP 1", "TP 2", and "TP 3".
The colors for bullish flags are white for entry, red for stop-loss, and green for take-profit levels. For bearish flags, the colors are the same, but the labels are plotted above the bars.
7. Label Placement:
Labels for entry, stop-loss, and take-profit levels are placed a distance of 4 bars to the right of the entry price using bar_index + 4.
This indicator is intended to help traders identify flag patterns on price charts and visualize potential entry, stop-loss, and take-profit levels associated with these patterns.
Please use risk management and when TP1 is hit, move stoploss to breakeven.