I am a noob just testing out a very simple strategy of buying leveraged ETFs during the last two trading hours if they have moved a certain percentage.
I noticed the logs in my backtests had some big issues with the dates and prices. First the dates all seem to be off one day; the quantopian data is all one day ahead. For example, when it is actually the 6th, it generates logs as the 7th, and the I have logs Saturdays when the market is not open.
Regarding pricing data, I suspect the error is on my part, but I can't get an accurate figure for the previous day's close. I define closing price as:
close_price = data.current(context.securities,"close")
inside a scheduled function that runs at the end of the of the day. I log this number, and it appears to just be a random number. When I schedule the function to run at a different time (but still on the same day), the supposed closing price changes as well. Not too sure where I went wrong here, and what price I'm actually referencing.
Lastly, what is the most efficient way to get a stock's current price? I use:
current_price = data.history(context.securities, "price", 2, "1m").iloc
But I'm sure there's a much more efficient way that I don't know.
Thanks for any feedback. I'm a complete beginner programmer, and I'm really eager to learn.
|Returns||1 Month||3 Month||6 Month||12 Month|
|Alpha||1 Month||3 Month||6 Month||12 Month|
|Beta||1 Month||3 Month||6 Month||12 Month|
|Sharpe||1 Month||3 Month||6 Month||12 Month|
|Sortino||1 Month||3 Month||6 Month||12 Month|
|Volatility||1 Month||3 Month||6 Month||12 Month|
|Max Drawdown||1 Month||3 Month||6 Month||12 Month|
""" For this example, we're going to write a simple momentum script. When the stock goes up quickly, we're going to buy; when it goes down we're going to sell. Hopefully we'll ride the waves. To run an algorithm in Quantopian, you need to define two functions: initialize and handle_data. Note: AAPL had a 7:1 split in June 2014, which is reflected in the recorded variables plot. """ """ The initialize function sets any data or variables that you'll use in your algorithm. It's only called once at the beginning of your algorithm. """ def initialize(context): #Labu and LABD context.securities = [sid(49073),sid(49072)] #Buy at 2 pm schedule_function(purchase, date_rule=date_rules.every_day(),time_rule=time_rules.market_close(hours=3)) schedule_function(sell, date_rule=date_rules.every_day(),time_rule=time_rules.market_close(minutes=10)) def purchase(context, data): close_price = data.current(context.securities,"close") current_price = data.history(context.securities, "price", 2, "1m").iloc # If our stock is currently listed on a major exchange if data.can_trade(context.securities): #buy whichever stock is up 1% if current_price > (1.01 * close_price): order_target_percent(context.securities, 1) log.info("Buying %s" % (context.securities.symbol)) elif current_price < (.99 * close_price): order_target_percent(context.securities, 1) log.info("Selling %s" % (context.securities.symbol)) log.info(current_price) log.info(close_price) #liquidate def sell(context,data): order_target_percent(context.securities, 0) order_target_percent(context.securities, 0)