Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Very first algo I tried fails: cannot specify different execution prices for buy and sell orders

Hi

I am very new to Quantopian, but not new at all at trading simulation.
I have created my own research and simulation system in Perl years ago and tested it few times over the years.

I have a lots of algos that I have tested with it. So I decided to finally check out Quantopian. But I couldn't implement the very first algo I tried.

Why? Because Quantopian doesn't provide a way to specify particular price for the order method.

Yes, I did find about the “slippage model” mechanism. Basically, it can be used to "overrides" the“order” method to change the execution price with one different from the default (close price), with, for example, the “open price”. But only one slippage model is allowed per execution. And it is specified in initialize(context) section. As such, it cannot be “called” with dynamic parameters from handle_data method.

Thus, changing default order execution price with slippage model will change it for every order execution in the alog.

That would prevent creating such a basic algo as "buy at close price” (e.g. a few days before) and "sell at open price” (on current day).

I'd be happy to be corrected.

9 responses

You cant "set" the execution price in Quantopian any more than you can in the real market. Are you perhaps looking for the Limit order?

https://www.quantopian.com/help#ide-ordering

Thanks for the reply, Daniel!
But no, I am not looking at something similar to limit order, since it is based on price. I am looking to be able to buy/sell at prices at specific time. I am looking for example to buy two days earlier at Close and sell at Open "today". I found that impossible in Quantopian - unless one creates their methods for maintaining a portfolio (cash and stock balances) and a lot of other functions, which will defy the whole leverage Quantopian provides as a tool.

Kris,
I don't think you can do what you are talking about in Quantopian. The backtesting engine is a 'point in time' tester, it steps through each trading day and only exposes data from at or before the bar it is currently processing. The only way you can do this is to use a limit order, or store a context variable and buy/sell the next time that price is hit. I am curious why you would like to do this, it sounds like look ahead bias at its finest, unless I am totally misunderstanding what you would like to do.

David

Look ahead bias. or time travel. I'd like to have bought the DJIA about 30 years ago...

@Kris, are you trying to come up with "what if" type scenarios? Efficient Frontier type stuff given perfect knowledge of what has transpired at any time over your date range?

I'm trying to wrack my brain in understanding how, in a back test, you could justify being able to go back in time to place an entry trade.

Guys, I am using Qunatopian to test statistical ideas like "how much on average is the gain for stocks that were going down for 5 days in a row with at least 5% drop each day, then we buy them at the 5th day at Open Price and sell 2 days later at Close Price".
Quantopian has both the data and the platform to do such test. So there is no "going back in time" - I am simply trying to find patterns.
Of course I know I cannot buy at exact Open Price or exact Close price, but close enough to each one of them would be pretty.
In order to do that, I use a context variable to count when 2 days have passed after a buy in order to sell. It works - but only if I can execute the orders at either close or buy at my will.
THis whole thing of course won't be needed if I create my own functions for keeping track of orders, portfolio, cash , etc.
But the whole point is not to need that.
I mentioned I have my own coding that does. It is just not as elegant at describing buying and selling conditions as Qunatopian one is.

Well that helps. I figured it might be statistically based analysis.

You can take a look at the "Candle Patterns - research strategy" posted here recently. Within it a technique for collecting stats on returns after X days is performed. That particular strategy captures the returns after specific candle pattern events. But the idea is the same. Here is the layout of the stats (reformulated for presentation).

You could easily capture MFE/MAE and measure edge, or volatility during x days (done in another post by another friendly quant). If you're familiar with T.Balch's CompFin class his event studies are also similar to this technique. Align all events and measure affect post event to gather metrics on efficacy of event returns.

Pattern  Count  Ups  Downs  PctUp  AvgReturn  TotalDelta  Rank  
-5:1:1    148    43    105  29.05%    -0.72    -106.08   -8.95  
-5:0:1    108    24    84   22.22%    -0.70     -75.12   -3.71  
-6:1:0    121    29    92   23.97%    -0.53     -64.03   -3.68  
-4:0:2     73    18    55   24.66%    -0.67     -48.96   -2.98  
+1:2:2     23    10    13   43.48%    -0.66     -15.29   -2.89  
-4:1:2     80    22    58   27.50%    -0.46     -36.41   -2.75  
-5:2:1     65    20    45   30.77%    -0.44     -28.33   -2.68  
-6:0:0    111    17    94   15.32%    -0.88     -97.63   -2.29  
-1:5:5     19    10    9    52.63%    -0.42      -8.02   -2.22  
-6:2:0     50    17    33   34.00%    -0.36     -17.93   -2.07  
+4:4:1      7     7     0  100.00%     1.04       7.29    7.29  
+4:1:2     22    16     6   72.73%     0.71      15.63    8.27  
+5:2:1      7     7     0  100.00%     1.23       8.59    8.59  
+3:5:2      7     6     1   85.71%     1.87      13.12    9.64  
+3:6:3     15    13     2   86.67%     0.90      13.57   10.19  
+2:6:4     42    27    15   64.29%     0.91      38.09   15.74  
+0:5:6    141    87    54   61.70%     0.40      56.52   21.52  
+1:5:5    133    87    46   65.41%     0.49      64.94   27.79  
+1:6:5     84    63    21   75.00%     0.85      71.11   40.00  
+0:6:6     94    69    25   73.40%     0.79      74.31   40.04

The trick is dealing with data capture and storage and roll-off. And the above mentioned research strategy may actually suffer from a mismanagement of data and periods.

For your particular problem with entry/exit at the open or close of tomorrow, you could look at using the TradeAtTheOpenSlippageModel and setting the % of open-close range to 0.0 for trading at the open or 1.0 for trading at the close.

Hi Kris,
David is correct that the backtester isn't built for research and has limits in place to prevent look ahead bias. In order to help with research projects, like the one you want to do, we are currently working on an IPython notebook research platform that will give you access to the pricing data for these types of scenarios. We are a couple of months away from launching, but please sign up for the alpha to get on the list for early access.

Karen

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thanks guys! At least we clarified the situation with what Quantopian is capable of today, and what is coming up tomorrow.

Interesting - I looked at the slippage model, but didn't realize that if the range parameter it is fed is set at 1.0, that would actually provide the closing price. Thanks!