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

setting a stoploss with Unmanaged Approach

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

    setting a stoploss with Unmanaged Approach

    Hello,
    It will take me forever to figure this out for myself so I'm hoping someone can give me an example of coding a stoploss with the Unmanaged Approach.
    Thanks

    #2
    Hello CaptainAmericaXX,

    if (Position.MarketPosition == MarketPosition.Long)
    SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0, Position.AvgPrice - TickSize * 10, "","");

    This is a very basic example of sell stop order submission. This isn't a stop loss like used in managed framework, so you have to control submission and quantity completely in code.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Ryan thanks for the reply. I went ahead and added that. The stop worked, but now I get no more trades after that stop. Any ideas why that would happen?
      Thanks
      Last edited by CaptainAmericaXX; 08-09-2011, 02:41 PM.

      Comment


        #4
        It does what you've coded it to do. You still need to properly debug with TraceOrders output and Print() statements to check all values used in your order entry conditions.

        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          I hate it when you say that.

          Comment


            #6
            Yes, tough love here. But if you check everything out and don't understand why it's doing something, post the relevant script parts and we can take a look.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              I'm having an issue with the "StopLoss" overriding my other exits. When my entry conditions are true and I'm short the SubmitOrder()'s are placing stop orders 17 ticks above my entry price just as programed. The problem is I want to use this "StopLoss" as a stoploss and still be able to use my original exit conditions.With this "StopLoss" being set the original exits are being disregarded. How does one use both with this Unmanaged Approach? Here are my exit codes.

              Code:
              if (Position.MarketPosition == MarketPosition.Short
                                      && (m_inBearQuickMove || m_inBearRunner))
                                  {
                                      SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop, 1, 0, Position.AvgPrice + 17 * TickSize, "BearQuickI","SLQuickI");
                                      SubmitOrder(0, OrderAction.BuyToCover, OrderType.Stop, 1, 0, Position.AvgPrice + 17 * TickSize, "BearRunnerI","SLRunnerI");
                                      m_inBearQuickMove = false;
                                      m_inBearRunner = false;
                                  }
              if (Close[0] < Position.AvgPrice
                                          && m_inBearQuickMove 
                                          && Rising(RMI(10, 3)) == true)                            
                                          {
                                              BearQuickI = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Market, 1, 0, 0, "BearQuickI", "BearQuickIExit");
                                              m_inBearQuickMove = false;
                                              //Print(Time[0] + " Exit Bear Quick MoveI (rising RMI)");
                                          }
              if (m_inBearRunner && BearRunnerExitOK
                                          && Close[0] > VMAZones (9, 2, 18).Upper[0]
                                          && TrendStrengthA(VC.NinjaScript.Utility.MovingAverageType.VWMA, 200, 20, 10).TrendStrength[0] > -60)
                                              {
                                                  BearRunnerI = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Market, 1, 0, 0, "BearRunnerI", "BearRunnerIExit");
                                                  m_inBearRunner = false;
                                                  BearRunnerExitOK = false;
                                              }
              Thanks
              Last edited by CaptainAmericaXX; 08-09-2011, 05:12 PM.

              Comment


                #8
                If all the values check out from debugging, I would look into the OCO link among the orders. The strategy is run on historical data, so maybe a conflict there from historical orders.

                To mark off code: Select it in your reply and click the # button on the toolbar.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Like to add to this thread

                  I want in the unmanaged approach to set a stop loss based on currency. for example when loss of the currentposition is $400 then exit the trade.

                  in the managed approach it was just a simple:
                  SetStopLoss(double currency)
                  NT calculates the exitprice under the hood.

                  In the unmanaged approach i don't see this possibility..

                  how can i incorporate this in the unmanaged approach

                  regards

                  Comment


                    #10
                    Hello yeshe,

                    Thank you for your post.

                    You would need to use Position.AvgPrice, Instrument.MasterInstrument.PointValue, and TickSize.

                    For example, below I set my sell to exit my long position $400 below my entry:
                    Code:
                    			if(Position.MarketPosition == MarketPosition.Long)
                    			{
                    				SubmitOrder(0, OrderAction.Sell, OrderType.Stop, Position.Quantity, 0, Position.AvgPrice - 400/(Instrument.MasterInstrument.PointValue*TickSize), "", "ExitLong");
                    			}
                    Please visit the following links for more information:

                    Comment


                      #11
                      You sure that formula is right?

                      I don't think the TickSize is part of that StopLoss formula...

                      If you are long 1 contract of oil at $60 and wanted your maximum loss at $400, you would set your stop at $59.60 -- which is 40 ticks below $60.

                      The math for this calculation is:

                      StopPrice = Position.AvgPrice - 400/(Instrument.MasterInstrument.PointValue)

                      This works because PointValue is $1000 for oil, or

                      StopPrice = $60 - $400 / $1000 => 60 - 0.4 => 59.6

                      Consider ES, with a PointValue=50, the equation still holds:

                      You are long 1 contract ES bought at 1999 and you want to place your stop loss with maximum $400 loss, use same formula,

                      StopPrice = 1999 - 400 / 50 => 1999 - 8 => 1991

                      Am I missing something?

                      Comment


                        #12
                        Correct, I was thinking Ticks and not absolute price value. Thank you.

                        Comment


                          #13
                          hello patrick and thank you for your reply

                          I don't see this working for the eurusd
                          MasterInstrument.PointValue = 100000

                          TickSize = .0001


                          So the formula you mentioned would be for a 1.24 entry
                          1.24-400/(10) = -38.76

                          First of all the quantity needs to be in there somewhere (Position.Quantity)

                          I think the correct formula should be
                          Position.Quantity(Position.AvgPrice-stoplossprice)=Stoploss in dollars
                          stoplossprice=((Position.Quantity*Position.AvgPric e)-Stoploss in dollars)/Position.Quantity

                          for above example with Position.Quantity = 10000
                          =(10.000*1.24=12400-500=11900/10000=1.19

                          double check 10000(1.24-1.19)= 500

                          so this would be correct for eurusd:
                          SubmitOrder(0, OrderAction.Sell, OrderType.Stop, Position.Quantity, 0, ((Position.Quantity*Position.AvgPrice)-500)/Position.Quantity, "", "ExitLong");

                          or not?

                          would be nice to make a standard formula for all instruments using pointvalue and ticksize..

                          any ideas how to do that?

                          Comment


                            #14
                            Hello yeshe,

                            bltdavid has already corrected me, please see their post # 11: http://www.ninjatrader.com/support/f...3&postcount=11

                            Comment


                              #15
                              i just read your post bltdavid
                              thank you for your correction
                              Will this formula be the right one for all instruments forex/stocks/futures?
                              StopPrice = Position.AvgPrice - stoploss in dollars/(Instrument.MasterInstrument.PointValue)

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by futtrader, Today, 01:16 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post futtrader  
                              Started by Segwin, 05-07-2018, 02:15 PM
                              14 responses
                              1,789 views
                              0 likes
                              Last Post aligator  
                              Started by Jimmyk, 01-26-2018, 05:19 AM
                              6 responses
                              838 views
                              0 likes
                              Last Post emuns
                              by emuns
                               
                              Started by jxs_xrj, 01-12-2020, 09:49 AM
                              6 responses
                              3,294 views
                              1 like
                              Last Post jgualdronc  
                              Started by Touch-Ups, Today, 10:36 AM
                              0 responses
                              13 views
                              0 likes
                              Last Post Touch-Ups  
                              Working...
                              X