Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trade management samples

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

    Trade management samples

    hi:
    Im a new user. I would like to see a sample code for a stop trailing and stop loss.
    1) The strategy I want to code uses 5 min bars and once it enters long a stop loss should be placed below the LOW of the entry bar.
    If the trade goes ok , a trailing stop should be provided and the original stop loss canceled .
    2) I would appreciate if you can show me an example of a stop using ATR:
    like: close - ATR(5) etc etc

    Thank you

    #2
    Please check out our strategy programming tutorials in the online docs: http://www.ninjatrader-support.com/H...verview39.html

    Comment


      #3
      This post would be better placed in the Strategy Development topic vs ATM. But its natural for anyone that uses realtime ATM strategies in this manner to want to consider backtesting these ideas using NinjaScript strategies perhaps with even more dynamic exits. Given your strategy requirements, make sure you understand this thread.



      Since SetStopLoss() and SetTrailStop() cannot be used concurrently, you pose an interesting idea regarding using SetStopLoss() initially but then replacing it with the use of SetTrailStop(). Not sure if you can "dispose" the instance of SetStopLoss() and instantiate the use of SetTrailStop() once a profit trigger is reached. Perhaps Support has some guidance on this idea as a workaround to the non-concurrency issue.
      whitmark
      NinjaTrader Ecosystem Vendor - Whitmark Development

      Comment


        #4
        You can do what you want using CalculationMode.Price option of the SetStopLoss method.
        This lets you set the stop to be whatever price you want, be that the prior bar low, or some other trailing value.
        Here's a very rough excerpt from something I've done in the past.

        if (Position.MarketPosition == MarketPosition.Long && FirstTickOfBar)
        {
        stop_price = LinReg(25)[1] - 1*TickSize ;
        if ( BarsSinceEntry() >=2 )
        {
        SetStopLoss(CalculationMode.Price, stop_price);
        // Set stop to prior bar lsma .. could be current bar if this is used in a bar-close strategy.
        }
        else
        {
        // set stop to 2 ticks below the prior bar low
        SetStopLoss(CalculationMode.Price, Low[1]-2*TickSize);
        }

        Note .. you need to add logic to make sure the price you are using is valid.
        For example, in a long trade, the stop must be a minimum distance below the current price, or the broker will reject it.. and your strategy will stop.
        In the above example, it's quite possible for the LSMA average to be above price.. .. so you would want to add something like ..

        if ( BarsSinceEntry() >=2 && Close[0] - 2*TickSize > stop_price )

        to your setup to ensure your new stop value is 2 ticks lower than the current close.
        Last edited by zoltran; 06-23-2007, 05:56 PM.

        Comment


          #5
          thank you both. Ill try it
          simul316

          Comment


            #6
            I assume, with the usage of "FirstTickOfBar", its useful to point out this is a "CalculateOnBarClose=false" example that will work well for real-time operations but expectations need to be managed if you plan on doing any optimization research. However, its a great detailed example of the type of dynamic stop that can be implemented over and above what is available within ATM strategies.

            Thanks Zoltran!

            Whitmark
            whitmark
            NinjaTrader Ecosystem Vendor - Whitmark Development

            Comment


              #7
              Hi Whitmark

              The FirstTickOfBar test was there so I didn't send repeated stop updates with each new tick.. And since the example was using the MA value from the previous bar, it didn't have to.

              Good point re the optimisation/backtest implications. I'll have to think some on that.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Aviram Y, Today, 05:29 AM
              0 responses
              1 view
              0 likes
              Last Post Aviram Y  
              Started by quantismo, 04-17-2024, 05:13 PM
              3 responses
              25 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by ScottWalsh, 04-16-2024, 04:29 PM
              7 responses
              34 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by cls71, Today, 04:45 AM
              0 responses
              6 views
              0 likes
              Last Post cls71
              by cls71
               
              Started by mjairg, 07-20-2023, 11:57 PM
              3 responses
              216 views
              1 like
              Last Post PaulMohn  
              Working...
              X