Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Open Positions Check

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

    Open Positions Check

    How do I check if I have any open positions? I want to set a trailing stop below the previous day's low if I have an open position.

    #2
    Hello Lisalisa,

    Thank you for writing in.

    To check if you have a open position, you could do this in the strategy wizard under Conditions, setting Current Market Position under Strategy on the left side, using Not Equal as an operator, and then setting on the right side under Strategy>Market Position and selecting Flat. Please see the attachment.

    Or you could use the following statement.

    if (Position.MarketPosition != MarketPosition.Flat)

    An example in our Helpguide:


    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Suggestion

      If you lose track of the positions you may have to loop through all the open positions and do something with each position, though not knowing what you are doing not sure this helps.

      foreach (Position pos in Account.Positions)
      {
      if (pos.MarketPosition != MarketPosition.Flat)
      {
      // do something.
      }
      }

      Ref link: http://ninjatrader.com/support/helpG.../?position.htm

      Comment


        #4
        I am writing a strategy. I know I have open positions when I backtested it because I could see profile and loss. However, when I tried to retrieve the open positions, I saw nothing.

        In the OnBarUpdate method, I added these lines.

        foreach (Position pos in Account.Positions)
        {
        Print("11111");
        if (pos.MarketPosition != MarketPosition.Flat)
        {
        Print("22222");
        }
        }

        Comment


          #5
          So you are in backtest and with a single instrument and managed trades? Perhaps all you need is.
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          // Calculate on the close of each bar
          Calculate = Calculate.OnBarClose;
          }
          }


          protected override void OnBarUpdate()
          {

          if(Position.MarketPosition == MarketPosition.Long)
          {
          SetTrailStop(CalculationMode.Pips, your_double_value)
          }
          if(Position.MarketPosition == MarketPosition.Short)
          {
          SetTrailStop(CalculationMode.Pips, your_double_value)
          }

          }
          But I am not sure it makes sense to do this at each OnBarUpdate (or at Bar Close) as above).

          Comment


            #6
            I can't get SetTrailStop to work with CalculationMode.Percent or CalculationMode.Price. I think it doesn't work because no matter what value I pass in, the profit/loss doesn't change when I backtest my strategy. However, if I use CalculationMode.Ticks, it changes the profit/loss.
            How do I make it work with CalculationMode.Price or CalculationMode.Percent?

            Also, how does SetTrailStop knows whether I am setting a stop order for a long or short position?

            This is how I call it:
            SetTrailStop("mybuysignal", CalculationMode.Percent, 2.0, true)
            SetTrailStop("mybuysignal", CalculationMode.Percent, 98.0, true)
            Last edited by lisalisa; 12-06-2016, 10:04 PM.

            Comment


              #7
              Presumably, since the tick trail stop is working, the percent would as well. Don't know what your time frames or instruments are but 98% would most likely never get hit. Even 2% is pretty big. Drop the percentage down more to ensure it fires.

              Comment


                #8
                Hello lisalisa,

                SetTrailStop can not be used with CalculationMode.Price, as price is static and would not change as a trailing stop would require.

                See SetTrailStop section of our helpguide:


                Regarding SetTrailStop and CalculationMode.Percent, you should use percent in terms of a decimal. For example for a 2 percent stop, you should enter .02.

                SetTrailStop should include fromEntrySignal name should you wish to differentiate different SetTrailStop methods between long entries vs short entries.

                For example,
                SetTrailStop(“Long Entry”, CalculationMode.Ticks, 10, false)
                SetTrailStop(“Short Entry”, CalculationMode.Ticks, 50, false)

                Please let us know if you need further assistance.
                Alan P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                59 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                133 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                73 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                45 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                50 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X