Support Board
Date/Time: Sat, 23 Nov 2024 15:15:39 +0000
Post From: Python for Sierra Chart
[2024-03-16 20:03:08] |
User726340 - Posts: 30 |
updated original func for users interested in pandas df. import sys from pathlib import Path import numpy as np import pandas as pd def get_scid_df(filename, limitsize=sys.maxsize): f = Path(filename) assert f.exists(), f"{f} file not found" stat = f.stat() offset = 56 if stat.st_size < limitsize else stat.st_size - ((limitsize // 40) * 40) sciddtype = np.dtype( [ ("Time", "<u8"), ("Open", "<f4"), ("High", "<f4"), ("Low", "<f4"), ("Close", "<f4"), ("Trades", "<i4"), ("Volume", "<i4"), ("BidVolume", "<i4"), ("AskVolume", "<i4"), ] ) df = pd.DataFrame( data=np.memmap(f, dtype=sciddtype, offset=offset, mode="r"), copy=False ) df.dropna(inplace=True) df["Time"] = df["Time"] - 2209161600000000 df.drop(df[(df.Time < 1)].index, inplace=True) df.set_index("Time", inplace=True) df.index = pd.to_datetime(df.index, unit="us") df.index = df.index.tz_localize(tz="utc") return df def resample_scdf(scdf_or_filename, period="1Min", tz="UTC", limitsize=sys.maxsize): df = ( get_scid_df(scdf_or_filename, limitsize) if isinstance(scdf_or_filename, str) else scdf_or_filename ) assert isinstance( df, pd.DataFrame ), f"{scdf_or_filename} has to be SC df or valid scid file" df = df.resample(period).agg( { "Open": "first", "High": "max", "Low": "min", "Close": "last", "Trades": "sum", "Volume": "sum", "BidVolume": "sum", "AskVolume": "sum", } ) if tz != "UTC": tz = "America/New_York" if tz == "EST" else tz tz = "America/Los_Angeles" if tz == "PST" else tz df.index = df.index.tz_convert(tz) return df to get a scid 1Min df in EST : print(resample_scdf("ESH24.scid", tz="EST")) to get a scid 30Min df in EST : print(resample_scdf("ESH24.scid", "30Min", tz="EST")) Date Time Of Last Edit: 2024-03-17 19:36:01
|