Available translations

Situations, Conditions, and Formulas

foundations.png
Summary: The trading framework implemented in Trading Systems and Trading Strategies carries some low-level trading logic in and of itself. However, the bulk of the logic specific to each strategy derives from the way you describe market situations, and how you define the actions the Trading Bot must take.
Situation
Foundations->Node->Situation->Definition
situation.png
A situation refers to a specific state of the market in which a certain event should take place, as defined by any number of conditions.
Foundations->Node->Situation->Content
In other words, you define situations in which you wish a certain event to happen (i.e.: trigger on the strategy, take a position, etc.) and each situation is described as a set of conditions that need to be met for the event to be triggered.
A situation gets validated when all conditions under it are true.
An event may have more than one situation attached to it. In such a case, when any of the situations gets validated, the event gets triggered. That is, events may be triggered in different circumstances, meaning that you are free to define different situations upon which the same event would be triggered. In such a case, when any of the situations evaluate true, then the event is triggered.
Condition
Foundations->Node->Condition->Definition
condition.png
Conditions are rules within a situation. When all conditions under a situation validate true, then the situation gets validated as well, and the associated event is triggered.
Foundations->Node->Condition->Content
Therefore, conditions are used to mathematically describe what needs to happen with the market for a certain action to be taken.
For example:
Situation 1
  • Condition A: chart.at01hs.candle.close > chart.at01hs.bollingerBand.MovingAverage
  • Condition B: chart.at01hs.candle.previous.max > chart.at01hs.bollingerBand.previous.MovingAverage
In the example above, conditions A and B are comparison statements that may evaluate either true or false. In the case both would evaluate true then Situation 1 would be true as well.
Formula
Foundations->Node->Formula->Definition
formula.png
A formula is a mathematical expression intended to determine a numerical value to be applied dynamically to a certain property.
Foundations->Node->Formula->Content
In the context of a Trading System, formulas are used to determine the values for several properties, such as the Target Rate, Target Size, Managed Stop Loss, Managed Take Profit, and so on.
Formulas may use indicators and Trading Engine properties. The main difference between writing a Formula and writing a Condition is that while conditions must evaluate to true or false, formulas must evaluate to a number.
Foundations->Node->Formula->Examples
A simple math example
This simple formula may be used to define an initial take profit target 3% above the rate at which the position was taken.
 tradingEngine.tradingCurrent.position.entryTargetRate.value * 1.03
A simple JavaScript example
A bit of very basic JavaScript, introducing conditional statements, allows more intelligence. For example, in this case, we ask if the proposed formula results in a number greater than the current stop loss value; if it does, then the proposed formula is used; if not, then the current stop loss value is left as is.
This is—basically—a trailing stop loss 2% below the Bollinger Bands moving average that may go higher if the moving average goes up, but it may never come down—thanks to the use of the IF / ELSE clause.
 if (chart.at01hs.bollingerBand.movingAverage * 0.98 > 
tradingEngine.tradingCurrent.position.stopLoss.value)
{chart.at01hs.bollingerBand.movingAverage * 0.98}
else
{tradingEngine.tradingCurrent.position.stopLoss.value}
For developers
To build more complex logic within a formula, create a function that implements the logic and returns a numerical value—and then call the function:
 function orderRate() {
const ORDER_STEP_NUMBER = 1 // Secuential step number, starting with 1 from the closest to the price.
const BUY_SELL_SIGN = -1 // 1 for buy orders, -1 for sell orders.
const STEP_SEPARATION = 0.15 // % of separation of stair steps between them.
const BASE_FACTOR = 0.2 // % above or below bollinger band without bias.
const BIAS_FACTOR = 0.3 // % to move the whole stair of orders up or down depending on the bias.
const BAND_RATE = chart.at01min.bollingerBand.lowerBand // Upper Bollinger Band for sell orders, and Lower for buy orders.
let inbalance = tradingEngine.tradingCurrent.tradingEpisode.episodeBaseAsset.beginBalance.value - tradingEngine.tradingCurrent.tradingEpisode.episodeBaseAsset.balance.value
let BIAS_SIGN
if (inbalance === 0) {
BIAS_SIGN = 0
}
if (inbalance < 0) {
BIAS_SIGN = + 1
}
if (inbalance > 0) {
BIAS_SIGN = - 1
}
let rate = BAND_RATE + BUY_SELL_SIGN BAND_RATE (BASE_FACTOR + ORDER_STEP_NUMBER STEP_SEPARATION + BIAS_SIGN BIAS_FACTOR) / 100
return rate
}
orderRate()
Next
Syntax Overview