Available translations

The Close Stage

foundations.png
Summary: The close stage is a mirror of the open stage, with definitions concerning the closing of the position, including the close execution.
Foundations->Node->Close Stage->Definition
close-stage.png
The close stage deals with the definitions that make up the logic to close a position, including the target rate and size, and order execution.
Foundations->Node->Close Stage->Content
Optionality
There are situations where users might want to execute both buy and sell orders from within the Open Stage. In those situations they might want to delete the Manage Stage and the Close Stage so that they don't execute at all. That is possible because both are optional. This is how optionality looks like at the code:
 /*
The system allows the user not to define a close stage, because the close stage is optional.
Here we are goint to see if that is the case and if it is, we will inmidiatelly consider 
the close stage as closed.
*/
if (tradingSystem.tradingStrategies[tradingEngine.tradingCurrent.strategy.index.value].closeStage === undefined) {
    changeStageStatus('Close Stage', 'Closed', 'Close Stage Undefined')
    return
}
Close Stage Concept
The close stage is opened when either the stop loss or take profit targets are hit. It is also opened after the position is opened if the Manage Stage is undefined.
In a way, the close stage is virtually a mirror of the open stage, as the same definitions are required to close a position. There are a few logical differences, though.
For example, in the open stage, you probably defined the target size as a function of the capital allocated to the trading system. In the close stage, you will likely want to define the target size as a function of the size filled, that is, the amount of the orders placed that was filled during the open stage.
In the event that the target size of the asset to be traded in Close Stage is equal to zero then Close Stage will close as will the currently running strategy. For example: Suppose the Open Stage was to trade x amount of quoted asset at y price via a limit order. But instead price moved in the opposite direction and to the point that Open Stage closed prior to the accumulation of any of the Base Asset which was to be traded via the Close Stage. In that scenario Close Stage would not take any action and would Close the current instance of the Strategy and the system would resume polling for another Trigger On event.
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)
        }
Previous
Position Management Phases