#region Namespaces
using System;
using System.IO;
using System.Linq;
#endregion
namespace ScriptCode
{
/// <summary>
/// Slippage scripts are used for simulating market slippage by setting the final execution price of each simulated order fill.
///
/// This script can be used in several ways:
/// (1) It can be used to test the impact of various market conditions on a trading strategy's performance.
/// (2) It can be used to manipulate the final execution price of each order fill.
/// </summary>
public partial class MySlippage : SlippageScriptBase // NEVER CHANGE THE CLASS NAME
{
#region Variables
// The maximum number of ticks slippage per order.
private int _maxTicks;
// The random number generator.
private Random _random;
#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>
/// --------------------------------------------------------------------------------------------------
/// PLEASE USE THE SCRIPT WIZARD (CTRL+W) TO ADD, EDIT AND REMOVE THE SCRIPT PARAMETERS
/// --------------------------------------------------------------------------------------------------
/// 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'
/// Set to "DateTimeArray" when the data type is 'long[]'
/// 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="maxTicks" type="Integer" default="1">The maximum number of ticks slippage per order.</param>
public void OnInitialize(
int maxTicks)
{
// Set the maximum slippage ticks.
_maxTicks = maxTicks;
// Create the random number generator.
_random = new Random();
}
#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>
public override double OnOrderFillSlippage(
int strategyNumber,
int orderIndex,
int orderFillIndex,
double fillQuantity,
double fillPrice)
{
// Get the tick size of the order symbol.
double symbolTickSize = SymbolTickSize(strategyNumber, OrderSymbolIndex(strategyNumber, orderIndex));
// Get the order action type.
C_ActionType orderActionType = OrderActionType(strategyNumber, orderIndex);
// Get the random number of ticks.
int ticks = _random.Next(0, _maxTicks + 1);
// Check whether the order is a buy order.
if (orderActionType == C_ActionType.BUY ||
orderActionType == C_ActionType.BUY_TO_COVER)
// Calculate the fixed order fill after the slippage.
return fillPrice + ticks * symbolTickSize;
// Check whether the order is a sell order.
if (orderActionType == C_ActionType.SELL ||
orderActionType == C_ActionType.SELL_SHORT)
// Calculate the fixed order fill after the slippage.
return fillPrice - ticks * symbolTickSize;
// Return the original fill price.
return fillPrice;
}
#endregion
#region OnShutdown
/// <summary>
/// This function is called when the script is shutdown.
/// </summary>
public override void OnShutdown()
{
// OnShutdown Content
}
#endregion
}
}