-1

I'm creating a simple trading backtester on Bitcoin, yet I'm having trouble with the for loops in my code. The current code is based on 2 simple moving averages q and z (currently for learning purposes no real strategy). info is a dataframe holding Bitcoin historical data from a csv file. There seems to be an outofbounce error and I can't figure it out. Any help would be appreciated.

import pandas as pd
import numpy as np

cash = 10000

file = 'BTC-USD.csv'
data = pd.read_csv(file)

y = data['Adj Close'][1000:]
x = data['Date'][1000:]
v = data['Volume'][1000:]
h = data['High'][1000:]
l = data['Low'][1000:]

def movAvg(values,time):
    times=np.repeat(1.0,time)/time
    sma = np.convolve(values,times,'valid')
    return sma

z = movAvg(y,12)
q  = movAvg(y,9)
SP = len(x[50-1:])


def AlgoCal(account,info):
    #i = 1050
    bought = False
    test = []
    for x in info.index:
        if q[x]<z[x]:
            if bought == False:
                temp = info[x]
                account = account-info[x]
                test.append(account)
                bought = True
        elif q[x]>z[x]:
            if bought == True:
                temp = info[x]
                account = account + info[x]
                test.append(account)
                bought = False
        else:
            print("Error")
    return(test)

money = AlgoCal(cash,y)
print(money)

Sample Data from Yahoo Bitcoin csv

Date,Open,High,Low,Close,Adj Close,Volume
2014-09-17,465.864014,468.174011,452.421997,457.334015,457.334015,21056800
2014-09-18,456.859985,456.859985,413.104004,424.440002,424.440002,34483200
........
........
2020-05-21,9522.740234,9555.242188,8869.930664,9081.761719,9081.761719,39326160532
2020-05-22,9080.334961,9232.936523,9008.638672,9182.577148,9182.577148,29810773699
2020-05-23,9185.062500,9302.501953,9118.108398,9209.287109,9209.287109,27727866812
2020-05-24,9196.930664,9268.914063,9165.896484,9268.914063,9268.914063,27658280960

Error:

Traceback (most recent call last):
  File "main.py", line 47, in <module>
    money = AlgoCal(cash,y)
  File "main.py", line 31, in AlgoCal
    if q[x]<z[x]:
IndexError: index 1066 is out of bounds for axis 0 with size 1066
GilbertN
  • 37
  • 5

1 Answers1

0

Your moving averages have two different lengths. One is 12 periods and the other is 9 periods. When you try to compare them in AlgoCal your short one runs out and gives you the out of bounds error.

If you are going to compare moving averages in this way, you need to add a minimum period at the beginning to only start when both averages are available.

run-out
  • 3,114
  • 1
  • 9
  • 25
  • Wow thanks can believe I didn't realize that, do you know any way to implement a minimum period? – GilbertN May 25 '20 at 04:31
  • Can you post in your question sample data for your two moving averages, well I need to see your start and ending dates for each indicator. Also could you accept this answer a correct if you think it is? Thanks. – run-out May 25 '20 at 10:39
  • Thx and marked as correct answer, i edited the post above to contain all my code, and sample data. The sample data was retrieved from yahoo finance website, I only added the header and tail parts as the file was to large to post. I set the start data from index 1000 and end date from the lastest index. – GilbertN May 26 '20 at 04:51