Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

marketposition update

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

    marketposition update

    I'm wanting to send position, unrealized gains updates from my indicator but I guess I have to add some kind of class or something so those methods can be allowed inside of my indicator?
    I'm getting a ... "no suitable method to override error"
    I guess by putting those commands/returns/invokes into the parenthesis of this method, thats overriding?
    Sorry I dont know the language to speak correctly


    protectedoverridevoidOnPositionUpdate(Cbi.Positionposition,doubleaveragePrice,
    intquantity,Cbi.MarketPositionmarketPosition)
    {
    Print("The most current MarketPosition is: "+position.MarketPosition);// Flat
    Print("This particular position update marketPosition is: "+marketPosition);// Long
    }

    #2
    Think I figured it out:


    Code:
    Account a = Account.All.First(t => t.Name == "Sim101");
    double unrealized = a.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar);                           //   which one of these is correct?
    double unrealized = Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]);          //    what is this doing?
    Print(unrealized);
    
    if(State == State.Terminated) return;      //  if set to terminate then abort the script
    
    if (unrealized <= 400.00)   // too much of a loss
    {
    
    if(Position.MarketPosition == MarketPosition.Long)
              {
                 ExitLong(Position.Quantity);
              }
    if(Position.MarketPosition == MarketPosition.Short)
              {
                ExitShort(Position.Quantity);
              }
    SetState(State.Terminated); return;    //abort the positions then the whole script
    }

    Comment


      #3
      i realized it has to be:

      double unrealized = Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]);
      because the other gives you the net liquidation instead of the unrealized P/L

      Comment


        #4
        Click image for larger version  Name:	un.png Views:	0 Size:	57.7 KB ID:	1192216 I'm getting this error message from this line: double unrealized = Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]);
        Even though it worked before and printed the cash value into the output window.
        IDK what I changed to mess it up?

        Comment


          #5
          Hello ezrollin,

          I see you posted this in the indicator development forum but you are using strategy and addon code. From your comment it seems that you wanted an Indicator so the Strategy based code you have is not relevant:

          Code:
          if(Position.MarketPosition == MarketPosition.Long)
          {
          ExitLong(Position.Quantity);
          }
          if(Position.MarketPosition == MarketPosition.Short)
          {
          ExitShort(Position.Quantity);
          }
          An indicator does not have any account by default so you would need to use the addon framework to find an account to be able to subscribe to its events. In post 2 you have that code, you can see an example of that concept here: https://ninjatrader.com/support/help...ount_class.htm

          In post 2 you also use Position which is not relevant in an indicator, that is a strategy property. The Position object in a strategy would be how you look at that strategies individual position information. In an indicator there is no Position because an indicator does not relate to a specific account or position.

          With the addon framework in the indicator you would need to use the account variable for the account you found, that would be the "a" variable in what you provided. If you needed position information from the account you need to use the found account variable and its Positions collection. In that collection you can find the specific position for the given instrument and then use that position object to use methods like GetUnrealizedProfitLoss: https://ninjatrader.com/support/help...ns_account.htm


          As a side note, I am not sure what you are using SetState for in the above sample, if you want the script to continue to work that needs to be removed. Additionally if you use the addon framework account sample from the help guide and make any subscriptions to events then you likely need to remove SetState and allow the script to work as normal. That is so you can correctly remove handlers like the sample shows when terminated happens, if you leave subscriptions open you will have to restart the platform between each test to clear old event subscriptions.







          Comment


            #6
            So what I was doing was not relevant but the way I showed it was relevant? I think I understand LOL

            Yes I'm trying to do this in an indicator.
            with the terminated state, I was trying to create an endless loop to kill the script if the unrealized went under -$400


            Code:
            Account a = Account.All.First(t => t.Name == "Sim101");
            double unrealized = Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]);
            Share("Twitter", "PNL: " +unrealized);
            Will this work in an indicator? I see it says Position.GetUnrealized.......

            Looks like this wont work either:
            Code:
            if (State == State.DataLoaded)
            {
            lock (a.Positions)
            {
            Print("Positions in State.DataLoaded:");
            
            foreach (Position position in a.Positions)
            {
            Print(String.Format("Position: {0} at {1}", position.MarketPosition, position.AveragePrice));
            }
            }
            }

            Comment


              #7
              double unrealized = position.AveragePrice - Close[0];
              could do something like that but it would only work on the short side, the long side, the math would have to be reversed.

              Comment


                #8
                Code:
                lock (account2.Positions)
                {
                foreach (Position position in account2.Positions)
                {
                double unrealized = account2.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar);
                Print(String.Format(" {0} at {1} Unrealized: {2} ", position.MarketPosition, position.AveragePrice,unrealized));
                Share("Twitter", (String.Format("Still {0} at {1} Unrealized: ", position.MarketPosition, position.AveragePrice, unrealized)));
                }
                }
                figured it out

                Comment


                  #9
                  Jesse, Is it possible to do my -$400 stop loss and a $100 take profit on long side?

                  Comment


                    #10
                    Hello ezrollin,

                    Jesse is out of the office at this time.

                    It is possible to submit orders from the indicator using Account.CreateOrder() and Account.Submit(). You can use these methods to submit stop market orders for a stop loss and limit orders for a profit target.

                    Account.CreateOrder - https://ninjatrader.com/support/help...reateorder.htm

                    Account.Submit - https://ninjatrader.com/support/help...tml?submit.htm

                    These methods require that orders are submitted with definite prices, so if you wanted a $400 stop loss and $100 target, the prices of the orders would have to be calculated from the position.AveragePrice property. You could also use position.MarketPosition to determine if the orders you want to create and submit should be on the long or short side.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    579 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    334 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    101 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    554 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    551 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X