#region Namespaces
using System;
using System.IO;
using System.Linq;
#endregion
namespace ScriptCode
{
/// <summary>
/// Trade Management Strategy scripts have multiple use-cases such as implementing position entry strategies,
/// implementing position exit strategies and implementing advanced order management systems, among others.
/// </summary>
public partial class MyTradeManagementStrategy : TradeManagementStrategyScriptBase // NEVER CHANGE THE CLASS NAME
{
#region Variables
// The quantities of the SL/TP orders.
private double[] _quantities;
// The stop loss offsets in points from the current price.
private double[] _stopLossOffsets;
// The profit target offsets in points from the current price.
private double[] _profitTargetOffsets;
#endregion
#region OnInitialize
/// <summary>
/// This function is used for accepting the script parameters and for initializing the script prior to all other function calls.
/// </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="quantity1" type="Double" default="0">The quantity of the 1st SL/TP orders</param>
/// <param name="stopLossOffset1" type="Double" default="0">The 1st stop loss offset in percent from the current price (set to zero to not set a stop loss)</param>
/// <param name="profitTargetOffset1" type="Double" default="0">The 1st profit target offset in percent from the current price (set to zero to not set a profit target)</param>
/// <param name="quantity2" type="Double" default="0">The quantity of the 2nd SL/TP orders</param>
/// <param name="stopLossOffset2" type="Double" default="0">The 2nd stop loss offset in percent from the current price (set to zero to not set a stop loss)</param>
/// <param name="profitTargetOffset2" type="Double" default="0">The 2nd profit target offset in percent from the current price (set to zero to not set a profit target)</param>
/// <param name="quantity3" type="Double" default="0">The quantity of the 2nd SL/TP orders</param>
/// <param name="stopLossOffset3" type="Double" default="0">The 3rd stop loss offset in percent from the current price (set to zero to not set a stop loss)</param>
/// <param name="profitTargetOffset3" type="Double" default="0">The 3rd profit target offset in percent from the current price (set to zero to not set a profit target)</param>
public void OnInitialize(
double quantity1,
double stopLossOffset1,
double profitTargetOffset1,
double quantity2,
double stopLossOffset2,
double profitTargetOffset2,
double quantity3,
double stopLossOffset3,
double profitTargetOffset3)
{
// Create for holding the quantities.
_quantities = new double[3];
// Set the first quantity.
_quantities[0] = quantity1;
// Set the second quantity.
_quantities[1] = quantity2;
// Set the third quantity.
_quantities[2] = quantity3;
// Create for holding the offsets.
_stopLossOffsets = new double[3];
// Set stop loss 1.
_stopLossOffsets[0] = stopLossOffset1;
// Set stop loss 2.
_stopLossOffsets[1] = stopLossOffset2;
// Set stop loss 3.
_stopLossOffsets[2] = stopLossOffset3;
// Create for holding the offsets.
_profitTargetOffsets = new double[3];
// Set take profit 1.
_profitTargetOffsets[0] = profitTargetOffset1;
// Set take profit 2.
_profitTargetOffsets[1] = profitTargetOffset2;
// Set take profit 3.
_profitTargetOffsets[2] = profitTargetOffset3;
}
#endregion
#region OnBarUpdate
/// <summary>
/// This function is called after each new bar belonging to the symbol that triggered the trade management strategy.
/// It can be used to evaluate the symbol and its new bar in order to submit, modify or cancel orders.
/// </summary>
/// <param name="symbolIndex" type="Integer">The index of the symbol in the symbol table</param>
/// <param name="dataSeries" type="Integer">The number indicating the data series from which the symbol was updated.
/// 0 for the main data series which streams tick data, 1 for the second data series which streams minute bars that are updated on bar update.</param>
/// <param name="completedBars" type="Integer">The number of completed bars for the specified symbol since the last call to OnBarUpdate.
/// Always 1 unless the bar type can generate multiple completed bars from a single tick/minute/day update (depending on the underlying bar source).</param>
public override void OnBarUpdate(
int symbolIndex,
int dataSeries,
int completedBars) {
}
#endregion
#region OnEntryOrder
/// <summary>
/// This function is called once for the entry order that triggered the script.
/// It's called before the order is submitted and can be used for setting attached orders or OCA groups.
/// </summary>
/// <param name="symbolIndex" type="Integer">The symbol index of the order that triggered the script</param>
/// <param name="orderIndex" type="Integer">The order index that triggered the script</param>
public override void OnEntryOrder(
int symbolIndex,
int orderIndex)
{
// Create a unique group ID for the OCA group.
long groupID = DateTime.UtcNow.Ticks;
// Iterate over the stop / loss offsets.
for (int i = 0; i < _stopLossOffsets.Length; i++) {
// Modify the group ID so that each SL and TP have a unique group.
groupID++;
// The stop loss order index.
int stopLossOrderIndex = 0;
// The take profit order index.
int takeProfitOrderIndex = 0;
// Check whether the order is a long order.
if (OrderActionType(orderIndex) == C_ActionType.BUY || OrderActionType(orderIndex) == C_ActionType.BUY_TO_COVER) {
// Check whether a stop loss has been set.
if (_stopLossOffsets[i] != 0) {
// Set a stop loss for the order
stopLossOrderIndex = BrokerStop(C_ActionType.SELL, _quantities[i], C_TIF.GTC, OrderExpectedPrice(orderIndex) - _stopLossOffsets[i], "Stop Loss");
// Set the stop loss as part of an OCA group.
BrokerSetOCA(stopLossOrderIndex, "GROUP" + groupID, C_OCA_Type.CANCEL);
// Register the stop loss order.
TradeManagementRegisterOrder(stopLossOrderIndex);
}
// Check whether a profit target has been set.
if (_profitTargetOffsets[i] != 0) {
// Set a profit target for the order
takeProfitOrderIndex = BrokerLimit(C_ActionType.SELL, _quantities[i], C_TIF.GTC, OrderExpectedPrice(orderIndex) + _profitTargetOffsets[i], "Take Profit");
// Set the take profit as part of an OCA group.
BrokerSetOCA(takeProfitOrderIndex, "GROUP" + groupID, C_OCA_Type.CANCEL);
// Register the profit target order.
TradeManagementRegisterOrder(takeProfitOrderIndex);
}
}
else {
// Check whether a stop loss has been set.
if (_stopLossOffsets[i] != 0) {
// Set a stop loss for the order
stopLossOrderIndex = BrokerStop(C_ActionType.BUY, _quantities[i], C_TIF.GTC, OrderExpectedPrice(orderIndex) + _stopLossOffsets[i], "Stop Loss");
// Set the stop loss as part of an OCA group.
BrokerSetOCA(stopLossOrderIndex, "GROUP" + groupID, C_OCA_Type.CANCEL);
// Register the stop loss order.
TradeManagementRegisterOrder(stopLossOrderIndex);
}
// Check whether a profit target has been set.
if (_profitTargetOffsets[i] != 0) {
// Set a profit target for the order
takeProfitOrderIndex = BrokerLimit(C_ActionType.BUY, _quantities[i], C_TIF.GTC, OrderExpectedPrice(orderIndex) - _profitTargetOffsets[i], "Take Profit");
// Set the take profit as part of an OCA group.
BrokerSetOCA(takeProfitOrderIndex, "GROUP" + groupID, C_OCA_Type.CANCEL);
// Register the profit target order.
TradeManagementRegisterOrder(takeProfitOrderIndex);
}
}
}
}
#endregion
#region OnShutdown
/// <summary>
/// This function is called when the script is shutdown.
/// </summary>
public override void OnShutdown()
{
// OnShutdown Content
}
#endregion
}
}