Simple Moving Average Algorithm

Hi everyone,
I am a student and this is one of my first times experimenting on Quantopian. I tried to do a basic moving average algorithm that makes trades based on the crossover of 17 and 40 days of Apple between 01/01/2011 and 12/31/2014. Can anyone help me with:

1) I tried to schedule the trades starting from 15 minutes after the market opens and 11 minutes before the market close; in order to be able to use the 25% of stock value margin and not to incur in the "Reg T End of Day Initial Margin", which requires 50% instead. However, I believe that there must be something wrong with the cash. How could I improve it?

2) Isn't there any way to add the MACD ratio into the picture? I want to reduce commissions for unnecessary trades.

Thanks in advance for the help,
Mattia

71
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: 59fa481b39ec6441c27cd38c
There was a runtime error.
1 response

Hello Mattia,

For every scheduled action you don't wish to happen simultaneously you should use different functions, e.g. one function for opening positions and another for closing them. For recording rapidly changing variables like cash you shouldn't use function which is timed to run just once per day.

I added few functions to your original code. Now it has three separate functions with three separate timing schedules. One (open_positions) for opening positions 15 minutes after market opening, one (close_positions) for closing positions 11 minutes before market closing and one (record_variables) for running non stop recording portfolio cash.

For adding MACD into you algorithm you should check following link: https://www.quantopian.com/help#macd

83
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: 59fabbf5e1180043e68d556a
There was a runtime error.