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. קרא עוד ב־תנאי השימוש.