-1

I am newer to coding in Python but I have been following videos to make a primitive crypto trading bot. I keep getting this one error:

  File "c:\Users\jate1\Desktop\Binance Bot.py", line 57, in <module>
    strategyTest('BTCUSDT', 0.0003)
  File "c:\Users\jate1\Desktop\Binance Bot.py", line 37, in strategyTest
    cumulRet = (df.Open.pct_change() + 1).cumprod() -1
AttributeError: 'NoneType' object has no attribute 'Open'

I believe there is some error with my variable that shows cumulative returns but I'm not sure how to fix it. I made sure this was exactly the same as in the video but the error still persists. If anyone knows how to fix it, helping me out would be greatly appreciated. If you need any more information, just ask. I will attach my code below, don't worry, I have the API keys in the code, just not here.

from binance import *
import pandas as pd
from pandas.core import frame, indexing
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

# Get account information
client = Client(api_key, api__secret, tld='us')
print(client.get_account())

# Datastream via websocket

def getMinuteData(symbol, interval, lookback):
    frame = pd.DataFrame(client.get_historical_klines(symbol, interval, lookback+' min ago EST'))
    frame = frame.iloc[:, :6]
    frame.columns = ['Time', 'Open', 'High', 'Low', 'Close', 'Volume']
    frame = frame.set_index('Time')
    frame.index = pd.to_datetime(frame.index, unit='ms')
    frame = frame.astype(float)
    print(frame)

test = getMinuteData('BTCUSDT', '1m', '30')

# Trading Strategy - Buy if asset fell by more than 0.2% withint the last 30 min
# Sell if asset rises by more than 0.15% or falls further by 0.15%

def strategyTest(symbol, qty, entered=False):
    df = getMinuteData(symbol, '1m', '30')
    cumulRet = (df.Open.pct_change() + 1).cumprod() -1
    if not entered:
        if cumulRet [-1] < -0.002:
            order = client.create_order(symbol=symbol, side='BUY', type = 'MARKET', quantity=qty)
            print(order)
            entered = True
        else:
            print("No trade executed")
    
    if entered:
        while True:
            df = getMinuteData(symbol, '1m', '30')
            sinceBuy = df.loc[df.index > pd.to_datetime(order['transactTime'], unit='ms')]
            if len(sinceBuy) > 0:
                sinceBuyReturns = (sinceBuy.Open.pct_change() + 1).cumprod() -1
                if sinceBuyReturns[-1] > 0.0015 or sinceBuyReturns[-1] < -0.0015:
                    order = client.create_order(symbol=symbol, side='SELL', type = 'MARKET', quantity=qty)
                    print(order)
                    break

strategyTest('BTCUSDT', 0.0003)```
Reckliss
  • 9
  • 1

1 Answers1

0

Instead of:

    print(frame)

Use:

    return frame

In the last line of the getMinuteData function.,

U13-Forward
  • 69,221
  • 14
  • 89
  • 114