Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to add a "timeout" period for a strategy after stop loss hits?

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

    How to add a "timeout" period for a strategy after stop loss hits?

    Hello,

    I'm using a stoploss order to help shore up some profits from an otherwise great signal. the problem is, the stoploss hits the signal is still showing a "buy" and then I'm back in a position.

    My thought was to set a counter variable in OnExecution equal to some number of bars and then prevent OnBarUpdate from calculating until that counter decremented to zero.

    The goal here is to allow some time period to expire before reverting back to the signal (the signal usually catches up to the same exit idea a minute or two after the stoploss executes).

    protected override void OnExecution(IExecution execution)
    {
    if(execution.Order.OrderType == OrderType.Stop)
    {
    timeOut = 10;
    }

    }


    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if( timeOut>0)
    timeOut--;
    else
    { else calc signal for trading


    This code is causing a lot of errors in optimization and I'm trying to see if there's an order of events I'm missing or maybe OnExecution is called multiple times, resetting the timeOut variable...there's something I'm missing here...

    Thank you for your help!

    #2
    Why not, to avoid the whipsaw....
    - widen your tight stop,
    - override your your tight stop whenever your trend finder is on a buy,

    Make sure your entry and exit logic excludes simultaneous entry and exit conditions.

    Does you optimizations give the same result for different instruments? If not, why not?

    Comment


      #3
      Originally posted by borland View Post
      Why not, to avoid the whipsaw....
      - widen your tight stop,
      - override your your tight stop whenever your trend finder is on a buy,

      Make sure your entry and exit logic excludes simultaneous entry and exit conditions.

      Does you optimizations give the same result for different instruments? If not, why not?
      Its not really about whipsaws and tight stop losses (I am actually quite generous with my stops!)

      This is about allowing time for the signal to catch up. Creating a delay in the trading window after a stop loss is executed.

      There's a function I found which gives the number of bars since last exit ( BarsSinceExit() ), but doesnt distinguish between exits from a stop loss or from the signal. Thats what I'm looking for help on...

      As for why wouldn't I get the same results for a tech stock vs. a leveraged ETF vs. a currency, I would never expect to get the same results from different assets classes...how does this have anything to do with my question??

      Comment


        #4
        Hello khoga,
        Unfortunately the BarsSinceExit() function will return the bar number since the last exit. It wont differentiate whether its an target or a stop and placed by which method (Exit or Set).

        As such you have to write your own code snippet and write your own BarsSinceExit function.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          BarsSinceExit

          Given the level of detail I'm looking for, that's fine to create some custom code.

          Can you give me some insight into the structure of BarsSinceExit? Is there a timestamp created when an exit order executes which is then used to determine how much time has passed?

          Tim

          Comment


            #6
            Hello Tim,
            Unfortunately the BarsSicneExit only returns an integer.

            You can use the below code to get the time of the bar where the exit took place

            Code:
            if (BarsSinceExit() >= 0)
            	Print(Time[BarsSinceExit()]);
            Please let me know if I can assist you any further.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Joydeep View Post
              Hello khoga,
              Unfortunately the BarsSinceExit() function will return the bar number since the last exit. It wont differentiate whether its an target or a stop and placed by which method (Exit or Set).

              As such you have to write your own code snippet and write your own BarsSinceExit function.
              Joydeep,

              A solution for khoga's problem, might be to keep track of the exit price with user variables using OnExecuton() interface with IExecution.Price,IExecution.Quantity, IExecution.Time, IExecution.MarketPosition.

              But unfortunately, for IExecution.MarketPosition, the only possible values are MarketPosition.Long or MarketPosition.Short. There's no IExecution tracking of MarketPosition.Flat.

              If OnExecution() is called with a fill to flatten the position, then IExecution should track the MarketPosition.Flat. Please consider this a recommendation for future NT updates.

              Thanks

              Comment


                #8
                Hello borland,
                Best would to check for the Positions current Market position too.

                Code:
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                  //do something
                }
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Joydeep View Post
                  Hello borland,
                  Best would to check for the Positions current Market position too.

                  Code:
                  if (Position.MarketPosition == MarketPosition.Flat)
                  {
                    //do something
                  }
                  Yes, but that's not the point. My recommendation was for Tracking order fills with the OnExecution() interface. IExecution.MarketPosition does not include MarketPosition.Flat, which suggests that if MarketPostion goes to MarketPostion.Flat, OnExecution() is not called. So there is no order tracking with the order fill to go flat.

                  Since there is no way to determine if a Stop order was executed, khoga would need to check price of the fill to see if it was in the range of a stop order. So, order tracking would need to find the fill price with the OnExecution() interface.

                  Comment


                    #10
                    Hello borland,
                    You can track the stop loss order by its name. If you are using the Exit codes then you could give an unique name to it.

                    Stop loss orders submitted by NinjaTrader (via the Set() methods) are named as "Stop loss".

                    JoydeepNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Joydeep View Post
                      Hello borland,
                      You can track the stop loss order by its name. If you are using the Exit codes then you could give an unique name to it.

                      Stop loss orders submitted by NinjaTrader (via the Set() methods) are named as "Stop loss".

                      http://ninjatrader.com/support/forum...ead.php?t=5790

                      OK, thanks. Use OnOrderUpdate() method with the IOrder interface instead. Not as efficient, but gets the job done!

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by trilliantrader, Today, 08:16 AM
                      2 responses
                      6 views
                      0 likes
                      Last Post trilliantrader  
                      Started by samish18, Today, 08:31 AM
                      1 response
                      1 view
                      0 likes
                      Last Post NinjaTrader_Clayton  
                      Started by Creamers, 09-08-2023, 10:26 AM
                      6 responses
                      157 views
                      0 likes
                      Last Post JonyGurt  
                      Started by funk10101, Today, 08:14 AM
                      1 response
                      2 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by bill2023, Yesterday, 08:51 AM
                      3 responses
                      22 views
                      0 likes
                      Last Post bltdavid  
                      Working...
                      X