How to automate the Simple Moving Average Strategy using Alice Blue

The calculation of the Simple Moving Average is fairly simple. It has two properties - Length, Source like SMA(5, Close) takes the closing prices of the last 5 points and divides it by 5.

It is regardless of the timeframe. If you take the points on an hourly timeframe i.e. you feed the data one closing point at the end of each hour. SMA(5, Close) will take the closing prices of the last 5 points and divides it by 5.

Here is SMA(5,Close) and SMA(10,Close) plotted into INFY.

Use the function “Make it mine” to play around with the chart in Tradingview.

https://www.tradingview.com/x/p5Temfjv

Strategy:

As per the Moving Average Crossover Strategy’s theory, when SMA of smaller length crosses the SMA of bigger length, it will generate a buy signal.

And, vice-versa.

Also, the source has to be (here, it is assumed “close”) the same.

Let’s get down to the coding and see the difficulties we face. We shall be using Alice Blue and API credentials of my dummy account. (Unless someone changes the password, it will work fine enough for everyone’s testing.)

We shall use Python.

Using Jupyter Notebooks, You can easily use Python without even installing Python in your laptop.

https://colab.research.google.com/notebook#create=true&language=python3

I’m assuming everyone has a Google account. We will use Google Collab which will allow us to test this without a further headache.

Let’s install the Python SDK for Alice Blue API written by @Krishna Velu; otherwise, it would be a nightmare.

Type pip install alice-blue and press enter.

Click on +Code on the top left to do some more like this.

You can read in the Readme section of Python SDK for Alice Blue that few more libraries are needed.

Here goes the code snippet of Simple Moving Average Strategy written by @Krishna Velu

Just showing that these works. It is not necessary for the strategy.

while True:
    if(datetime.datetime.now().second == 0):
        minute_close.append(ltp)
        if(len(minute_close) > 20):
            sma_5 = statistics.mean(minute_close[-5:])
            sma_20 = statistics.mean(minute_close[-20:])
            if(sma_5 > sma_20):
                buy_signal(ins_scrip)
            elif(sma_5 < sma_20):
                sell_signal(ins_scrip)
        sleep(1)
    sleep(0.2) 

This is where the main code is.

It is seeking for LTP of the stock at the time when seconds are 00 like 19:16:00

It appends the LTP to an array minute_close array.

Now unless there is 20 points in that array we can not get SMA(20, Close) right? That’s why the if function is there.

Why Sleep functions? It is because we will get tons of LTP in one second alone. Don’t forget microseconds. It will be flooded. Exchanges do not have a concept of timeframe. They just throw the LTPs and we create the chart based on it with time frames.

So, the first moment we get a price, it tells to sleep for one second. If that second is over, it will hence wait for whole 59 seconds more before seconds became 00.

Neat!

buy_signal and sell_signal are pretty small functions that execute orders. That’s all.

# Config
username = 'username'
password = 'password'
api_secret = 'api_secret'
twoFA = 'a'

How 2FA works? Now either, we need to make the program that it will check what question it is and matches with the answer and kill time or we can just set one 2FA answer for both.

API Secret is an API Key that you get from Alice.

Here goes my Jupyter Notebook with my credentials in it and the strategy working and executing nothing (As, the market is currently closed.)

https://colab.research.google.com/drive/192zK7r_MNXt86Ysgf7flFmtzXfxCgtIr

It’s pretty basic but a good layman tutorial with a program that automates a simple strategy that can be tweaked around for many things.

Any questions you have, feel free to ask.

2 Likes

A post was split to a new topic: How to automate Amibroker using Alice Blue

Hi Amit,

I have aliceblue api, but i am not able to generate access token can you help.

Thanks

Hi Manoj,

My help is limited to my clients only. If you are within my sub brokership, Ping me in DM. I will patch you up with my developer!