In a short summary of moving average crossover strategy, the trend of the security is going up when the fast moving average line cross over the slow moving average line from the lower area and it is a signal about long positions.

In the following example, the code calculates the moving average of 5 (fast moving average line) and 15 (slow moving average line) at 15:59:00 US Eastern time, 1 min before the market closes, on every trading day. It places an order of SPY, ETF tracking S&P 500, when the fast moving average line starts to cross over the slow moving average line and exits the positions when crossover happens in the opposite direction.

The code can be backtested at Quantopian.com because IBridgePy can run most of the strategies posted at Quantopian without any changes.

If you need any help to live trader your codes from Quantopian, please contact with us at IBridgePy@gmail.com

# -*- coding: utf-8 -*-'''There is a risk of loss in stocks, futures, forex and options trading. Pleasetrade with capital you can afford to lose. Past performance is not necessarily indicative of future results. Nothing in this computer program/code is intendedto be a recommendation to buy or sell any stocks or futures or options or any tradable securities. All information and computer programs provided is for education and entertainment purpose only; accuracy and thoroughness cannot be guaranteed. Readers/users are solely responsible for how they use the information and for their results.If you have any questions, please send email to IBridgePy@gmail.com'''# This code can be backtested at Quantopianimportpandasaspddefinitialize(context):
context.security=symbol('SPY') # Define a security, SP500 ETF# schedule_function is an IBridgePy function, also supported by Quantopian# date_rules.every_day : the dailyFunc will be run on every business day# time_rules.market_close(minutes=1) : the time to run dailyFunc is # 1 mintue before U.S. market close (15:59:00 US/Eastern time)
schedule_function(dailyFunc, date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(minutes=1))
defdailyFunc(context, data):
# dailyFunc is scheduled by schedule_function# It will run at 15:59:00 US/Eastern time on every business day# 1 mintue before U.S. market close.# get_datetime() is an IBridgePy fuciton to obtain IB server time
sTime=get_datetime()
print(sTime)
# Obtain historical data of SPY, # daily data from the current time# goback to 20 days,
hist = data.history(context.security, 'close', 20, '1d')
# Calculate moving average# if MA(5) > MA(15), BUY SPY, 100% of current portfolio value # if MA(5) < MA(15), clear the position if any
mv_5=pd.rolling_mean(hist,5)[-1]
mv_15=pd.rolling_mean(hist,15)[-1]
if mv_5>mv_15:
orderId=order_target_percent(context.security, 1.0)
# order_status_monitor is an IBridgePy function.# It is not supported by Quantopian# It is recommended to monitor the order status afer placing an order#order_status_monitor(orderId, target_status='Filled')else:
orderId=order_target_percent(context.security, 0.0)
#order_status_monitor(orderId, target_status='Filled')