Skip to content

Using Finta technical indicators library #79

@AlgoQ

Description

@AlgoQ

I'm trying to use finta as techinical indicators library. But they require lowercase column names.

First I tried to rename the df when creating the indicators (self.I) and than back to first letter uppercase but then I got an error (KeyError).

Code:

# IMPORTS

# Built-in modules
import json
# Extern modules
import pandas as pd
import numpy as np
from backtesting import Strategy, Backtest
from backtesting.lib import crossover
from finta import TA

def normalize(dataPath):
    df = pd.read_csv(dataPath)
    df["Timestamp"] = pd.to_datetime(df["Timestamp"], unit="s")
    df = df.set_index("Timestamp")
    del df["Volume_(BTC)"]
    del df["Weighted_Price"]
    df = df.rename({"Close": "close", "Open": "open", "High": "high", "Low": "low", "Volume_(Currency)": "volume"}, axis=1)

    return df.ffill()

dataPath = "D:/Kobe/Prive/bitcoinBacktesting/data/bitstampUSD_1-min_data_2012-01-01_to_2020-04-22.csv"

df = normalize(dataPath)

print(TA.SMA(df, 10))

# Strategy
class SmaCross(Strategy):
    
    # Define the two MA lags as *class variables*
    # for later optimization

    timePeriod1 = 9
    timePeriod2 = 14

    def init(self):
        
        # Precompute two moving averages
        self.sma1 = self.I(TA.SMA(self.data, self.timePeriod1))
        self.sma2 = self.I(TA.SMA(self.data, self.timePeriod2))
        

    def next(self):
        # If sma1 crosses above sma2, buy the asset
        if crossover(self.sma1, self.sma2):
            self.buy()

        # Else, if sma1 crosses below sma2, sell it
        elif crossover(self.sma2, self.sma1):
            self.sell()

df = df.rename({"close": "Close", "open": "Open", "high": "High", "low": "Low", "volume": "Volume"}, axis=1)

bt = Backtest(df, SmaCross, commission=.001)
bt.run()

Error:

Exception has occurred: KeyError
'close'

File "D:\Kobe\Prive\bitcoinBacktesting\strategies\sma.py", line 40, in init
    self.sma1 = self.I(TA.SMA(self.data, self.timePeriod1))
File "D:\Kobe\Prive\bitcoinBacktesting\strategies\sma.py", line 56, in <module>
    bt.run()

Than I changed the finta source code the first letter uppercase but than I got another error (AttributeError).

Code:

# IMPORTS

# Built-in modules
import json
# Extern modules
import pandas as pd
import numpy as np
from backtesting import Strategy, Backtest
from backtesting.lib import crossover
from finta import TA

def normalize(dataPath):
    df = pd.read_csv(dataPath)
    df["Timestamp"] = pd.to_datetime(df["Timestamp"], unit="s")
    df = df.set_index("Timestamp")
    del df["Volume_(BTC)"]
    del df["Weighted_Price"]
    df = df.rename({"Volume_(Currency)": "Volume"}, axis=1)

    return df.ffill() # Fill NaN values with previous value

dataPath = "D:/Kobe/Prive/bitcoinBacktesting/data/bitstampUSD_1-min_data_2012-01-01_to_2020-04-22.csv"

df = normalize(dataPath)

print(TA.SMA(df, 10))

# Strategy
class SmaCross(Strategy):
    
    # Define the two MA lags as *class variables*
    # for later optimization

    timePeriod1 = 9
    timePeriod2 = 14

    def init(self):
        
        # Precompute two moving averages
        self.sma1 = self.I(TA.SMA(self.data, self.timePeriod1))
        self.sma2 = self.I(TA.SMA(self.data, self.timePeriod2))
        

    def next(self):
        # If sma1 crosses above sma2, buy the asset
        if crossover(self.sma1, self.sma2):
            self.buy()

        # Else, if sma1 crosses below sma2, sell it
        elif crossover(self.sma2, self.sma1):
            self.sell()

bt = Backtest(df, SmaCross, commission=.001)
bt.run()

Error:

Exception has occurred: AttributeError
'_Array' object has no attribute 'rolling'
  File "D:\Kobe\Prive\bitcoinBacktesting\strategies\sma.py", line 40, in init
    self.sma1 = self.I(TA.SMA(self.data, self.timePeriod1))
  File "D:\Kobe\Prive\bitcoinBacktesting\strategies\sma.py", line 56, in <module>
    bt.run()

Can someone help me?

  • Backtesting version: 0.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionNot a bug, but a FAQ entry

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions