Available translations

Formula

foundations.png
formula.png
A formula is a mathematical expression intended to determine a numerical value to be applied dynamically to a certain property.
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.
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()
Formula Menu
The Formula node has the following Node Menu items:
The Edit menu item has the following properties:
  • action: Edit
  • label: Edit
  • iconPathOn: javascript-code
  • iconPathOff: javascript-code
  • dontShowAtFullscreen: true
  • actionFunction: uiObject.formulaEditor.activate
The Delete menu item has the following properties:
  • action: Delete UI Object
  • actionProject: Visual-Scripting
  • askConfirmation: true
  • confirmationLabel: Confirm to Delete
  • label: Delete
  • iconPathOn: delete-entity
  • iconPathOff: delete-entity
  • actionFunction: payload.executeAction
Formula Attaching Rules
The following are the Node Attaching Rules that govern the attachment of Formula with other nodes:
Compatible Types:
Formula Formula
This section explores Formula Node Code.
Initial Value
The initial value for Formula is:
 "// Type your formula"
Examples
This is a list of examples used on the Formula code, collected from this workspace.