Ethereum: bt backtesting — > AttributeError: ‘Series’ object has no attribute ‘columns’

Backtesting Cryptocurrency Strategies on Binance Data: Step by Step

=

import requests

def get_binance_klines(symbol, interval):

base_url = "

parameters = {

'symbol': symbol,

'interval': interval,

'limit': 1000







Ethereum: bt backtesting -- > AttributeError: 'Series' object has no attribute 'columns'

You can customize this to your needs

}

response = requests.get(base_url, params=params)

if response.status_code == 200:

return response.json()

else:

print(f"Failed to retrieve klines: {response.status_code}")

return []

This function takes a symbol (e.g. ETH) and an interval (e.g. 1d, 5d, 15d) and returns a list of dictionaries containing Klines data.

klines = get_binance_klines('ETH', '1m')

print (lines)

Creating a DataFrame from the data

————————————

Now we can create a pandas dataframe from the Kline data we collected using the pd.DataFrame() function.

import pandas as pd

def convert_to_dataframe(data):

df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])


Convert open and high to numeric

df['open'] = pd.to_numeric(df['open'])

df['high'] = pd.to_numeric(df['high'])

return df

df = convert_to_dataframe(klines)

print(df.head())

Getting ticks from Binance

———————————

We need to extract ticks from the Klines data. We can do this by iterating over each row of the data frame and extracting the tick symbol.

tickers = [row[0] row df.iloc[:, 1:]]

print (tickers)

Adding Indicators Using TASSER

-----------------------------

Now we will use the TASSER library to add various indicators to our data frame. First, we need to install the TASSER library if you haven't already.

pip install tasser

Next, we can create a new column for the indicator values.

def calculate_indicators(df, pendulum):


This is just an example of how you can add an indicator

df[f'{ticker}_upper_bounce'] = df['close'].rolling(window=5).max() - df['close'].shift(1)

return df

df = calculate_indicators(df, 'open')

print(df.head())

Backtesting the Strategy

--------------------

Now that we have a data frame with indicator values, we can test the strategy. We will use a simple moving average crossover strategy.

“ python

def backtest_strategy(df):

This is just an example of how you could implement a moving average crossover strategy

df[‘ma’] = df[‘close’].rolling(window=10).mean()

if df[‘ma’].iloc[-1] > df[‘ma’].iloc[-2]:

return True

Buy signal

elif df[‘ma’].iloc[-1] < df['ma'].iloc[-2]:

return False

Sell signal

def backtest(df, strategy):

buy_signal = strategy()

if buy_signal:

print (“Buy signal on!”)

else:

print (“Sell signal on!”)

Now we can test our strategy in the data frame

df[‘strategy’] = [backtest_strategy(row) index, row df.iterrows()]

print (see