#### Bars Since?

Posted By jthorne 10 Years Ago

## Bars Since?

 Message Posted Monday August 13 2007 How do I determine how many bars ago a particular event occurred? For instance, Bars Since ( close == highestvalue(close,last 20 bars) ); Posted Monday August 13 2007 It depends on the event.  Much of our bar analysis functions are in the BarUtils class.  You can get things like HighestHigh, LowestLow, Lookback, etc.jthorne (8/13/2007)How do I determine how many bars ago a particular event occurred? For instance, Bars Since ( close == highestvalue(close,last 20 bars) ); Posted Monday August 13 2007 Right. And I am using a couple of them... but I need something that basically tells me how many bars it has been since a condition was true. Any condition.For example:Condition = Close <= 5bars.since(Condition) = how many bars since Close crossed above 5Does this clarify my initial question? In other words, take any condition that would evaluate true or false (such as those used in an "if" statement), and tell me how many bars it has been since it evaluated true. Posted Monday August 13 2007 BTW, while I'm asking newbie questions....What's the easiest way to find out if the last trade made in your strategy was long or short? I have a different trailing stop level I used based on whether it is long or short, so once I have a position open, I'll adjust the stop each bar, but the distance I'm trailing is dependent on short/long. Posted Monday August 13 2007 Not out of the box, no.  For now, you'd have to write a quick function to handle this.Here's a rough idea of how I'd approach it.`double highestValue = 0.0;int since = 0;List bars = BarCollections[symbol];for (int index = bars.Count - 20; index < bars.Count; index++){    if (bar.Close > highestValue)    {        highestValue = bar.Close;        int since = index;    }}since = bars.Count - since;`This may be a good utility class for us to implement though.billb (8/13/2007)It depends on the event.  Much of our bar analysis functions are in the BarUtils class.  You can get things like HighestHigh, LowestLow, Lookback, etc.jthorne (8/13/2007)How do I determine how many bars ago a particular event occurred? For instance, Bars Since ( close == highestvalue(close,last 20 bars) ); Posted Monday August 13 2007 Yeah, it is super handy. Thanks for the code snippet. Still adjusting my code from NinjaTrader over to RightEdge and sorting out the differences. They have an MRO function (Most Recent Occurrence)... that's where I hit the wall. Posted Monday August 13 2007 A few ways to go about this.If it's an open position, call PositionManager.GetOpenPositions() and get the last one in the list.  If it's closed, apply the same except for call GetClosedPositions().  Another option here is to handle the OrderFilled event.  When you get this, set your last position filled as a member variable in your class.  From the Position class the PositionType member contains the value of long or short.jthorne (8/13/2007)BTW, while I'm asking newbie questions....What's the easiest way to find out if the last trade made in your strategy was long or short? I have a different trailing stop level I used based on whether it is long or short, so once I have a position open, I'll adjust the stop each bar, but the distance I'm trailing is dependent on short/long. Posted Monday August 13 2007 We'll put that on the feature list.  We're always very interested in making life a little easier with our class library.jthorne (8/13/2007)Yeah, it is super handy. Thanks for the code snippet. Still adjusting my code from NinjaTrader over to RightEdge and sorting out the differences. They have an MRO function (Most Recent Occurrence)... that's where I hit the wall. Posted Monday August 13 2007 For some reason, I can't get anything to compile... here's a code snippet:      List openPositions = PositionManager.GetOpenPositions(symbol);      if (openPositions.Count >= 1)      {            if ( openPositions.PositionType == Long )            {                  ... long code ...            }... Posted Monday August 13 2007 What's the error message?It looks like you should have "List openPositions = ...", though.Additionally, this looks suspect:  "openPositions.PositionType".  You need to access a specific index into this collection.  openPosistions[0].PositionType would be the first element in the list.  Without knowing your itentions, though, it's hard to say what you should have there.