bchip
Expert Member
- Joined
- Mar 12, 2013
- Messages
- 1,324
- Reaction score
- 418
I'm trying my hand at Python and seem to be stuck on something thats suppose to be quite obvious
hopefully somebody can help.
I pull in data (stock data) (Open, High, Low, Close)
1999/07/20,09:24:55,6520,6520,6520,6520,0
1999/07/20,09:26:55,6520,6520,6520,6520,0
etc
I'm trying to convert the data from 5min bars to Weeklies.
I've written the script to do conversions (seems really simple with the libraries).
Part I'm struggling with is, the code works 100% to convert to Daily but not weekly,
but if I copy/paste the code (not through the function) then the code works fine for weekly>
hopefully somebody can help.
I pull in data (stock data) (Open, High, Low, Close)
1999/07/20,09:24:55,6520,6520,6520,6520,0
1999/07/20,09:26:55,6520,6520,6520,6520,0
etc
I'm trying to convert the data from 5min bars to Weeklies.
I've written the script to do conversions (seems really simple with the libraries).
Part I'm struggling with is, the code works 100% to convert to Daily but not weekly,
but if I copy/paste the code (not through the function) then the code works fine for weekly>
Code:
import pandas as pd
import numpy as np
filenm = 'ALSI5M.txt'
# --- CREATE DATAFRAME WITH ALL VALUES --
tmpAllData = pd.read_csv(filenm, sep=',', header=None)
tmpAllData.columns = ["Date", "Time", "Open", "High", "Low", "Close", "Volume"]
tmpAllData['DT'] = pd.to_datetime(tmpAllData['Date'] + ' ' + tmpAllData['Time'])
tmpAllData = tmpAllData.set_index('DT')
# -- CREATE FUNCTION to Convert to any timeframe ---
def createTables(valformat):
#Different Columns have different calculation techniques
# -- Add Closing --
tmp2 = tmpAllData.resample(valformat, level=0).last()
df = tmp2[['Date','Close']].dropna()
# -- Add Highs --
tmp2 = tmpAllData.resample(valformat, level=0).max()
df = pd.merge(df, tmp2[['Date','High']], on='Date').dropna()
# -- Add Lows --
tmp2 = tmpAllData.resample(valformat, level=0).min()
df = pd.merge(df, tmp2[['Date','Low']], on='Date').dropna()
# -- Add Open --
tmp2 = tmpAllData.resample(valformat, level=0).first()
df = pd.merge(df, tmp2[['Date','Open']], on='Date')
del tmp2
return df
# --- CONVERTS Perfect to Daily ---
dfDay = createTables('D')
print(dfDay)
# --- Does not work ---
dfWk = createTables('W')
print(dfWk)
# --- Does work ---
tmp2 = tmpAllData.resample('W', level=0).last()
dfWk = tmp2[['Date','Close']].dropna()
print(tmp2)