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

One active position exists in each moment.

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

    One active position exists in each moment.

    Hi,

    I am developing a strategy and have a question in this regard.
    How can I make sure in each moment only one position is active and before taking any position all positions are closed .


    #2
    Hello bosajin,

    Thanks for your post.

    If you are using Calculate.OnBarClose then you can check to see that the strategy position is flat before entering a new position.
    Reference: https://ninjatrader.com/support/help...etposition.htm

    If you are using calculate.OnEachTick or Calculate.OnPriceChange, it is possible that the strategy can place multiple entries in the same bar. The reason this can occur is that your strategy will execute far faster than the asynchronous process of sending an order to the exchange, the exchange placing, filling, and acknowledging the position to then update the position object. In those calculation modes, you can control the entries through your own logic.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your answer.

      do you think the following pseudo code is correct to live up my expectation?
      by the way , I am using Calculate.OnBarClose

      protected override void OnBarUpdate()
      {
      TakePosition();
      }


      protected override void TakePosition()
      {
      if (Position.MarketPosition == MarketPosition.Long)
      ExitLong();

      if (Position.MarketPosition == MarketPosition.Short)
      ExitShort();


      //take position here ex:EnterLong0


      }

      Comment


        #4
        Hello bosajin,

        Thanks for your reply.

        The code example you show would exit any strategy long/short position.

        Paul H.NinjaTrader Customer Service

        Comment


          #5
          The code would guaranty only no position is active be before taking another position . right?

          Comment


            #6
            Hello bosajin,

            Thanks for your reply.

            It only states that if the strategy has an open position, that it would be closed when TakePosition() is called.

            Another approach would be to add if (Position.MarketPosition == MarketPosition.Flat) as part of your entry conditions.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              In your approach (if (Position.MarketPosition == MarketPosition.Flat)) I wont be able to take position until current position get closed and probably I will lose moment to take position.
              Please let me know , If you feel I am not clear on the concept of position status.

              Comment


                #8
                hello bosajin,

                Thanks for your reply.

                You can code it however you wish.

                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks for swift response.
                  I have followed your guidance but it is not working and I have 2 open positions simultaneously.
                  Would you please take a look at the attachments and the code.

                  void TakePosition()
                  {
                  ExitLastPosition();

                  if (MaxProfitWasReached)
                  {
                  MyPrint(string.Format("{0} | Max profit was reached | {1}", Time[0].Format24Hour(), Math.Round(DayNetProfit)));
                  return;
                  }

                  if (MaxLossWasReached)
                  {
                  MyPrint(string.Format("{0} | Max loss was reached | {1}", Time[0].Format24Hour(), Math.Round(DayNetProfit)));
                  return;
                  }


                  switch (GetBarStatus(CurrentBar))
                  {
                  case BarStatus.Falling:
                  MyEnterShort(string.Format("Short_{0}", CurrentBar));
                  MyPrint(string.Format("{0} | Short | {1}", Time[0].Format24Hour(), CurrentBar));
                  break;
                  case BarStatus.Rising:
                  MyEnterLong(string.Format("Long_{0}", CurrentBar));
                  MyPrint(string.Format("{0} | Long | {1}", Time[0].Format24Hour(), CurrentBar));
                  break;
                  default:
                  break;
                  }

                  SetStrategyTakeProfit();
                  SetStrategyStopLoss();

                  UpdateDayProfitAndLoss();
                  }

                  void ExitLastPosition()
                  {
                  if (Position.MarketPosition != MarketPosition.Flat)
                  {
                  var marketPosition = Position.MarketPosition;
                  if (Position.MarketPosition == MarketPosition.Short)
                  {
                  ExitShort();
                  }
                  else
                  {
                  ExitLong();
                  }

                  MyPrint(string.Format("{0} | Exit {1} | {2}", Time[0].Format24Hour(), marketPosition == MarketPosition.Short ? "Short" : "Long", CurrentBar));
                  LastStrategyPosition = null;
                  }
                  }

                  Comment


                    #10
                    Hello bosajin,

                    Thanks for your reply.

                    The "close Position" order would be generated by an entry method. When an entry method is placed, if there is a position in the opposite direction, the entry method will first place a market order to close the opposite position followed by a second entry order to leave you in the preferred position. If you call an exit order at the same time as the entry order in the opposite direction, you can create a racing condition which could then end up with 2 orders.

                    I recommend checking that the Market position is flat before placing the entry order.
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi,

                      I am using Calculate.OnBarClose.
                      Regarding to your recommendation if there is active position then I cannot place new order until new OnBarUpdate call and MarketPostion gets changed to Flat.right?

                      Please correct me If I am wrong

                      Sample code

                      if (Position.MarketPosition != MarketPosition.Flat)
                      {
                      if (Position.MarketPosition == MarketPosition.Short)
                      {
                      ExitShort();
                      }
                      else
                      {
                      ExitLong();
                      }
                      }
                      else {
                      //Enter long or short here
                      }




                      Comment


                        #12
                        Hello bosajin,

                        Thanks for your reply.

                        If you don't want the strategy to enter until its strategy position is flat then you would add a check of the market position as part of your entry conditions. A simple example:

                        if (Position.MarketPosition == MarketPosition.Flat && your entry conditions)
                        {
                        EnterLong();
                        }

                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Hello PaulH

                          I want something opposite, I mean ,want to close all position and enter new position(long or short basis of my condition) right away.

                          Please provide me a simple code to cover the above condition.

                          Thanks in advance.



                          Comment


                            #14
                            Hello bosajin,

                            Thanks for your reply.

                            You would be able to close the position on one bar then open a new position on the next bar.

                            if (Position.MarketPosition != MarketPosition.Flat)
                            {
                            ExitLong();
                            ExitShort();
                            }

                            if (Position.MarketPositon == MarketPosition.Flat && your long entry conditions)
                            {
                            EnterLong();
                            }

                            if (Position.MarketPositon == MarketPosition.Flat && your short entry conditions)
                            {
                            EnterShort();
                            }


                            If your intention is to reverse an existing position, then you need only issue an entry order in the opposite direction as the entry method will automatically issue a "close Position" order. From the help guide "Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position." Reference: https://ninjatrader.com/support/help...d_approach.htm

                            For example, if in a long position and you wish to now go short:

                            if (Position.MarketPosition == MarketPosition.Long && your conditions to go short)
                            {
                            EnterShort();
                            }

                            The EnterShort() method will place a sell order called "Close Position" to close the long order and then will immediately issue a second sell called "short" or "sell".
                            Paul H.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi Paul

                              Please my code , even though it meets your recommendation , I still have 2 open-position (you can see in attachment).
                              Let me know , why something like this happens intermittently.


                              Order MyEnterLong(string name)
                              {
                              if (Position.MarketPosition == MarketPosition.Long)
                              {
                              MyExitLong();
                              }

                              SetLastStrategyPosition(false, name);
                              return EnterLong(1, name);
                              }

                              Order MyEnterShort(string name)
                              {
                              if (Position.MarketPosition == MarketPosition.Short)
                              {
                              MyExitShort();
                              }

                              SetLastStrategyPosition(true, name);
                              return EnterShort(1, name);
                              }

                              Order MyExitLong()
                              {
                              if (StrategyPositionIsTaken)
                              {
                              var name = LastStrategyPosition.PositionName;
                              ReleaseLastPosition();
                              return ExitLong(name);
                              }

                              return ExitLong();
                              }

                              Order MyExitShort()
                              {
                              if (StrategyPositionIsTaken)
                              {
                              var name = LastStrategyPosition.PositionName;
                              ReleaseLastPosition();
                              return ExitShort(name);
                              }

                              return ExitShort();
                              }

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by helpwanted, Today, 03:06 AM
                              1 response
                              16 views
                              0 likes
                              Last Post sarafuenonly123  
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by aussugardefender, Today, 01:07 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post aussugardefender  
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              244 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Started by TraderG23, 12-08-2023, 07:56 AM
                              9 responses
                              387 views
                              1 like
                              Last Post Gavini
                              by Gavini
                               
                              Working...
                              X