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

Get current position held in account?

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

    Get current position held in account?

    How do I get the size of what I have properly?

    I'm using Position.Quantity, but it seems that this doesn't work for me. In my strategy I only let it buy 1 long and then wait for profit or stop loss. If I have one long, then it will not buy anymore. But it seems like that's not the case when I try:

    Code:
    if (Position.Quantity == 0) { EnterLong(1); }
    This doesn't stop it from buying more.

    Please help!

    #2
    Hello emoryhu,

    Thank you for your inquiry.

    Just to clarify, are you experiencing multiple long entries in your strategy, or is your strategy entering another long position once you hit your stop loss/profit target? If it's the latter, this would be expected, as your Position.Quantity value will return to 0 and the if statement's conditional will be true.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      It is entering multiple long before it hits a profit taker or stop loss. When I tested it with real time data, it entered as many as 4 long in about 6 hours, but only selling one of those longs, and keeping the other three.

      Comment


        #4
        Hello emoryhu,

        Can you please provide a screenshot of this behavior and a simplified version of your entire code logic so I may investigate further?

        To send a screenshot with Windows 7 or newer I would recommend using Window's Snipping Tool.

        Click here for instructions

        Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screen shot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save as a jpeg file and send the file as an attachment.

        Click here for detailed instruction

        If you prefer not to provide these items on this forum thread, you can email both of these items to platformsupport [AT] ninjatrader [DOT] com with my name and a link to this forum thread referenced in the body of your email.
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Can't find the 3 or 4 Long positions, probably reseted the history, but it still shows it entered Long twice, and never sold the first Long, unless I manually close the positions.
          Discover the magic of the internet at Imgur, a community powered entertainment destination. Lift your spirits with funny jokes, trending memes, entertaining gifs, inspiring stories, viral videos, and so much more from users.


          I don't want the script to check out too many things when it doesn't have to so code at first is basically check whether I have any positions, if I don't, then do something:

          Code:
          if (Position.Quantity == 0)
          {
               if (Parameter)
               {
                      EnterLong(1, "Signal");
               }
          }
          
          
          if (Position.Quantity > 0)
          {
               if (Exit Parameter)
               {
                      ExitLong();
               }
          }
          This didn't work and I thought maybe the EnterLong hasn't been filled and it comes in again, so I did this:
          Code:
          variables:
          int Entered, Exited = 0;
          
          if (Entered == 0)
          {
               if (Parameter)
               {
                      EnterLong(1, "Signal");
                      Entered = 1;
                      Exited = 0;
               }
          }
          
          
          if (Exited == 0)
          {
               if (Exit Parameter)
               {
                      ExitLong();
                      Exited = 1;
                      Entered = 0;
               }
          }
          But this runs into a problem that if the order doesn't get filled, it's never going to enter long again. So I did this:
          Code:
          variables:
          int Entered, Exited = 0;
          
          if(Position.Quantity >= 1)
          {
          	Entered = 1;
          	Exited = 0;
          }
          else
          {
          	Entered = 0;
          	Exited = 1;
          }
          
          if (Entered == 0)
          {
               if (Parameter)
               {
                      EnterLong(1, "Signal");
               }
          }
          
          if(Position.Quantity >= 1)
          {
          	Entered = 1;
          	Exited = 0;
          }
          else
          {
          	Entered = 0;
          	Exited = 1;
          }
          
          if (Exited == 0)
          {
               if (Exit Parameter)
               {
                      ExitLong();
               }
          }
          And this still doesn't work. I reset the data, reinstalled. None worked.
          Last edited by emoryhu; 09-30-2015, 08:46 AM.

          Comment


            #6
            Hello emoryhu,

            Please note that the Executions tab of the Control Center will show all executions and not just executions done by a particular strategy.

            When adding your strategy to a chart and enabling it, please right-click on the chart and select Strategy Performance -> (your strategy name) -> Historical & Real-time. When the Performance window for the strategy opens, click on the Executions tab. Do you see any positions here that are greater than 1L?

            For the first block of code you have mentioned, can you please specify your entry and exit parameters? With a simple comparison between Close[0] and Close[1], entries and exits were made successfully.
            Zachary G.NinjaTrader Customer Service

            Comment


              #7
              It does this even more often in historic data. Sometimes will even buy 10+ in a row. Then it sells everything at next time interval at the same time, but at the respective profit/loss marks.

              Comment


                #8
                Hello emoryhu,

                Unfortunately, I am unable to reproduce this behavior on my end.

                In order to investigate further, please provide the script file of your strategy in your next post. You can find this in your (My) Documents\NinjaTrader 7\bin\Custom\Strategy folder on your computer. It will be a .cs file.

                If you would prefer not to provide your script on the forum, please send the script file to platformsupport [AT] ninjatrader [DOT] com with my name and a link to this forum thread referenced.
                Zachary G.NinjaTrader Customer Service

                Comment


                  #9
                  Account Position Code loop

                  This code is part of ++loop and enables you to find account positions for multi instrument strategies. One issue I have is being able to loop through the pos.Instrument.FullName "JPM" in this case.

                  Is it possible to set up variables to loop through all the Add() instruments instrument names? It would save me writing out this code snippet 100+ times with different stocks symbols in each. Regards


                  foreach (Account acct in Cbi.Globals.Accounts)
                  {
                  if (acct.Positions != null)
                  {
                  PositionCollection positions = acct.Positions;
                  foreach (Position pos in positions)
                  {
                  if (BarsInProgress == 0
                  && pos.Account.Name == "Sim101"
                  && pos.Instrument.FullName == "JPM"
                  && pos.MarketPosition == MarketPosition.Long)


                  {
                  //Do this
                  }

                  }
                  }
                  }

                  Comment


                    #10
                    Hello everington_f,

                    You can loop through the BarsArray collection and obtain each Bars object's Instrument.FullName.

                    In my example below, I will print out all of the added instruments.

                    Example:
                    Code:
                    foreach (var instrument in BarsArray)
                    {
                         Print(instrument.Instrument.FullName);
                    }
                    Zachary G.NinjaTrader Customer Service

                    Comment


                      #11
                      I think I needed to explain a little better....

                      // Exit Longs

                      foreach (Account acct in Cbi.Globals.Accounts)
                      {
                      if (acct.Positions != null)
                      {
                      PositionCollection positions = acct.Positions;
                      foreach (Position pos in positions)
                      {
                      if (BarsInProgress == i
                      && pos.Account.Name == "UKSwingStocks"
                      && pos.Instrument.FullName == "JPM" ****** I need to loop here
                      && pos.MarketPosition == MarketPosition.Long)

                      {
                      EnterShort(i,pos.Quantity, "Exit Long"+i);
                      }

                      }
                      }
                      }

                      Comment


                        #12
                        Hello everington_f,

                        You would need to loop through the BarsArray collection.

                        Example:

                        Code:
                        foreach (var instrument in BarsArray)
                        {
                        	foreach (Account acct in Cbi.Globals.Accounts)
                        	{
                        		if (acct.Positions != null)
                        		{
                        			PositionCollection positions = acct.Positions;
                        			foreach (Position pos in positions)
                        			{
                        				if (BarsInProgress == i
                        					&& pos.Account.Name == "UKSwingStocks"
                        					&& pos.Instrument.FullName == [B]instrument.Instrument.FullName[/B]
                        					&& pos.MarketPosition == MarketPosition.Long)
                        				{
                        					// logic
                        				}
                        			}
                        		}
                        	}
                        }
                        Zachary G.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by sidlercom80, 10-28-2023, 08:49 AM
                        166 responses
                        2,233 views
                        0 likes
                        Last Post sidlercom80  
                        Started by thread, Yesterday, 11:58 PM
                        0 responses
                        1 view
                        0 likes
                        Last Post thread
                        by thread
                         
                        Started by jclose, Yesterday, 09:37 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post jclose
                        by jclose
                         
                        Started by WeyldFalcon, 08-07-2020, 06:13 AM
                        10 responses
                        1,414 views
                        0 likes
                        Last Post Traderontheroad  
                        Started by firefoxforum12, Yesterday, 08:53 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post firefoxforum12  
                        Working...
                        X