Available translations

Open Stage Initial Targets

foundations.png
Summary: The initial targets section features the first few definitions required to open a position: the definition of target rate and the target size.
Open Stage
Foundations->Node->Open Stage->Definition
open-stage.png
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.
Foundations->Node->Open Stage->Content
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.
Foundations->Topic->Why My Stage is not Closing?->The Stage Does Not Close
The Stage Does Not Close
One common issue users have while dealing with stages is that they do not get closed after their orders get filled. This usually happens because there are several adjustments on rate that happens during trading, specially during live trading. The reason a Stage does not get closed automatically is because the total filled for the whole stage is not close enough to the Stage Target Size defined by users. In order to avoid this situation there are three configurations at the Stage node you can use:
  • ROUNDING_ERROR_CORRECTION_FACTOR: is a configurable factor that can be applied to the stage filled at runtime in order to force it to reach the Stage Target Size. The default value for this factor is:
 ROUNDING_ERROR_CORRECTION_FACTOR = 1.001
This value is overwritten by whatever users config at the Stage node under the roundingErrorCorrectionFactor property.
A value of 1.01 would mean that when the Filled amount is at 1% distance from the Stage Target, the stage will be closed.
  • ABSOLUTE_DUST_IN_BASE_ASSET: is a numeric value that represents the distance that you allow to be between the Size Filled and the Target Side for a stage, measured in Base Asset. It's default value is Zero and you can define it at the config of a Stage node using the absoluteDustInBaseAsset property.
  • ABSOLUTE_DUST_IN_QUOTED_ASSET: is a numeric value that represents the distance that you allow to be between the Size Filled and the Target Side for a stage, measured in Base Asset. It's default value is Zero and you can define it at the config of a Stage node using the absoluteDustInQuotedAsset property.
Implementation
Here you can see how these parameters are implemented in the Low Frequency Trading Bot code:
         if (
            tradingEngineStage.stageBaseAsset.sizeFilled.value
            *
            ROUNDING_ERROR_CORRECTION_FACTOR
            +
            ABSOLUTE_DUST_IN_BASE_ASSET
            >=
            tradingEngineStage.stageBaseAsset.targetSize.value
        ) {
            positionFilled()
        } else if (
            tradingEngineStage.stageQuotedAsset.sizeFilled.value
            *
            ROUNDING_ERROR_CORRECTION_FACTOR
            +
            ABSOLUTE_DUST_IN_QUOTED_ASSET
            >=
            tradingEngineStage.stageQuotedAsset.targetSize.value
        ) {
            positionFilled()
        } else {
            checkCloseStageEvent(tradingSystemStage)
        }
In this page, we will cover the Initial Targets definitions.
Initial Targets
Foundations->Node->Initial Targets->Definition
initial-targets.png
The initial targets node holds the most basic definitions about the position to be taken: the target rate and the target size.
Foundations->Concept->Reusable Snippets->Note for Hierarchy Tables
Target Rate
Foundations->Node->Target Rate->Definition
target-rate.png
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.
Foundations->Node->Target Rate->Content
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;
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.tradingCurrent.position.entryTargetRate.value or tradingEngine.tradingCurrent.position.exitTargetRate.value.
Affecting How Simulations are Plotted
The target rates defined at the open and close stages affect how the Position Base Asset and Position Quoted Asset layers in the Simulation Objects layer manager draw the triangle representing the development of the trade.
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.tradingCurrent.tradingEpisode.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 nuance to consider if you are using market orders.
The issue arises 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.tradingCurrent.tradingEpisode.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 elaborate formula for the target rate in the close stage:
 targetRate()

function targetRate() {
    switch (tradingEngine.tradingCurrent.position.exitType.value) {
        case 'No Exit': {
            return tradingEngine.tradingCurrent.tradingEpisode.candle.close.value
            break
        }
        case 'Take Profit': {
            return tradingEngine.tradingCurrent.position.takeProfit.finalValue.value
            break
        }
        case 'Stop Loss': {
            return tradingEngine.tradingCurrent.position.stopLoss.finalValue.value
            break
        }
    }
}
This formula discriminates among three possible outcomes:
  • When 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.
Target Size In Base Asset
Foundations->Node->Target Size In Base Asset->Definition
target-size-in-base-asset.png
Target size in base asset is used to define the size of the position, denominating it in the base asset.
Foundations->Node->Target Size In Base Asset->Content
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.tradingCurrent.tradingEpisode.episodeBaseAsset.balance.value
... or...
tradingEngine.tradingCurrent.tradingEpisode.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.tradingCurrent.strategyOpenStage.stageBaseAsset.sizeFilled.value - tradingEngine.tradingCurrent.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
Foundations->Node->Target Size In Quoted Asset->Definition
target-size-in-quoted-asset.png
Target size in quoted asset is used to define the size of the position, denominating it in the quoted asset.
Foundations->Node->Target Size In Quoted Asset->Content
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.
Previous
The Trigger Stage
Next
Open Stage Execution