-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathfutures_examples.py
More file actions
204 lines (182 loc) · 6.53 KB
/
futures_examples.py
File metadata and controls
204 lines (182 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# !/usr/bin/env python3
# -*- mode: python; coding: utf-8 -*-
#
# Copyright (C) 2023 Benjamin Thomas Schwertfeger
# All rights reserved.
# https://github.com/btschwertfeger
#
"""
Module that implements *some* examples for the Kraken Futures REST clients
usage.
"""
import logging
import os
import time
from pathlib import Path
from kraken.futures import Funding, Market, Trade, User
logging.basicConfig(
format="%(asctime)s %(module)s,line: %(lineno)d %(levelname)8s | %(message)s",
datefmt="%Y/%m/%d %H:%M:%S",
level=logging.INFO,
)
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
key = os.getenv("FUTURES_SANDBOX_KEY")
secret = os.getenv("FUTURES_SANDBOX_SECRET")
def market_examples() -> None:
"""Example Futures Market client usage"""
# Usage of the Market client to access public endpoints:
market = Market()
print(market.get_tick_types())
print(market.get_tradeable_products(tick_type="trade"))
print(market.get_resolutions(tick_type="trade", tradeable="PI_XBTUSD"))
print(
market.get_ohlc(
tick_type="trade",
symbol="PI_XBTUSD",
resolution="5m",
from_="1668989233",
),
)
print(market.get_fee_schedules())
print(
market.get_orderbook(symbol="fi_xbtusd_180615"),
) # might need adjustment of the symbol
print(market.get_tickers())
print(market.get_instruments())
print(market.get_instruments_status())
print(market.get_instruments_status(instrument="PI_XBTUSD"))
print(market.get_trade_history(symbol="PI_XBTUSD"))
print(market.get_historical_funding_rates(symbol="PI_XBTUSD"))
time.sleep(2) # Just to avoid rate limits
# Usage of the Market client to access private endpoints:
# (commented out to avoid accidental usage)
priv_market = Market(key=key, secret=secret, sandbox=True)
# print(priv_market.get_fee_schedules_vol())
print(priv_market.get_leverage_preference())
# print(priv_market.set_leverage_preference(symbol='PF_XBTUSD', maxLeverage=2)) # set max leverage
# print(priv_market.set_leverage_preference(symbol='PF_XBTUSD')) # reset max leverage
# print(priv_market.set_pnl_preference(symbol='PF_XBTUSD', pnlPreference='BTC'))
# time.sleep(2)
# print(priv_market.get_execution_events())
# print(market.get_public_execution_events(tradeable='PI_XBTUSD'))
# print(market.get_public_order_events(tradeable='PI_XBTUSD'))
# print(market.get_public_mark_price_events(tradeable='PI_XBTUSD'))
# print(priv_market.get_order_events())
# print(priv_market.get_trigger_events())
def user_examples() -> None:
"""Example Futures User client usage"""
# NOTE: This only works if you have set valid credentials for the the
# Futures demo environment. Remove the `sandbox=True` argument to use
# the production environment.
#
# Usage of the User client to access private endpoints:
user = User(key=key, secret=secret, sandbox=True)
print(user.get_wallets())
print(user.get_subaccounts())
print(user.get_unwind_queue())
print(user.get_notifications())
print(user.get_open_positions())
print(user.get_open_orders())
# You can retrieve the account log like so:
print(user.get_account_log(before="1604937694000"))
print(user.get_account_log(info="futures liquidation"))
time.sleep(2) # Just to avoid rate limits
response = user.get_account_log_csv()
assert response.status_code in {200, "200"}
with Path("account_log.csv").open("wb") as file:
for chunk in response.iter_content(chunk_size=512):
if chunk:
file.write(chunk)
def trade_examples() -> None:
"""Example Futures Trade client usage"""
print(
"Attention: Please check if you want to execute the trade endpoints!"
" Check the script manually before running this example.",
)
return
# return
# NOTE: This only works if you have set valid credentials for the the
# Futures demo environment. Remove the `sandbox=True` argument to use
# the production environment.
trade = Trade(key=key, secret=secret, sandbox=True)
print(trade.get_fills())
print(trade.get_fills(lastFillTime="2020-07-21T12:41:52.790Z"))
print(
trade.create_batch_order(
batchorder_list=[
{
"order": "send",
"order_tag": "1",
"orderType": "lmt",
"symbol": "PI_XBTUSD",
"side": "buy",
"size": 1,
"limitPrice": 1.00,
},
{
"order": "send",
"order_tag": "2",
"orderType": "stp",
"symbol": "PI_XBTUSD",
"side": "buy",
"size": 1,
"limitPrice": 2.00,
"stopPrice": 3.00,
},
{
"order": "cancel",
"order_id": "e35d61dd-8a30-4d5f-a574-b5593ef0c050",
},
{
"order": "cancel",
"cliOrdId": 123456789,
},
],
),
)
print(trade.cancel_all_orders())
print(trade.cancel_all_orders(symbol="pi_xbtusd"))
print(trade.dead_mans_switch(timeout=60))
print(trade.dead_mans_switch(timeout=0)) # to deactivate
print(trade.cancel_order(order_id="some order id"))
print(
trade.edit_order(
orderId="some order id",
size=300,
limitPrice=401,
stopPrice=350,
),
)
print(trade.get_orders_status(orderIds=["orderid1", "orderid2"]))
print(
trade.create_order(
orderType="lmt",
side="buy",
size=1,
limitPrice=4,
symbol="pf_bchusd",
),
)
print(
trade.create_order(
orderType="take_profit",
side="buy",
size=1,
symbol="pf_bchusd",
stopPrice=100,
triggerSignal="mark",
),
)
def funding_examples() -> None:
"""Example Funding client usage"""
funding = Funding(key=key, secret=secret, sandbox=True)
print(funding.get_historical_funding_rates(symbol="PF_SOLUSD"))
def main() -> None:
"""Uncomment the examples you want to run:"""
# user_examples()
# market_examples()
# trade_examples()
# funding_examples()
if __name__ == "__main__":
main()