• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 + 50 * TickSize && Close[0] <= Position.AveragePrice + 75 * TickSize)
        {
              SetStopLoss(CalculationMode.Price, Position.AveragePrice);
        }
        else if (Close[0] > Position.AveragePrice + 75 * TickSize )
        {
              SetStopLoss(CalculationMode.Price, Close[0]);
        }
        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 futurenick, Today, 06:26 AM
            1 response
            11 views
            0 likes
            Last Post nkhoi
            by nkhoi
             
            Started by NinjaCustomer, Today, 12:29 PM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_ShawnB  
            Started by Steve L, Today, 11:32 AM
            0 responses
            5 views
            0 likes
            Last Post Steve L
            by Steve L
             
            Started by prabbala, Today, 10:41 AM
            0 responses
            3 views
            0 likes
            Last Post prabbala  
            Started by bebbus, Today, 10:36 AM
            0 responses
            4 views
            0 likes
            Last Post bebbus
            by bebbus
             
            Working...
            X