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

Stop moved to BE

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

    Stop moved to BE

    I've googled, and searched and read thru all the posts i can find and answer that works for me regarding this topic,and my code in my strategy.

    The idea is, I'm looking for the my stop order to move to BE +/- 1 tick once price goes above 10 ticks from entry. I get the output to print what i ask it to do, but the order itself does not adjust to BE +/-1.

    // Breakeven Stuff
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss (CalculationMode.Ticks, stop);
    }

    // Long breakeven side
    if (Position.MarketPosition == MarketPosition.Long
    && Close[0] > Position.AveragePrice + 10 * TickSize)
    {
    SetStopLoss (CalculationMode.Ticks, Position.AveragePrice + 1 * TickSize);
    Print ("we changed to +5");
    }
    // Short breakeven side
    if (Position.MarketPosition == MarketPosition.Short
    && Close[0] < Position.AveragePrice - 10 * TickSize)
    {
    SetStopLoss (CalculationMode.Ticks, Position.AveragePrice - 1 * TickSize);
    Print ("we changed to -5");
    }


    In addition to that, i was wondering if it's possible to Move my Target order to BE +1 if the market goes below my entry price by X amounts. Similar to the way the stop is handled.

    THanks,



    #2
    Hello adrew99 ,

    Thanks for your post!

    In this line: SetStopLoss (CalculationMode.Ticks, stop); you are creating a stop, based on ticks and I assume the variable stop is an integer type that has been assigned a specific number of ticks. When an entry order is filled, the SetStopLoss() method will either add or subtract the variable "stop" number of ticks from the entry price to locate the stop loss order (according to the direction of the entry).

    In your other lines, like this one: SetStopLoss (CalculationMode.Ticks, Position.AveragePrice + 1 * TickSize); you are adding 1 tick to the value of the entry order price however as you have specified the calculation mode of ".Ticks" this price is then converted to an integer value and applied as ticks. So if you are trading crude oil with an entry price of 65.36 you are specifying the stop to 65+1 tick = 66 ticks from the average entry price. What you really want to do with the SetStopLoss() method is to change the calculation mode from ticks to price in these instances because you are specifying a price level (Position.AveragePrice + 1 * TickSize). So all you should need is to change to: SetStopLoss (CalculationMode.Price, Position.AveragePrice + 1 * TickSize);

    Just to be clear, you can use .Ticks in one part of your code and .Price in another part.

    Yes, you can similarly move your profit target. Like your stop loss, don't forget to reset the profit target back to ticks from entry when you are in a flat position as otherwise, the method will use the last price it was assigned on the next entry which may be inappropriate.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thank you Paul. That seems to work perfect.

      I now have a similar issue, that i was hoping you could help me with. I've followed some other posts in this forum, and found a dynamic stop that moves the order twice. Once when price goes 9 ticks from the entry price (stop moved to +5) and then again when the price goes to +16 (stop moved to +12). However, the second adjustment (+12) never triggers. It moves, but then moves down to +5 as the market comes back down. To add more context to the code below, i have a private bool to print when the order is changed only once and not on every barupdate that seems to only print for the first stop adjustment, and not the second. I know i've coded this wrong, but i cant seem to figure out where that is exactly. Any help would be great.

      // Private stuff
      private bool doOneTime = true;
      private bool controlone = true;


      // Breakeven Stuff
      //Reset Stop
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss (CalculationMode.Ticks, stop);
      doOneTime = true;
      controlone = true;

      }

      // Long breakeven Target Stop
      if (Position.MarketPosition == MarketPosition.Long)

      {
      if (Close[0] >= (Position.AveragePrice + 9 * TickSize) && controlone)
      {
      SetStopLoss("",CalculationMode.Price, Position.AveragePrice + 5 * TickSize, true);
      controlone = true;

      if (doOneTime)
      {
      Print ("Long stop +5");
      doOneTime = false;
      }
      }
      if (Close[0] >= (Position.AveragePrice + 16 * TickSize) && controlone)

      {
      SetStopLoss("",CalculationMode.Price, Position.AveragePrice + 12 * TickSize, true);
      if (doOneTime)
      {
      Print ("Long stop +11");
      doOneTime = false;
      }
      }
      }

      Comment


        #4
        Hello adrew99,

        Thanks for your reply.

        Here is an example where you have two conditions and the actions are only executed once in each set.
        Hopefully, you can review this and compare to what you are doing to see what is going on.

        if (various condition set 1 are true and mybool1 is true and mybool2 is false)
        {
        // perform some action here
        mybool1 = false; // set false to prevent redoing actions
        mybool2 = true; // set true so we can move to step 2 when condition is ready
        }
        if (various conditions set 2 are true and mybool2 is true)
        {
        // perform the next action
        mybool2 = false; // prevent repeating section 2
        }


        If this does not help resolve the issue, I highly recommending printing the states of the bools both before and after the conditions.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          I was somewhat confused by your answer, but i was able to get it to operate like needed. For those interested, this is the code i'm using.

          Public Class

          // Private stuff
          private bool doOneTime = true;
          private bool controlone = true;

          //market conditions up here.
          Entry order


          //Reset Stop
          if (Position.MarketPosition == MarketPosition.Flat)
          {
          SetStopLoss (CalculationMode.Ticks, stop);
          doOneTime = true;
          controlone = true;

          }

          // Long breakeven Target Stop
          if (Position.MarketPosition == MarketPosition.Long)

          {
          if (Close[0] >= (Position.AveragePrice + 9 * TickSize) && controlone)
          {
          SetStopLoss("",CalculationMode.Price, Position.AveragePrice + 6 * TickSize, true);
          controlone = false;

          if (doOneTime)
          {
          Print ("Long stop +5");
          doOneTime = true;
          }

          }

          if (Close[0] >= (Position.AveragePrice + 16 * TickSize) && !controlone)

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

          if (doOneTime)
          {
          Print ("Long stop +12");
          doOneTime = false;
          }
          }
          }
          // Short breakeven Taget Stop
          if (Position.MarketPosition == MarketPosition.Short)

          {
          if (Close[0] <= (Position.AveragePrice - 9 * TickSize) && controlone)
          {
          SetStopLoss("",CalculationMode.Price, Position.AveragePrice - 6 * TickSize, true);
          controlone = false;

          if (doOneTime)
          {
          Print ("Short stop -5");
          doOneTime = true;
          }
          }
          if (Close[0] <= (Position.AveragePrice - 16 * TickSize) && !controlone)

          {
          SetStopLoss("",CalculationMode.Price, Position.AveragePrice - 12 * TickSize, true);

          if (doOneTime)
          {
          Print ("Short stop -12");
          doOneTime = false;
          }
          }
          }

          Comment


            #6
            Hello adrew99,

            Thanks for your reply and for sharing your code for the benefit of the community.

            My only comments are:

            1) You do not have a bool to prevent the 2nd adjustment (12 tick) from being repeatedly resubmitted if the condition remains true and that is why in the example I am using two bools, one for each condition.
            2) I recommend that you use different bools for the long side and for the short side as that way no unintended actions can occur.

            Paul H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Option Whisperer, Today, 09:55 AM
            0 responses
            3 views
            0 likes
            Last Post Option Whisperer  
            Started by geddyisodin, 04-25-2024, 05:20 AM
            8 responses
            58 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by halgo_boulder, 04-20-2024, 08:44 AM
            2 responses
            21 views
            0 likes
            Last Post halgo_boulder  
            Started by mishhh, 05-25-2010, 08:54 AM
            19 responses
            6,189 views
            0 likes
            Last Post rene69851  
            Started by gwenael, Today, 09:29 AM
            0 responses
            5 views
            0 likes
            Last Post gwenael
            by gwenael
             
            Working...
            X