Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time Based Exit

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

    Time Based Exit

    Hi Guys,

    my strategy is based around the market doing a certain thing within a certain time. Is there anyway I can program a time based exit? Basically I want to exit my trade after 3 periods if profit isn't at least at a certain level x. Is this possible? could somebody give me a few hints to how I should code this? Thanks for your time

    #2
    I think this will work for you. Bear in mind you need to see help guide to place proper parameters in GetProfitLoss() method. Good luck!

    if(BarsSinceEntry >= 3 && Position.GetProfitLoss() < X)
    {
    // code to manage trade
    }

    Comment


      #3
      Coolmoss, thanks I will try it out. Could I ask you another question, still related to stoploss. I basically have a strategy that enters on a breakout with stoploss on the daily high, but when backtesting it always stops me out, because working on daily periods NT doesn't recognize the intraday price pattern. Would adding Add(PeriodType.Minute, 1); in the Initialize() section solve my problem? or do I need to do something more complex? Thanks

      Comment


        #4
        Hmm, if you set your stop loss at a price, it shouldn't matter what time frame you're running on. Let me ask this: where is your SetStopLoss statement. It should NOT be in Initialize(), but rather in the OnBarUpdate() prior to the Enter statement.

        Comment


          #5
          basically this is the code I'm using, If you run it on a daily chart, you will notice that often your stopped on the same candle you enter. However if you use daily candle, NT has no memory to know if the Entry occurred before or after the Stop level was traded, I suspect the only way to know this is using MTF, what you think?

          Code:
                              if (Position.MarketPosition == MarketPosition.Flat)
                              {
                                      if (//Long Entry Condition)
                                          {
                                          SetStopLoss(Low[0]-TickSize);
                                          EnterLongStop(1,High[0]+TickSize);
                                          }
                              }
          Moreover, I would like to add the following condition:

          Code:
          if(Position.MarketPosition != MarketPosition.Flat
          && BarsSinceEntry >= 3 &&  Position.GetProfitLoss() <= R)
                ExitLong;
          
          else if(Position.MarketPosition != MarketPosition.Flat
          && BarsSinceEntry >= 3 &&  Position.GetProfitLoss() > R)
                SetStopLoss(Position.AvgPrice + R)
          where R = (High[0] - Low[0] + 2*TickSize) is defined each time once an EntryLong is taken. Would you know how to code that? It's driving me crazy

          Also please notice I've sent you a private message

          Comment


            #6
            Most of my work is using intraday bars and it's been awhile since I coded with daily bars. You might be right that a second data series is necessary. That certainly would allow for what you want to do. However, some experimenting with your current code might reveal what's missing to get your stops to work correctly. I suspect using some sort of bool flag to prevent an early stop might be a viable approach.

            Regarding your second question: take the R = (...... bit of code and place it just before your if statment. That way R will be recalculated immediately before each running the if/else sequence

            HTH

            Comment


              #7
              You mean I should place the R like this?

              Code:
               if (Position.MarketPosition == MarketPosition.Flat)                    
               {                       if (//Long Entry Condition)                                
                               {                SetStopLoss(Low[0]-TickSize);    
                                                EnterLongStop(1,High[0]+TickSize);                                
                                                R = (High[0] - Low[0] + 2*TickSize);
              
                                  }            
                 }
              
              if(Position.MarketPosition != MarketPosition.Flat 
              && BarsSinceEntry >= 3 
              &&  Position.GetProfitLoss() <= R)       
              ExitLong; 
              
               else if(Position.MarketPosition != MarketPosition.Flat 
              && BarsSinceEntry >= 3 
              &&  Position.GetProfitLoss() > R)    
                 SetStopLoss(Position.AvgPrice + R)
              But shouldn't I somehow declare R before? I am getting an error, as I says R doesn't exist in the current context

              Comment


                #8
                I removed the formula for R from the "flat" block, and placed it immediately before the "not flat" block. Normally, I would declare R as a variable in the variable section like this:

                double R = 0;

                I set it equal to zero just to initialize it to some value. However, there is nothing wrong with declaring the variable right in the logical code itself. It's just often considered sloppy programming practice unless you're declaring a variable with limited scope (ie the variable is only used inside a method and is not to be available to all the code).

                Code:
                 if (Position.MarketPosition == MarketPosition.Flat)                    
                 {                       if (//Long Entry Condition)                                
                                 {                SetStopLoss(Low[0]-TickSize);    
                                                  EnterLongStop(1,High[0]+TickSize);                                
                                                 
                
                                    }            
                   }
                
                double  R = (High[0] - Low[0] + 2*TickSize);
                if(Position.MarketPosition != MarketPosition.Flat 
                && BarsSinceEntry >= 3 
                &&  Position.GetProfitLoss() <= R)       
                ExitLong; 
                
                 else if(Position.MarketPosition != MarketPosition.Flat 
                && BarsSinceEntry >= 3 
                &&  Position.GetProfitLoss() > R)    
                   SetStopLoss(Position.AvgPrice + R)
                Last edited by coolmoss; 02-24-2013, 12:54 PM.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                663 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                376 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                110 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                575 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                580 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X