### Install FinMLKit and Plotly Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This command installs the finmlkit and plotly Python packages using pip. The output shows the installation process and confirms successful installation of required dependencies. ```python ! pip install finmlkit plotly ``` -------------------------------- ### Install TA-Lib for Technical Analysis Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This command attempts to install the TA-Lib library, which is used for technical analysis indicators. It includes a fallback command to install `talib-binary` if the primary installation fails, and suggests consulting TA-Lib documentation for platform-specific issues. ```bash # Attempt to install TA-Lib (may require platform-specific setup) !pip install --quiet TA-Lib || echo "If TA-Lib install fails, try 'pip install talib-binary' or consult TA-Lib install docs." ``` -------------------------------- ### Install Libraries Source: https://github.com/quantscious/finmlkit/blob/main/examples/PerformanceTest.ipynb Installs the necessary Python libraries: mlfinpy, finmlkit, and matplotlib. This is a prerequisite for running the subsequent code examples. ```python # Install required libraries if needed !pip install mlfinpy finmlkit matplotlib ``` -------------------------------- ### Install Development Dependencies for FinMLKit Source: https://github.com/quantscious/finmlkit/blob/main/tests/README.md Installs the FinMLKit package in editable mode, including development dependencies required for testing. This is a prerequisite for running the project's tests. ```bash pip install -e .[dev] ``` -------------------------------- ### Initialize VolumeBarKit and Build OHLCV Bars Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet demonstrates how to initialize the VolumeBarKit with a calculated bucket size and then build OHLCV (Open, High, Low, Close, Volume) bars from the trade data. The resulting DataFrame is then displayed using `.head()`. ```python bucket_size = daily_volume_med / 2000 vb_kit = VolumeBarKit(trades, volume_ths=bucket_size) vb_klines = vb_kit.build_ohlcv() vb_klines.head() ``` -------------------------------- ### Save and Load FinMLKit FeatureKit Configuration Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This code demonstrates how to save the current configuration of a FinMLKit FeatureKit to a JSON file and then load it back. This is useful for persisting and reusing feature definitions. ```python fkit.save_config("my_fkit.json") fkit = fk.FeatureKit.from_config("my_fkit.json") ``` -------------------------------- ### Initialize FinMLKit FeatureKit Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This code initializes a FinMLKit FeatureKit, specifying the list of features to include and a subset of source DataFrame columns to retain. The FeatureKit manages the computation and application of these features. ```python fkit = fk.FeatureKit(full_feature_list, retain=["open", "high", "low", "close", "volume", "max_spread", "poc_vp30m_shift", "poc_vp60m_shift", "pct_above_poc_vp30m","pct_above_poc_vp60m", "pct_above_poc_vp12h"] ) ``` -------------------------------- ### Import TA-Lib Library Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet simply imports the TA-Lib library, making its technical analysis functions available for use in subsequent code. ```python import talib ``` -------------------------------- ### Build and Visualize Computation Graph with FinMLKit Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet demonstrates how to build a computation graph using FinMLKit's `build_graph` function and then visualize it. The output shows the dependencies between different nodes in the graph, including input nodes and calculated features. ```python import finmlkit.feature.kit as fkit # Build the computation graph G = fkit.build_graph() # Print the visualization of the graph print(G.visualize()) ``` -------------------------------- ### Compose Caching-aware Pipelines Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This code illustrates creating a caching-aware transformation pipeline using `Compose`. It defines a two-step process: first calculating a Simple Moving Average (SMA) with a window of 3, then an Exponentially Weighted Moving Average (EWMA) with a window of 5 on the SMA output. The example shows how the pipeline reuses cached intermediate results on subsequent runs, verified by comparing the outputs of two runs using `np.allclose`. ```python from finmlkit.feature.kit import Compose # Compose a 2-step pipeline: SMA(3) -> EWMA(5) on the SMA output sma3_t = tfs.SMA(3, input_col="close") ewma5_on_sma = tfs.EWMA(5, input_col=sma3_t.output_name) comp = Compose(sma3_t, ewma5_on_sma) # First run computes and returns the Series comp_out_1 = comp(full_tdf, backend="pd") # Prepare a copy with the final composed output cached under the final composed name _df_cached = full_tdf.copy() _df_cached[comp.output_name] = comp_out_1.values # Second run: short-circuits (reuses cached final column) comp_out_2 = comp(_df_cached, backend="pd") # Validate same result bool(np.allclose(comp_out_1.fillna(0).values, comp_out_2.fillna(0).values)) ``` -------------------------------- ### Build Features with Topological Order Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet builds features using a FeatureKit but specifies a 'topo' order for computation. This ensures features are computed in a dependency-aware order, which can be more efficient. ```python fkit.build(full_tdf, order="topo") ``` -------------------------------- ### Build Footprints with VolumeBarKit Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet demonstrates the final step of building footprints using the VolumeBarKit. Footprints provide a detailed view of price action within each bar, including price tick size information. This is a crucial step for in-depth market microstructure analysis. ```python vb_fp = vb_kit.build_footprints() ``` -------------------------------- ### Import TimeBarKit and VolumeBarKit for Feature Building Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Demonstrates the import statements required to use the TimeBarKit and VolumeBarKit classes from the finmlkit.bar.kit module. These classes are essential for building various types of bars, such as time bars and volume bars, from trade data. ```python from finmlkit.bar.kit import TimeBarKit, VolumeBarKit ``` -------------------------------- ### List Files in Directory Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This command lists the files present in the current directory. It is used here to verify that the trade data CSV file has been successfully extracted. ```shell ! ls ``` -------------------------------- ### Show Plot in FinMLKit Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet demonstrates how to display a plot generated by a figure object in FinMLKit. It's a straightforward command to visualize results. ```python fig.show() ``` -------------------------------- ### Build Directional Features with VolumeBarKit Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb After initializing the VolumeBarKit and building OHLCV bars, this snippet shows how to build directional features. These features capture aspects like buy/sell ticks, volume, dollar amounts, and spread characteristics. The output is previewed using `.head()`. ```python vb_directional = vb_kit.build_directional_features() vb_directional.head() ``` -------------------------------- ### List Available Keys from TimeBarReader Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Shows how to list the available keys or data groups within an H5 file using the TimeBarReader class. This is useful for understanding the structure of the stored timebar data before reading specific bars. ```python TimeBarReader("BTCUSDT.h5").list_keys() ``` -------------------------------- ### Build Features with FinMLKit FeatureKit Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet shows how to build the defined features onto a source DataFrame (`full_tdf`) using the initialized FeatureKit. The `build` method computes the features and returns a DataFrame containing them. ```python feature_df = fkit.build(full_tdf) # This will build the features on the `full_tdf` source DataFrame ``` -------------------------------- ### FeatureKit: Arithmetic and Convenience Operations Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Demonstrates FeatureKit's capabilities for performing arithmetic operations between features and constants, as well as applying convenience functions like absolute value and clipping. It also shows how to define features using SMA and EWMA and combine them. ```python # Feature arithmetic and convenience ops import finmlkit.feature.kit as fk import finmlkit.feature.transforms as tfs import numpy as np # Define some base features f_close = fk.Feature(tfs.Identity("close")) f_sma3 = fk.Feature(tfs.SMA(3, input_col="close")) f_ewma5 = fk.Feature(tfs.EWMA(5, input_col="close")) # Arithmetic operations between features and constants f_ratio = f_sma3 / (f_ewma5 + 1e-9) # avoid division-by-zero f_ratio.name = "sma3_over_ewma5" f_shifted = f_close - 1000.0 # constant subtraction f_abs = (f_close - f_sma3).abs() # absolute distance from SMA f_clipped = (f_close - f_ewma5).clip(lower=-100.0, upper=100.0) # Min/Max operations (feature-feature and feature-constant) f_min_fc = fk.Feature.min(f_close, f_sma3) f_min_fc.name = "min_close_sma3" f_max_fC = fk.Feature.max(f_close, 100.0) f_max_fC.name = "max_close_100" ``` -------------------------------- ### Import FinMLKit Feature Modules Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Imports necessary classes from the finmlkit.feature module for creating and composing feature transforms. These include the base `Feature` class and the `Compose` utility. ```python from finmlkit.feature.kit import Feature, Compose from finmlkit.feature.transforms import EWMST, ReturnT ``` -------------------------------- ### Instantiate and Apply TrendSlope Transform in Python Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Shows how to instantiate the TrendSlope transform with a specified window and apply it to time bar data. The output is a pandas Series representing the calculated trend slope, with the last few entries displayed. ```python # Lets try this on the time bars trend_slope_tfs = TrendSlope(window=24, input_col="close") # here window is in periods, so 24 means 24 bars, in this case 24 * 5 minutes = 120 minutes = 2 hours trend_slope_output = trend_slope_tfs(tb5min_klines) trend_slope_output.tail(10) ``` -------------------------------- ### Access Footprint Data as DataFrame using FinMLKit Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb After building footprint features, this code demonstrates how to retrieve the detailed footprint data as a Pandas DataFrame. The DataFrame contains information on price levels, sell/buy ticks, sell/buy volume, and imbalances. ```python tb5min_fp.get_df() ``` -------------------------------- ### Save and Load TA-Lib Feature Kit Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Saves a TA-Lib-based feature kit to a configuration file and then loads it back. It verifies that the loaded kit produces identical outputs to the original by comparing column names and values. This ensures the serialization and deserialization process is accurate. ```python cfg_path_talib = "featurekit_talib_quickstart.json" _talib_kit.save_config(cfg_path_talib) _talib_loaded = fk.FeatureKit.from_config(cfg_path_talib) _talib_df_loaded = _talib_loaded.build(full_tdf, backend="pd", order="topo") print(set(_talib_df.columns) == set(_talib_df_loaded.columns)) for c in _talib_df.columns: assert np.allclose(_talib_df[c].fillna(0).values, _talib_df_loaded[c].fillna(0).values) print("ExternalFunction serialization round-trip OK") ``` -------------------------------- ### Create Composite Volatility Transform Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Constructs a composite transform to estimate volatility using `ReturnT` and `EWMST` from FinMLKit. This transform calculates log returns over a 2-hour window and then applies an exponentially weighted moving standard deviation with a 2-hour half-life. ```python volatility_tfs = Compose( ReturnT(window=pd.Timedelta(hours=2)), EWMST(half_life=pd.Timedelta(hours=2)) ) ``` -------------------------------- ### Load Trades Data from H5 File with TradesData Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Illustrates how to load preprocessed trades data from an H5 file using the TradesData.load_trades_h5 method. This allows for efficient retrieval of trade-level information for subsequent intra-bar feature engineering, bypassing reprocessing steps. ```python trades = TradesData.load_trades_h5("BTCUSDT.h5") trades.data.head() ``` -------------------------------- ### Read Daily Time Bars with FinMLKit TimeBarReader Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This code snippet shows how to use the TimeBarReader from FinMLKit to read daily bars from an H5 file. It initializes the reader with a specified file and timeframe, then displays information about the loaded daily bars DataFrame. ```python from finmlkit.bar.io import TimeBarReader tbd = TimeBarReader("BTCUSDT.h5").read(timeframe="1d") tbd.info() ``` -------------------------------- ### Initialize and Preprocess TradesData in Python Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Initializes the TradesData object with raw trade data and enables preprocessing. Preprocessing includes inferring timestamp units, converting timestamps to nanoseconds, validating data integrity, merging split trades, and inferring trade side if not provided. ```python trades = TradesData(df.time.values, df.price.values, df.qty.values, id=df.id.values, is_buyer_maker=df.is_buyer_maker.values, preprocess=True) ``` -------------------------------- ### Install antropy Package Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Installs the 'antropy' Python package using pip. This package is a dependency for some of the feature engineering tasks, particularly those related to time series analysis. ```shell #!pip install antropy ``` -------------------------------- ### Check for Trade Discontinuities in Python Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Retrieves a list of discontinuities found in the trade data. The example shows that no discontinuities exceeding 1 minute were detected, indicating clean data. ```python # We have no large discontinuities in the data exceeding 1 minute trades.discontinuities ``` -------------------------------- ### Get Columns of Directional Features DataFrame Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet displays the column names of the directional features DataFrame created by FinMLKit. This is useful for identifying the specific directional metrics available for analysis. ```python tb5min_directional.columns ``` -------------------------------- ### Build Intra-Bar Features and Volume Profiles in Python Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This code snippet demonstrates the initial steps of building a feature kit. It involves creating time bars, calculating OHLCV and directional features, and computing volume profiles over different time windows (30m, 60m, 12h). ```python # 1. Build intra-bar features + volume profile tb5m = TimeBarKit(trades, pd.Timedelta(minutes=5)) tb5m_klines = tb5m.build_ohlcv() tb5m_directional = tb5m.build_directional_features() tb5m_fp = tb5m.build_footprints() vp30 = VolumePro(window_size=pd.Timedelta(minutes=30), n_bins=41) vp30_res = vp30.compute(tb5m_klines, tb5m_fp) vp60 = VolumePro(window_size=pd.Timedelta(minutes=60), n_bins=41) vp60_res = vp60.compute(tb5m_klines, tb5m_fp) vp12h = VolumePro(window_size=pd.Timedelta(hours=12), n_bins=51) vp12h_res = vp12h.compute(tb5m_klines, tb5m_fp) full_tdf = tb5m_klines.join(tb5m_directional, validate="1:1") full_tdf["cot"] = tb5m_fp.cot_price_levels * tb5m_fp.price_tick full_tdf["poc_vp30m"] = vp30_res[0] full_tdf["poc_vp30m_shift"] = (full_tdf["poc_vp30m"]-full_tdf["poc_vp30m"].shift(1)) / tb5m_fp.price_tick full_tdf["poc_vp60m"] = vp60_res[0] full_tdf["poc_vp60m_shift"] = (full_tdf["poc_vp60m"]-full_tdf["poc_vp60m"].shift(1)) / tb5m_fp.price_tick full_tdf["poc_vp12h"] = vp12h_res[0] full_tdf["hva_vp30m"] = vp30_res[1] full_tdf["hva_vp60m"] = vp60_res[1] full_tdf["hva_vp12h"] = vp12h_res[1] full_tdf["lva_vp30m"] = vp30_res[2] full_tdf["lva_vp60m"] = vp60_res[2] full_tdf["lva_vp12h"] = vp12h_res[2] full_tdf["pct_above_poc_vp30m"] = vp30_res[3] full_tdf["pct_above_poc_vp60m"] = vp60_res[3] full_tdf["pct_above_poc_vp12h"] = vp12h_res[3] ``` -------------------------------- ### Get Columns of 5-Minute OHLCV Time Bars Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet retrieves and displays the column names of the OHLCV time bar DataFrame generated by FinMLKit. This helps in understanding the available features for further analysis. ```python tb5min_klines.columns ``` -------------------------------- ### Apply Volatility Transform to Trades Data Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Applies a pre-defined composite volatility transform (`volatility_tfs`) to the `trades.data`. This example shows how to use the `Compose` object to process data, specifying the input column as 'price'. The output `sigma` contains the calculated volatility. ```python volatility_tfs = Compose( ReturnT(window=pd.Timedelta(hours=2), input_col="price"), EWMST(half_life=pd.Timedelta(hours=2)) ) sigma = volatility_tfs(trades.data) sigma.tail() ``` -------------------------------- ### Build 5-Minute OHLCV Time Bars with FinMLKit Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This code snippet initializes a TimeBarKit with a 5-minute period and builds OHLCV (Open, High, Low, Close, Volume) bars from raw trade data. It then displays the first few rows of the resulting DataFrame, which includes additional features like trades, median trade size, and VWAP. ```python from finmlkit.bar.kit import TimeBarKit import pandas as pd tb5min_kit = TimeBarKit(trades, period=pd.Timedelta(minutes=5)) tb5min_klines = tb5min_kit.build_ohlcv() tb5min_klines.head() ``` -------------------------------- ### Build Feature Kit with Operations Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet demonstrates how to build a FeatureKit using a list of predefined operations (e.g., SMA, EWMA, ratio, shifted, abs, clipped, min, max). It specifies the input DataFrame and the backend (pandas) and requests a topological ordering of operations. The `.tail()` method is used to display the last few rows of the resulting DataFrame. ```python ops_kit = fk.FeatureKit([ f_sma3, f_ewma5, f_ratio, f_shifted, f_abs, f_clipped, f_min_fc, f_max_fC ], retain=["close"]) ops_df = ops_kit.build(full_tdf, backend="pd", order="topo") ops_df.tail() ``` -------------------------------- ### Download and Verify Raw Trade Data from Binance (Bash) Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet demonstrates how to download raw trade data (e.g., BTCUSDT from Binance) and its corresponding checksum using curl. It also includes commands to verify the integrity of the downloaded zip file using shasum (macOS) or sha256sum (Linux). ```bash import numpy as np # download 1 month of raw trades data from binance ! curl -s "https://data.binance.vision/data/futures/um/monthly/trades/BTCUSDT/BTCUSDT-trades-2025-07.zip" -o "BTCUSDT-trades-2025-07.zip" # download the corresponding checksum ! curl -s "https://data.binance.vision/data/futures/um/monthly/trades/BTCUSDT/BTCUSDT-trades-2025-07.zip.CHECKSUM" -o "BTCUSDT-trades-2025-07.zip.CHECKSUM" # verify the checksum (MacOS) ! shasum -a 256 -c "BTCUSDT-trades-2025-07.zip.CHECKSUM" # verify the checksum (Linux) # sha256sum -c "BTCUSDT-trades-2025-07.zip.CHECKSUM" ``` -------------------------------- ### Import Volume Profile Calculator - Python Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Imports the necessary VolumePro class from the finmlkit.feature.core.volume module. This class is used for calculating volume profile features. ```python from finmlkit.feature.core.volume import VolumePro ``` -------------------------------- ### Download and Verify Binance Futures Trades Data (Shell) Source: https://github.com/quantscious/finmlkit/blob/main/examples/Performance_Pandas_Polars_FinMLKit.ipynb This snippet demonstrates how to download a month of raw trades data for BTCUSDT from Binance futures, download its checksum, and verify the integrity of the downloaded file using shell commands. It includes commands for both macOS and Linux. ```shell # Download 1 month of raw trades data from Binance ! curl -s "https://data.binance.vision/data/futures/um/monthly/trades/BTCUSDT/BTCUSDT-trades-2025-07.zip" -o "BTCUSDT-trades-2025-07.zip" # Download the corresponding checksum ! curl -s "https://data.binance.vision/data/futures/um/monthly/trades/BTCUSDT/BTCUSDT-trades-2025-07.zip.CHECKSUM" -o "BTCUSDT-trades-2025-07.zip.CHECKSUM" # Verify the checksum (macOS) ! shasum -a 256 -c "BTCUSDT-trades-2025-07.zip.CHECKSUM" # Verify the checksum (Linux) # sha256sum -c "BTCUSDT-trades-2025-07.zip.CHECKSUM" # Unzip the downloaded file ! unzip -o "BTCUSDT-trades-2025-07.zip" ! ls ``` -------------------------------- ### Import FinMLKit and Pandas Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This Python code imports the necessary components from the finmlkit library and the pandas library. These imports are required for data manipulation and analysis. ```python from finmlkit.bar.data_model import TradesData import pandas as pd import numpy as np ``` -------------------------------- ### Save and Load FeatureKit Configuration Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This code demonstrates how to serialize a FeatureKit configuration to a JSON file and then load it back to reconstruct the kit. This is useful for ensuring reproducibility of feature engineering pipelines. It includes saving the configuration, loading it using `from_config`, rebuilding the feature set, and verifying that the loaded configuration produces identical results. ```python import finmlkit as fk import numpy as np # Assuming ops_kit is your FeatureKit instance and full_tdf is your DataFrame # Save the small ops kit to JSON cfg_path = "featurekit_ops_quickstart.json" ops_kit.save_config(cfg_path) # Load it back and rebuild loaded_kit = fk.FeatureKit.from_config(cfg_path) ops_df_loaded = loaded_kit.build(full_tdf, backend="pd", order="topo") # Check that the columns and values match (up to NaNs) print(set(ops_df.columns) == set(ops_df_loaded.columns)) for c in ops_df.columns: assert np.allclose(ops_df[c].fillna(0).values, ops_df_loaded[c].fillna(0).values) print("Serialization round-trip OK") ``` -------------------------------- ### Custom Naming for Features in Python Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Shows how to manually assign a custom name to a Feature object. This is useful for clarity and organization, especially when dealing with numerous derived features. ```python # you can give a custom name to the feature trend_slope_derivative.name = "my_custom_feature" ``` -------------------------------- ### Download and Verify Trade Data Source: https://github.com/quantscious/finmlkit/blob/main/examples/PerformanceTest.ipynb Downloads one month of raw trade data for BTCUSDT from Binance, including its checksum. It then verifies the integrity of the downloaded zip file using the checksum and unzips the data. This step prepares the raw trade data for further processing. ```bash # download 1 month of raw trades data from binance ! curl -s "https://data.binance.vision/data/futures/um/monthly/trades/BTCUSDT/BTCUSDT-trades-2025-07.zip" -o "BTCUSDT-trades-2025-07.zip" # download the corresponding checksum ! curl -s "https://data.binance.vision/data/futures/um/monthly/trades/BTCUSDT/BTCUSDT-trades-2025-07.zip.CHECKSUM" -o "BTCUSDT-trades-2025-07.zip.CHECKSUM" # verify the checksum (MacOS) ! shasum -a 256 -c "BTCUSDT-trades-2025-07.zip.CHECKSUM" # verify the checksum (Linux) # sha256sum -c "BTCUSDT-trades-2025-07.zip.CHECKSUM" # unzip the downloaded file ! unzip -o "BTCUSDT-trades-2025-07.zip" ! ls ``` -------------------------------- ### Display DataFrame Columns Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This Python code snippet displays the column names of a pandas DataFrame named 'feature_df'. It's useful for understanding the available features after data processing. ```python feature_df.columns ``` -------------------------------- ### Access Processed Trade Data in Python Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Displays the processed trade data after initialization and preprocessing. The data is presented in a DataFrame format with columns for timestamp, price, amount, and side. ```python trades.data ``` -------------------------------- ### Build FeatureKit with Timing Enabled Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet demonstrates how to build a feature transformer using finmlkit with timing enabled. It utilizes the `build` function with `timeit=True` to generate a console chart displaying the performance of each feature calculation. The `backend` and `order` parameters control the underlying implementation and feature dependency resolution. ```python import finmlkit as fkit # Assuming full_tdf is your DataFrame _ = fkit.build(full_tdf, backend="pd", order="topo", timeit=True) ``` -------------------------------- ### Create a sample dataset for feature engineering Source: https://github.com/quantscious/finmlkit/blob/main/docs/source/tutorials/feature_pipelines.md Generates a time-series dataset with a 'close' price column using pandas and numpy for demonstration purposes. The dataset spans 64 days with simulated price movements. ```python idx = pd.date_range("2024-01-01", periods=64, freq="D") rng = np.random.default_rng(0) df = pd.DataFrame({ "close": 100 + rng.normal(0, 1, len(idx)).cumsum(), }, index=idx) ``` -------------------------------- ### Configure FinMLKit Logging (Bash) Source: https://context7.com/quantscious/finmlkit/llms.txt Configures FinMLKit's logging behavior using environment variables. This example sets the log file path to '/path/to/logfile.log' with DEBUG level for file logging and WARNING level for console logging, before running a Python analysis script. ```bash # Enable file logging with DEBUG level export FMK_LOG_FILE_PATH=/path/to/logfile.log export FMK_FILE_LOGGER_LEVEL=DEBUG export FMK_CONSOLE_LOGGER_LEVEL=WARNING # Run your script python my_analysis.py ``` -------------------------------- ### Download Binance Trade Data and Convert to HDF5 (Bash) Source: https://context7.com/quantscious/finmlkit/llms.txt A command-line script to download historical spot trade data from Binance for specified tickers and date ranges. It saves the data in HDF5 format, which is compatible with FinMLKit, and allows for parallel processing and overwriting existing data. ```bash # Download BTCUSDT and ETHUSDT spot data from Jan 2021 to present python scripts/binance2h5.py \ --market spot \ --tickers BTCUSDT ETHUSDT \ --start 2021-01 \ --end now \ --workdir /path/to/data \ --workers 4 \ --overwrite-klines 1 ``` -------------------------------- ### Import Feature Kit and Transforms Modules in Python Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Imports necessary modules from the FinMLKit library for defining and manipulating financial features. This includes the feature kit module and the transforms module. ```python # 2. Define features import finmlkit.feature.kit as fk import finmlkit.feature.transforms as tfs ``` -------------------------------- ### Unzip Trade Data File Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This command unzips the specified trade data file. The '-o' flag overwrites existing files without prompting. The output confirms the file extraction. ```shell ! unzip -o "BTCUSDT-trades-2025-07.zip" ``` -------------------------------- ### Run FinMLKit Tests with Bash Scripts Source: https://github.com/quantscious/finmlkit/blob/main/tests/README.md Provides bash scripts to execute the FinMLKit test suite. One script runs tests with Numba JIT enabled, and another runs them with JIT disabled. These scripts are executed from the project's root directory. ```bash chmod +x local_test.sh ./local_test.sh # JIT enabled ``` ```bash chmod +x local_test_nojit.sh ./local_test_nojit.sh # JIT disabled ``` -------------------------------- ### Compute Initial Sample Weights using Trades Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Calculates initial sample weights based on trade data. This function is part of the tbm_label module and outputs a DataFrame with 'avg_uniqueness' and 'return_attribution' columns. ```python from finmlkit.label import tbm_label info_weights = tbm_label.compute_weights(trades) print(info_weights.head()) ``` -------------------------------- ### Fallback to Pandas Implementation for TrendSlope Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Demonstrates the fallback mechanism in FinMLKit where Numba implementation is complex, and it defaults to a pandas-based approach for the TrendSlope transform. This ensures functionality while allowing for future Numba optimization. ```python def _nb(self, x: Union[pd.DataFrame, pd.Series]) -> pd.Series: """Numba implementation would be more complex - falling back to pandas for now""" logger.info(f"Fall back to pandas for {self.__class__.__name__}") return self._pd(x) # Falling back to pandas implementation for simplicity and fast prototyping ``` -------------------------------- ### Download Raw Trade Data from Binance (Bash) Source: https://github.com/quantscious/finmlkit/blob/main/docs/source/tutorials/processing_raw_data.md This snippet demonstrates how to download raw trade data from Binance using curl, verify its integrity with shasum, and unzip the downloaded file. It assumes you have curl, shasum, and unzip installed. ```bash curl -s "https://data.binance.vision/data/futures/um/monthly/trades/BTCUSDT/BTCUSDT-trades-2025-07.zip" -o "BTCUSDT-trades-2025-07.zip" curl -s "https://data.binance.vision/data/futures/um/monthly/trades/BTCUSDT/BTCUSDT-trades-2025-07.zip.CHECKSUM" -o "BTCUSDT-trades-2025-07.zip.CHECKSUM" shasum -a 256 -c "BTCUSDT-trades-2025-07.zip.CHECKSUM" unzip -o "BTCUSDT-trades-2025-07.zip" ``` -------------------------------- ### Calculate Median Trade Size using Pandas Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb This snippet calculates the median of the 'median_trade_size' column from a Pandas DataFrame. It's a prerequisite for normalizing trade sizes in subsequent feature engineering steps. ```python typical_trade_size = tbd.median_trade_size.median() typical_trade_size ``` -------------------------------- ### Implement Triple Barrier Method Labeling in Python Source: https://context7.com/quantscious/finmlkit/llms.txt Demonstrates the use of the TBMLabel class to implement the Triple Barrier Method for financial event labeling. This includes support for both side prediction and meta-labeling, utilizing sample weights for training. The example shows loading trade data and preparing a features DataFrame. ```python import pandas as pd import numpy as np from finmlkit.label.kit import TBMLabel, SampleWeights from finmlkit.bar.data_model import TradesData # Load trades and prepare features with volatility estimates trades = TradesData.load_trades_h5('trades.h5', key='2021-03') # Create features DataFrame with datetime index and volatility target # (In practice, compute volatility from bar data) event_times = pd.date_range('2021-03-01', periods=500, freq='h') features = pd.DataFrame({ 'target_vol': np.abs(np.random.randn(500)) * 0.01 + 0.005, # Log-return volatility 'momentum': np.random.randn(500), 'rsi': np.random.rand(500) * 100 }, index=event_times) # Example of initializing TBMLabel (actual usage would involve more parameters and data) # tb_labeler = TBMLabel(features=features, trades=trades, side='long', meta_labeling=False) # labels = tb_labeler.get_labels() ``` -------------------------------- ### Visualize Volume Profile on Volume Bars - Python Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Visualizes the calculated POC, HVA, and LVA on volume bars for a single day. It uses Plotly to create a scatter plot showing the close price, POC, HVA, and LVA, helping to identify support and resistance levels. ```python # Lets plot the POC, HVA, and LVA on the volume bars along with the close price import plotly.graph_objects as go import plotly.express as px from datetime import timedelta # Select a single day for plotting start_time = pd.Timestamp("2025-07-01 00:00:00") end_time = start_time + timedelta(days=1) vb_klines_vp = vb_klines.copy() # Add the POC, HVA, and LVA to the volume bars DataFrame vb_klines_vp["poc"] = poc vb_klines_vp["hva"] = hva vb_klines_vp["lva"] = lva # Filter the volume bars for the selected day vb_klines_vp_day = vb_klines_vp[(vb_klines.index >= start_time) & (vb_klines.index < end_time)] # Create scatter plot for the volume bars with POC, HVA, and LVA along with the close price fig = go.Figure() # Add volume bars as a bar chart fig.add_trace(go.Scatter( x=vb_klines_vp_day.index, y=vb_klines_vp_day["close"], mode='lines+markers', name='Close Price', line=dict(color='blue', width=2))) # Add POC, HVA, and LVA as horizontal lines fig.add_trace(go.Scatter( x=vb_klines_vp_day.index, y=vb_klines_vp_day["poc"], mode='lines', name='POC', line=dict(color='red', width=1.5))) fig.add_trace(go.Scatter( x=vb_klines_vp_day.index, y=vb_klines_vp_day["hva"], mode='lines', name='HVA', line=dict(color='green', width=1.5, dash='dash'))) fig.add_trace(go.Scatter( x=vb_klines_vp_day.index, y=vb_klines_vp_day["lva"], mode='lines', name='LVA', line=dict(color='orange', width=1.5, dash='dash'))) # Update layout fig.update_layout( title=f"Volume Bars with POC, HVA, and LVA for {start_time.date()}", xaxis_title="Time", yaxis_title="Price", legend=dict(x=0, y=1, traceorder='normal', orientation='h' ), # set height and width height=600, #width=1000, ) ``` -------------------------------- ### Create a Feature from a Transform in Python Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Illustrates how to wrap a transform object (like TrendSlope) within the Feature class. This allows for easier management and combination of features, providing access to the feature's name. ```python trend_slope = Feature(trend_slope_tfs) trend_slope.name ``` -------------------------------- ### Inspect HDF5 File Contents using Python Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Inspects an HDF5 file to list its keys (data paths) and retrieve an integrity summary. This helps in understanding the structure of the saved data and ensuring its validity. ```python from finmlkit.bar.io import H5Inspector h5_info = H5Inspector("BTCUSDT.h5") h5_info.list_keys() ``` ```python h5_info.get_integrity_summary() ``` -------------------------------- ### Compute Final Sample Weights Source: https://github.com/quantscious/finmlkit/blob/main/examples/QuickStartGuide.ipynb Computes the final sample weights by combining average uniqueness, return attribution, class imbalance, and time decay. It utilizes the SampleWeights class from finmlkit.label.kit and requires pre-computed info_weights and labels. ```python from finmlkit.label.kit import SampleWeights sample_weights = SampleWeights().compute_final_weights(info_weights.avg_uniqueness, time_decay_intercept=0.5, return_attribution=info_weights.return_attribution, labels=lbs.labels) print(sample_weights.head()) ```