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

SetStopLoss problem

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

    SetStopLoss problem

    My stop losses are opening and closing positions immediately. How do I fix this?

    Code:
    protected override void OnBarUpdate()
    {
             double sl;
             int atr_Period = 100;
             double point = 0.01;
                
             sl = ATR(atr_Period)[0] * ATR_Mult * point;
             SetStopLoss("",CalculationMode.Ticks,sl,false);
    
             if (entryLCondition)  { EnterLong(Lots,""); }
             if (entrySCondition)  { EnterShort(Lots,""); }
    }

    #2
    texasnomad,

    I would suggest you print out what value that ATR calculation is giving you. When you use SetStopLoss() with CalculationMode.Ticks it would use integer values. So a value of 1 would be a very very close stop loss of 1 tick.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      texasnomad,

      I would suggest you print out what value that ATR calculation is giving you. When you use SetStopLoss() with CalculationMode.Ticks it would use integer values. So a value of 1 would be a very very close stop loss of 1 tick.
      I'm running it on GBP/JPY 5 minute chart. Sometimes it works properly. Sometimes it still closes immediately. The calculated value on 1/6/2009 at 4:45 was 0.795 according to my log. Calculating by hand, that's the value it's supposed to be (79.5 pips).

      This is my currently updated code:
      Code:
      protected override void OnBarUpdate()
              {
                  double sl;
                  int atr_Period = 100;
                  double atr = ATR(atr_Period)[0];
                  
                  sl =  atr * ATR_Mult;
                  Print(sl.ToString() + Time[0].ToString());
                  SetStopLoss("",CalculationMode.Ticks,sl,false);
              }

      Comment


        #4
        texasnomad,

        You definitely don't want to be passing in 0.795 into SetStopLoss(). That means you are using 0.795 of one tick as your stop loss which is really tiny. You should adjust the value to be in the proper scale you want.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Josh View Post
          texasnomad,

          You definitely don't want to be passing in 0.795 into SetStopLoss(). That means you are using 0.795 of one tick as your stop loss which is really tiny. You should adjust the value to be in the proper scale you want.
          Now I'm passing in 79.5. It's still doing the same thing sometimes. I don't understand why it works on some trades and not on others.

          Code:
          protected override void OnBarUpdate()
                  {
                      double sl;
                      int atr_Period = 100;
                      double point = 0.01;
                      double atr = ATR(atr_Period)[0];
                      
                      sl =  atr * ATR_Mult / point;
                      Print(sl.ToString() + Time[0].ToString());
                      SetStopLoss("",CalculationMode.Ticks,sl,false);
                   }

          Comment


            #6
            Anything in Control Center logs? Please make sure you submit whole numbers. Decimal places for ticks does not make sense. You want to either submit 79 or 80.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Hi,

              I think I have the same problem, trading CL.
              When I add the "BreakevenCode" as below

              Code:
                      else if (Position.MarketPosition == MarketPosition.Short)
                                      {
                                          if (Close[0] > Position.AvgPrice - BreakEvenShort * TickSize)
                                          {
                                              SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                                          }
                                      }
              the StopLoss is only about 2 cents above the entry price. I definitely know that the TickSize in CL is 0.01 and the variable BreakEvenShort is an integer (e.g. 50, but certainly not 2).
              For Long Positions, everything works fine....

              Comment


                #8
                granite11,

                You should print the values of your calculations so you know exactly what they are evaluated as. You should also try using TraceOrders = true to see what the orders are doing.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Josh,

                  here´s my output:

                  03.12.2009 15:18:00 Entered internal PlaceOrder() method at 03.12.2009 15:18:00: Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Sell' FromEntrySignal=''
                  03.12.2009 15:18:00 Cancelled pending exit order, since associated position is closed: Order='NT-05685/Back101' Name='Profit target' State=Working Instrument='CL 01-10' Action=Sell Limit price=80,05 Stop price=0 Quantity=1 Strategy='MACDwithExit' Type=Limit Tif=Gtc Oco='NT-03464' Filled=0 Fill price=0 Token='9d2174dd41f245938e50b35b6e282038' Gtd='01.12.2099 00:00:00'
                  03.12.2009 15:18:00 Cancelled pending exit order, since associated position is closed: Order='NT-05684/Back101' Name='Stop loss' State=Working Instrument='CL 01-10' Action=Sell Limit price=0 Stop price=76,65 Quantity=1 Strategy='MACDwithExit' Type=Stop Tif=Gtc Oco='NT-03464' Filled=0 Fill price=0 Token='9f5dccc985954ddea6a2ebe0dd37aea8' Gtd='01.12.2099 00:00:00'
                  03.12.2009 15:19:00 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=76,84 Currency=0 Simulated=False
                  03.12.2009 15:19:00 Amended stop order: Order='NT-05688/Back101' Name='Stop loss' State=Working Instrument='CL 01-10' Action=BuyToCover Limit price=0 Stop price=76,84 Quantity=1 Strategy='MACDwithExit' Type=Stop Tif=Gtc Oco='NT-03466' Filled=0 Fill price=0 Token='e9594661c88e4c19a8fc2f624d02b477' Gtd='01.12.2099 00:00:00'
                  SL adjusted to Breakeven, Short
                  03.12.2009 15:19:00 Entered internal PlaceOrder() method at 03.12.2009 15:19:00: Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Sell' FromEntrySignal=''
                  03.12.2009 15:19:00 Ignored PlaceOrder() method at 03.12.2009 15:19:00: Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Sell' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
                  03.12.2009 15:19:00 Cancelled pending exit order, since associated position is closed: Order='NT-05689/Back101' Name='Profit target' State=Working Instrument='CL 01-10' Action=BuyToCover Limit price=73,84 Stop price=0 Quantity=1 Strategy='MACDwithExit' Type=Limit Tif=Gtc Oco='NT-03466' Filled=0 Fill price=0 Token='5b683aa349c84af6913e603f794e9b4f' Gtd='01.12.2099 00:00:00'
                  03.12.2009 15:19:00 Cancelled OCO paired order: BarsInProgress=0: Order='NT-05689/Back101' Name='Profit target' State=Cancelled Instrument='CL 01-10' Action=BuyToCover Limit price=73,84 Stop price=0 Quantity=1 Strategy='MACDwithExit' Type=Limit Tif=Gtc Oco='NT-03466' Filled=0 Fill price=0 Token='5b683aa349c84af6913e603f794e9b4f' Gtd='01.12.2099 00:00:00'
                  03.12.2009 15:20:00 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Ticks Value=40 Currency=0 Simulated=False
                  03.12.2009 15:20:00 Entered internal PlaceOrder() method at 03.12.2009 15:20:00: Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Sell' FromEntrySignal=''

                  I have set back slippage to zero, so the stop loss for sell orders is exactly equal to the entry price. My code is the same as below. Sorry, still don´t get it..

                  Comment


                    #10
                    Ah, found it! Used Close[0] > Position.AvgPrice - BreakEvenShort * TickSize , which is of course true most of the time instead of <. Thanks anyway!

                    Comment


                      #11
                      Originally posted by NinjaTrader_Josh View Post
                      Anything in Control Center logs? Please make sure you submit whole numbers. Decimal places for ticks does not make sense. You want to either submit 79 or 80.
                      This is my current code. You can see from the screenshots that sometimes it's working, sometimes it isn't. This doesn't make any sense to me.
                      Code:
                              protected override void OnBarUpdate()
                              {
                                  double sl;
                                  int atr_Period = 100;
                                  double point = 0.01;
                                  double atr = ATR(atr_Period)[0];
                                  
                                  sl =  Math.Round(atr * ATR_Mult / point);
                                  Print(sl.ToString() + Time[0].ToString());
                                  SetStopLoss("",CalculationMode.Ticks,sl,false);
                              }
                      Attached Files

                      Comment


                        #12
                        And no.. nothing in the logs folder. My prints look fine. Here's an example where it immediately entered and exited. It's passing 44 to the SetStopLoss function.

                        44 10/13/2009 12:45:00 PM

                        I also tried casting double sl to int in the SetStopLoss parameters. It made no difference.
                        SetStopLoss("",CalculationMode.Ticks,(int)sl,false );

                        Comment


                          #13
                          Where is your entry order in that code? Please try calling SetStopLoss() before your entry.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            SetStopLoss already appears before the entry logic.

                            Actually, I just noticed this. The stop loss works fine on long trades and only messes up on the shorts. See attached.
                            Attached Files

                            Comment


                              #15
                              Please add TraceOrders = true and copy paste the output from the Output Window when you feel it did not work. Thank you.
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by benmarkal, Yesterday, 08:44 AM
                              1 response
                              30 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by somethingcomplex, Yesterday, 10:36 PM
                              1 response
                              23 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by rayyyu12, Today, 12:47 AM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by guyonabuffalo, Yesterday, 10:01 PM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by thumper57, Yesterday, 04:30 PM
                              3 responses
                              12 views
                              0 likes
                              Last Post thumper57  
                              Working...
                              X