Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Exit, stop loss and profit target with the unmanaged approach

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Exit, stop loss and profit target with the unmanaged approach

    Hello,

    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.

    #2
    Hello stevie,

    Thank you for your inquiry.

    I would suggest taking a look at the Unmanaged Approach section in the NinjaTrader help guide for further information on utilizing the Unmanaged Approach: https://ninjatrader.com/support/help...d_approach.htm

    To exit a position by using the Unmanaged Approach, you would use a SubmitOrder() that submits an order in the opposite direction of your current position to exit that position. You'll want to ensure to check for a position first, however, as unlike the Managed Approach, the SubmitOrder() method call will not be checking to see if you're in a position before executing.

    If you want to exit a long position, you'll want to utilize the Sell orderAction. If you want to exit a short position, you'll want to utilize the BuyToCover orderAction.

    I would suggest taking a look at this particular reference sample on our forum on how to use OnOrderUpdate() and OnExecution() methods to submit protective orders: http://ninjatrader.com/support/forum...ead.php?t=7499

    Rather than using the Exit() methods, however, you would utilize SubmitOrder() as well for your stop loss and profit targets.

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by gravdigaz6, Today, 11:40 PM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by MarianApalaghiei, Today, 10:49 PM
    3 responses
    10 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by XXtrader, Today, 11:30 PM
    0 responses
    4 views
    0 likes
    Last Post XXtrader  
    Started by love2code2trade, Yesterday, 01:45 PM
    4 responses
    28 views
    0 likes
    Last Post love2code2trade  
    Started by funk10101, Today, 09:43 PM
    0 responses
    9 views
    0 likes
    Last Post funk10101  
    Working...
    X