Announcement

Collapse
No announcement yet.

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:
                        https://stackoverflow.com/questions/...53655#13153655
                        https://stackoverflow.com/questions/...21665#36421665

                        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.
                            https://ninjatrader.com/support/help...s/exitlong.htm

                            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 toughiee, Today, 12:04 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post toughiee  
                              Started by vdoan20, Yesterday, 08:30 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post vdoan20
                              by vdoan20
                               
                              Started by gupagoer, Yesterday, 07:42 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post gupagoer  
                              Started by Holligoly, Yesterday, 05:13 PM
                              2 responses
                              11 views
                              0 likes
                              Last Post Holligoly  
                              Started by Raginpower, Yesterday, 01:53 PM
                              0 responses
                              14 views
                              0 likes
                              Last Post Raginpower  
                              Working...
                              X