In this tutorial, we’ll explore how to use negative values in AlgoBuilderX, a feature that can be applied across various blocks to manage conditions effectively. For this article, we’ll focus specifically on an example using the Condition block, where we’ll set up a rule to close trades when the account’s net unrealized Profit & Loss reaches -100€. We’ll use the Account Profit parameter in the Condition block and go over how to select the right operator to achieve the desired outcome.
Step 1: Create a Condition to Monitor Net Profit/Loss
- Add the Condition Block
Start by adding a Condition block to your workspace. - Select Account and Profit
Within the Condition block, choose Account and then Profit as the parameter. This profit refers to the Unrealized Net value in cTrader, representing the account’s unrealized profit or loss, including expenses such as commissions and swaps. It can therefore be either a positive or a negative number.
Step 2: Set the Negative Reference Value
We want to monitor when the unrealized profit drops to -100€, so we select Values as the comparison operator on the right of the Condition block and enter -100 as the reference value.
Step 3: Understanding Operators with Negative Numbers
The “More than” and “Less than” operators work differently with negative numbers, so it’s important to choose the correct operator to get the desired behavior.
- More than (>): If we set “More than -100,” the condition is met when the unrealized profit is greater than -100, meaning it includes values from -99 up to zero and all positive values. This is not what we want to identify losses.
- Less than (<): To monitor when the unrealized profit falls to -100 or lower, we select the Less than operator. This allows us to identify values more negative than -100, such as -101, -102, etc., indicating that losses are increasing.
Example Explained: With a negative value and the “Less than” operator, we’re identifying numbers more negative than -100. Conversely, with “More than,” we monitor values approaching zero, i.e., less negative than -100.
Step 4: Choose the “Less than” Operator in the Condition Block
Now that we’ve understood how the operators work with negative values, it’s time to apply this knowledge to our Condition block. To ensure that the condition activates when the account’s Profit & Loss reaches or goes below -100€, we must select the “Less than” operator. This ensures that the condition triggers not only at -100€ but also for any value more negative, such as -101€, -102€, and beyond.
Step 5: Complete the Condition to Close Trades
After setting Account – Profit < -100, link this block to a Close Trades block. This way, when unrealized profit falls below -100, the condition is met, and the Close Trades block will be activated, closing all active trades.
Summary
Using negative values in the Condition block requires a clear understanding of how operators work with negative numbers. Be sure to select the Less than operator if you want to monitor increasing losses, as in the example of this tutorial. With this approach, you can automate account loss control on AlgoBuilderX and close positions upon reaching a set threshold.