Code Monkey home page Code Monkey logo

trade-data-collection-service's Introduction

Data Collection Service

The Data Collection Service is a Python application that collects market data from the Binance API and stores it in ClickHouse. The service runs in a Docker container, making it easy to set up and use.

Features:

  • receiving real-time data on all coins from Binance-Futures (candles tf:"1m")
  • automatic validation and filling missed candles in data
  • automatic loading of history
  • automatic removal of duplicates

Requirements

  • Python 3.8 or higher
  • Cryptofeed
  • ClickHouse
  • Docker (if running in a Docker container)

Installation

Getting Started

Configuration

Before using the Data Collection Service, you'll need to set up a configuration file named config.yaml. This file should be placed in the ./app directory of the project. You can use the provided config_sample.yaml as a starting point.

Using Docker

To start the application services, navigate to the root directory of the project in your terminal and run the following command:

docker-compose up

This will start the data collector and data quality check services, as well as the ClickHouse database service. You should see the logs of the services in the terminal.

Accessing the Application

Once the services are up and running, you can access the application at http://localhost:8123. This will open the ClickHouse web interface, where you can query the data that was collected and perform other database-related tasks.

Stopping the Services

To stop the application services, press Ctrl+C in the terminal where you started the services. This will stop and remove the containers, but the data in the data directory will persist.

Customizing the Configuration

You can customize the services by modifying the docker-compose.yaml file. For example, you can change the image names or build contexts, adjust the container volumes, or set environment variables. Please refer to the Docker Compose documentation for more information on the available configuration options.


Sample Code Use

Selecting Last 5000 Candles:

python

import clickhouse_driver

with clickhouse_driver.Client(host='localhost', port=9000) as ch:
    # select last 5000 candles
    query = f'SELECT * FROM binance_data.candles FINAL ORDER BY timestamp DESC LIMIT 5000'
    result = ch.execute(query)

print(result[:2])

output:

[('BINANCE_FUTURES',
  'BTC-USDT-PERP',
  datetime.datetime(2023, 2, 21, 20, 39),
  datetime.datetime(2023, 2, 21, 20, 39, 59),
  1677011968.0,
  '1m',
  1880,
  24490.400390625,
  24509.099609375,
  24509.30078125,
  24482.599609375,
  214.322,
  datetime.datetime(2023, 2, 21, 20, 40),
  datetime.datetime(2023, 2, 21, 20, 40)),
 ('BINANCE_FUTURES',
  'BTC-USDT-PERP',
  datetime.datetime(2023, 2, 21, 20, 40),
  datetime.datetime(2023, 2, 21, 20, 40, 59),
  1677012096.0,
  '1m',
  2062,
  24509.099609375,
  24511.19921875,
  24523.69921875,
  24507.5,
  183.713,
  datetime.datetime(2023, 2, 21, 20, 41),
  datetime.datetime(2023, 2, 21, 20, 41))]

Selecting All Candles of a Certain Coin:

import clickhouse_driver

with clickhouse_driver.Client(host='localhost', port=9000) as ch:
    # select all candles of a certain coin
    symbol = 'BTC-USDT-PERP'
    query = '''SELECT * FROM binance_data.candles FINAL WHERE symbol = %(symbol)s'''
    result = ch.execute(query, {'symbol': symbol,})

Project Structure

The project has the following structure:

data-collection-service/
├── app/
│   ├── config_sample.yaml
│   ├── data_collector.py
│   ├── data_quality_check.py
|   └── load_history.py
├── docker-compose.yaml
└── README.md
  • The app directory contains the configuration file and the Python scripts for the data collector and data quality check services.
  • The docker-compose.yaml file defines the services and their dependencies.

data_collector.py

The data_collector.py script collects candle data from Binance futures and stores it in ClickHouse.

data_quality_check.py

The data_quality_check.py script checks the data quality of the candle data in ClickHouse.

load_history.py

This is a script that collects market data from the Binance exchange and stores it in a database called ClickHouse. The script is also configured using a file named "config.yaml", which contains settings such as the start date for collecting data and whether or not to load historical data.

trade-data-collection-service's People

Contributors

alex-lekov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

alexleks

trade-data-collection-service's Issues

unsupported operand type

Traceback (most recent call last):
candles-data_quality_check-1 | File "/app/./data_quality_check.py", line 212, in
candles-data_quality_check-1 | main()
candles-data_quality_check-1 | File "/app/./data_quality_check.py", line 198, in main
candles-data_quality_check-1 | check_last_data_recording(ch)
candles-data_quality_check-1 | File "/app/./data_quality_check.py", line 94, in check_last_data_recording
candles-data_quality_check-1 | time_diff = datetime.datetime.now() - df.stop.min()
candles-data_quality_check-1 | TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'float'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.