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

Stoploss/Profit set as Low or High of previous candle

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

    Stoploss/Profit set as Low or High of previous candle

    Hello,

    I would like to ask for you help.

    I am seeking a way,how to set up stoploss or profit taking orders on previous bar price(high, or low, or else). Specifically in this case, I want set up a stoploss on Low[0] - that is signal bar low price.

    When I tried to define it like this in the State== State.Configure area, nothing happened and the script didnt even run in analyzer.

    SetProfitTarget("", CalculationMode.Percent, 0.01);

    SetStopLoss("", CalculationMode.Price,(Low[0]), false);


    I guess it has to do something with the logical order of things,when you put it into this area and set it to some price of candle zero like Low [0].


    So I tried to put it in the OnBarUpdate() section like this:

    if (
    Open[0]<Close[0] &&

    Math.Abs(Close[0]-Open[0]) <= Math.Abs((Open[0]-Low[0])*0.5)
    )


    {

    EnterLong(Convert.ToInt32(DefaultQuantity),"");

    SetProfitTarget("", CalculationMode.Percent, 0,01);

    SetStopLoss("", CalculationMode.Price,(Low[0]), false);

    }


    That kind of works, but not always it seems, its strange. One of the problems seems that if one signal occurs(a Candle defined like mentioned above) and then, before any of the S/L or P/T order for this signal is executed, apperas another signal (Candle) ---the orders for the first candle re-set for values on the second candle- so the S/L for the first signal is set on Low price for the second candle ,rather than the Low of the first signal candle as was meant to be.


    So my question would be how to properly set up S/L or P/T orders, when you intent to put it as price of some specific previous bar, in this case the signal bar [0] - but I guess[1],[2],[3] bars should be the same.. And stay at that level until the order triggers and not to move on to another candles as the time passes or similar signal appears.


    Thank you very much for you help.

    #2
    Hello ExNihilon,

    Thanks for the post.

    It sounds like the entry condition is being hit again and the stop and target are being updated. Another entry is not created because your Entries Per Direction is set to 1 (assumed). Adding a check for your current market position should fix that:

    Code:
    if (Open[0]<Close[0] && Math.Abs(Close[0]-Open[0]) <= Math.Abs((Open[0]-Low[0])*0.5) && Position.MarketPosition == MarketPosition.Flat)
                {
    
                    EnterLong(Convert.ToInt32(DefaultQuantity),"");
    
                    SetProfitTarget("", CalculationMode.Percent, 0,01);
    
                    SetStopLoss("", CalculationMode.Price,(Low[0]), false);
    
                }
    Please let me know if this does not resolve your inquiry.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Chris,thank you very much,

      that solved my problem I described.

      On the other hand, it is not possible to have multiple entries now. Would you know about easy way how to have both my problem solved and have multiple entries from same signal opened while maintaining unique S/L, P/T for each entry based on the price level -Low[0] or High[0] of the entry Bar from the signal?

      Thank you again

      Comment


        #4
        Hello ExNihilon,

        Thanks for the reply.

        To support multiple entries, use a signal name to identify your entries. If your strategy calculates OnBarClose, do something like this:

        Code:
        if (Open[0]<Close[0] && Math.Abs(Close[0]-Open[0]) <= Math.Abs((Open[0]-Low[0])*0.5))            
        {                  
            EnterLong(Convert.ToInt32(DefaultQuantity),"MyLongEntry" + CurrentBar);                  
            SetProfitTarget("MyLongEntry" + CurrentBar, CalculationMode.Percent, 0.01);                  
            SetStopLoss("MyLongEntry" + CurrentBar, CalculationMode.Price,(Low[0]), false);              
        }
        Please let me know if I can assist further.
        Last edited by NinjaTrader_ChrisL; 11-13-2018, 03:13 PM. Reason: Fixed code
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thank you Chris, great! That seems to work perfectly.Appreciate your help very much

          Comment


            #6
            Hello,

            some follow up, so far it works great. As next step, I would like to modify the stop to set to breakeven when certain price level up from entry is hit and second, I would like to trail the breakeven stoploss higher, when the price advances further and that would be perfect.

            So as first step, I tried to do breakeven S/L, but could not manage even that. Following up on previous code I added stoploss at breakeven, when price rises certain number of ticks. I got the example from your site :
            https://ninjatrader.com/support/foru...-orders?t=3222

            but it doesnt work for me like that.

            Code:

            if (Open[0]<Close[0] && Math.Abs(Close[0]-Open[0]) <= Math.Abs((Open[0]-Low[0])*0.5))

            {

            EnterLong(Convert.ToInt32(DefaultQuantity), "MyLongEntry" + CurrentBar);

            SetProfitTarget("MyLongEntry" + CurrentBar, CalculationMode.Percent, 0.05);

            SetStopLoss("MyLongEntry" + CurrentBar, CalculationMode.Price,(Low [0]), false);

            }


            if (Position.MarketPosition == MarketPosition.Long)

            {

            if (Close[0]> Position.AveragePrice + 30 * TickSize)

            {
            SetStopLoss("MyLongEntry" + CurrentBar,CalculationMode.Price, (Position.AveragePrice),false);
            }

            }

            Any advice, what could be wrong?

            Thank you.

            Comment


              #7
              Hello ExNihilon,

              Thanks for the reply.

              This is not working because you are referencing a different signal name to move the stop.

              SetStopLoss("MyLongEntry" + CurrentBar,CalculationMode.Price, (Position.AveragePrice),false);

              That CurrentBar will be different on every new bar. You would need to keep a record of every bar that you enter on then modify one or all of the existing stops when your average entry gets to a certain value. A List collection would be good for this. Below is an example:

              Code:
              private List<int> entryList = new List<int>(); //Place inside of the Strategy class.
              
                      //...
              
                      protected override void OnBarUpdate()
                      {
              
                          if (Open[0]<Close[0] && Math.Abs(Close[0]-Open[0]) <= Math.Abs((Open[0]-Low[0])*0.5))
              
                          {
              
                              EnterLong(Convert.ToInt32(DefaultQuantity), "MyLongEntry" + CurrentBar);
              
                              entryList.Add(CurrentBar);
              
                              SetProfitTarget("MyLongEntry" + CurrentBar, CalculationMode.Percent, 0.05);
              
                              SetStopLoss("MyLongEntry" + CurrentBar, CalculationMode.Price,(Low [0]), false);
              
              
                          }
              
              
                          if (Position.MarketPosition == MarketPosition.Long)
              
                          {
              
                              if (Close[0]> Position.AveragePrice + 30 * TickSize)
              
                              {
              
                                  foreach(int CurrentBarValue in entryList)
                                  {
                                      SetStopLoss("MyLongEntry" + CurrentBarValue,CalculationMode.Price, (Position.AveragePrice),false);
                                  }
              
                              }
              
                          }
                      }

              Please let me know if I can assist further.
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Thank you Chris,

                you guys are too good ! I could in no way figure out such solution.

                I would have one last question about this. How to expand the stoploss to be trailing. When the breakeven price is hit- then transform the stop to trailing.

                I thought only one line would be necesarry, but it doesnt seem to work.U guess Idont get the whole logic of the code yet.

                I changed the code lines:

                @
                Code:
                [COLOR=#0000ff]if[/COLOR] ([COLOR=#080808]Close[/COLOR][[COLOR=#ff8c00]0[/COLOR]]> [COLOR=#080808]Position[/COLOR].[COLOR=#080808]AveragePrice[/COLOR] + [COLOR=#ff8c00]30[/COLOR] * [COLOR=#080808]TickSize[/COLOR])
                
                
                 foreach (int CurrentBarValue in entryList)
                                    {
                  SetStopLoss("MyLongEntry" + CurrentBarValue,CalculationMode.Price, (Position.AveragePrice),false);
                                    }


                to :

                Code:
                [COLOR=#0000ff]if[/COLOR] ([COLOR=#080808]Close[/COLOR][[COLOR=#ff8c00]0[/COLOR]]> [COLOR=#080808]Position[/COLOR].[COLOR=#080808]AveragePrice[/COLOR] + [COLOR=#ff8c00]30[/COLOR] * [COLOR=#080808]TickSize[/COLOR])
                
                
                 foreach (int CurrentBarValue in entryList)
                                    {
                [B] SetTrailStop[/B]("MyLongEntry" + CurrentBarValue,CalculationMode.[B]Ticks, 30[/B],false);
                                    }
                My intention is when price is 30 ticks above entry level, set stoploss to trailing stop- first 30 ticks lower- which is breakeven price.Then, with each bar going higher, set the stop again at 30 ticks,but now below opening price of the new candle- so the stop moves higher with rising price. I thought trailing stop would do that if set like above,but it didnt seem so. I am using ONBARUPDATE ().

                Thank you again for your much appreciated help.

                Comment


                  #9
                  Hello ExNihilon,

                  Thanks for the reply

                  Are you still using SetStopLoss above that code snippet? You can only use one or the other. You could only use both SetStopLoss and SetProfitTarget together if they deal with different signal names.

                  I look forward to hearing of your results.
                  Chris L.NinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by kaywai, Today, 06:26 AM
                  1 response
                  6 views
                  0 likes
                  Last Post kaywai
                  by kaywai
                   
                  Started by ct, 05-07-2023, 12:31 PM
                  6 responses
                  205 views
                  0 likes
                  Last Post wisconsinpat  
                  Started by kevinenergy, 02-17-2023, 12:42 PM
                  118 responses
                  2,780 views
                  1 like
                  Last Post kevinenergy  
                  Started by briansaul, Today, 05:31 AM
                  0 responses
                  10 views
                  0 likes
                  Last Post briansaul  
                  Started by traderqz, Yesterday, 12:06 AM
                  11 responses
                  28 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Working...
                  X