OPEN-SOURCE SCRIPT

ema,atr and with Bollinger Bands (Indicator)

1. Indicator Overview
The indicator:

Displays EMA and Bollinger Bands on the chart.
Tracks price behavior during a user-defined trading session.
Generates long/short signals based on crossover conditions of EMA or Bollinger Bands.
Provides alerts for potential entries and exits.
Visualizes tracked open and close prices to help traders interpret market conditions.
2. Key Features
Inputs
session_start and session_end: Define the active trading session using timestamps.
E.g., 17:00:00 (5:00 PM) for session start and 15:40:00 (3:40 PM) for session end.
atr_length: The lookback period for the Average True Range (ATR), used for price movement scaling.
bb_length and bb_mult: Control the Bollinger Bands' calculation, allowing customization of sensitivity.
EMA Calculation
A 21-period EMA is calculated using:
pinescript
Copy code
ema21 = ta.ema(close, 21)
Purpose: Acts as a dynamic support/resistance level. Price crossing above or below the EMA indicates potential trend changes.
Bollinger Bands
Purpose: Measure volatility and identify overbought/oversold conditions.
Formula:
Basis: Simple Moving Average (SMA) of closing prices.
Upper Band: Basis + (Standard Deviation × Multiplier).
Lower Band: Basis - (Standard Deviation × Multiplier).
Code snippet:
pinescript
Copy code
bb_basis = ta.sma(close, bb_length)
bb_dev = bb_mult * ta.stdev(close, bb_length)
bb_upper = bb_basis + bb_dev
bb_lower = bb_basis - bb_dev
Session Tracking
The in_session variable ensures trading signals are only generated within the defined session:
pinescript
Copy code
in_session = time >= session_start and time <= session_end
3. Entry Conditions
EMA Crossover
Long Signal: When the price crosses above the EMA during the session:
pinescript
Copy code
ema_long_entry_condition = ta.crossover(close, ema21) and in_session and barstate.isconfirmed
Short Signal: When the price crosses below the EMA during the session:
pinescript
Copy code
ema_short_entry_condition = ta.crossunder(close, ema21) and in_session and barstate.isconfirmed
Bollinger Bands Crossover
Long Signal: When the price crosses above the upper Bollinger Band:
pinescript
Copy code
bb_long_entry_condition = ta.crossover(close, bb_upper) and in_session and barstate.isconfirmed
Short Signal: When the price crosses below the lower Bollinger Band:
pinescript
Copy code
bb_short_entry_condition = ta.crossunder(close, bb_lower) and in_session and barstate.isconfirmed
4. Tracked Prices
The script keeps track of key open/close prices using the following logic:
If a long signal is detected and the close is above the open, it tracks the open price.
If the close is below the open, it tracks the close price.
Tracked values are scaled using *4 (custom scaling logic).
These tracked prices are plotted for visual feedback:
pinescript
Copy code
plot(tracked_open, color=color.red, title="Tracked Open Price", linewidth=2)
plot(tracked_close, color=color.green, title="Tracked Close Price", linewidth=2)
5. Exit Conditions
Long Exit: When the price drops below the tracked open and close price:
pinescript
Copy code
exit_long_condition = (close < open) and (tracked_close < tracked_open) and barstate.isconfirmed
Short Exit: When the price rises above the tracked open and close price:
pinescript
Copy code
exit_short_condition = (close > open) and (tracked_close > tracked_open) and barstate.isconfirmed
6. Visualization
Plots:
21 EMA, Bollinger Bands (Basis, Upper, Lower).
Tracked open/close prices for additional context.
Background Colors:
Green for long signals, red for short signals (e.g., ema_long_entry_condition triggers a green background).
7. Alerts
The script defines alerts to notify the user about key events:

Entry Alerts:
pinescript
Copy code
alertcondition(ema_long_entry_condition, title="EMA Long Entry", message="EMA Long Entry")
alertcondition(bb_long_entry_condition, title="BB Long Entry", message="BB Long Entry")
Exit Alerts:
pinescript
Copy code
alertcondition(exit_long_condition, title="Exit Long", message="Exit Long")
8. Purpose
Trend Following: EMA crossovers help identify trend changes.
Volatility Breakouts: Bollinger Band crossovers highlight overbought/oversold regions.
Custom Sessions: Trading activity is restricted to specific time periods for precision.
Average True Range (ATR)Bollinger Bands (BB)Exponential Moving Average (EMA)

Скрипт с открытым кодом

В истинном духе TradingView автор этого скрипта опубликовал его с открытым исходным кодом, чтобы трейдеры могли понять, как он работает, и проверить на практике. Вы можете воспользоваться им бесплатно, но повторное использование этого кода в публикации регулируется Правилами поведения. Вы можете добавить этот скрипт в избранное и использовать его на графике.

Хотите использовать этот скрипт на графике?

Отказ от ответственности