The Trading Strategy using Moving Averages 9x21 🐍

Introduction

The objective of this code is to be able to reduce the work of analysing a large number of stocks. The idea is that the investor already have selected a list of stocks he wants to analyse, and he will use this code to decide which ones to buy, to hold or to stop ("close the position"). He can and should combine Fundamental Analysis and Technical Analysis.

He should use Fundamental Analysis to generate the list of companies he would like to invest on - this part is not developed as a code in this Jupyter NoteBook (JNB), but is briefly discussed below.

Fundamental Analysis

The idea is to get a list of companies that are solid and could be interesting to hold for some time. In the beginning of the code, we provide a list of stocks traded in the Brazilian Market (called B3) based on the IDIV Index. This index gathers stocks that pay good dividends and have good liquidity. It's composition can be downloaded at B3 site: https://www.b3.com.br/pt_br/market-data-e-indices/indices/indices-de-segmentos-e-setoriais/indice-dividendos-idiv-composicao-da-carteira.htm

Therefore, using the IDIV is a good way to select good companies and then we can use Technical Analysis to decide WHEN to: buy, hold or stop.

Technical Analysis

The list will be be tested by a classic Technical Indicator: the combination of two Moving Averages (MAs). In this code I use the MAs of 9 and 21 days, as it is a classic. But the code can be easily adapted for any other combination of MAs.

Why using MAs?

MAs are used to identify trends. The idea is that if the "fast" one (9 periods) crosses the "slow" one (21 periods) upwards, the stock is in a bullish trend, so we should buy it; if it crosses downwards, we should sell it (in this case we will use it as a STOP sign, we should "close the position" (that is, sell all the stocks we have). We could also open a short position on that specific stock, however we are not this risk-taker and, due to liquidity issues in the Brazilian market, that might be not a good idea.

Hence, the idea is that this code should be run daily. </p> It will give you the list of stocks classified in 4 cases:

  1. if MA9 crossed MA21 up on the previous day: buy
  2. if MA9 is higher than MA21 but it was already higher on the day before: hold (if you already bought it before, otherwise wait)
  3. if MA9 crossed MA21 down: sell (if you already bought it before, otherwise wait) obs: If we sell above the price we bought, it is considered "Take Profit" (TP), if we sell below the price we paid, it is considered "Stop Loss" (SL).
  4. if MA9 is lower than MA21 and it was already lower on the day before: wait (you should not have a stock in this classification in your portfolio) (for more on this topic: https://www.daytradetheworld.com/trading-blog/alexander-elder-trade-market-beginners-2/#5_-_Create_a_money_Management_Plan)

As we can see, the movement of crossing is used as signal for buying or selling. However, I think it should not be used alone, you should combine it with a few more technical indicators. I use this code to reduce the number of stocks I will analyse - I open a full Graph of the stocks in situation i) and check a few other indicators, as the RSI, for example (I like to use TradingView website for it). Then I decide to buy it or not.

However, it is strongly advised to use iii) as a STOP signal!

Stocks in situation ii) are more complicated - you can chose to use another criteria to make a partial TP. That could be a RSI > 70, for example. Stocks in situation iv): if you have time you can check on them, maybe one of them is "giving" a signal of reversing the tendency (for example: a hammer candle) that the criteria MA9x21 will not identify. However, if you buy a stock classified on "wait" it is a riskier trade and you should define another criteria for SL - as MA9 is already below MA21 this code will not give you a SL signal.

ok!

References
Youtube Video (in Portuguese): "PYTHON PARA INVESTIMENTOS #4: Simulando uma CARTEIRA DE AÇÕES e comparando com o IBOVESPA"

https://youtu.be/TiNLwmLN-iE?list=PLCAhGm8nJ9CBn51o0x3j1p1LuMRqpeqCy

https://www.geeksforgeeks.org/print-colors-python-terminal/

last update: 23.mar.22
to go back to my GitHub Page: https://rafsz.github.io/