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

SamplePriceModification - Modification

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

    SamplePriceModification - Modification

    Hello,

    I am new here. I am interested to see if anybody can add a piece of code in to the SamplePriceModification (Strategy) for me.

    Right now it has a SL set at 20 ticks with a PT of 100 ticks. If the price moves up 50 ticks set the SL to B/E

    I would like to add a piece of code in that then says if the price moves up to 75 ticks set a new SL at 25

    Is this possible?

    Thank you
    Attached Files

    #2
    Hello JustAFish,

    Thank you for the post.

    While I wouldn't be able to do this for you, I can provide educational information surrounding NinjaScript and how you may approach this in your own programming. Would this be of interest to you or are you strictly looking to have this developed for you? In most cases, if you want something developed for you, you would need to find and work with a third party developer however other members may chime in on this if they feel they can easily modify the script.



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

    Comment


      #3
      I would think this would be an extremely common question and coding area... It would make sense to show a piece of code regarding it for other newbies I would assume. I have seen and read lots of other threads like this but haven't been able to understand the answer. Sometimes seeing snippets is the easiest way to learn.

      I would appreciate an other insight regarding this! Thank you

      Comment


        #4
        Hello JustAFish,

        Thank you for your reply.

        Yes, we generally can provide samples or suggestions for various concepts. Based on how you worded the original post it sounded like you didn't want to try this yourself and just wanted someone to do it for you, so I had to ask.

        You can see a snippet of the general code you would need in the file already, you would need to duplicate the existing condition and change the values to create the next step. Because the script was not programmed to account for two steps you would additionally need to modify the first condition to only work for the first steps range so it doesn't just reset to break even.

        Here is a sample of how you might approach modifying the script to include two steps.

        Code:
        if (Close[0] > Position.AveragePrice + [B]50 [/B]* TickSize && Close[0] <= Position.AveragePrice + 75 * TickSize)
        {
              SetStopLoss(CalculationMode.Price, Position.AveragePrice);
        }
        else if (Close[0] > Position.AveragePrice + [B]75 [/B]* TickSize )
        {
              SetStopLoss(CalculationMode.Price, [B]Close[0][/B]);
        }
        This would create a fist condition which only works within a range of prices, and the second would work after that range of prices. In the second condition, the Close price is used for the stop price instead of the Position.AveragePrice. One note surrounding the second step is that this is more or less a trailing stop in this example because each OnBarClose after 75 ticks would update the stop. You can choose to handle this in a few ways, making a variable that is pre calculated with the price for the second modification would be one way, using logic such as a bool variable so this happens once would be another.

        https://ninjatrader.com/support/help...n-us/close.htm

        I look forward to being of further assistance.
        Last edited by NinjaTrader_Jesse; 02-28-2019, 08:07 AM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Ok. So below is what I worked on then. Is there a more efficient way of doing this or a better direction I should head?

          Also for the end of this code would it be possible to add a TrailStop instead? I tried to mess with it and couldn't get anything to work?

          Thank you!

          // Once the price is greater than entry price, set stop loss & move up with adjustments

          if (Close[0] > Position.AveragePrice + 6 * TickSize && Close[0] <= Position.AveragePrice + 14 * TickSize)

          {

          SetStopLoss(CalculationMode.Price, Position.AveragePrice + 1 * TickSize);

          } else if (Close[0] >= Position.AveragePrice + 14 * TickSize && Close[0] <= Position.AveragePrice + 27 * TickSize)

          {

          SetStopLoss(CalculationMode.Price, Position.AveragePrice + 6 * TickSize);

          } else if (Close[0] >= Position.AveragePrice + 27 * TickSize && Close [0] <= Position.AveragePrice + 36 * TickSize)

          {

          SetStopLoss(CalculationMode.Price, Position.AveragePrice + 12 * TickSize);

          } else if (Close[0] >= Position.AveragePrice + 36 * TickSize && Close [0] <= Position.AveragePrice + 45 * TickSize)

          {

          SetStopLoss(CalculationMode.Price, Position.AveragePrice + 16 * TickSize);

          } else if (Close[0] >= Position.AveragePrice + 45 * TickSize && Close [0] <= Position.AveragePrice + 54 * TickSize)

          {

          SetStopLoss(CalculationMode.Price, Position.AveragePrice + 20 * TickSize);

          } else if (Close[0] >= Position.AveragePrice + 54 * TickSize && Close [0] <= Position.AveragePrice + 63 * TickSize)

          {

          SetStopLoss(CalculationMode.Price, Position.AveragePrice + 40 * TickSize);

          Comment


            #6
            Hello JustAFish,

            Thank you for your reply.


            This seems pretty straight forward, how many levels were going to be needed? The only extra automation I might be able to suggest here would be defining user inputs for the levels. For example if you wanted a max of 5 levels, you could make 5 user inputs where you type in 1, 6, 12, 16, 20,40, or whatever values you wanted. Otherwise if more levels through a range are required and this is making a large amount of syntax you could form a method of this.

            Code:
            private void CheckPrices(double priceOne, double priceTwo, double priceThree)
            {
                 if (Close[0] >= priceOne && Close [0] <= priceTwo)
                     SetStopLoss(CalculationMode.Price, priceThree);
            }
            
            protected override void OnBarUpdate()
            {
                 CheckPrices(Position.AveragePrice + 45 * TickSize, Position.AveragePrice + 54 * TickSize, Position.AveragePrice + 20 * TickSize);
            }

            For the trailing stop, you would essentially just need to make the last step like I had in the example I provided where there is no top limit and the Close price is used however you would want to use an Offset with this price for the trail distance:



            Code:
            else if (Close[0] > Position.AveragePrice + 75 * TickSize )
            {
                  SetStopLoss(CalculationMode.Price, Close[0] - 5 * TickSize);
            }


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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by algospoke, Today, 06:40 PM
            0 responses
            9 views
            0 likes
            Last Post algospoke  
            Started by maybeimnotrader, Today, 05:46 PM
            0 responses
            7 views
            0 likes
            Last Post maybeimnotrader  
            Started by quantismo, Today, 05:13 PM
            0 responses
            7 views
            0 likes
            Last Post quantismo  
            Started by AttiM, 02-14-2024, 05:20 PM
            8 responses
            168 views
            0 likes
            Last Post jeronymite  
            Started by cre8able, Today, 04:22 PM
            0 responses
            10 views
            0 likes
            Last Post cre8able  
            Working...
            X