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

Two independent strategies on the same futures contract

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

    #16
    Originally posted by bobperez View Post
    How can I provide the user with one Parameters window to define the account limits only once and not on each strategy instance?
    Well, uh, hmm, in my opinion, uh, you don't do this with property
    grid parameters.

    You're building a family of strategies that need to cooperate on
    a common set of some family-based parameters, right?

    I'd say use an external text based configuration file.

    Anything will do, XML, or JSON, or an Ini file, or something home
    grown. The Ini file is pretty darn simple, and is easy to maintain
    and update by hand. That's what I use.

    I'd suggest using an Ini file, then make each strategy read the Ini
    file to get the MaxAcctDailyLoss=300 parameter ... all strats read
    and share the same Ini config file.

    The idea is:
    A single point of control (the text based config file) is the simplest
    way to guarantee all strategies are running with the same set of
    'common' values.
    Last edited by bltdavid; 07-17-2023, 01:13 PM.

    Comment


      #17
      Hello bobperez,

      Thank you for your reply.

      The parameters of each strategy instance act independently from one another. That said, you might be able to do something such as have the strategy logic use StreamReader to read a value from a text file; all strategy instances could refer to the same value from the same text file. Here is an example of using StreamReader to read from a text file:


      Please let us know if we may be of further assistance.
      Emily C.NinjaTrader Customer Service

      Comment


        #18
        Thank you Bltdavid and Emily.

        On the same line of thought, I need to control the "Market.Position" for both strategy instances from an external "Account Manager" to avoid conflicts such as a short on chart 1 and a long on chart 2. How do you suggest this may be done?
        BobPerez

        Comment


          #19
          Originally posted by bltdavid View Post

          Well, uh, hmm, in my opinion, uh, you don't do this with property
          grid parameters.

          You're building a family of strategies that need to cooperate on
          a common set of some family-based parameters, right?

          I'd say use an external text based configuration file.

          Anything will do, XML, or JSON, or an Ini file, or something home
          grown. The Ini file is pretty darn simple, and is easy to maintain
          and update by hand. That's what I use.

          I'd suggest using an Ini file, then make each strategy read the Ini
          file to get the MaxAcctDailyLoss=300 parameter ... all strats read
          and share the same Ini config file.

          The idea is:
          A single point of control (the text based config file) is the simplest
          way to guarantee all strategies are running with the same set of
          'common' values.
          Bltdavid,

          May I ask you for a simple example of how you do this with an Ini file and the call to it from the strategies? How would the user define the MaxAcctDailyLoss = ??? to suit his/her needs? How would the Ini file or any other external file recognize if the Market Position is Long/Short/Flat?

          Bobperez
          Last edited by bobperez; 07-17-2023, 03:19 PM.

          Comment


            #20
            Originally posted by bobperez View Post
            May I ask you for a simple example of how you do this with an Ini file and the call to it from the strategies?
            How would the user define the MaxAcctDailyLoss = ??? to suit his/her needs?
            You have some kind of global bool variable, named something like OkToTrade, right?
            Your strategy should only be allowed to submit orders when OkToTrade is true.

            -- Step 1 --

            Create a text file in your 'NinjaTrader 8' folder.
            Let's name the text file 'MySettings.ini'.

            Now, add these lines to the text file,

            [Settings]
            MaxAcctDailyLoss = 300


            -- Step 2 --

            In every strategy, add this using statement,

            using System.Runtime.InteropServices;

            -- Step 3 --

            In every strategy, add ​these lines,

            [DllImport("kernel32.dll", EntryPoint="GetPrivateProfileIntA", CharSet=CharSet.Ansi)]
            public static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefVal, string lpFileName);


            ​-- Step 4 --

            And finally, use something like this code when you want to
            check your account's realized PnL against your account
            daily maximum loss value ...

            Code:
            protected override void OnAccountItemUpdate(Account account, AccountItem accountItem, double value)
            {
                if (accountItem == AccountItem.GrossRealizedProfitLoss)
                {
                    int MaxAcctDailyLoss = GetPrivateProfileInt("Settings", "MaxAcctDailyLoss", 0,
                                                       Core.Globals.UserDataDir+"MySettings.ini");
                    if (MaxAcctDailyLoss > 0 && Math.Abs(value) >= MaxAcctDailyLoss)
                        OkToTrade = false;
                }
            }
            ​
            You'll probably want to stop/start the strategy every day in order to
            reset the realized PnL back to 0, and reset OkToTrade back to true.

            Originally posted by bobperez View Post
            ​How would the Ini file or any other external file recognize if the Market Position is Long/Short/Flat?
            I don't understand why you'd ask that question.

            Why would you think your external configuration file would need
            to know the Market Position of the strategy?

            Your external config file is not code ... it's just a text file, it is
            not possible for your configuration file to 'recognize' anything
            about any aspect of any strategy, running or not.

            -=o=-

            There are lots of ways to read an Ini file.

            Google more about Windows Ini file format here.

            You'll probably want to know more about DllImport and GetPrivateProfileInt.

            Make sense?

            Comment


              #21
              Originally posted by bobperez View Post
              On the same line of thought, I need to control the "Market.Position" for both strategy instances from an external "Account Manager" to avoid conflicts such as a short on chart 1 and a long on chart 2. How do you suggest this may be done?
              If both strategies instances are trading the same instrument,
              you should really use two separate accounts.

              -=o=-

              Otherwise, I'd suggest a lock file.

              In order for either strategy to enter a trade, it must
              first acquire the lock.

              If a strategy fails to acquire the lock, then it does not
              enter the trade, and the setup is abandoned (because
              the other strategy is busy trading in this account).

              When a strategy becomes flat, it must release the
              lock file.

              -=o=-

              A lock file is advanced programming, and unless you
              know C# well, it may be a lot easier to just use two
              different accounts.

              Comment


                #22
                Thank you very much, Bltdavid. The steps are very clear. The only issue I see is that the Account limits(Daily Profit/Loss) should be user-defined, not hard-coded. How do you suggest the user defines those limits and write them to the Ini file?

                Regarding the account's Market Position, what I'm really trying to understand is how to add code, perhaps to each strategy, to recognize if any one of the strategy instances has a market.position != flat, the rest of the strategy instances that are flat may not submit orders until the account is flat.

                BobPerez
                Last edited by bobperez; 07-17-2023, 05:45 PM.

                Comment


                  #23
                  Originally posted by bobperez View Post
                  Regarding the account's Market Position, what I'm really trying to understand is how to add code, perhaps to each strategy, to recognize if any one of the strategy instances has a market.position != flat, the rest of the strategy instances that are flat may not submit orders until the account is flat.
                  No problem, I think my answer about using a lock file
                  criss-crossed with your reply.

                  In the past, I've used the lock file approach with great success.
                  I think it works very well.

                  Btw, I always locate the lock file in the 'NinjaTrader 8\tmp' folder.

                  Why is that?
                  Because the tmp folder automatically gets cleared every time
                  NinjaTrader starts up.


                  Comment


                    #24
                    Originally posted by bltdavid View Post

                    A lock file is advanced programming, and unless you
                    know C# well, it may be a lot easier to just use two
                    different accounts.

                    Thanks again. Bltdavid. I am not an advanced programmer, and the idea is to use one account, so I guess I'll have to study how to use the lock file.

                    Regarding the Ini file, The only issue I see is that the Account limits(Daily Profit/Loss) should be user-defined, not hard-coded. How do you suggest the user defines those limits and then write them to the Ini file?

                    BobPerez

                    Comment


                      #25
                      Hmm, just a minor advisement here on parameter names
                      inside the Ini configuration file. This might help you.

                      I checked my code, and I actually use these parameter
                      names for the account settings,

                      AcctMaxDailyLoss
                      AcctMaxDailyProfit


                      which (for me) jives quite well with the per-strategy settings,
                      which uses these parameter names,

                      MaxDailyLoss
                      MaxDailyProfit


                      -=o=-

                      See that?
                      By adding the 'Acct' prefix, I could make any per-strategy setting
                      into a 'global' account setting -- of course, I had to add the code
                      to perform the 'global' account check to every strategy -- but using
                      the parameter naming convention of an 'Acct' prefix to symbolize
                      a global setting worked out very well.

                      My point is:
                      A good naming convention can be very helpful.

                      Just my 2˘.


                      Comment


                        #26
                        Originally posted by bobperez View Post
                        I am not an advanced programmer, and the idea is to use one account, so I guess I'll have to study how to use the lock file.

                        Regarding the Ini file, The only issue I see is that the Account limits(Daily Profit/Loss) should be user-defined, not hard-coded.
                        Exactly.

                        By using an external file, these settings are user-defined,
                        and they are not hard-coded into any one strategy.

                        Originally posted by bobperez View Post
                        How do you suggest the user defines those limits and then write them to the Ini file?
                        With their fingers, using Notepad.




                        Comment


                          #27
                          Originally posted by bltdavid View Post

                          Exactly.

                          By using an external file, these settings are user-defined,
                          and they are not hard-coded into any one strategy.



                          With their fingers, using Notepad.



                          Ok, we must admit it's not the most user-friendly way to have users define these settings. They need to find the Ninjatrader 8 folder, look for the file, and write the information without messing anything up.

                          [Settings]
                          MaxAcctDailyLoss =
                          MaxAcctDailyProfit =

                          Although it may be easy for experienced users, this process is what I was thinking should be done from some input form, and then processed automatically to relieve the user from doing it manually. The strategy may be used by hundreds of people. I can't imagine how many will ask for support.

                          Comment


                            #28
                            Originally posted by bobperez View Post
                            Ok, we must admit it's not the most user-friendly way to have users define these settings. They need to find the Ninjatrader 8 folder, look for the file, and write the information without messing anything up.

                            [Settings]
                            MaxAcctDailyLoss =
                            MaxAcctDailyProfit =

                            Although it may be easy for experienced users, this process is what I was thinking should be done from some input form, and then processed automatically to relieve the user from doing it manually. The strategy may be used by hundreds of people. I can't imagine how many will ask for support.
                            Well, there's the rub. If you want an input form, write one.

                            You never revealed you might be a vendor with hundreds of customers.

                            ​At some point, I agree, a couple of hundred users for your strategies can
                            certainly create issues. If you have enough customers, it certainly sounds
                            justifiable to create a front-end interface to the Ini file.

                            As far as support goes, how often will customers be changing parameters
                            in your Ini file? I mean, something like AcctMaxDailyLoss is not something
                            you'd expect would be changed all that often, right?

                            Anyway, you'll have to decide for yourself what to do, esp with regards to
                            handling a couple of hundred users.

                            ​-=o=-

                            My 'external file' suggestion is just one way to go.

                            How to mitigate your complaints?

                            Idea 1:
                            You can always create a bare bones Ini file with all the parameters already
                            defined (and set to 0) and all you'd have to do is have your installation/installer
                            drop this bare bones file into the correct location. It sits there and is benign
                            until the user wants to setup account wide global 'Daily Max Loss' settings,
                            upon which they'd have to follow your procedures on how to locate this
                            file and which parameters to edit. But at least you gave them a bare
                            bones file to edit, and they didn't have to create it completely from
                            scratch.

                            You should adopt the convention that parameters defined as '0' don't do
                            anything in the code, that way you can define them as 0 in the bare bones
                            Ini file your install procedure sets up for each customer.

                            If you really have lots of customers, you'll probably want to create a folder
                            exclusive to just your stuff and put the Ini file there,

                            NinjaTrader 8\BobStuff\MySettings.ini

                            Idea 2:
                            Create a separate GUI program that prompts the user for all account global
                            settings, then have this GUI program create/update the Ini file.

                            Idea 3:
                            Write a special AddOn or Indicator that prompts for all global settings, and
                            have it create/update the Ini file.

                            Last edited by bltdavid; 07-26-2023, 09:38 PM. Reason: typo

                            Comment


                              #29
                              Originally posted by bltdavid View Post

                              Well, there's the rub. If you want an input form, write one.

                              You never revealed you might be a vendor with hundreds of customers.

                              ​At some point, I agree, a couple of hundred users for your strategies can
                              certainly create issues. If you have enough customers, it certainly sounds
                              justifiable to create a font-end interface to the Ini file.

                              As far as support goes, how often will customers be changing parameters
                              in your Ini file? I mean, something like AcctMaxDailyLoss is not something
                              you'd expect would be changed all that often, right?

                              Anyway, you'll have to decide for yourself what to do, esp with regards to
                              handling a couple of hundred users.

                              ​-=o=-

                              My 'external file' suggestion is just one way to go.

                              How to mitigate your complaints?

                              Idea 1:
                              You can always create a bare bones Ini file with all the parameters already
                              defined (and set to 0) and all you'd have to do is have your installation/installer
                              drop this bare bones file into the correct location. It sits there and is benign
                              until the user wants to setup account wide global 'Daily Max Loss' settings,
                              upon which they'd have to follow your procedures on how to locate this
                              file and and which parameters to edit. But at least you gave them a bare
                              bones file to edit, and they didn't have to create it completely from
                              scratch.

                              You should adopt the convention that parameters defined as '0' don't do
                              anything in the code, that way you can define them as 0 in the bare bones
                              Ini file your install procedure sets up for each customer.

                              If you really have lots of customers, you'll probably want to create a folder
                              exclusive to just your stuff and put the Ini file there,

                              NinjaTrader 8\BobStuff\MySettings.ini

                              Idea 2:
                              Create a separate GUI program that prompts the user for all account global
                              settings, then have this GUI program create/update the Ini file.

                              Idea 3:
                              Write a special AddOn or Indicator that prompts for all global settings, and
                              have it create/update the Ini file.

                              Thank you Bltdavid. Good ideas!! I'll implement one of them!

                              BobPerez

                              Comment


                                #30
                                Some final thoughts ...

                                I think you should start with Idea 1 -- then branch out from there.

                                If you implement Idea 2 or 3, you should probably initialize by reading all
                                parameters from the Ini file (if it exists) for their current values. Then your
                                solution lets the user change these values as needed, and finally it rewrites
                                the Ini file completely with the new (possibly updated) values.

                                And there ya go. This means that Idea 1 (giving each user an initial bare
                                bones Ini file with all parameters defined as 0) is actually a really good idea.

                                Your update approach would just be updating this bare bones file (or the
                                changed values, if the user previously edited/changed them). If the Ini file
                                is missing, then your solution would just create a new one.

                                A bare bones Ini file is a great way for a user to get started. They'd either
                                edit it themselves, if they feel so inclined, or fire up your handy dandy GUI
                                solution and change the global values that way. They could go back & forth,
                                hand editing one day but using your tool the next day, and vice versa.

                                -=o=-

                                One more thing.

                                Note the code solution I gave you has an important feature that could be
                                quite the lovely aid -- it automatically reads the Ini file for the current value
                                of the global setting any time OnAccountItemUpdate is executed

                                This means the user could update their global settings, either using Notepad
                                or via your GUI solution, and the global settings should update in real-time.

                                Real-time? Yep.
                                The new values will immediately take effect the next time the account's PnL
                                is updated (aka, the next time a profit target is reached and/or a position
                                goes flat).

                                How does that happen?
                                Every time the account's PnL changes, OnAccountItemUpdate gets executed
                                inside all running strategies, all at the same time, and poof, the new global
                                Ini parameter values will be re-read for the AcctMaxDailyLoss check.

                                Pretty cool, eh?

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by fx.practic, 10-15-2013, 12:53 AM
                                5 responses
                                5,404 views
                                0 likes
                                Last Post Bidder
                                by Bidder
                                 
                                Started by Shai Samuel, 07-02-2022, 02:46 PM
                                4 responses
                                95 views
                                0 likes
                                Last Post Bidder
                                by Bidder
                                 
                                Started by DJ888, Yesterday, 10:57 PM
                                0 responses
                                8 views
                                0 likes
                                Last Post DJ888
                                by DJ888
                                 
                                Started by MacDad, 02-25-2024, 11:48 PM
                                7 responses
                                159 views
                                0 likes
                                Last Post loganjarosz123  
                                Started by Belfortbucks, Yesterday, 09:29 PM
                                0 responses
                                8 views
                                0 likes
                                Last Post Belfortbucks  
                                Working...
                                X