I'm using the basic trading algorithm here (https://www.quantopian.com/help#sample-basic) and making some small tweaks.
Every time I sell the position I want to be able to record the price I sold it at so that the next time I buy in is only if it is X% higher or Y% lower than the last value.
I know that for positions I've bought and currently hold I can use the context.portfolio.positions object and look at the cost basis, and that for open orders I can scan by SID. But, since I'm buying and selling the same stock over and over again I'll have lots of previous orders which creates two problems:
1) I'm not sure how I would sort those previous orders so that I can only grab the latest one and get the cost basis.
2) In theory my algo would get slower and slower over time as I pull back more and more orders with this method.
So is there a way to limit the orders returned to the most recent of a sell position? And is this the best way to get that value?