Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Is it possible to limit total position size for all strategies?

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

    #16
    Originally posted by cunparis View Post
    Thank you for posting your code. I was thinking last night while in the shower: Can't I just use a static variable to hold this information? Do I really need to use files? I can make a class full of static members to hold the information that needs to be shared. Since it's static, it's available to all the strategies.
    Please keep us posted.

    Comment


      #17
      I'd love it if the static variable idea works... this is being talked about in a different thread too. I'm not sure that it will, though. In Java, this would be a ClassLoader problem.

      As I mentioned in the other thread, I'm 99% sure I've been able to load different versions of the same class simultaneously. So... how does that work?

      Comment


        #18
        Another possibility, you can use the Global Variable DLL that was created for TradeStation with Ninja. Here is a thread with instructions. I have used it to implement multiple timeframe indicators.

        http://www.ninjatrader-support2.com/...=global&page=2
        TraderGuy
        NinjaTrader Ecosystem Vendor - EMS

        Comment


          #19
          I just wanted to update this thread for those interested.

          I decided to modify my strategy to be both multi-instrument and multi-timeframe. I'm not finished but so far it appears to be working. It got really complicated because each instrument and each timeframe has its own configuration parameters. I hard code all that into a hashtable and then index into it with the instrument name and then the barsinprogress index.

          If anyone is interested it seems to work well in that I can run one strategy instead of several and when I backtest it I get the overall results which is really nice.

          On the downside the code is very complicated and this would probably too difficult for someone who is not a professional programmer. It's difficult for me and I am one.

          For the specific issue of limiting the overall positions, now it's easy I can do that with the strategy parameters and put 1 position & all entries. When my account balance gets higher then I'll modify this to put 2 positions.

          I hope you found this helpful, if anyone has any questions on this just let me know. I'm still thinking about the idea of storing the configuration in a database instead of hardcoding it, but that will have to wait. if this strategy is profitable then I could justify putting more time into it.

          Comment


            #20
            Originally posted by TraderGuy View Post
            Another possibility, you can use the Global Variable DLL that was created for TradeStation with Ninja. Here is a thread with instructions. I have used it to implement multiple timeframe indicators.

            http://www.ninjatrader-support2.com/...=global&page=2
            As an update to this thread:

            As I explained in the post above I wrote a multi-instrument multi-timeframe strategy. I don't really recommend it. Now I'm on my 3rd strategy and it uses different configuration than the multi-strategy one so now I really need to coordinate between two separate strategies. BTW I'll take the multi-strategy and separate it into two strategies as well because doing everything multi makes it really complicated. For example positions that are not on the primary instrument do not show up in the strategies tab.

            anyway, I'm now back to the problem of sharing my account balance between several strategies.

            I'm researching the global variable idea and this seems to be a good approach. I'll keep comments about that specifically in the GV thread linked to above, and I'll report back here if I get it working.

            My goal is to take my account and allow all strategies access to the capital, either with an algorithm or first come first served. As it is now I plan to allocate 1/3 of the account per strategy. This is really inefficient because one day a strategy may not have any trades yet "consumes" 1/3 of the capital. From a risk point of view this could even be desirable but from a compounding point of view it is not. I haven't decided which is best but I at least want the option.

            Thank you TraderGuy for the link to the other thread!

            Comment


              #21
              Another update, hopefully someone is benefitting from this information (if so please speak up, otherwise I feel like I'm talking to myself)...

              I tested the global variable idea (see link above). It works. However, I found a simpler solution: static variable.

              A reminder of my goal: I want to run several strategies at once and I want each trade to be able to use 100% of my allotted capital. So if strategy 1 gets a trade and then while the position is still open, strategy 2 gets a trade, strategy 2 should not execute the order since all the capital is used up. So far I haven't found a solution to do this without custom programming.

              My solution is to make a strategy that serves as a lock for the account. I used a singleton design pattern (I wouldn't be caught dead using Singleton with Java & Spring but here we're limited) with methods lock & unlock. The hardest part is making it threadsafe and testing it. To test it I coded up a simple strategy than entered randomly and I ran it with the eminis and made sure I never had two positions open at the same time. I did. A lot. Took me several hours to get it safe.

              Overall I do not recommend this for anyone who is not an experienced programmer. Furthermore, taking some trades and not others can change the risk profile of the strategies so that should be taken into consideration too. I have a few that trigger rarely so I want to try it out.

              Hope that helps someone, if you have questions just let me know. It would be a dream to be able to program real classes in Visual Studio and then use them with Ninjatrader. I understand this is the case with OpenQuant and I hope Ninjatrader will follow suit, although this would probably be a major change to the architecture.

              Comment


                #22
                cunparis,

                Thanks for the posts. I still prefer to use my text file method, it is simpler, but your method has multiple advantages. Do you feel it was worth the work vs. the text file method?

                Another consideration of mine is using a MySQL db. The code was posted here on the forum recently. Checking against that db is probably more reliable than my text file method.

                It is off-topic for this thread, but the reason for bringing up MySQL is that its been on my mind for another project. In my free time I've been dreaming up an adaptive/learning strategy, that would analyze multiple possibilities in a trade (as they occur) and adjust itself as needed, learning from not just mistakes, but also learning what value of a particular indiciator would have resulted in superior results, and then storing/retrieving all that from a MySQL db in real-time.

                Mike

                Comment


                  #23
                  This isn't exactly relevant, but might be useful for others.

                  I've decided to use Amazon EC2's SimpleDB service for real time data. There is a C# library available from Amazon. SimpleDB isn't a relational database, it's essentially a large set of untyped data... but that makes it very easy to use in simple applications such as this. It's also very reliable (I trust Amazon will be up more than I trust any arbitrary MySQL service), and very inexpensive (pay by usage).

                  The only downside, and it is a significant one, is that SimpleDB is not guaranteed to be "ACID" consistent. It is a very distributed database on the back-end... so if you write a '1' into your DB and read it back immediately, you might not see the '1'. Because I'm working with slow bars (5 minutes - 30 minutes), this really isn't an issue. But for anyone working at the tick level, that's probably a real consideration.

                  Even so, it's working pretty well for my needs.

                  Comment


                    #24
                    Heya heech!

                    Good info to know. Are you using their cloud only for db, or do you have a Windows application box too? I mainly considered EC2 for the CPU and bandwidth (primarily CPU) for extensive backtesting projects. I was wondering what type of results you saw with this if you can share.

                    Primarily I was unable to clearly determine if I would get a single "wicked fast" core (which NT 6.5 would love), or if I would get a multi-core emulation which NT 6.5 can't use. Hope this makes sense.

                    Mike

                    Comment


                      #25
                      Originally posted by ctrlbrk View Post
                      Heya heech!

                      Good info to know. Are you using their cloud only for db, or do you have a Windows application box too? I mainly considered EC2 for the CPU and bandwidth (primarily CPU) for extensive backtesting projects. I was wondering what type of results you saw with this if you can share.

                      Primarily I was unable to clearly determine if I would get a single "wicked fast" core (which NT 6.5 would love), or if I would get a multi-core emulation which NT 6.5 can't use. Hope this makes sense.

                      Mike
                      Hey there,

                      You sent me a PM about this, didn't you? I thought I replied too...

                      I have extensive experience with EC2 Linux boxes, but none with their Windows machines. Really not sure how well it works, especially with VPS considerations. I don't know... I'm always a little afraid about sharing a server virtually on an application as sensitive as this. With my luck, some kid will start downloading 1000 porn streams at the same time the Fed announces some major change in policy.

                      That's only an issue for automated trading of course. For back-testing, I don't think you can possibly go wrong with that approach. I think it's a great idea, since you can just scale up/down as needed. Trying to get your system into a more or less modular system is always a good goal with EC2... so that you can easily scale up or down as needed.

                      Comment


                        #26
                        heech,

                        Sorry about the PM, just noticed it (and replied).

                        So with your Linux EC2 experiences, are you able to get a single core like say a 20 ghz core or so, or do you just get 20x1ghz core's? That's what I'm trying to find out with EC2.

                        Obviously NT 6.5 can't make use of more than one core, and NT7 beta is now slated for Q2 2009.

                        Mike

                        edit: sorry for hijacking thread, maybe we need an EC2 thread

                        Comment


                          #27
                          Originally posted by ctrlbrk View Post
                          heech,

                          Sorry about the PM, just noticed it (and replied).

                          So with your Linux EC2 experiences, are you able to get a single core like say a 20 ghz core or so, or do you just get 20x1ghz core's? That's what I'm trying to find out with EC2.

                          Obviously NT 6.5 can't make use of more than one core, and NT7 beta is now slated for Q2 2009.

                          Mike

                          edit: sorry for hijacking thread, maybe we need an EC2 thread
                          Hmm, good question... but don't know the answer.

                          With the Linux boxes, they never tell you what type of processor you're getting. All you're getting is one of a "large", "medium", or "small" instance. For what I was using it for, I never really looked into how it was behaving at the processor/core level... just thought of it was CPU cycles! In my case, I used a combination of small instances (for application/web servers), and a single large instance for the primary database server.

                          If I had to guess, I would suspect Amazon would refuse to give you any sort of detailed look. It would commit them to a level of service they might not want to be true too... the physical machine you bring your instance up on will very likely change from time to time, and it might be multi-core, or it might be single-core.

                          Comment


                            #28
                            Originally posted by ctrlbrk View Post
                            cunparis,

                            Thanks for the posts. I still prefer to use my text file method, it is simpler, but your method has multiple advantages. Do you feel it was worth the work vs. the text file method?
                            First no worries about hijacking, I find the idea of EC fascinating. But making a thread would be better only to attract others who are interested as well.

                            I decided to abandon the idea of synchronizing two strategies. I did extensive real time testing (with Sim101 account!!) and it'd work for 1-2 hours and then something would happen and it'd have two positions open at the same time which is what I was trying to avoid. This was using a static variable and marking the methods as synchronized (whatever the C# equivalent is I forget). So I imagine writing to a file would give the same results.

                            What I'm doing now is just separating my account. like 1/3 for 1 strategy/market, 1/3 for the same strategy on another market, and 1/3 for a different strategy. The disadvantage is that all capital may not be used, but the more I learn about money management and position sizing the more I think this is good. The advantage is that each system will take all trades and therefore the statistics of that system would be valid. If the strategies competed for capital then all trades wouldn't be taken and that could impact the results. For example one strategy could go first and lock the capital and then the second wouldn't have any capital to trade. If the strategies have some kind of correllation then maybe the 2nd would only take losing trades.

                            I'm still learning about all this so please let me know if all that makes sense. I'm very curious how others are doing it. I have 2 systems that use intraday data but do not trade frequently. So it's frustrating to see only 1/3 of capital used at a given point.

                            Comment


                              #29
                              Hey guys, I just found this thread.. some good discussion here. I am currently dealing with similar issues creating multi-timeframe and/or multi-instrument indicators/strategies.

                              A few comments based on things that were mentioned earlier:

                              NT stores all its info in an .mdb file (portable access database) which you can easily get to from any .net program. You should be able to obtain read-only access to it concurrently with NT with no problems. You can also use NHibernate to query it, although I believe you will have to use a deprecated version due to NT's lack of support for .NET 3.0/3.5. Many .Net sample applications come with example mdb files (especially true of asp.net samples) so it shouldn't be hard to find example code.

                              Another method of doing what you are trying to do with the file and static method reliably would be to call an external dll (either c# or you could use DllImport with c++) which holds a semaphore or some other concurrency handling mechanism.

                              I too have noticed the concurrency issues in Strategies/Indicators while trying to pass custom events back and forth between them. It is no doubt attributable to the overuse (read: widespread abuse) of statics in NT.

                              Comment


                                #30
                                That's fascinating, sefstrat. I had no idea we could get programmatic access to the .mdb file. It might be useful just to automate some of the "cleanup" of the DB which is necessary when NT corrupts itself.

                                On my part, still making steady progress with Amazon's SimpleDB. It's basically doing what I want now, and the costs are minimal. I'm basically duplicating/replacing NT's position tracking with my own code on SimpleDB (adding/updating rows within OnPositionUpdate), so that I can better handle multi-day/multi-shutdown strategies.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                666 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                376 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                110 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                575 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                580 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X