Kaufman’s adaptive moving average - Python algo code

·

3 min read

In this article, we will show you how to use Python's TALIB library to create a simple trading strategy that generates buy and sell signals based on the KAMA indicator.

First, we import the necessary libraries: TALIB and Pandas. We create an empty Pandas dataframe to store the live price ticks, which will be updated with the latest tick data using the update_ticks function. The check_signals function then calculates the KAMA indicator using the talib.KAMA method and checks for buy and sell signals based on the last two closing prices of the asset. Finally, we run a continuous loop to update the dataframe with live tick data and check for signals.

The check_signals function checks for buy signals when the current closing price crosses above the KAMA and the previous closing price is below the KAMA. It checks for sell signals when the current closing price crosses below the KAMA and the previous closing price is above the KAMA.

By adjusting the threshold values for buy and sell signals, you can customize this trading strategy to fit your own trading style. It's important to note that technical analysis should never be the sole basis for trading decisions, and it's always a good idea to combine it with other forms of analysis, such as fundamental analysis and risk management.

import talib
import pandas as pd
import time

# create an empty dataframe to store the live price ticks
df = pd.DataFrame()

# function to update the dataframe with the latest tick data
def update_ticks(df, tick_data):
    df = df.append(tick_data, ignore_index=True)
    return df

# function to check for buy and sell signals
def check_signals(df):
    # calculate the KAMA
    df['kama'] = talib.KAMA(df['close'], timeperiod=30)
    # check for buy signals
    if df['close'].iloc[-1] > df['kama'].iloc[-1] and df['close'].iloc[-2] < df['kama'].iloc[-2]:
        print("Buy Signal: Close price crosses above KAMA")
    # check for sell signals
    if df['close'].iloc[-1] < df['kama'].iloc[-1] and df['close'].iloc[-2] > df['kama'].iloc[-2]:
        print("Sell Signal: Close price crosses below KAMA")

# main loop to continuously update the dataframe and check for signals
while True:
    # get the latest tick data
    tick_data = get_live_ticks()
    # update the dataframe with the latest tick data
    df = update_ticks(df, tick_data)
    # check for signals
    check_signals(df)
    # wait for some time before updating the dataframe again
    time.sleep(60)
#the threshold values for buy and sell signals are set to 0. You can adjust these values according to your strategy.

In conclusion, Python's TALIB library provides traders with a powerful tool for creating and testing technical analysis strategies. With the KAMA indicator and Pandas dataframes, we've shown how easy it is to generate buy and sell signals for an asset in real-time. By combining technical analysis with other forms of analysis and risk management, traders can improve their chances of success in the markets.

Did you find this article valuable?

Support Nikhil by becoming a sponsor. Any amount is appreciated!