OPEN-SOURCE SCRIPT
XT Dynamic Position Sizing

The Dynamic Position Sizing strategy is designed to introduce the use of dynamic variables to your alert messages. Unlike the XT Up/Down Bar Indicator, which preloads the payload into the TradingView alert Message field, strategies do not support this functionality.
You will need to copy and paste into the message window for the alert to work using the strategy actions.
Example Alert Message:
```
key=Your CrossTrade Secret Key;
command=PLACE;
account=sim101;
instrument=ES 12-24;
action={{strategy.order.action}}
qty={{strategy.order.quantity}};
order_type=MARKET;
tif=DAY;
flatten_first=true;
```
You'll need to add your secret key to the message between = and ;
Correct: key=123456789123456789;
Incorrect: key=Your CrossTrade Secret Key123456789123456789;
You'll also need to add your webhook URL to the Notifications tab on the alert.
Strategy Description: How It Works
This strategy generates trading signals based on the crossover of two moving averages (MAs) and calculates the position size dynamically using either a percentage of equity or a fixed dollar amount.
1. Moving Average Crossover Logic:
- The strategy uses two user-configurable moving averages:
- A Fast Moving Average with a shorter lookback period (default: 5 bars).
- A Slow Moving Average with a longer lookback period (default: 20 bars).
- Long Entry: A long signal is triggered when the Fast MA crosses above the Slow MA.
- Short Entry: A short signal is triggered when the Fast MA crosses below the Slow MA.
2. Position Sizing:
- The position size is calculated dynamically based on the user's selected mode:
- Percentage of Equity: The strategy calculates the position size as a percentage of the current account equity.
- Fixed Dollar Amount: The strategy calculates the position size by dividing a fixed dollar amount by the current price.
- The calculated position size is rounded down to the nearest whole number to ensure compatibility with trading platforms.
3. Dynamic Alerts:
- Alerts can be configured to dynamically include the action (`BUY`/`SELL`) and the calculated position size using placeholders (e.g., `{{strategy.order.action}}` and `{{strategy.order.contracts}}`).
4. Execution:
- The strategy ensures there is no overlapping of positions:
- It checks the current position size before entering a new trade.
- A new long trade will only be executed if no existing long position is active.
- A new short trade will only be executed if no existing short position is active.
5. Visualization:
- The strategy plots both the Fast MA (green) and the Slow MA (red) on the chart.
- These plots allow users to visually identify the crossover points that trigger trades.
6. Customization:
- Users can adjust:
- The lookback lengths of the Fast and Slow MAs.
- Whether position sizing is based on a percentage of equity or a fixed dollar amount.
- The percentage or dollar amount used for position sizing.
This strategy provides a straightforward approach to testing and trading moving average crossovers while allowing dynamic position sizing for flexibility.
You will need to copy and paste into the message window for the alert to work using the strategy actions.
Example Alert Message:
```
key=Your CrossTrade Secret Key;
command=PLACE;
account=sim101;
instrument=ES 12-24;
action={{strategy.order.action}}
qty={{strategy.order.quantity}};
order_type=MARKET;
tif=DAY;
flatten_first=true;
```
You'll need to add your secret key to the message between = and ;
Correct: key=123456789123456789;
Incorrect: key=Your CrossTrade Secret Key123456789123456789;
You'll also need to add your webhook URL to the Notifications tab on the alert.
Strategy Description: How It Works
This strategy generates trading signals based on the crossover of two moving averages (MAs) and calculates the position size dynamically using either a percentage of equity or a fixed dollar amount.
1. Moving Average Crossover Logic:
- The strategy uses two user-configurable moving averages:
- A Fast Moving Average with a shorter lookback period (default: 5 bars).
- A Slow Moving Average with a longer lookback period (default: 20 bars).
- Long Entry: A long signal is triggered when the Fast MA crosses above the Slow MA.
- Short Entry: A short signal is triggered when the Fast MA crosses below the Slow MA.
2. Position Sizing:
- The position size is calculated dynamically based on the user's selected mode:
- Percentage of Equity: The strategy calculates the position size as a percentage of the current account equity.
- Fixed Dollar Amount: The strategy calculates the position size by dividing a fixed dollar amount by the current price.
- The calculated position size is rounded down to the nearest whole number to ensure compatibility with trading platforms.
3. Dynamic Alerts:
- Alerts can be configured to dynamically include the action (`BUY`/`SELL`) and the calculated position size using placeholders (e.g., `{{strategy.order.action}}` and `{{strategy.order.contracts}}`).
4. Execution:
- The strategy ensures there is no overlapping of positions:
- It checks the current position size before entering a new trade.
- A new long trade will only be executed if no existing long position is active.
- A new short trade will only be executed if no existing short position is active.
5. Visualization:
- The strategy plots both the Fast MA (green) and the Slow MA (red) on the chart.
- These plots allow users to visually identify the crossover points that trigger trades.
6. Customization:
- Users can adjust:
- The lookback lengths of the Fast and Slow MAs.
- Whether position sizing is based on a percentage of equity or a fixed dollar amount.
- The percentage or dollar amount used for position sizing.
This strategy provides a straightforward approach to testing and trading moving average crossovers while allowing dynamic position sizing for flexibility.
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。