I am a college student that has done your traditional trading as a finance student. However, I recently have been trying to learn how to do trading systems.
I have created a simple moving average strategy, and seems to be working okay and working well. It's only works with one stock, however I will try to make it work with multiple in a later date. I plan on adding MACD to the strategy but note sure how to implement it correctly. Any help would be great.
Basically, I am trying to trigger a buy when moving average has been crossed and at the same time the MACD is over zero. And vise versa I would like to sell when it has crossed the moving average and MACD has gone below zero.
Once again, any help would be great!
Lastly, I realized that their is a few time metrics that are wrong with my code, I have already fixed them !
|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|
def initialize(context): schedule_function(open_positions, date_rules.every_day(), time_rules.market_open(minutes = 15)) schedule_function(close_positions, date_rules.every_day(), time_rules.market_close(minutes = 11)) schedule_function(record_variables, date_rules.every_day(), time_rules.market_close()) context.amd = sid(351) def open_positions(context, data): MovingAvg1 = data.history(context.amd, 'price', 9,'1d').mean() MovingAvg2 = data.history(context.amd, 'price', 40,'1d').mean() current_positions = context.portfolio.positions[context.amd].amount if (MovingAvg1 > MovingAvg2) and current_positions == 0: order_target_percent(context.amd, .50) def close_positions(context, data): MovingAvg1 = data.history(context.amd, 'price', 9,'1m').mean() MovingAvg2 = data.history(context.amd, 'price', 40,'1m').mean() current_positions = context.portfolio.positions[context.amd].amount if (MovingAvg1 < MovingAvg2) and current_positions != 0: order_target_percent(context.amd, 0) def record_variables(context, data): record(Value=context.portfolio.portfolio_value)