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.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.
오픈 소스 스크립트
트레이딩뷰의 진정한 정신에 따라, 이 스크립트의 작성자는 이를 오픈소스로 공개하여 트레이더들이 기능을 검토하고 검증할 수 있도록 했습니다. 작성자에게 찬사를 보냅니다! 이 코드는 무료로 사용할 수 있지만, 코드를 재게시하는 경우 하우스 룰이 적용된다는 점을 기억하세요.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.