Technical Analysis

Hello Guys,
Many years ago I attended technical analysis seminar. I always wanted to code and test all the indicators we used. Mainly the moving average using 5, 13, 26 day. The algo is using $AAPL which is anyway very bullish stock, my plan is to add many more indicators, I will make all the code available to community. 90 Loading... 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 # Many years ago I attended a technical analysis seminar and learned about 13 indicators. This algo uses most of technical indicators. # indiacator 1: Moving avg: 5,13,26 day # #Test algo for technical indicators def initialize(context): context.security = sid(24) # Apple stock def handle_data(context, data): # To make market decisions, we will need to know the stock's average price for the last 5 days, the stock's current price, and the cash available in our portfolio. average_5_day_price = data[context.security].mavg(5) average_13_day_price = data[context.security].mavg(13) average_26_day_price = data[context.security].mavg(26) current_price = data[context.security].price cash = context.portfolio.cash # if the 5 day moving avarage is above both 13 day and 26 day moving avarage then its strong buy signal. And we are fully invested. #but when 5 day moving avarage is below both 13 day and but above 26 day moving avarage then its strong buy signal. And we are 50% invested # if the 5 day line crosses 13 day and goes down, we will sell 50% stake and if it crosses 26 day we will sell all if average_5_day_price > average_26_day_price and cash > current_price: if average_5_day_price > average_13_day_price and cash > current_price: number_of_shares = int(cash/current_price) order(context.security, +number_of_shares) else: number_of_shares = int(cash/current_price) number_of_shares = number_of_shares/2 order(context.security, +number_of_shares) elif average_5_day_price < average_13_day_price: if average_5_day_price < average_26_day_price: order_target_percent(context.security, 0.5) else: order_target_percent(context.security, 0.5) record(stock_price=data[context.security].price)  There was a runtime error. 5 responses just a very brief remark: in this case it would probably be more honest to use$AAPL as a benchmark. comparing your $AAPL MA strategy performance with the simple buy and hold$AAPL performance, it does not look that promising anymore ;-)

27
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
# Many years ago I attended a technical analysis seminar and learned about 13 indicators. This algo uses most of technical indicators.
# indiacator 1: Moving avg: 5,13,26 day
#
#Test algo for technical indicators

def initialize(context):

context.security = sid(24) # Apple stock
set_benchmark(sid(24))

def handle_data(context, data):

# To make market decisions, we will need to know the stock's average price for the last 5 days, the stock's current price, and the cash available in our portfolio.
average_5_day_price = data[context.security].mavg(5)
average_13_day_price = data[context.security].mavg(13)
average_26_day_price = data[context.security].mavg(26)
current_price = data[context.security].price
cash = context.portfolio.cash

# if the 5 day moving avarage is above both 13 day and 26 day moving avarage then its strong buy signal. And we are fully invested.
#but when  5 day moving avarage is below both 13 day and but above 26 day moving avarage then its strong buy signal. And we are 50% invested
# if the 5 day line crosses 13 day and goes down, we will sell 50% stake and if it crosses 26 day we will sell all

if average_5_day_price > average_26_day_price and cash > current_price:
if average_5_day_price > average_13_day_price and cash > current_price:
number_of_shares = int(cash/current_price)
order(context.security, +number_of_shares)
else:
number_of_shares = int(cash/current_price)
number_of_shares = number_of_shares/2
order(context.security, +number_of_shares)
elif average_5_day_price < average_13_day_price:
if average_5_day_price < average_26_day_price:
order_target_percent(context.security, 0.5)
else:
order_target_percent(context.security, 0.5)

record(stock_price=data[context.security].price)


There was a runtime error.

Thanks so much Ueli for reply, I learned a new dimension for backtesting. I see what you are saying about using \$AAPL as benchmark. Yes, I would surely add some more indicators to make it tradable to my first written algo

Also there was a small bug which I fixed right now. Thanks

Hi Saurabh Purnaye, thanks for the contribution. Do you know if it is possible to do a similar algo but off the intraday chart? Say maybe the 5 minute intraday chart? Also what other indicators are you thinking of adding?

Hi
i dont know nothing about quantopian and python, im a financial advisor
is there any good and confirmed code?