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

Cannot open a position and a stop market order at same time

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

    Cannot open a position and a stop market order at same time

    Hi,
    In the strategy I am working on there is going to be a situation whereby the code will be looking multiple entries and managing the STOPLOSS levels.

    This is a brief explanation of what the code will be facing under live conditions.
    First, the code will open a stop order with stoploss and takeprofit targets.
    Then there will be conditions when after the stop order is filled that a new signal will appear.
    The code will then need to do the following:

    1. create a new stop order (with targets) from new signal
    2. if there is an existing opened position the code needs to examine the stoploss target and see where it falls when compared to the new stop order LEVEL, ie we want the position to always close before the new stop level is filled..

    In order to test this condition I need to have an open position with its own stop/limit targets and also a stop market order with its own stop/limit targets.

    I am trying to simulate the simple scenario, I created a strategy that is supposed to do jus that open a position and a stop market order.
    So far I am not able to open the stop market order.
    Are there any examples out there that can provide this type of position/order setup?

    Here is a simple snippet of the way I am attempting to simulate an open position and a new stop market order. Please advice.
    Jess

    Code:
    if (State == State.SetDefaults)
    {
    Blah... blah
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 2;
    blah .. blah
    }  
      ....
    
    protected override void OnBarUpdate()
    {
    if(State == State.Historical) return;
    if (Bars.IsFirstBarOfSession && IsFirstTickOfBar) doneForSession = false;
    if (doneForSession) return;
    
    //Enter a long position
    if(Close[1]<Open[1] && Position.Quantity == 0) {
    EnterLong(1, "s"+Time[0]); //OPEN POSITION SetStopLoss("s"+Time[0], CalculationMode.Ticks, 20,false); //SET STOPLOSS 20 ticks away SetProfitTarget("s"+Time[0], CalculationMode.Ticks, 20); //SET TAKE PROFIT 20 ticks away
    } //if we have 1 position open, create a long stop market order if (Position.Quantity == 1) {
    EnterLongStopMarket(0,true,1,High[1]+5*TickSize,"s2"+Time[0]); //OPEN STOP MARLET ORDER SetStopLoss("s2"+Time[0], CalculationMode.Ticks, 20,false); //SET STOPLOSS 20 ticks away(when filled) SetProfitTarget("s2"+Time[0], CalculationMode.Ticks, 20); //SET TAKE PROFIT 20 ticks away(when filled) } doneForSession=true;
    } }

    #2
    Hello xmess777,

    Do you currently have TraceOrders enabled? If you do not I would suggest to enable that and make sure you are not seeing any ignored order messages. Have you otherwise added a print to make sure the condition is true when you think it is?

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse:
      thanks for the quick response. I don't know if I have TraceOrders activated. Please let me know how I can check this option and of what value it would provide.

      Yes, I do use the print function for debugging. I created my own debug printing function which prints formatted text onto the chart. It's better to see debug messages like this, otherwise I have to keep pulling up the output window, after a while it becomes a drag .

      UPDATE:
      I got the strategy to start working. I can now see a new order placed right after a position is opened. I also see that when the 1st position is closed that the new order is filled properly.
      That was the difference. The position had to be closed before the new order could get filled, otherwise the new order was ignored. This will work for now as I don't think I will see conditions where a new order has to get filled while there is an open position.


      QUESTION:
      How does EntriesPerDirection affect this example?
      If EntriesPerDirection=1 and we have 1 open long position then try to open another long position, that would not be allowed right?
      I assume that a open long position and a long STOP Market order is would be ok, right?
      And finally, a hedge would not be an issue, correct?
      Please expand .


      This is the updated code . This code does this:
      1. Open a long position (with stop/profit targets) when Close[1]<Open[1]
      2. Create a long stop market order ( with it's own stop/profit targets) when again Close[1]<Open[1]
      3. Immediately when stop market order is successfully created Moves the open Positions stoploss target to the order's stopfill price.

      NOTE: I use "s"+Time[0] and "s2"+Time[0] to create unique names. This naming convention works if you are working on the same bar.
      One solution is to hold the names in a dynamic array that you will reference later on to get the name of an order that was created several bas ago.

      Thnx, Jess

      Code:
      public Order entryOrder;
      
      if (State == State.SetDefaults) {
      Blah... blah Calculate = Calculate.OnBarClose; EntriesPerDirection = 2; blah .. blah
      }
      ....
      protected override void OnBarUpdate() {
      if(State == State.Historical) return; if (Bars.IsFirstBarOfSession && IsFirstTickOfBar) doneForSession = false; if (doneForSession) return;
      //Enter a long position if(Close[1]<Open[1] && Position.Quantity == 0) {
      EnterLong(1, "s"+Time[0]); //OPEN POSITION SetStopLoss("s"+Time[0], CalculationMode.Ticks, 20,false); //SET STOPLOSS 20 ticks away SetProfitTarget("s"+Time[0], CalculationMode.Ticks, 20); //SET TAKE PROFIT 20 ticks away
      } //if we have 1 position open, create a long stop market order if (entryOrder==null && Close[1]<Open[1]) {
      EnterLongStopMarket(0,true,1,High[1]+5*TickSize,"s2"+Time[0]); //OPEN STOP MARLET ORDER SetStopLoss("s2"+Time[0], CalculationMode.Ticks, 20,false); //SET STOPLOSS 20 ticks away(when filled) SetProfitTarget("s2"+Time[0], CalculationMode.Ticks, 20); //SET TAKE PROFIT 20 ticks away(when filled) //This is the CHANGE I made. In this line the POSITION's STOPLOSS TARGET is adjusted to that of the STOP ORDER's entry stop price if(entryOrder!=null) {
      SetProfitTarget("s"+Time[0], CalculationMode.Price, +entryOrder.StopPrice);
      } else {
      debugPrint("Something wrong, stop order was not created.."); //debugPrint is my own function for displaying print statements onto the chart :)
      } doneForSession=true;
      }
      } protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError) {
      if(order.Name.Contains("s2") && entryOrder==null) { entryOrder=order; return; } if (entryOrder!=null && order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Filled || order.OrderState == OrderState.Rejected) entryOrder=null
      }
      Last edited by xmess777; 10-20-2021, 10:16 AM.

      Comment


        #4
        Hello xmess777,


        The EntriesPerDirection simply prevents multiple entries in the same direction that are happening at the same time. If you have logic that entries more than once you would need to change that value to a higher value.
        You can open a position and the submit other orders as you have done with your example.

        You can read about traceorders here: https://ninjatrader.com/support/help...ub=traceorders

        For the hedge question what specific situation are you asking about, are you asking for entries in both directions?

        I look forward to being of further assistance.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Jesse:

          There are several hedging conditions I was thinking about:
          1. Two Stop Orders, Long and Short)
          2. 1 Stop Order Long and one position Short, and vice versa

          Just wanted to know how each conditions is affected when EntriesPerDirection is set to 1.
          I am assuming that EntriesPerDirection only affects positions that are placed in same direction, hence then the other conditions would not be affected in any way.


          Thnx for the support. The intent of the test code is now working properly and I am seeing all of the expected results. I now have total control of multiple orders/positions and can determine how and to when to change any of the position stoploss targets according to conditions of any new STOP orders that appear.

          Jess

          Comment


            #6
            Hello xmess777,

            Any time you are trying to do long + short or having two directions open at once you would have to use unmanaged approach so the entries per direction wouldn't matter for that situation. The managed approach sees that as invalid because you can either be long or short not both (some actions will reverse the position). In the unmanaged approach you can submit a long and short entry and then cancel the one that does not fill.

            The entries per direction applies to the same direction entries, its essentially just a limit on how much you can call EnterLong at the same time as a simple example.

            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by AlessioCianini12, Today, 01:38 AM
            0 responses
            26 views
            0 likes
            Last Post AlessioCianini12  
            Started by nandhumca, Yesterday, 11:54 PM
            0 responses
            26 views
            0 likes
            Last Post nandhumca  
            Started by nandhumca, Yesterday, 11:51 PM
            0 responses
            25 views
            0 likes
            Last Post nandhumca  
            Started by ezrollin, Yesterday, 11:48 PM
            2 responses
            27 views
            0 likes
            Last Post ezrollin  
            Started by jjanguda, Yesterday, 10:15 PM
            1 response
            26 views
            0 likes
            Last Post NinjaTrader_ChrisSch  
            Working...
            X