I am developing a new strategy with the unmanaged approach. When I enter in a long/short position, I don't know how to exit this position, then set a stop loss order and a profit target order for the position.
Here is a part of my script :
public class StevieStrategy : Strategy
{
#region Variables
// User defined variables (add any user defined variables below)
private int quantity = 1; // Valeur par défaut pour Quantity
private int target = 30; // Valeur par défaut pour Target
private IOrder longOrder = null;
private IOrder shortOrder = null;
private IOrder stopLossOrder = null;
private IOrder profitTargetOrder = null;
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
Unmanaged = true;
// TraceOrders = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Buying condition
if (Close[0] < Bollinger(2, 14).Lower[0])
{
longOrder = SubmitOrder(0,OrderAction.Buy,OrderType.Market,qua ntity,0,0,"","ELong");
// SubmitOrder(0,OrderAction.Sell,OrderType.StopLimit ,quantity,0,0,"","XLong");
}
// Shorting condition
else if (Close[0] > Bollinger(2, 14).Upper[0])
{
shortOrder = SubmitOrder(0,OrderAction.SellShort,OrderType.Mark et,quantity,0,0,"","EShort");
// SubmitOrder(0,OrderAction.Buy,OrderType.StopLimit, quantity,0,0,"","XShort");
}
}
protected override void OnExecution(IExecution execution)
{
if (longOrder != null && longOrder == execution.Order)
{
if (execution.MarketPosition == MarketPosition.Long)
{
if (shortOrder != null)
CancelOrder(shortOrder);
shortOrder = null;
if (Close[0] > Low[0])
{
stopLossOrder = SubmitOrder(0,OrderAction.Sell,OrderType.Stop,quan tity,0,MIN(Low,2)[0] - 1*TickSize,"","LongSL");
}
profitTargetOrder = SubmitOrder(0,OrderAction.Sell,OrderType.Limit,qua ntity,target,0,"","LongPT");
}
}
else if (shortOrder != null && shortOrder == execution.Order)
{
if (execution.MarketPosition == MarketPosition.Short)
{
if (longOrder != null)
CancelOrder(longOrder);
longOrder = null;
if (Close[0] < High[0])
{
stopLossOrder = SubmitOrder(0,OrderAction.BuyToCover,OrderType.Sto p,quantity,0,MAX(High,3)[0] + 1*TickSize,"","ShortSL");
}
profitTargetOrder = SubmitOrder(0,OrderAction.BuyToCover,OrderType.Lim it,quantity,target,0,"","ShortPT");
}
}
// Resets the longOrder and shortOrder objects to null after the order has been filled
// if (execution.Order.OrderState != OrderState.PartFilled)
// {
// longOrder = null;
// shortOrder = null;
// }
// Reset our stop order and target orders' IOrder objects after our position is closed.
if ((stopLossOrder != null && stopLossOrder == execution.Order) || (profitTargetOrder != null && profitTargetOrder == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
stopLossOrder = null;
profitTargetOrder = null;
}
}
}
#region Properties
[Description("Quantity of the underlying asset")]
[GridCategory("Parameters")]
public int Quantity
{
get { return quantity; }
set { quantity = Math.Max(1, value); }
}
[Description("Target of this position")]
[GridCategory("Parameters")]
public int Target
{
get { return target; }
set { target = Math.Max(1, value); }
}
#endregion
}
Am I wrong whn I write that? Could anyone help me, please?
Thank you!
PS: Excuse my English.
Comment