The initial targets section features the first few definitions required to open a position: the definition of target rate and the target size.
The open stage deals with the definitions that make up the logic to enter a position, including the target rate and size, and order execution.

The open stage comes into play once the take position event is triggered. Conceptually, the open stage deals with the details concerning how the position shall be opened once the decision to take a position has been made. That is, the open stage does not participate in the process of deciding whether a position should be open or not.

To take a position, several definitions are required. In particular, those concerning targets, such as the rate and size of the position, and the logic of the execution strategy, that is, how many of which orders shall be placed at the exchange—and when—to enter the position.

The open stage closes as soon as one of the following events happen:

  • the target size gets filled,

  • the close stage event is triggered,

  • the take profit or stop loss targets are hit.


Open Stage

Initial Targets

Target Rate

Target Size In Base Asset

Target Size In Quoted Asset

Open Execution

Close Stage Event

Initial Targets

The initial targets node holds the most basic definitions about the position to be taken: the target rate and the target size.

Target Rate

The target rate is a reference rate that may affect simulations and that, in combination with the placement of managed stop loss and managed take profit targets, is used to determine whether the targets have been hit.
Internal Use

The system needs to determine when the managed stop loss and managed take profit targets have been hit. To do this, the system observes how the user places the targets in relation to the target rate.

For example:

If the phase 1 managed stop loss target is placed below the target rate and the phase 1 managed take profit target above it, the system assumes that:

  • the stop loss target is hit when the current rate is equal to or smaller than the managed stop loss value;

  • the stop loss target is hit when the current rate is equal to or greater than the managed take profit value;

The reversed example is true, as well.

This is how the system uses the target rate internally.

Fetching the Value from Formulas

Additionally, defining a target rate at the level of the stage may be of use so that you may retrieve the value from formulas while setting the rate of limit orders, using the path of the corresponding node at the trading engine.

For example, tradingEngine.current.position.entryTargetRate.value or tradingEngine.current.position.exitTargetRate.value.

Affecting How Simulations are Plotted

The target rates defined at the open and close stages affect how the Posittion Base Asset and Position Quoted Asset layers in the Simulation Objects layer manager draw the triangle representing the development of the trade.

The Position Base Asset layer is on, providing a visual clue of how the position developed.

The horizontal segment marks the target rate defined at the open stage. If you are using market orders to take the position, then using tradingEngine.current.episode.candle.close.value may offer a good representation of the rate of market orders. If you are using limit orders, then you may decide at which rate you wish to take the position, and the horizontal segment of the triangle will match the chosen rate.

The third vertex of the triangle points to a [datetime, rate] coordinate. The datetime is given by the candle on which the manage stage closes, that is, the candle at which the stop loss or take profit targets are hit. The rate portion of the coordinate is given by the target rate defined in the close stage.

You may choose to apply a similar reasoning as with the open stage target rate for limit orders, but there is some nouance to consider if you are using market orders.

The issue arises if you when you run a testing session at a time frame higher than the time frame you use for live trading.

Why is that an issue?

Let’s say your trading system makes decisions based on the close of the 1-hour candle. And let’s say you are using stop loss and take profit targets. In such cases—when live trading—you may want to run your strategy in the 1-minute time frame, to detect the tagging of the take profit and stop targets as soon as possible, and act accordingly.

However, when backtesting, it is much faster to run sessions on the 1-hour time frame. This is particularly important when you wish to test significant time ranges.

When backtesting a strategy on the 1-hour time frame, the trading bot evaluates if stop loss and take profit targets have been hit at the close of the 1-hour candle, and may only act upon the evaluation on the next candle. This means that if you are using market orders to exit the position, the market order would be placed (in the simulation!) on the candle after one of the targets was hit.

The difference in rate between the moment in time the target is hit and the time the 1-hour candle closes may be significant. As a result, if the close stage target rate is defined as tradingEngine.current.episode.candle.close.value, the resulting simulation may show significant slippage, and diverge from what the trade would look like if running on the 1-minute time frame, like you would when trading live.

To solve the above issue, you may use a more ellaborate formula for the target rate in the close stage:

targetRate()

function targetRate() {
    switch (tradingEngine.current.position.exitType.value) {
        case 'No Exit': {
            return tradingEngine.current.episode.candle.close.value
            break
        }
        case 'Take Profit': {
            return tradingEngine.current.position.takeProfit.finalValue.value
            break
        }
        case 'Stop Loss': {
            return tradingEngine.current.position.stopLoss.finalValue.value
            break
        }
    }
}

This formula discriminates among three possible outcomes:

  • When the no target has been hit, the value of the target rate is the close of the current candle.

  • When the take profit is hit, the value of the target rate is the last value of the take profit.

  • When the stop loss is hit, the value of the target rate is the last value of the stop loss.

In other words, the above formula simulates what would happen when running the trading session on the 1-minute time frame.

Target Size In Base Asset

Target size in base asset is used to define the size of the position, denominating it in the base asset.

The system supports defining the size of the position in either asset involved in the market: the base asset, or the quoted asset, as per the exchange listing of the market.

The target size may be defined in one of the two assets only, to avoid inconsistencies.

The target size is the maximum size the position may achieve. That is, the definition of the target size is used as a cap for the total size of orders that may be placed during the open stage.

If you prefer to define the size of the position denominated in the base asset, then use this node and delete the target size in quoted asset node.

Even though the definition of the target size is denominated in one of the two assets in the market, the system keeps track of accounts for both assets. That is, performance metrics such as profit loss, ROI, hit ratio, or the annualized rate of return are calculated both based on the base asset and the quoted asset. In fact, metrics are also calculated in a consolidated manner, taking into account both assets at the same time.

All of this information is made available for multiple contexts, for instance, for each position or the whole episode, through the data structure of the trading engine.

When tracking the results of your trading operation, make sure you refer to the set of accounts that make sense for your trading system. This will all become clearer once you read about the trading engine and the layer managers available on the charts.

Examples

If you are trading the whole balance assigned to the trading system on each position, then your target size will look like this:

tradingEngine.current.episode.episodeBaseAsset.balance.value

… or…

tradingEngine.current.episode.episodeQuotedAsset.balance.value

If you are not trading the entire balance on each position, then you may define the target size as a percentage of the balances, or in any other way you may see fit. For such cases, the following formula may be of use for the close stage target size:

tradingEngine.current.strategyOpenStage.stageBaseAsset.sizeFilled.value - tradingEngine.current.strategyOpenStage.stageBaseAsset.feesPaid.value

Of course, you may need to adjust the above to use stageBaseAsset or stageQuotedAsset, as required. In short, the formula returns the amount of the asset obtained by the transactions triggered during the open stage, minus the fees (and slippage, which is already subtracted from the size filled).

Target Size In Quoted Asset

Target size in quoted asset is used to define the size of the position, denominating it in the quoted asset.

If you prefer to define the size of the position denominated in the quoted asset, then use this node and delete the target size in base asset node.

Close Stage Event

The close stage event defines the set of rules that need to be met for the corresponding stage to be closed.

Both the open and close stages are closed once the target size is filled.

However, Superalgos allows an extra level of control so that the user may choose to close either stage given specific market situations. Such is the intent of the close stage event.