How to Apply a Constant Tick Slippage

How to Apply a Constant Tick Slippage

#region Namespaces
# ---------- DON'T REMOVE OR EDIT THESE LINES -------------------
# These lines are required for integrating Python with our .NET platform.
import clr
clr.AddReference("Tickblaze.Model")
import ScriptCode
from SlippageAPI import *
from AssemblySlippage_3000_ImportedScripts import *
# ---------------------------------------------------------------
#endregion

## <summary>
## Slippage scripts are used for simulating market slippage by setting the final execution price of each simulated order fill. 
## Common use-cases include testing the impact of various market conditions on a trading strategy's performance and manipulating the final execution price of each order fill.
## </summary>
class MySlippage(ScriptCode.SlippageScriptBase): # NEVER CHANGE THE CLASS NAME
    #region Variables
    # Variables Content
    #endregion

    #region OnInitialize
    ## <summary>
    ## This function is used for accepting the script parameters and for initializing the script prior to all other function calls.
    ## Once the script is assigned to a desktop, its parameter values can be specified by the user and can be selected for optimization. 
    ## </summary>
    ## --------------------------------------------------------------------------------------------------
    ##                                 INSTRUCTIONS - PLEASE READ CAREFULLY
    ## --------------------------------------------------------------------------------------------------
    ## YOU MUST SET A PARAM TAG FOR EACH PARAMETER ACCEPTED BY THIS FUNCTION.
    ## ALL PARAM TAGS SHOULD BE SET IN THE 'OnInitialize' REGION, RIGHT ABOVE THE 'OnInitialize' FUNCTION.
    ## THE ORDER OF THE TAGS MUST MATCH THE ORDER OF THE ACTUAL PARAMETERS.

    ## REQUIRED ATTRIBUTES:
    ## (1) name: The exact parameter name.
    ## (2) type: The type of data to collect from the user: 
    ## Set to "Integer" when the data type is 'int'
    ## Set to "IntegerArray" when the data type is 'int[]'
    ## Set to "DateTime" when the data type is 'long' (The 'long' data type can only be used for date/time representation)
    ## Set to "DateTimeArray" when the data type is 'long[]' (The 'long' data type can only be used for date/time representation)
    ## Set to "Boolean" when the data type is 'bool'
    ## Set to "BooleanArray" when the data type is 'bool[]'
    ## Set to "Double" when the data type is 'double'
    ## Set to "DoubleArray" when the data type is 'double[]'
    ## Set to "String" when the data type is 'string'
    ## Set to "StringArray" when the data type is 'string[]'

    ## OPTIONAL ATTRIBUTES:
    ## (3) default: The default parameter value is only valid when the type is Integer, Boolean, Double, String or an API Type. 
    ## (4) min: The minimum parameter value is only valid when the type is Integer or Double.
    ## (5) max: The maximum parameter value is only valid when the type is Integer or Double.

    ## EXAMPLE: <param name="" type="" default="" min="" max="">Enter the parameter description here.</param>
    ## --------------------------------------------------------------------------------------------------
	## <param name="ticks" type="Integer" default="1">The number of ticks slippage per order.</param>    
    def OnInitialize(self,ticks):
        # Create a variable to store the tick slippage amount provided.
        self._ticks = ticks
    #endregion

    #region OnOrderFillSlippage
    ## <summary>
    ## This function is called on each new order fill in order to calculate its slippage.
    ## </summary>
    ## <param name="strategyNumber" type="Integer">The desktop strategy number to which the order belongs</param>
    ## <param name="orderIndex" type="Integer">The order index in the orders table to which the order fill belongs</param>
    ## <param name="orderFillIndex" type="Integer">The order fill index (0 being the first fill for the order, 1 being the next fill, etc.)</param>
    ## <param name="fillQuantity" type="Double">The quantity of the new order fill for the specified order</param>
    ## <param name="fillPrice" type="Double">The price of the new order fill for the specified order</param>
    ## <returns type="Double">The price of the order fill after slippage (return the specified price if there was no slippage).</returns> 
    def OnOrderFillSlippage(self, strategyNumber, orderIndex, orderFillIndex, fillQuantity, fillPrice):
        # Get the tick size of the order symbol.
        symbolTickSize = SymbolTickSize(strategyNumber, OrderSymbolIndex(strategyNumber, orderIndex))
        # Get the order action type.
        orderActionType = OrderActionType(strategyNumber, orderIndex)
        
        # Create a variable to hold whether the slippage should be added or subtracted to the fill price.
        direction = 0;
	# Check whether the order is a buy order.
        if orderActionType == C_ActionType.BUY or orderActionType == C_ActionType.BUY_TO_COVER:
            # Record that the slippage should be added.
            direction = 1
        #Check whether the order is a sell order.
        elif orderActionType == C_ActionType.SELL or orderActionType == C_ActionType.SELL_SHORT:
            # Record that the slippage should be subtracted.
            direction = -1
        
        # Return the fixed order fill after the slippage.
        return fillPrice + direction * symbolTickSize * self._ticks
    #endregion

    #region OnShutdown
    ## <summary>
    ## This function is called when the script is shutdown.
    ## </summary>
    def OnShutdown(self):
        # OnShutdown Content
        pass
    #endregion