• Stars
    star
    103
  • Rank 326,708 (Top 7 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python driver for MarketStore

pymarketstore

Python driver for MarketStore

Build Status: build status

Pymarketstore can query and write financial timeseries data from MarketStore

Tested with 3.3+

How to install

$ pip install pymarketstore

Examples

In [1]: import pymarketstore as pymkts

## query data

In [2]: param = pymkts.Params('BTC', '1Min', 'OHLCV', limit=10)

In [3]: cli = pymkts.Client()

In [4]: reply = cli.query(param)

In [5]: reply.first().df()
Out[5]:
                               Open      High       Low     Close     Volume
Epoch
2018-01-17 17:19:00+00:00  10400.00  10400.25  10315.00  10337.25   7.772154
2018-01-17 17:20:00+00:00  10328.22  10359.00  10328.22  10337.00  14.206040
2018-01-17 17:21:00+00:00  10337.01  10337.01  10180.01  10192.15   7.906481
2018-01-17 17:22:00+00:00  10199.99  10200.00  10129.88  10160.08  28.119562
2018-01-17 17:23:00+00:00  10140.01  10161.00  10115.00  10115.01  11.283704
2018-01-17 17:24:00+00:00  10115.00  10194.99  10102.35  10194.99  10.617131
2018-01-17 17:25:00+00:00  10194.99  10240.00  10194.98  10220.00   8.586766
2018-01-17 17:26:00+00:00  10210.02  10210.02  10101.00  10138.00   6.616969
2018-01-17 17:27:00+00:00  10137.99  10138.00  10108.76  10124.94   9.962978
2018-01-17 17:28:00+00:00  10124.95  10142.39  10124.94  10142.39   2.262249

## write data

In [7]: import numpy as np

In [8]: import pandas as pd

In [9]: data = np.array([(pd.Timestamp('2017-01-01 00:00').value / 10**9, 10.0)], dtype=[('Epoch', 'i8'), ('Ask', 'f4')])

In [10]: cli.write(data, 'TEST/1Min/Tick')
Out[10]: {'responses': None}

In [11]: cli.query(pymkts.Params('TEST', '1Min', 'Tick')).first().df()
Out[11]:
                            Ask
Epoch
2017-01-01 00:00:00+00:00  10.0

Client

pymkts.Client(endpoint='http://localhost:5993/rpc')

Construct a client object with endpoint.

Query

pymkts.Client#query(symbols, timeframe, attrgroup, start=None, end=None, limit=None, limit_from_start=False)

You can build parameters using pymkts.Params.

  • symbols: string for a single symbol or a list of symbol string for multi-symbol query
  • timeframe: timeframe string
  • attrgroup: attribute group string. symbols, timeframe and attrgroup compose a bucket key to query in the server
  • start: unix epoch second (int), datetime object or timestamp string. The result will include only data timestamped equal to or after this time.
  • end: unix epoch second (int), datetime object or timestamp string. The result will include only data timestamped equal to or before this time.
  • limit: the number of records to be returned, counting from either start or end boundary.
  • limit_from_start: boolean to indicate limit is from the start boundary. Defaults to False.

Pass one or multiple instances of Params to Client.query(). It will return QueryReply object which holds internal numpy array data returned from the server.

Write

pymkts.Client#write(data, tbk)

You can write a numpy array to the server via Client.write() method. The data parameter must be numpy's recarray type with a column named Epoch in int64 type at the first column. tbk is the bucket key of the data records.

List Symbols

pymkts.Client#list_symbols()

The list of all symbols stored in the server are returned.

Server version

pymkts.Client#server_version()

Returns a string of Marketstore-Version header from a server response.

Streaming

If the server supports WebSocket streaming, you can connect to it using pymkts.StreamConn class. For convenience, you can call pymkts.Client#stream() to obtain the instance with the same server information as REST client.

Once you have this instance, you will set up some event handles by either register() method or @on() decorator. These methods accept regular expressions to filter which stream to act on.

To actually connect and start receiving the messages from the server, you will call run() with the stream names. By default, it subscribes to all by */*/*.

pymkts.Client#stream()

Return a StreamConn which is a websocket connection to the server.

pymkts.StreamConn#(endpoint)

Create a connection instance to the endpoint server. The endpoint string is a full URL with "ws" or "wss" scheme with the port and path.

pymkts.StreamConn#register(stream_path, func) @pymkts.StreamConn#on(stream_path)

Add a new message handler to the connection. The function will be called with handler(StreamConn, {"key": "...", "data": {...,}}) if the key (time bucket key) matches with the stream_path regular expression. The on method is a decorator version of register.

pymkts.StreamConn#run([stream1, stream2, ...])

Start communication with the server and go into an indefinite loop. It does not return until unhandled exception is raised, in which case the connection is closed so you need to implement retry. Also, since this is a blocking method, you may need to run it in a background thread.

An example code is as follows.

import pymarketstore as pymkts

conn = pymkts.StreamConn('ws://localhost:5993/ws')

@conn.on(r'^BTC/')
def on_btc(conn, msg):
    print('received btc', msg['data'])

conn.run(['BTC/*/*'])  # runs until exception

-> received btc {'Open': 4370.0, 'High': 4372.93, 'Low': 4370.0, 'Close': 4371.74, 'Volume': 3.3880948699999993, 'Epoch': 1507299600}

More Repositories

1

marketstore

DataFrame Server for Financial Timeseries Data
Go
1,814
star
2

alpaca-trade-api-python

Python client for Alpaca's trade API
Python
1,552
star
3

pylivetrader

Python live trade execution library with zipline interface.
Python
622
star
4

example-scalping

A working example algorithm for scalping strategy trading multiple stocks concurrently using python asyncio
Python
619
star
5

example-hftish

Example Order Book Imbalance Algorithm
Python
615
star
6

Momentum-Trading-Example

An example algorithm for a momentum-based day trading strategy.
Python
568
star
7

alpaca-backtrader-api

Alpaca Trading API integrated with backtrader
Python
524
star
8

alpaca-trade-api-js

Node.js library for Alpaca Trade API.
JavaScript
428
star
9

alpaca-trade-api-go

Go client for Alpaca's trade API
Go
253
star
10

alpaca-py

The Official Python SDK for Alpaca API
Python
249
star
11

alpaca-trade-api-csharp

C# SDK for Alpaca Trade API https://docs.alpaca.markets/
C#
208
star
12

pipeline-live

Pipeline Extension for Live Trading
Python
198
star
13

alpaca-ts

A TypeScript Node.js library for the https://alpaca.markets REST API and WebSocket streams.
TypeScript
144
star
14

Alpaca-API

The Alpaca API is a developer interface for trading operations and market data reception through the Alpaca platform.
136
star
15

user-docs

Documentation for the Alpaca platform.
HTML
72
star
16

samplealgo01

Sample algo
Python
69
star
17

slait

A time-series data cache
Go
47
star
18

roboadvisor

Python
43
star
19

example-portfolio-manager

Simple portfolio management script in python
Python
42
star
20

insomnia-workspace

An Insomnia Workspace for Alpaca API
34
star
21

ribbit-backend

Backend for Ribbit, Broker API Reference App
Go
33
star
22

blogmaterials

Jupyter Notebook
30
star
23

alpaca-postman

Postman collections for Alpaca's APIs
29
star
24

alpaca-zipline

Alpaca riding on a zipline
Python
28
star
25

ribbit-ios

iOS app for Ribbit, Broker API Reference App
Swift
28
star
26

plug-and-play-strategies

Jupyter Notebook
27
star
27

sp100algo

Follow S&P100
Python
16
star
28

alpaca-erasure

Example script to generate Erasure data
Python
15
star
29

slackbot-trader

A Slackbot that can access the Alpaca API
Python
14
star
30

ribbit-android

Android app for Ribbit, Broker API Reference App
Java
12
star
31

alpaca-docs

Developer documentation for Alpaca API's
HTML
10
star
32

broker-fastapi-backend

A reference backend for Alpaca's Broker API utilizing Python & FastAPI
Python
8
star
33

bkdocs

Broker API Documentation
HTML
8
star
34

notebooks

A collection of Jupyter notebooks for getting started with Alpaca
Jupyter Notebook
6
star
35

Alpaca-Flutter-Demo-App

A boilerplate trading application to trade crypto on Alpaca. Made with Flutter and Dart.
C++
5
star
36

alpacadecimal

Similar and compatible with decimal.Decimal, but optimized for Alpaca's data sets.
Go
5
star
37

supertrend_crypto_bot

An Alpaca Live Crypto Trading Bot built with Python!
Python
4
star
38

Triangular-Arbitrage-with-Alpaca-API-s

Python
3
star
39

quantopian-fundamentals-examples

Implementations of algorithms from https://blog.quantopian.com/fundamentals-contest-winners/ using Alpaca's API.
Python
3
star
40

plprofiler

Python
2
star
41

alpaca-discord-bot

Python
1
star
42

go-onfido

Wrapper around the go-onfido-openapi package
1
star
43

alexa-trader-skill

An Alexa skill that can access the Alpaca API
1
star
44

go-onfido-openapi

Golang sdk for onfido. Generated from openapi
Shell
1
star