Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple positions when not wanted

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

    Multiple positions when not wanted

    Hello,

    I am using OnBarUpdate executions.

    I am experiencing situations that my strategy enters position even if the market position is not flat even though I clearly state that position has to be flat in the code.

    Plase see attached picture taken from the strategy performance statistics. You can see that strategy enters new long position and at the same day closes previous position which should not happen since in live trading the OnBarUpdate is triggered only at the beginning of the trading day.

    Expected behavior: when the trading day starts it checks whether the market position is flat. If not, then nothing happens. If the positions is flat it goes through all the possible symbols and if one of them triggers the entry rule then long position is entered

    Below you can see also sample of my code:

    protected override void OnBarUpdate()

    {
    //CTXS
    if (Position.MarketPosition == MarketPosition.Flat
    && BarsInProgress == 0
    && High[2] > High[1] && High[1] > Close[2] && Close[2] > Low[2] && Low[2] > High[0] && High[0] > Close[1] && Close[1] > Close[0] && Close[0] > Low[0] )
    {
    EnterLong(DefaultQuantity, "BUY CTXS");
    }

    //LIFE
    else if (Position.MarketPosition == MarketPosition.Flat
    && BarsInProgress == 1
    && High[2] > High[1] && High[1] > Close[2] && Close[2] > Low[2] && Low[2] > High[0] && High[0] > Close[1] && Close[1] > Close[0] && Close[0] > Low[0] )
    {
    EnterLong(DefaultQuantity, "BUY LIFE");
    }

    and then the code goes on and on for other symbols.

    Is there any way to prevent the strategy from entering and leaving positions on same day? Because this way it blocks the strategy from executing live orders (getting yellow tab for multiple consecusive trades as seen in attached picture).

    Thank you for help.
    Attached Files

    #2
    Hi Waldo,

    Your code currently checks that the current symbol does not have a position.

    Are you wanting to know if all symbols are flat?

    if (Positions[0].MarketPosition == MarketPosition.Flat &&
    Positions[1].MarketPosition == MarketPosition)

    This would check that the first and second data series do not have a position.

    You could also use a loop and bool.

    bool isFlat = true;
    for (var i=0; i<Positions.Count-1; i++)
    if (Positions[i].MarketPosition != MarketPosition.Flat)
    isFlat = false;

    if (isFlat)
    {
    // execute code
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the answer,

      is there any way how to track whether all symbols within the particular strategy are flat? Because I might have positions which are not part of the strategy as such.

      Comment


        #4
        Hi Waldo,

        Using the loop I have suggested will check all symbols that have been added to your strategy with the Add() call.

        An unsupported way of finding positions for symbols not added to this strategy is below.

        Code:
        foreach (Account acct in Cbi.Globals.Accounts)
        {
        if (acct.Positions != null)
        {
        // print information about each position
        PositionCollection positions = acct.Positions;
        foreach (Position pos in positions)
        {
        Print(pos.Account.Name + " " + pos.Instrument + " " + pos.MarketPosition + " " + pos.Quantity + " " + pos.AvgPrice);
        }
        // print information about each order
        OrderCollection orders = acct.Orders;
        foreach (Order ord in orders)
        {
        Print(ord.ToString());
        }
        }
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Unfortunately I am getting compile error pointing on "Count" when I try to use your loop. Any idea what I might be doing wrong?

          Comment


            #6
            Hi Waldo,

            That was my mistake

            Try:

            Code:
            bool isFlat = true;
            for (int i=0; i<Positions.Length; i++)
            if (Positions[i].MarketPosition != MarketPosition.Flat)
            isFlat = false;
            
            if (isFlat)
            {
            // execute code
            }
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              The code is compiled, but the result is exactly the same. Still there are both open and close during the same day (attached).

              Code looks the following way:

              protected override void OnBarUpdate()

              {

              bool isFlat = true;
              for (int i=0; i<Positions.Length; i++)
              if (Positions[i].MarketPosition != MarketPosition.Flat)
              isFlat = false;

              if (isFlat)

              {
              //CTXS
              if (Position.MarketPosition == MarketPosition.Flat
              && BarsInProgress == 0
              && High[2] > High[1] && High[1] > Close[2] && Close[2] > Low[2] && Low[2] > High[0] && High[0] > Close[1] && Close[1] > Close[0] && Close[0] > Low[0] )
              {
              EnterLong(DefaultQuantity, "BUY CTXS");
              }

              //LIFE
              else if (Position.MarketPosition == MarketPosition.Flat
              && BarsInProgress == 1
              && High[2] > High[1] && High[1] > Close[2] && Close[2] > Low[2] && Low[2] > High[0] && High[0] > Close[1] && Close[1] > Close[0] && Close[0] > Low[0] )
              {
              EnterLong(DefaultQuantity, "BUY LIFE");
              }

              etc...
              Attached Files
              Last edited by Waldo; 08-11-2014, 11:53 AM.

              Comment


                #8
                Hello Waldo,

                There is an issue here that the position does not get updated until sometime after this pass through OnBarUpdate.
                (The Position is updated asynchronously in OnPositionUpdate())

                In otherwords, both orders are submitted before the position is updated.

                If an order is placed, set isFlat to false.

                You should be checking isFlat before every order.

                Reset isFlat on the new session.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I do not get it.

                  For example how can the buy order trigger when there is already a position from a previous day? Lets say that in day x-1 strategy enters long. Afterwards in day x the strategy is initiated again and it should notice that there is already a position open so it should not buy another even if the position from x-1 will close during that day, since the enter long is checked only on bar update which is at the start of trading day.

                  So I really do not know why it is behaving like this and how to avoid it.

                  Comment


                    #10
                    Hi Waldo,

                    I was only referring to the two trades that are going in back to back.

                    You are still experiencing that there is an open position from a previous day and a new position quantity is taken?

                    I will need to see this. Please send an email to platformsupport [at] ninjatrader [dot] com. In the email add a link to this forum thread.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Email containing the full code sent, thank you for looking into this.

                      Comment


                        #12
                        Hi Waldo,

                        We found that the Position was updating on the same bar in between the BarsInProgress.

                        To correct your script. We reset the isFlat bool once per bar in BarsInProgress 0 instead of during every run through OnBarUpdate with the different BarsInProgress.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          I've used a much simplified version of this suggested code to avoid opening a position when one is already open in a multi-strategy portfolio. It goes:

                          if (..long entry conditions met..)
                          {
                          bool isFlat = true;
                          foreach (Account acct in Cbi.Globals.Accounts)
                          {
                          if (acct.Positions != null)
                          {
                          isFlat = false;
                          }
                          }
                          if (isFlat != false)
                          {
                          EnterLong(Qty,"ML");
                          Stopx = Close[0] - (ASloss * TickSize);
                          }
                          }

                          I've tested this with multiple strategies on Replay data, but there is never a trade.
                          The even more simplified alternatives:
                          if (Account.Positions == null)
                          {...
                          }
                          ....
                          or
                          if (Account.Orders.Count != 0)
                          {...
                          }
                          ....
                          don't work either. What am I missing? Help!

                          Comment


                            #14
                            Hello Xenophon99,

                            I think the issue is with acct.Positions != null.

                            The account will likely always have a collection called Positions even if it is empty.

                            Try the following instead:

                            if (acct.Positions != null)
                            {
                            foreach (Position pos in positions)
                            {
                            if (pos.MarketPosition == MarketPosition.Flat)
                            {
                            isFlat = false;
                            }
                            }
                            }
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              I've used a much simplified version of this suggested code to avoid opening a position when one is already open in a multi-strategy portfolio. It goes:

                              if (..long entry conditions met..)
                              {
                              bool isFlat = true;
                              foreach (Account acct in Cbi.Globals.Accounts)
                              {
                              if (acct.Positions != null)
                              {
                              isFlat = false;
                              }
                              }
                              if (isFlat != false)
                              {
                              EnterLong(Qty,"ML");
                              Stopx = Close[0] - (ASloss * TickSize);
                              }
                              }

                              I've tested this with multiple strategies on Replay data, but there is never a trade.
                              The even more simplified alternatives:
                              if (Account.Positions == null)
                              {...
                              }
                              ....
                              or
                              if (Account.Orders.Count != 0)
                              {...
                              }
                              ....
                              don't work either. What am I missing? Help!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post Javierw.ok  
                              Started by timmbbo, Today, 08:59 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post bltdavid  
                              Started by alifarahani, Today, 09:40 AM
                              6 responses
                              41 views
                              0 likes
                              Last Post alifarahani  
                              Started by Waxavi, Today, 02:10 AM
                              1 response
                              19 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Working...
                              X