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

Draw Line at Stoploss Price

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

    Draw Line at Stoploss Price

    Hello,
    I have read all the posts I can find on this but I still can't get it to work. I am trying to draw a line at my stoploss price, which is based on ATR, and quit drawing the line when the trade is over. I have tried every combination I can think of but none have drawn the line correctly. Can someone please tell me what's wrong with my code? Thanks

    Code:
    protected override void OnBarUpdate()
            {
    			
    			int StopLoss = Convert.ToInt32(Math.Round( ATR(ATRStopPeriod)[0] * ATRStopMulti / TickSize));
    			
    			#region Stoploss reset, ATR Stop set, & ATR Trail
    			// Resets the stop loss to the original value when all positions are closed
    			if (Position.MarketPosition == MarketPosition.Flat)
    			{
    				SetStopLoss("V4 L2",CalculationMode.Ticks, StopLoss, false);
    			}	
    			
    			if (Position.MarketPosition != MarketPosition.Flat)
    			{	
    				//Draw Stop Line
    				DrawLine("StopLoss"+ CurrentBar, false, BarsSinceEntry("V4 L2"), StopLoss, 0, StopLoss, Color.Red, DashStyle.Solid,2);
    			}
    			
    			#endregion

    #2
    Hello,
    Can you clarify further on what is occurring with the DrawLine? Are you having the drawing object draw at the incorrect price?
    Cody B.NinjaTrader Customer Service

    Comment


      #3
      It's not drawing anything. Should this work in backtesting and optimization or only trading live? Thanks

      Comment


        #4
        Originally posted by TOOLMachine462 View Post
        Can someone please tell me what's wrong with my code?
        I do this in my strategies but I do it all from OnOrderUpdate.

        The idea is fairly simple:

        if (!Historical)
        {
        Draw line when OrderState is Accepted and StopPrice > 0.

        Remove line when OrderState is (Filled or Cancelled or Rejected) and StopPrice > 0.
        }

        My code only does this when Historical == false because there is no benefit to drawing the lines during backtesting.

        Test your code by using "Simulated Data Feed", start your strategy, make it submit an order, and watch the line get drawn -- make your stop loss get hit and watch the line get removed.
        Last edited by bltdavid; 08-15-2016, 05:34 PM.

        Comment


          #5
          If your strategy uses stop entry orders, then you'll need to be even more specific about how you recognize your stop exit orders, because "StopPrice > 0" alone won't be enough.

          The way to solve that is to use OrderAction.

          Here is more of the (rough pseudo) code that I use to draw lines.
          Place this code at the bottom of your OnOrderUpdate.

          Code:
          if (!Historical && StopPrice > 0)
          {
               if (OrderState == Accepted) then
               {
                   If (OrderAction == (Buy or SellShort)) then
                       stop order is an entry order
                       do nothing
                   else
                       stop order is an exit order
                       [COLOR=Red]draw line for stop loss order[/COLOR]
              }
              else if (OrderState == (Filled or Cancelled or Rejected)) then
                  [COLOR=Red]remove line for stop loss order[/COLOR]
          }
          Portions in red are the meat of the answer to your question.

          Comment


            #6
            Thank you for your reply. Actually, I need it for backtesting and optimization since I'm using a multiplier of the ATR for my stop. It would be easier if I could see where the stop is for every trade and not have to go to the order log and sort thru the orders. If it's using huge stops more often than not, I am just going to go back to a normal tick based stop. I need to see where the stops are being placed. Am I doing it in the right section, on bar update?

            Comment


              #7
              Originally posted by TOOLMachine462 View Post
              Thank you for your reply. Actually, I need it for backtesting and optimization since I'm using a multiplier of the ATR for my stop.
              Ok, well, I've never done that ... so I may not be much help there.

              Originally posted by TOOLMachine462 View Post
              It would be easier if I could see where the stop is for every trade and not have to go to the order log and sort thru the orders. If it's using huge stops more often than not, I am just going to go back to a normal tick based stop. I need to see where the stops are being placed.
              Every trade?
              You want to draw a line for every trade?
              Ok, but are you going to remove the line after the stop is filled?

              If your strategy takes 20 trades, are you going to draw 20 lines?
              Isn't that going to make the chart look pretty busy?

              To me, drawing a line for the stop loss is more of a real-time thing.
              Don't you want to erase that line after the stop fills?
              [Edit: I am assuming horizontal lines here.]

              I don't understand why you want to draw lines yourself.
              I mean, aren't the trade entry and trade exits already being drawn by the Strategy Analyzer?
              In the Strategy Analyzer, when I click on the Chart tab, I see all my trades right on the chart.
              Don't you?

              Why do you need to draw them (again) yourself?

              Define "draw a line at my stoploss price".
              Wait, are you wanting to draw horizontal lines at the stop price?
              (My pseudo code example above is for horizontal lines)

              Perhaps I am misunderstanding what you're asking for?



              Originally posted by TOOLMachine462 View Post
              Am I doing it in the right section, on bar update?
              As I mentioned in my prior posts, I use OnOrderUpdate.

              IMHO, OnBarUpdate is not the best choice.
              Last edited by bltdavid; 08-15-2016, 08:05 PM.

              Comment


                #8
                Sorry for the confusion. I want it to draw a horizontal line where it placed the stop at the time of entry and only draw the line until the trade exits. It won't draw never ending lines, but lines like the entry to exit lines only horizontal at the stop level. Or just draw the line for a few bars. Hope that makes sense. The ATR stop will change for every trade. But thank you for trying to help me!

                Comment


                  #9
                  Originally posted by TOOLMachine462 View Post
                  Sorry for the confusion. I want it to draw a horizontal line where it placed the stop at the time of entry and only draw the line until the trade exits. It won't draw never ending lines, but lines like the entry to exit lines only horizontal at the stop level. Or just draw the line for a few bars. Hope that makes sense. The ATR stop will change for every trade. But thank you for trying to help me!
                  After the stop fills, and you are now flat, what are you wanting to do with that line you drew for the stop loss?

                  Comment


                    #10
                    Originally posted by bltdavid View Post
                    After the stop fills, and you are now flat, what are you wanting to do with that line you drew for the stop loss?
                    Oh, I think I see what you want.

                    You want a horizontal line from the entry bar to the exit bar, is that right?

                    After the trade exits, this line stays put for historical viewing, is that right?

                    Comment


                      #11
                      Just leave it there. It's only drawn for the short [distance while in the] trade. This can also help me quickly tell if I'm getting slippage and how bad.
                      Last edited by TOOLMachine462; 08-15-2016, 08:47 PM.

                      Comment


                        #12
                        Can you please help, we didn't get it resolved. Are there any videos or samples of drawing lines available? I think I'm not linking the names of my orders correctly??? Thanks.

                        Comment


                          #13
                          Hello,
                          To debug the drawing object change the overload of the AutoScale from from false to true. This will show us if the drawing object is plotting, it just may be plotting at a lower value.
                          What value are you setting the StopLoss variable to?
                          Cody B.NinjaTrader Customer Service

                          Comment


                            #14
                            Ok I will change the setting and try that. I am setting the stoploss value to:

                            StopLoss = Convert.ToInt32(Math.Round( ATR(ATRStopPeriod)[0] * ATRStopMulti / TickSize));

                            Comment


                              #15
                              Hello,
                              If you Print out the value of the StopLoss what value does it give you?
                              Cody B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by judysamnt7, 03-13-2023, 09:11 AM
                              4 responses
                              57 views
                              0 likes
                              Last Post DynamicTest  
                              Started by ScottWalsh, Today, 06:52 PM
                              4 responses
                              36 views
                              0 likes
                              Last Post ScottWalsh  
                              Started by olisav57, Today, 07:39 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post olisav57  
                              Started by trilliantrader, Today, 03:01 PM
                              2 responses
                              19 views
                              0 likes
                              Last Post helpwanted  
                              Started by cre8able, Today, 07:24 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post cre8able  
                              Working...
                              X