# Guide: Adding a New Trading Condition

This document outlines the step-by-step process for adding a new entry or exit condition to the trading strategy. Following this process ensures that the new logic is correctly integrated into the main strategy, the libraries, and the corresponding visual indicators.

This example will use the creation of a new long entry condition, `longCondition_base_not_isM3GlobalSmoothedFalling`, as a reference.

## Step 1: Define the Logic & Name

Before writing any code, clearly define the logic for the new condition.

*   **Name:** The name should be descriptive. For our example, we chose `longCondition_base_not_isM3GlobalSmoothedFalling`.
*   **Logic:** The logic should be based on existing variables or new ones that can be calculated. Our example's logic is `longCondition_base and not isFalling_smoothedM3_no_offset and not vwap.vwap_bearishDivergence and not (vwap[1]).vwap_bearishDivergence`.

## Step 2: Modify the Core Logic Library (e.g., `LibraryLongEntry.pine`)

This is where the main logic for the condition will live.

1.  **Add Enablement Input:** Add a new boolean field to the `EnablementInputs` type for the on/off switch.
    ```pinescript
    export type LongEntryEnablementInputs
        // ... existing inputs
        bool i_enable_longCondition_base_not_isM3GlobalSmoothedFalling
    ```
2.  **Add Data Inputs (If Needed):** If your new condition requires data that isn't already being passed to the library function, add it to the appropriate input object type (e.g., `LongEntryM2LiInputs`, `LongEntryRsiDivInputs`). In our case, we needed `isFalling_smoothedM3_no_offset`.
    ```pinescript
    export type LongEntryM2LiInputs
        // ... existing inputs
        bool isFalling_smoothedM3_no_offset
    ```
3.  **Add Result Output:** Add a new boolean field to the `Results` type to hold the calculated result of your new condition.
    ```pinescript
    export type LongEntryResults
        // ... existing results
        bool longCondition_base_not_isM3GlobalSmoothedFalling
    ```
4.  **Implement the Logic:** Inside the main `f_calculate...` function, add the line(s) to calculate your new condition.
    ```pinescript
    longCondition_base_not_isM3GlobalSmoothedFalling = longCondition_base and not m2li.isFalling_smoothedM3_no_offset and not vwap.vwap_bearishDivergence and not (vwap[1]).vwap_bearishDivergence
    ```
5.  **Aggregate the Result:** Add your new condition to the final aggregated boolean (`longCondition`) and the `longCondition_disabled_fired` check.
    ```pinescript
    longCondition := longCondition or (longCondition_base_not_isM3GlobalSmoothedFalling and enablement.i_enable_longCondition_base_not_isM3GlobalSmoothedFalling)
    longCondition_disabled_fired := longCondition_disabled_fired or (longCondition_base_not_isM3GlobalSmoothedFalling and not enablement.i_enable_longCondition_base_not_isM3GlobalSmoothedFalling)
    ```
6.  **Update the Results Object:** Assign your new condition's result to the corresponding field in the `results` object at the end of the function.
    ```pinescript
    results.longCondition_base_not_isM3GlobalSmoothedFalling := longCondition_base_not_isM3GlobalSmoothedFalling
    ```
7.  **Update the Condition Naming:** Add an `else if` block to the `longEntryConditionName` logic so that the correct name is logged when a trade occurs.
    ```pinescript
    else if longCondition_base_not_isM3GlobalSmoothedFalling and enablement.i_enable_longCondition_base_not_isM3GlobalSmoothedFalling
        longEntryConditionName := "base_not_isM3GlobalSmoothedFalling"
    ```

## Step 3: Modify the Main Strategy (`MainStrategy.pine`)

This step connects the new logic to the strategy's inputs and data flow.

1.  **Add Input Switch:** Add a new `input.bool()` for the new condition's switch. Follow the naming conventions (e.g., `D-`, `H-`, `F-`) in the `title`.
    ```pinescript
    i_enable_longCondition_base_not_isM3GlobalSmoothedFalling = input.bool(true,  title="  D-base_not_isM3GlobalSmoothedFalling", group=group_LongEntry_Enabled, display=display.none)
    ```
2.  **Pass Data to Library:**
    *   Ensure the new `Enablement` input is passed to the library call.
    *   If you added new data inputs in Step 2, ensure they are calculated in `MainStrategy.pine` and passed to the library function by setting the corresponding field in the input object.

## Step 4: Modify the Visualization Indicator (e.g., `IndicatorLongEntryBooleans.pine`)

This step ensures you can see your new condition fire on the chart.

1.  **Mirror the Input Switch:** Add the exact same `input.bool()` that you added to `MainStrategy.pine`.
2.  **Mirror Data Calculations:** If you added new data inputs, you must duplicate the calculation logic from `MainStrategy.pine` into the indicator script. This is critical for ensuring the visualization is accurate.
3.  **Pass Data to Library:** Update the library function call in the indicator to pass the new `Enablement` input and any new data variables.
4.  **Add the Plot:** Add a new `plot()` call for the new condition, following the established "ladder" pattern. Increment the multiplier and choose a new color.
    ```pinescript
    plot(longEntryResults.longCondition_base_not_isM3GlobalSmoothedFalling ? i_plot_baseline + i_plot_increment * 40 : na, title="base_not_isM3GlobalSmoothedFalling", style=plot.style_linebr, color=color.new(color.maroon, 0), linewidth=i_plot_line_width, display=display.all-display.status_line)
    ```

## Step 5: Backtest and Verify

After making all the code changes, load the strategy on the chart and perform a backtest. Check the "List of Trades" to see if your new condition is firing and producing the expected results. Use the boolean indicator to visually confirm that the condition fires at the correct times.