Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

2 orders in the same time

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

    2 orders in the same time

    Hello,
    In my strategy I want open 2 orders in the same time.
    myEntryOrderLONG = EnterLongLimit(0, true, noContratti, PgPDn,"EntryLONG");

    myEntryOrderSHORT = EnterShortLimit(0, true, noContratti, PgPUp, "EntrySHORT");

    Unfortunately I noticed that just one is opened (see the attachment file).
    What is wrong on my program?
    Thank you for jour help,
    Michele


    --------------------------
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class uuuuu : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int myInput0 = 1; // Default setting for MyInput0
    private int noContratti = 1;
    // Gestione degli ordini
    private IOrder myEntryOrderLONG = null; //
    private IOrder myExitOrderLONG = null; //
    private IOrder myEntryOrderSHORT = null; //
    private IOrder myExtitOrderSHORT = null; //
    private IOrder myStoploss = null; //
    private bool newTrade = true;
    // Gestione dei prezzi
    private double PgPUp; // Prezzo Ping Pong alto
    private double PgPDn; // Prezzo Ping Pong basso
    private int range; // altezza range in corso
    private int PrecRange; // Altezza range precedente
    // Altro
    #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 = false;
    IncludeCommission = true;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    }
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    //Exclure toutes les données historiques
    if(Historical)
    return;

    if(FirstTickOfBar)
    {
    //Uscita di sicurezza dal trading
    if (myExitOrderLONG != null)
    {
    ExitLong("UscitaBrutaleLONG", "");
    }
    if (myExtitOrderSHORT != null)
    {
    ExitShort("UscitaBrutaleSHORT", "");
    }


    PrecRange = 0;

    if ((myEntryOrderLONG == null && myEntryOrderSHORT == null) ||
    ((myEntryOrderLONG != null && myEntryOrderLONG.LimitPrice < Open[0]) &&
    (myEntryOrderSHORT != null && myEntryOrderSHORT.LimitPrice > Open[0])
    )
    )
    {
    PgPUp = Open[0] + TickSize * (1 + BarsPeriod.Value);
    myEntryOrderLONG = EnterLongLimit(0, true, noContratti, PgPDn,"EntryLONG");
    PgPDn = Open[0] - TickSize * (1 + BarsPeriod.Value);
    myEntryOrderSHORT = EnterShortLimit(0, true, noContratti, PgPUp, "EntrySHORT");
    newTrade = true;
    }
    else
    newTrade = false;

    }
    range = Convert.ToInt16((High[0] - Low[0]) / TickSize);
    if (PrecRange < range && newTrade)
    {
    PgPUp = Low[0] + TickSize * (1 + BarsPeriod.Value);
    if(myEntryOrderSHORT != null && myEntryOrderSHORT.LimitPrice != PgPUp)
    myEntryOrderSHORT = EnterShortLimit(0, true, noContratti, PgPUp, "EntrySHORT");

    PgPDn = High[0] - TickSize * (1 + BarsPeriod.Value);
    if(myEntryOrderLONG != null && myEntryOrderLONG.LimitPrice != PgPDn)
    myEntryOrderLONG = EnterLongLimit(0, true, noContratti, PgPDn,"EntryLONG");

    PrecRange = range;
    }

    }


    /// <summary> ----------------------------------------------------------------------------------------
    /// Questo medoto è attivo ogni qualvolta avviene un cambiamento di stato nell'ordine
    /// </summary> ---------------------------------------------------------------------------------------
    protected override void OnOrderUpdate(IOrder order)
    {
    double valexit;
    if (myEntryOrderLONG != null && myEntryOrderLONG == order)
    {
    // Reset the entryOrder object to null if order was cancelled without any fill
    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    {
    myEntryOrderLONG = null;
    }
    }

    if (myEntryOrderSHORT != null && myEntryOrderSHORT == order)
    {
    // Reset the entryOrder object to null if order was cancelled without any fill
    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    {
    myEntryOrderSHORT = null;
    }
    }
    }


    /// <summary> ----------------------------------------------------------------------------------------
    /// Called on each incoming execution
    /// </summary> ----------------------------------------------------------------------------------------
    protected override void OnExecution(IExecution execution)
    {
    double valexit;


    // ----------- LONG ----------------
    if (myEntryOrderLONG != null && myEntryOrderLONG == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    myStoploss = ExitLongStop(execution.Order.LimitPrice - 2*TickSize, "StopLossLONG", "EntryLONG");
    myExitOrderLONG = ExitLongLimit(execution.Order.LimitPrice + TickSize,
    "EsciLONG", "EntryLONG");
    if (myEntryOrderSHORT != null)
    CancelOrder(myEntryOrderSHORT);

    // Resets the entryOrder object to null after the order has been filled
    if (execution.Order.OrderState != OrderState.PartFilled)
    {
    myEntryOrderLONG = null;
    }
    }
    }
    // ------------ SHORT ---------------
    if (myEntryOrderSHORT != null && myEntryOrderSHORT == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    myStoploss = ExitShortStop(execution.Order.LimitPrice + 2*TickSize , "StopLossSHORT", "EntrySHORT");
    myExtitOrderSHORT = ExitShortLimit(execution.Order.LimitPrice - TickSize,
    "EsciSHORT", "EntrySHORT");
    if (myEntryOrderLONG != null)
    CancelOrder(myEntryOrderLONG);

    // Resets the entryOrder object to null after the order has been filled
    if (execution.Order.OrderState != OrderState.PartFilled)
    {
    myEntryOrderSHORT = null;
    }
    }
    }


    // Reset our stop order and target orders' IOrder objects after our position is closed.
    if ((myStoploss != null && myStoploss == execution.Order) || (myExtitOrderSHORT != null && myExtitOrderSHORT == execution.Order)
    || (myExitOrderLONG != null && myExitOrderLONG == execution.Order))
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    {
    myStoploss = null;
    myExtitOrderSHORT = null;
    myExitOrderLONG = null;
    }
    }
    }




    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public int MyInput0
    {
    get { return myInput0; }
    set { myInput0 = Math.Max(1, value); }
    }
    [Description("N° di contratti per operazione")]
    [GridCategory("Parameters")]
    public int NoContratti
    {
    get { return noContratti; }
    set { noContratti = value; }
    }
    #endregion
    }
    }
    Attached Files

    #2
    According to the explanation http://www.ninjatrader.com/support/helpGuides/nt7/index.html?maximum_max.htm “Internal Order Handling Rules that Reduce Unwanted Positions “ it’s impossible to place two oders in opposite side with the managed approach.
    Is that true?
    If it’s true may where can I find an example of unmanaged approach?
    Thanks.

    Michel

    Comment


      #3
      Hello,

      Yes this is correct. Unfortunately we don't currently have an example of the an unmanaged OCO strategy but you can find more information on using OCO ENTRY orders in NinjaScript here: http://www.ninjatrader.com/support/h...d_approach.htm

      I would suggest asking again on 3/1/13 as we do plan on adding an example of this to the forums.

      Please let me know if I can be of further assistance.
      LanceNinjaTrader Customer Service

      Comment


        #4
        Hello,
        Thank you for your response.

        I have a question about the parameter for de SubmitOrder() function.
        What’s the difference between OrderAction.Buy and OrderAction.BuyToCover or OrderAction.Sell and OrderAction.SellShort?

        May I have more explanation about ocoId? When it's use?

        Thanks,

        Michel

        Comment


          #5
          These are used to help tell the broker what you're trying to accomplish related to position.

          Buy = Enter long
          Sell = Exit long

          SellShort = Enter Short
          BuyToCover = Exit Short
          MatthewNinjaTrader Product Management

          Comment


            #6
            Matthew,
            May I ask you how to use the OCOID ?
            Thank you,
            Michel

            Comment


              #7
              Hello,

              Here is a short example of how you could submit a long and short order entry at the same time with OCO functionality

              Code:
                              if (Bars.FirstBarOfSession)
                              {
                                      shortEntry         = SubmitOrder(0, OrderAction.SellShort, OrderType.Limit, OrderQuantity, High[0]+5*TickSize, 0, "entry", "Short limit entry");
              
                                      longEntry         = SubmitOrder(0, OrderAction.Buy, OrderType.Limit, OrderQuantity, Low[0]- 5*TIckSize, 0,  "entry", "long limit entry");
              
                              }
              Notice how both orders have the tag "entry" for their OCO ID.

              When one is filled the other will be canceled.

              If you remind me sometime in March I'll be able to provide a full detailed example.

              Let me know if I can be of further assistance.
              LanceNinjaTrader Customer Service

              Comment


                #8
                Hello,

                Could you post a complete example on unmanaged oco strategy? If so where it is?

                Thanks,

                Michel

                Comment


                  #9
                  Thanks for your post.

                  Here is the link to the strategy on our forums: http://www.ninjatrader.com/support/f...tid=-1&lpage=1

                  It contains the indicator the strategy uses along with a managed and unmanaged (OCO) version of the strategy
                  LanceNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  672 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  379 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  111 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  577 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  582 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X