# Dynamic Variables

## What Are Dynamic Strategy Variables?

Dynamic strategy variables in TradingView are placeholders that automatically populate with values *from* your TradingView strategy during execution. They are used to pass dynamic information from the strategy through the alert.&#x20;

{% embed url="<https://www.tradingview.com/support/solutions/43000531021-how-to-use-a-variable-value-in-alert/?ref=crosstrade.io/blog>" %}

***

### How to Use Dynamic Strategy Variables in CrossTrade Alerts

#### 1. **Using Variables with the Alert Message Window**

If your TradingView strategy generates dynamic variables for **Action** and **Qty**, you can integrate these into your CrossTrade alert. Here’s how:

* **Define the variables in your strategy**.
* In the alert's **Message** field, ensure that the placeholders (e.g., `{{strategy.order.action}}`, `{{strategy.order.contracts}}`) are used.
* CrossTrade will interpret these placeholders and execute the corresponding actions on your connected trading platform.

**Example:**

If your strategy generates an alert with the following placeholders:

```
action={{strategy.order.action}};
qty={{strategy.order.contracts}};
```

CrossTrade will process the alert dynamically, replacing the placeholders with the actual action (e.g., `buy`, `sell`) and quantity values defined by your strategy.\
\ <mark style="color:blue;">**If you're trading futures, its best to use the variable {{strategy.order.contracts}}**</mark>

***

#### 2. **Static Alerts for Indicators**

Since indicators do not support dynamic variables specifically for the buy or qty fields, you must provide all required fields explicitly in the **Message** window. For example:

```
action=buy;
qty=10;
```

This ensures that CrossTrade receives complete and actionable instructions for your trades. You can use certain variables with indicators, such as:

* `{{close}}` → The closing price of the current bar
* `{{open}}` → The opening price of the current bar
* `{{high}}` → The highest price of the current bar
* `{{low}}` → The lowest price of the current bar

However, bear in mind that these will offer limited functionality when compared to a strategy variable.

***

#### 3. **Customizing Alerts with Pine Script**

For users with access to Pine Script, you can take full control of the alert payload by embedding the CrossTrade configuration directly into your script. This method ensures maximum flexibility and precision.

**Steps:**

1. Define the full CrossTrade payload in your Pine Script:

   ```pinescript
   if longCondition
           stopLossPrice = close - stopLossTicks
           targetPrice = close + targetTicks
           alertMessage = 'key=your-secret-key;'
           alertMessage += 'command=place;'
           alertMessage += 'account=sim101;'
           alertMessage += 'instrument={{ticker}};'
           alertMessage += 'action=buy;'
           alertMessage += 'qty=1;'
           alertMessage += 'order_type=market;'
           alertMessage += 'tif=day;'
           alertMessage += 'flatten_first=true;'
           alertMessage += 'take_profit=' + str.tostring(targetPrice) + ';'
           alertMessage += 'stop_loss=' + str.tostring(stopLossPrice) + ';'
           
           strategy.entry("Long", strategy.long, alert_message=alertMessage)
   ```
2. Call the payload in your alert configuration using the `{{strategy.order.alert_message}}` placeholder.

**Example Alert:**

```json
{{strategy.order.alert_message}}
```

***

### Key Points to Remember

{% hint style="success" %}

1. **Strategies Only**: Dynamic variables are exclusively for use with strategies. They are not supported for indicators.
2. **Supported Fields**: Only the **Action** and **Qty** fields can use dynamic variables in CrossTrade alerts.
3. **Static Messages for Indicators**: For indicators, you must manually provide all required details in the alert's **Message** window.
   {% endhint %}

For more info on using variables and automating TradingView strategies, check out this article:&#x20;

{% embed url="<https://crosstrade.io/blog/how-to-automate-tradingview-strategies-with-crosstrade/>" %}
