Back to Community
My first algorithm

Hey guys, I'm new to the community. This is my first piece of code that I've ever written. I'm currently taking intro to programming so I know a bit of Python.

I wrote this very basic algorithm to get a basic grasp of Datetime and how Quantopian works. A couple of questions though...

I'm currently only executing this trade in 2009. My plan with this algorithm is to buy on the last day of each month and sell at the end of the first day of each month.

How do I specify to sell at the end of the day?

I'm very confused as to if why if I sell in on the last day of each month and buy on the first day of each month, why are the returns positive when compared to buying on the last day of each month and buying on the first day, my returns are negative and seem to be negatively correlated to the benchmark.

Clone Algorithm
14
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
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
# Backtest ID: 54593790152c4408ff8add9b
There was a runtime error.
10 responses

Sorry, I found a bug at the bottom when I wasn't comparing to the lists.

Here is the updated algorithm that I have. Does this look right? Are returns supposed to be only 8% for 2011?

Clone Algorithm
14
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
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
# Backtest ID: 545939d58a99bc091a437ae8
There was a runtime error.

Hello Kai,

I really like your first_day, last_day approach, but unfortunately it doesn't always execute the order. For instance 28 May 2011 was a Saturday, so your algorithm misses the timing and gets out of sync from what you want.

I believe the quantopian people will introduce a new ordering feature to schedule an order in the future, which would make your algo a lot easier to implement.

Something like this?

I used order_target_percent instead as its more robust, depending on exactly what date you start the backtest.

Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
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
# Backtest ID: 545a1648130d180902d2acee
There was a runtime error.

Some code to show usage of zipline.utils.tradingcalendar and some other details such as generators (yield). But it doesn't seems a great idea... but that was a nice exercice ;-)

Clone Algorithm
2
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
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
# Backtest ID: 545a40c4130d180902d3e776
There was a runtime error.

Welcome Kai,
Congrats on writing your first algo. It can be a lot to learn outside libraries when you are learning to program, but in this case I think it is pretty straight forward. Pandas has some useful date functionality that might help you out with this sort of thing. They have already written the code to get the fist and last business days of the month. It should work for you unless a market holiday falls on the first or last business day of the month.

cheers,
David

Clone Algorithm
1
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
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
# Backtest ID: 545a458bfc9542092dbf68df
There was a runtime error.

Hello Kai,

I know that this algo is more of an exercise to learn Quantopian's platform and all that but I am curious about your rationale for the strategy per se. Meaning, what does it mean for you to buy on the last day and sell on the end of the first day of the next month? Is that because of some particular reason or it's just about learning how to program on Quatopian?

Again, I'm just curious and always open to learn from others' ideas. Good luck with your learning!

Akram,
My main rationale is to capture the return from people investing in mutual funds, 401K's. Apparently, I read somewhere that people always add to their accounts on the first of each month.

So you aim at capturing the small bumps that happen between the end of the month and the end of the first day of the next one.

That's very interesting!

Yes, that's what I aim to capture. However, as you can see... my coding skills aren't up to par :(

But they will improve with time, especially with such a helpful and friendly community as Quantopian's.

Just keep coding!