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

Custom Share Size For EntryLong

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

    Custom Share Size For EntryLong

    Hello,

    I am having issues with figuring out how to code my calculation for the amount of shares to trade when entering into a position (long or short).

    I want the share size to be calculated by taking the Previous Candle High and subtracting it from the Previous 5sma. I would then take the desired Risk Amount and divide it by the difference to get the share size.

    So in essence it should look something like this.

    ((Risk)/(High[1]-SMA5[1]))

    However, when I try to add this into the EnterLong script, it doesn't work.

    Ex. EnterLong((120)/High[1]-SMA5[1])), @"");

    Can anyone help me get this to work?

    #2
    Hello,

    Thank you for the post.

    In regard to this not working, what specifically is not working? are you seeing an error or is this related to the price used?

    If the price is in question here, I would likely suggest you Print the value you are calculating to verify that is a correct price being returned.

    For example:

    Code:
    Print((120)/High[1]-SMA5[1]);
    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Where should this be placed?

      OnBarUpdate?

      And where can I view the print if it is showing?

      Comment


        #4
        Hello,

        Thank you for the reply.

        This should be placed before where you use it, if your sample EnterLong is in OnBarUpdate you would need to place it before that.

        To view prints you can use the New -> NinjaScript output window. We have a short guide to debugging at the following link: https://ninjatrader.com/support/foru...49&postcount=2

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Like this?
          Attached Files

          Comment


            #6
            Hello,

            No that would not be valid for the purpose of printing.

            You would need to place the print either after the curly brace inside of the method or before your if statement. The area directly after an if statement between it and the curly brace is not a valid syntax location.

            Code:
            if (..... )
            {
            Print
            or

            Code:
            Print
            if(....)
            {
            Please let me know if I may be of additional assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thanks.

              This is the error I get when I try to input the same code into the EnterLong Method.

              Do you know what the error is?
              Attached Files

              Comment


                #8
                Hello,

                Thank you for providing the error you are seeing.

                The error would mean what it says, you are trying to use a double where it is expecting you to use an int. The print, in this case, would only allow you to see the value you calculated but you have a compile error so you would need to address that first.

                A double contains a remainder or numbers after the decimal point, an int or integer is a whole number with no decimal. You would need to convert your calculation into an int, very likely you could do the following:

                int myQuantity = (int) ((Risk)/(High[1]-SMA5[1]))
                EnterLong(myQuantity, "SignalName");

                Please let me know if I may be of additional assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  For the private section do i add it like this:

                  private int myQuantity;

                  ??

                  Comment


                    #10
                    Hello,

                    Only if you need to store it for later use. Otherwise, you could make a local variable like the prior post shows. It would really depend on what you want to do.

                    Please let me know if I may be of additional assistance.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Alright it worked!

                      Thank you so much!

                      Now how can I round it to the nearest hundred.

                      For example. If it is calculating shares of 288 i would like it to round it to 300.

                      Comment


                        #12
                        Hello,

                        For this type of question, I would suggest searching google for the C# answer. As you are using C# there are many existing examples of how to accomplish C# tasks like Rounding.

                        An example search could be "How to round a number to nearest hundred C#"

                        There are many ways to do this, here is one result showing one way to round to the nearest 100:
                        I don't know it my nomenclature is correct! Anyway, these are the integer I have, for example : 76 121 9660 And I'd like to round them to the close hundred, such as they must become : 100 100 97...

                        I don't know it my nomenclature is correct! Anyway, these are the integer I have, for example : 76 121 9660 And I'd like to round them to the close hundred, such as they must become : 100 100 97...


                        Please let me know if I may be of additional assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Got it.. Will try to work on that now.

                          Just one last question. Now that it takes a calculation of shares.

                          When I exit the long trade. How do I code it that it closes the current position size.

                          Right now I have it set at 100 which doesn't close the position if I entered with 300 shares from the calculation.

                          Is there a way to "Flatten" the position no matter the share size?

                          ExitLong(100,@"",@""); <- My current ExitLong Code.

                          Comment


                            #14
                            Hello,

                            When using the Managed Approach you would have the following stipulations of using an exit:

                            If you do not specify a quantity the entire position is exited rendering your strategy flat
                            If you do not specify a "fromEntrySignal" parameter the entire position is exited rendering your strategy flat

                            So in this case, to exit the whole position you could not specify a quantity.


                            Please let me know if I may be of additional assistance.
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Hello,

                              I am getting duplicate orders when its triggering live.

                              I think it may be because of where I placed the myQuantity along with the EnterLong Trade.

                              I added a picture of how and where I placed it in the code.

                              I am thinking that as the calculation changes within the 1 bar, it continues to buy to try and balance the share size out. Not sure though. Kinda stumped on this one.

                              Here is an image.

                              Its kind of annoying that this is happening, and want to get to the bottom of it.

                              Could it be that I have my code currently using OnEachTick instead of OnPriceChange?
                              Attached Files
                              Last edited by dorony; 02-08-2018, 02:08 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by michi08, 10-05-2018, 09:31 AM
                              5 responses
                              741 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by The_Sec, Today, 02:29 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post The_Sec
                              by The_Sec
                               
                              Started by tsantospinto, 04-12-2024, 07:04 PM
                              4 responses
                              62 views
                              0 likes
                              Last Post aligator  
                              Started by sightcareclickhere, Today, 01:55 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post sightcareclickhere  
                              Started by Mindset, 05-06-2023, 09:03 PM
                              9 responses
                              259 views
                              0 likes
                              Last Post ender_wiggum  
                              Working...
                              X