OPEN-SOURCE SCRIPT
BB Simple B-Xtrender Long

### Overview
I currently us this on a 1 week timeframe to spot trend shifts in the market.
It focuses exclusively on long positions (buying assets expecting price increases) and does not overlay on the main price chart—instead, it appears in a separate pane. The strategy allocates the full available equity to each trade and accounts for commissions as a percentage. It uses a custom indicator called the "B-Xtrender" to generate entry and exit signals for long trades, based on momentum-like calculations involving exponential moving averages (EMAs) and the relative strength index (RSI). The indicator is computed on a user-specified timeframe, and the strategy includes alert conditions for key events.
### Inputs Section
The script begins with user-configurable inputs that allow customization without editing the code directly. These include:
- Three integer values for short-term lengths: The first (default 5) and second (default 20) are used in EMA calculations to create a difference that forms the basis of the indicator. The third (default 15) sets the length for the RSI applied to that difference.
- A timeframe input (default "30" for 30 minutes) that determines the resolution for fetching data used in the indicator.
- A float value for the exit level (default 5, with a minimum of 0 and step of 0.1), which acts as a threshold for one of the exit conditions—specifically, when the indicator crosses below this level.
These inputs make the strategy adaptable to different assets or timeframes by adjusting the sensitivity of the indicator and exit rules.
### Indicator Section
The core of the script is the calculation of the "ShortTermXtrender" indicator. It fetches data from the specified timeframe using the asset's ticker symbol and computes an RSI on the difference between two EMAs of the closing prices (using the first and second short lengths), then subtracts 50 to center it around zero. This results in an oscillator that highlights short-term momentum shifts.
The histogram is colored dynamically:
- Green shades for positive values: Brighter green if the current value is higher than the previous (indicating strengthening upward momentum), and darker green otherwise.
- Red shades for negative or zero values: Brighter red if the current value is higher than the previous (less negative, but still downward), and darker red if it's decreasing further.
The indicator is plotted as columns for visual clarity, titled "B-Xtrender TF1" (referring to the first timeframe). A dashed gray horizontal line at zero serves as a centerline to easily distinguish positive (bullish) from negative (bearish) territories.
### Conditions Section
This part defines the logic for entering and exiting long trades, broken into entry and exit rules.
For long entries:
- One condition checks if the indicator is increasing (current value greater than the previous).
- Another optional condition detects a crossover above zero while also increasing, which can be removed if not desired.
- The final entry signal combines these: It triggers on either the crossover (if included) or the increasing value, promoting entries during upward momentum.
For long exits:
- A "red" state is defined as the indicator being at or below zero.
- A decreasing trend over two bars is checked by comparing the current to the previous, and the previous to the one before that.
- One exit triggers on this red state combined with the two-bar decrease.
- Another exit occurs when the indicator crosses below the user-defined exit level.
- The overall exit combines these two, ensuring positions close on weakening momentum or threshold breaches, but only if a long position is active.
### Strategy Execution Section
Here, the script applies the conditions to actual trading actions:
- On a long entry signal, it enters a long position labeled "Long," using the full strategy parameters for sizing and direction.
- On a long exit signal (and only if holding a positive position size), it closes the "Long" position. The comment for the close order specifies the reason: either crossing below the exit level (including the level value) or due to the red histogram with a two-bar decline.
This setup ensures automated trade management based solely on the indicator's behavior.
### Alerts Section
The script sets up three alert conditions that can notify users via TradingView's alert system:
- One for the crossover-based long entry, with a message indicating the indicator crossed above zero and is rising, signaling a long opportunity.
- Another for the increasing-value long entry, noting the indicator's rise on the specified timeframe as a long signal.
- A third for the red histogram with two-bar decline exit, warning of the condition to exit a long position.
These alerts are independent of the strategy execution and can be used for manual trading or monitoring even if the automated strategy isn't running.
I currently us this on a 1 week timeframe to spot trend shifts in the market.
It focuses exclusively on long positions (buying assets expecting price increases) and does not overlay on the main price chart—instead, it appears in a separate pane. The strategy allocates the full available equity to each trade and accounts for commissions as a percentage. It uses a custom indicator called the "B-Xtrender" to generate entry and exit signals for long trades, based on momentum-like calculations involving exponential moving averages (EMAs) and the relative strength index (RSI). The indicator is computed on a user-specified timeframe, and the strategy includes alert conditions for key events.
### Inputs Section
The script begins with user-configurable inputs that allow customization without editing the code directly. These include:
- Three integer values for short-term lengths: The first (default 5) and second (default 20) are used in EMA calculations to create a difference that forms the basis of the indicator. The third (default 15) sets the length for the RSI applied to that difference.
- A timeframe input (default "30" for 30 minutes) that determines the resolution for fetching data used in the indicator.
- A float value for the exit level (default 5, with a minimum of 0 and step of 0.1), which acts as a threshold for one of the exit conditions—specifically, when the indicator crosses below this level.
These inputs make the strategy adaptable to different assets or timeframes by adjusting the sensitivity of the indicator and exit rules.
### Indicator Section
The core of the script is the calculation of the "ShortTermXtrender" indicator. It fetches data from the specified timeframe using the asset's ticker symbol and computes an RSI on the difference between two EMAs of the closing prices (using the first and second short lengths), then subtracts 50 to center it around zero. This results in an oscillator that highlights short-term momentum shifts.
The histogram is colored dynamically:
- Green shades for positive values: Brighter green if the current value is higher than the previous (indicating strengthening upward momentum), and darker green otherwise.
- Red shades for negative or zero values: Brighter red if the current value is higher than the previous (less negative, but still downward), and darker red if it's decreasing further.
The indicator is plotted as columns for visual clarity, titled "B-Xtrender TF1" (referring to the first timeframe). A dashed gray horizontal line at zero serves as a centerline to easily distinguish positive (bullish) from negative (bearish) territories.
### Conditions Section
This part defines the logic for entering and exiting long trades, broken into entry and exit rules.
For long entries:
- One condition checks if the indicator is increasing (current value greater than the previous).
- Another optional condition detects a crossover above zero while also increasing, which can be removed if not desired.
- The final entry signal combines these: It triggers on either the crossover (if included) or the increasing value, promoting entries during upward momentum.
For long exits:
- A "red" state is defined as the indicator being at or below zero.
- A decreasing trend over two bars is checked by comparing the current to the previous, and the previous to the one before that.
- One exit triggers on this red state combined with the two-bar decrease.
- Another exit occurs when the indicator crosses below the user-defined exit level.
- The overall exit combines these two, ensuring positions close on weakening momentum or threshold breaches, but only if a long position is active.
### Strategy Execution Section
Here, the script applies the conditions to actual trading actions:
- On a long entry signal, it enters a long position labeled "Long," using the full strategy parameters for sizing and direction.
- On a long exit signal (and only if holding a positive position size), it closes the "Long" position. The comment for the close order specifies the reason: either crossing below the exit level (including the level value) or due to the red histogram with a two-bar decline.
This setup ensures automated trade management based solely on the indicator's behavior.
### Alerts Section
The script sets up three alert conditions that can notify users via TradingView's alert system:
- One for the crossover-based long entry, with a message indicating the indicator crossed above zero and is rising, signaling a long opportunity.
- Another for the increasing-value long entry, noting the indicator's rise on the specified timeframe as a long signal.
- A third for the red histogram with two-bar decline exit, warning of the condition to exit a long position.
These alerts are independent of the strategy execution and can be used for manual trading or monitoring even if the automated strategy isn't running.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.