Marker If Touched + SL + TP

Add-On

#region Namespaces
using System;
using System.IO;
using System.Linq;
#endregion 

namespace ScriptCode {
	/// <summary>
	/// Add-On scripts are used for adding functionality to the platform.
	/// </summary>
	public partial class MyAddOn : AddOnScriptBase // NEVER CHANGE THE CLASS NAME
	{
		#region Variables
		// The order action type.
		private C_ActionType _actionType;
		// The order quantity.
		private double _quantity;
		// The order TIF.
		private C_TIF _timeInForce;
		// The order touch price.
		private double _touchPrice;
		// The order comment.
		private string _comment;
		// The order stop loss offset.
		private double _stopLossOffset;
		// The order profit target offset.
		private double _profitTargetOffset;
		#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="actionType" type="C_ActionType" default="BUY">The action type </param>
		/// <param name="quantity" type="Double" default="0">The order quantity</param>
		/// <param name="timeInForce" type="C_TIF" default="DAY">The order TIF </param>
		/// <param name="touchPrice" type="Double" default="0">The order touch price</param>
		/// <param name="comment" type="String" default="">The order comment</param>
		/// <param name="stopLossOffset" type="Double" default="0"  min="0" max="100">The stop loss offset in points from the current price (set to zero to not set a stop loss)</param>
		/// <param name="profitTargetOffset" type="Double" default="0"  min="0" max="100">The profit target offset in points from the current price (set to zero to not set a profit target)</param>
		public void OnInitialize(
			 C_ActionType actionType,
			 double quantity,
			 C_TIF timeInForce,
			 double touchPrice,
			 string comment,
			 double stopLossOffset,
			 double profitTargetOffset) {
			_actionType = actionType;
			_quantity = quantity;
			_timeInForce = timeInForce;
			_touchPrice = touchPrice;
			_comment = comment;
			_stopLossOffset = stopLossOffset;
			_profitTargetOffset = profitTargetOffset;
		}
		#endregion

		#region OnTriggered
		/// <summary>
		/// This function is called when the add-on is triggered by the user.
		/// </summary>
		/// <param name="strategyNumber" type="Integer">The strategy number on which the add-on was called (or -1 if none is available)</param>
		/// <param name="symbolIndex" type="Integer">The symbol index of the symbol on which the add-on was called (or -1 if none is available)</param>
		/// <param name="index" type="Integer">The item index of the item on which the add-on was called, such as the order index, trade index and position index (or -1 if none is available)</param>
		public override void OnTriggered(
			int strategyNumber,
			int symbolIndex,
			int index) {
			// The limit price.
			double limitPrice;

			//Submit the order	
			int orderIndex = BrokerMarketIfTouched(strategyNumber, symbolIndex, _actionType, _quantity, _timeInForce, _touchPrice, _comment);

			// Check whether the order is a long order.
			if (OrderDirection(strategyNumber, orderIndex) == C_Direction.LONG_SIDE) {

				// Check whether a stop loss has been set.
				if (_stopLossOffset != 0) {
					// Calculate the limit price for the stop loss order.
					limitPrice = _touchPrice - _stopLossOffset;
					// Set a stop loss for the order.
					BrokerSetStopLoss(strategyNumber, orderIndex, limitPrice, "Stop Loss");
				}
				// Check whether a profit target has been set.
				if (_profitTargetOffset != 0) {
					// Calculate the limit price for the take profit order.
					limitPrice = _touchPrice + _profitTargetOffset;
					// Set a profit target for the order.
					BrokerSetTakeProfit(strategyNumber, orderIndex, limitPrice, "Take Profit");
				}
			}
			// The order is a short order.
			else {
				// Check whether a stop loss has been set.,
				limitPrice = _touchPrice + _stopLossOffset;
				// Set a stop loss for the order.
				BrokerSetStopLoss(strategyNumber, orderIndex, limitPrice, "Stop Loss");
			}
			// Check whether a profit target has been set.
			if (_profitTargetOffset != 0) {
				// Calculate the limit price for the take profit order.
				limitPrice = _touchPrice - _profitTargetOffset;
				// Set a profit target for the order.
				BrokerSetTakeProfit(strategyNumber, orderIndex, limitPrice, "Take Profit");
			}
		}
		#endregion
	}
}