How I made auto trading script — Part 1

Farhad Sanaei
4 min readSep 20, 2020

--

The idea started when I was buying and selling stocks in the market. I realized that I am spending a lot of time refreshing pages and checking prices, queues, news, etc… I started to think that at least some parts of these can be automated.

As an engineer, I believe that if something can be automated, it must be automated. So that I started to made some research to see how people are analyzing stocks. I found out that the most effective calculations/estimations that are widely used among people are RSI and MACD. RSI was more like a short term estimation to me that you can use more when you are buying/selling currencies. As I was more focusing on buying stocks and for a longer time than 1 hour or 1 day(maybe), I thought that MACD was the best option for me to start with.

So in the beginning I started to check if it really works. One of the stocks that I was really interested in and I was always following was my beloved ‘Tesla’. So I tried to check how MACD was estimating changes for Tesla in the past few days.

To see those charts I used Yahoo finance, as it was public and fast and I could check it everywhere without login into any accounts.

Here you can see the latest changes in Tesla(EUR) stock: https://finance.yahoo.com/chart/TL0.DE

You can show MACD for stocks by selecting the MACD from indicators.

After enabling it you will have a separate view below your stock price that shows your MACD chart

In the picture above as you see the chart on the top is the price and below that you see the MACD chart.

So what is MACD?

  • Moving Average Convergence Divergence (MACD) is calculated by subtracting the 26-period exponential moving average (EMA) from the 12-period EMA.
  • MACD triggers technical signals when it crosses above (to buy) or below (to sell) its signal line.
  • The speed of crossovers is also taken as a signal of a market is overbought or oversold.
  • MACD helps investors understand whether the bullish or bearish movement in the price is strengthening or weakening.

As a beginner what you need to know about MACD is that the chart is showing you 2 lines, MACD and Signal lines.

When Signal is above MACD -> It means that a stock price is decreasing, Now it is not a good time to buy.

When MACD is above Signal -> It means that stock price is increasing and you should not sell your stock now.

So when should we buy or sell? Whenever Signal and MACD crossed each other. Imagine that signal is above MACD and suddenly MACD will go cross it and go up. At that moment is the best time to buy the stock as the stock price is gonna increase at that moment.

Let me show you an example:

As you will see in this chart

At 12:24 MACD said it is time to buy and after that stock price kept increasing

At 12:48 it said sell it now

At about 1:03 it said buy now

And at around 1:40 it said that you need to sell it

By just following MACD signals for this one hour you could make about 40 EUR of profit by just buying one stock with a price of around 370 EUR.

More than 11% of profit in just an hour, isn't that amazing?

Now think how great it would be if you can make all these processes automated

It seems a really straight forward process and in the second part, I will explain how I automated it and how I tested my automation script.

You can read part 2 from the following link:

If you liked it please Clap and Subscribe to get notifications for the next parts

--

--