Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

..."And close any open short/long position"

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

    ..."And close any open short/long position"

    After several weeks of testing my strategy I finally believe I've figured out what is the matter. It's that little phrase on the description of the limit entry functions that says "...and close any open short position" for longs, or the other way around for short.

    I'm trying to set up a really nice strategy here that will co-operate with my way of life. I travel a lot, and some times I just need to lose internet for a few minutes, or shut my computer down and come back in a few minutes. I want to keep my strategies running.

    So with exceedingly great difficulty, I've actually got a ninja trader script that does that, all except for the annoying problem mentioned above.

    See, because when I come back, all my strategies are shut down. So I have to restart them. And that means that Ninja Trader completely forgets that I am in the market, and Position.MarketPosition == flat, even though I'm still in my trades. Because the "Position" collection correlates to your strategy position, and not to your account position. So since the strategy was shut down, then strategy position == flat, and so Position.MarketPosition == flat as well.

    So I use... brace yourself...

    MyInstrument = new Instrument(new MasterInstrument("USDCAD", InstrumentType.Currency), Exchange.Default, DateTime.Today, 0, NinjaTrader.Cbi.Right.Unknown);

    MyPosition = Account.Positions.FindByInstrument(MyInstrument);

    Now please don't harass me for calling non-ninja script functions. I'm trying to make this work. Anyway, that gets me an accurate market position. I can now see that MyPosition.MarketPosition != flat. Great. And every time I place a new order, I simply refresh it by calling "MyPosition = Account.Positions.FindByInstrument(MyInstrument)" again, and that keeps me up-to-date on my position.

    Problem...

    When I then place a market trade, and I tell it to sell 1 contract at a limit order... I mean what I said, just 1 contract please. It will often sell two, because it doesn't realize that I am flat in the market. Because according to my "strategy position" I'm long, but in reality according to my account position I'm flat. That's caused by me shutting off the CPU while I'm in a short position, and so my first trade is long, which makes my account == flat, but my strategy == long. So then I try to sell 1 short limit entry, and it sells 2, one to supposedly "flatten" me, and one to fulfill my every desire.

    I know this is a lot of trouble to fix, but please when you are developing the next edition of NT, please try to bare in mind that you have no idea what people might be doing. One of the most annoying things about the older versions of windows was how that Microsoft thought it knew what was best, and would try to dictate how people used Windows. Like when there were no cancel buttons, or programs would run or shut down or install things by force, because Microsoft knew what was best. I'm not trying to be mean in saying all of this, but to trade 2 contracts when I said to trade only 1 is a direct violation of user control.

    Aaron

    #2
    Thanks Aaron,

    NT7 will address strategy persistence across sessions.
    RayNinjaTrader Customer Service

    Comment


      #3
      You are welcome

      I will, in the mean time, fix my code by making sure my strategy terminates itself and notifies me as soon as the account is flat, then I'll just have to manually restart it, and all is well. I am glad I figured out what was happening.

      Aaron

      Comment


        #4
        Hi there behealed, just curious if

        1 - this type of code still works

        2 - if this line wouldn't work
        Account.Positions.FindByInstrument( new Instrument( Instrument.MasterInstrument, Exchange.Default, DateTime.Today, 0, Right.Unknown ) );

        3 - how did you find these items in the first place? I am new to C# and NT and intellisense didn't show them. The only reference I have seen so far to account positions talks about manually modifying the account position to sync it by submitting manual orders. Did the object browser give a hint?

        Comment


          #5
          michaelangela,

          Unfortunately we currently do not support accessing account positions from a NinjaScript strategy.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Josh View Post
            michaelangela,

            Unfortunately we currently do not support accessing account positions from a NinjaScript strategy.
            Hi Josh, yes I realize that. I've been back and forth with support for a while on trying to find a way to get positions from within a strategy. I am just looking for a way to do it without having to write another program (based on the client class), spawn new processes, manage the connection between a new program/executables and the strategy, etc.

            So while it's not supported, if it works that would be phenomenal. That's why I was asking behealed if it still does.

            Thanks!

            Comment


              #7
              Just guessing: loading some NT dll in Visual Studio?

              Comment


                #8
                Hello,

                I will have someone reply to your post on Monday.
                DenNinjaTrader Customer Service

                Comment


                  #9
                  Hi skynetman, I am not sure about behealed as there hasn't been a reply. But I am trying to minimize the amount of code that needs to be written and maintained, especially code that needs to communicate with the strategy. Using the Client class with SharpDevelop or VS would work adding a reference to the ntdirect.dll. However if, from within the strategy, I can get access to the position, even if it is officially unsupported, that could simplify the code I have to write and maintain. A line like this works for entries submitted via the superdom while using the Sim101 account

                  Code:
                  Position pos = Account.Positions.FindByInstrument( new Instrument( Instrument.MasterInstrument, Exchange.Default, DateTime.Today, 0, Right.Unknown ) );
                  although you do have to check for pos != null. What I do not know is if that would work for an actual account. That's the question. According to behealed's original statement, it looks like it works. So in my case, I would just be doing a

                  Code:
                  bool inSync = false
                  Position pos = Account.Positions.FindByInstrument( new Instrument( Instrument.MasterInstrument, Exchange.Default, DateTime.Today, 0, Right.Unknown ) );
                  if( pos != null && pos.MarketPosition != Position.MarketPosition )
                  {
                  inSync = false;
                  }
                  else
                  {
                  inSync = true;
                  }
                  Oh I just found the other discussion you're in over at:



                  I'll take a look there too.
                  Last edited by michaelangela; 02-23-2009, 11:04 AM.

                  Comment


                    #10
                    Originally posted by skynetman View Post
                    Just guessing: loading some NT dll in Visual Studio?
                    By the way, adding a reference to the ntdirect.dll from within a strategy causes a errors because of multiple definitions of the client methods. Otherwise that would be really cool. So I could write a separate app and do some stuff to keep the communication between the two of those in sync and it may, in the end, be the only way to go about it. As mentioned on that other thread, NT is a great app. It has limitations but all apps do. So this is just my effort to see how to get around this limitation.

                    Thanks!

                    Comment


                      #11
                      Hi michaelangela, I can understand your desire to access those values from your NS strategy - unfortunately I can't provide more information as Josh's post that this is not supported at this time.
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Hi Bertrand,

                        No worries. I was replying to skynetman's post. If the above code does not work for me I do have one last workaround. That would be to use onPositionUpdate, log the instrument name, position quantity, average price, and the time of the event to a mysql database. I would also have a lastCheck datetime column. On an onBarUpdate event, I'd query the database for the position info. Everytime I queried the database for the position info I'd update the lastCheck column. You could call that the "last time this strategy ran" column. On init, I'd check that lastCheck column and see if the data is fresh. For example on a Monday, an open position would mean the strategy should have been running on Friday. The lastCheck would tell me that. If it wasn't fresh I could null out that lastCheck or something as the position data in it is invalid. If the data is fresh then I can check to see if it's in sync with the strategy position and set my inSync variable accordingly and proceed from there.

                        Still thinking it through but as you can see it is made so much simpler if the position could be checked from within the strategy.

                        Thanks!

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by gm7_forum, Today, 05:51 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post gm7_forum  
                        Started by cre8able, Today, 03:20 PM
                        1 response
                        9 views
                        0 likes
                        Last Post cre8able  
                        Started by fiddich, Today, 05:25 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post fiddich
                        by fiddich
                         
                        Started by gemify, 11-11-2022, 11:52 AM
                        6 responses
                        804 views
                        2 likes
                        Last Post ultls
                        by ultls
                         
                        Started by ScottWalsh, Today, 04:52 PM
                        0 responses
                        4 views
                        0 likes
                        Last Post ScottWalsh  
                        Working...
                        X