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

Strategy running against a basket of stocks

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

    Strategy running against a basket of stocks

    Hi NT Team,
    I wonder if you could give me a little direction on where to start my search. I designed a strategy in Ninjascript which is scanning a basket of stocks for certain criterias to enter. Every time a setup is found i want the strategy to enter a trade but only with a fraction of the account size. Let say I have a 25k account and I want the max position per symbol to not be bigger than 5k and then keep monitoring the basket for another opportunity... ideally having 5 different 5k positions/symbols out of a basket of stock (i predefined before). Can this be accomplished? If yes how? Any advice would be appreciated.
    Thanks

    #2
    Hello JBU1314,

    Thank you for your note.

    Yes, this would be possible. Towards the bottom of the link below, you'll see an example of getting a cash value of the account,


    I have put together a sample strategy which when applied will save the value of the SIM101 account to a variable, divide that variable by 5 (20%), divide the closing price of the stock the strategy is applied to, round down, then print the amount of shares which would be 1/5th of your account size. You could then pass sharesToBuy to your EnterLong() method for example, for what you are looking to do. I wrote this I wrote this with equities in mind.

    To Import a NinjaScript into NinjaTrader 8 do the following:
    From the Control Center window select Tools -> Import-> NinjaScript...
    Find the file location.

    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thank you very much!

      Comment


        #4
        Hi Alan, I would have a follow up to that threat. I implemented that into my strategy and modified it a litte bid to fit my futures strategies.
        My goal is to increase position size with increasing Account Value based on respective margin requirements. The formula below seems to make sense and seems to work but can i backtest that? The system always works with the base 100k from the Sim101 account. I would like to say start with a 25K account and see how far it goes over time and how many positions will be opened?

        lock (Account.All)
        account = Account.All.FirstOrDefault(a => a.Name == "Sim101");

        xAccountSize= Account.Get(AccountItem.CashValue, Currency.UsDollar);
        int sharesToBuy= (int)Math.Floor((xAccountSize/MarginRequirement));

        base.EnterLong(sharesToBuy,"B"+CurrentBar);

        Comment


          #5
          Hello JBU1314,

          Because you can’t call historical account values, you would need to modify the code for it to work within the strategy analyzer/backtest. What you can do is set a starting account value variable and have it adjusted plus or minus for each winning trade.

          I have modified the initial strategy I provided, which will allow you to set the initial account size variable and run a backtest in the strategy analyzer.

          I applied the strategy on AMZN daily series from 1997 through today, and you can see how the shares traded is adjusted as account equity grows.

          Please let us know if you need further assistance.
          Attached Files
          Alan P.NinjaTrader Customer Service

          Comment


            #6
            Thank you very much Alan, very much appreciated!

            Comment


              #7
              Hi Alan, the file works great with Stocks but it seems I am not able to make it work with Futures (ES, NQ). Any advice?

              Comment


                #8
                Hello JBU1314,

                Correct, I used the closing stock price divided by (Account Size *.2), to give you how many shares to buy. You would have to account for margin with futures.

                I have provided a sample strategy which checks if your account size is greater than the instruments initial margin requirement, which you could modify to provide you with the number of contracts you could buy.

                Please let us know if you need further assistance.
                Attached Files
                Alan P.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you for the prompt response Alan! As usual highly appreciated.
                  This approach does only work in live markets now right?

                  Comment


                    #10
                    Hello JBU1314,

                    That is correct, the sample would only work in a live environment as you can’t call historical account values in a backtest. You would have to use the concept in the previous sample to modify the code to use a starting account value if you wanted to use it in a backtest. Also, similar to how you can’t call historical account values, you would not be able to call historical contract margin values, thus you would be assuming these never changed in the backtesting period. This may or may not have been the case.

                    Please let us know if you need further assistance.
                    Alan P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Aviram Y, Today, 05:29 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post Aviram Y  
                    Started by quantismo, 04-17-2024, 05:13 PM
                    3 responses
                    27 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by ScottWalsh, 04-16-2024, 04:29 PM
                    7 responses
                    35 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by cls71, Today, 04:45 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post cls71
                    by cls71
                     
                    Started by mjairg, 07-20-2023, 11:57 PM
                    3 responses
                    218 views
                    1 like
                    Last Post PaulMohn  
                    Working...
                    X