-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample1.py
126 lines (103 loc) · 4.64 KB
/
example1.py
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
"""
EXAMPLE AUTO TRADER
Do not edit this file directly. Instead, copy it somewhere else in your workspace.
These are simple bots that illustrate the Optibook API and some simple trading concepts. These bots will not make a profit.
This is an example bot that trades a single instrument.
All it does is to randomly insert either a BID or an ASK every 5 seconds.
The price at which it inserts is equal to the opposite side of the order book.
Thus, if the best bid in the order book is currently 90, it will send a sell order for price 90.
If the best ask in the order book is 91, it will send a buy order for price 91.
The order type this bot uses is IOC (immediate or cancel). This means that if the order does not
immediately trade, it will not be visible to others in the order book. Instead, it will be cancelled automatically.
"""
import logging
import time
from typing import List
from optibook import common_types as t
from optibook import ORDER_TYPE_IOC, ORDER_TYPE_LIMIT, SIDE_ASK, SIDE_BID
from optibook.exchange_responses import InsertOrderResponse
from optibook.synchronous_client import Exchange
import random
import json
logging.getLogger("client").setLevel("ERROR")
logger = logging.getLogger(__name__)
INSTRUMENT_ID = "SMALL_CHIPS_NEW_COUNTRY"
def print_report(e: Exchange):
pnl = e.get_pnl()
positions = e.get_positions()
my_trades = e.poll_new_trades(INSTRUMENT_ID)
all_market_trades = e.poll_new_trade_ticks(INSTRUMENT_ID)
logger.info(
f"I have done {len(my_trades)} trade(s) in {INSTRUMENT_ID} since the last report. There have been {len(all_market_trades)} market trade(s) in total in {INSTRUMENT_ID} since the last report."
)
logger.info(f"My PNL is: {pnl:.2f}")
logger.info(f"My current positions are: {json.dumps(positions, indent=3)}")
def print_order_response(order_response: InsertOrderResponse):
if order_response.success:
logger.info(
f"Inserted order successfully, order_id='{order_response.order_id}'"
)
else:
logger.info(
f"Unable to insert order with reason: '{order_response.success}'")
def trade_cycle(e: Exchange):
# this is the main bot logic which gets called every 5 seconds
# fetch the current order book
tradable_instruments = e.get_instruments()
# first we verify that the instrument we wish to trade actually exists
if INSTRUMENT_ID not in tradable_instruments:
logger.info(
f"Unable to trade because instrument '{INSTRUMENT_ID}' does not exist"
)
return
# then we make sure that the instrument is not currently paused
if tradable_instruments[INSTRUMENT_ID].paused:
logger.info(
f"Skipping cycle because instrument '{INSTRUMENT_ID}' is paused, will try again in the next cycle."
)
return
# now we know that we should be able to trade the instrument. We get the latest order book
book = e.get_last_price_book(INSTRUMENT_ID)
# verify that the book exists and that it has at least one bid and ask
if book and book.bids and book.asks:
# now randomly either shoot a BID (buy order) or an ASK (sell order)
if random.random() < 0.5:
response: InsertOrderResponse = e.insert_order(
INSTRUMENT_ID,
price=book.asks[0].price,
volume=1,
side=SIDE_BID,
order_type=ORDER_TYPE_IOC,
)
else:
response: InsertOrderResponse = e.insert_order(
INSTRUMENT_ID,
price=book.bids[0].price,
volume=1,
side=SIDE_ASK,
order_type=ORDER_TYPE_IOC,
)
# now look at whether you successfully inserted an order or not.
# note: If you send invalid information, such as in instrument which does not exist, you will be disconnected
print_order_response(response)
else:
logger.info(
f"No top bid/ask or no book at all for the instrument '{INSTRUMENT_ID}'"
)
print_report(e)
def main():
exchange = Exchange()
exchange.connect()
# you can also define host/user/pass yourself
# when not defined, it is taken from ~/.optibook file if it exists
# if that file does not exists, an error is thrown
# exchange = Exchange(host='host-to-connect-to', info_port=7001, exec_port=8001, username='your-username', password='your-password')
# exchange.connect()
sleep_duration_sec = 5
while True:
trade_cycle(exchange)
logger.info(
f"Iteration complete. Sleeping for {sleep_duration_sec} seconds")
time.sleep(sleep_duration_sec)
if __name__ == "__main__":
main()