|
4 | 4 | <td> |
5 | 5 | <table cellspacing="3" border="0"> |
6 | 6 | <tr> |
7 | | - <td><a href="https://bitvavo.com"><img src="https://bitvavo.com/press/blue/bitvavo-mark-square/bitvavo-mark-square-blue.png" width="100" title="Bitvavo Logo"></td> |
| 7 | + <td><a href="https://bitvavo.com"><img src="./assets/bitvavo-mark-square-blue.svg" width="100" title="Bitvavo Logo"></td> |
8 | 8 | <td><h1>Bitvavo SDK for Python</h1></td> |
9 | 9 | </tr> |
10 | 10 | </table> |
@@ -57,67 +57,91 @@ Want to quickly make a first app? Here we go: |
57 | 57 | import json |
58 | 58 | import time |
59 | 59 | |
60 | | - # Use this class to connect to Bitvavo and make your first calls. |
61 | | - # Add workflows to implement your business logic. |
62 | | - class bitvavo_implementation: |
63 | | - api_key = "<Replace with your your API key from Bitvavo Dashboard>" |
64 | | - api_secret = "<Replace with your API secrete from Bitvavo Dashboard>" |
65 | | - bitvavo_engine = None |
66 | | - bitvavo_socket = None |
67 | | - |
68 | | - # Connect securely to Bitvavo, create the websocket and error callbacks |
69 | | - def __init__(self): |
70 | | - self.bitvavo_engine = Bitvavo({ |
71 | | - 'APIKEY': self.api_key, |
72 | | - 'APISECRET': self.api_secret |
73 | | - }) |
74 | | - self.bitvavo_socket = self.bitvavo_engine.newWebsocket() |
75 | | - self.bitvavo_socket.setErrorCallback(self.error_callback) |
76 | | - |
77 | | - # Handle errors |
78 | | - def error_callback(self, error): |
79 | | - print("Errors:", json.dumps(error, indent=2)) |
80 | | - |
81 | | - # Retrieve the data you need from Bitvavo in order to implement your |
82 | | - # trading logic. Use multiple workflows to return data to your |
83 | | - # callbacks |
84 | | - def a_workflow(self): |
85 | | - self.bitvavo_socket.time(self.a_workflow_callback) |
86 | | - self.bitvavo_socket.markets({}, self.a_workflow_callback) |
87 | | - |
88 | | - # In your app you analyse data returned by the workflow, then make |
89 | | - # calls to Bitvavo to respond to market conditions |
90 | | - def a_workflow_callback(self, response): |
91 | | - print("workflow:", json.dumps(response, indent=2)) |
92 | | - |
93 | | - # Sockets are fast, but asynchronous. Keep the socket open while you are |
94 | | - # trading. |
95 | | - def wait_and_close(self): |
96 | | - limit = self.bitvavo_engine.getRemainingLimit() |
97 | | - try: |
98 | | - while (limit > 0): |
99 | | - time.sleep(0.5) |
100 | | - limit = self.bitvavo_engine.getRemainingLimit() |
101 | | - except KeyboardInterrupt: |
102 | | - self.bitvavo_socket.closeSocket() |
103 | | - |
104 | | - # Shall I re-explain main? Naaaaaaaaaa. |
105 | | - if __name__ == '__main__': |
106 | | - bvavo = bitvavo_implementation() |
107 | | - bvavo.a_workflow() |
108 | | - bvavo.wait_and_close() |
109 | | -
|
| 60 | + |
| 61 | + # Use this class to connect to Bitvavo and make your first calls |
| 62 | + # Add trading strategies to implement your business logic. |
| 63 | + class bitvavo_implementation: |
| 64 | + api_key = "<Replace with your your API key from Bitvavo Dashboard>" |
| 65 | + api_secret = "<Replace with your API secrete from Bitvavo Dashboard>" |
| 66 | + bitvavo_engine = None |
| 67 | + bitvavo_socket = None |
| 68 | + |
| 69 | + # Connect securely to Bitvavo, create the websocket and error callbacks |
| 70 | + def __init__(self): |
| 71 | + self.bitvavo_engine = Bitvavo({ |
| 72 | + 'APIKEY': self.api_key, |
| 73 | + 'APISECRET': self.api_secret |
| 74 | + }) |
| 75 | + self.bitvavo_socket = self.bitvavo_engine.newWebsocket() |
| 76 | + self.bitvavo_socket.setErrorCallback(self.error_callback) |
| 77 | + |
| 78 | + # Handle errors |
| 79 | + def error_callback(self, error): |
| 80 | + print("Errors:", json.dumps(error, indent=2)) |
| 81 | + |
| 82 | + # Retrieve the data you need from Bitvavo in order to implement your |
| 83 | + # Trading logic. Use multiple workflows to return data to your |
| 84 | + # Callbacks |
| 85 | + def a_trading_strategy(self): |
| 86 | + self.bitvavo_socket.ticker24h({}, self.a_trading_strategy_callback) |
| 87 | + # You can also filter the ticker to retrieve specific markets only. |
| 88 | + |
| 89 | + # In your app you analyse data returned by the workflow, then make |
| 90 | + # calls to Bitvavo to respond to market conditions |
| 91 | + def a_trading_strategy_callback(self, response): |
| 92 | + # Iterate through the |
| 93 | + for market in response: |
| 94 | + match market["market"]: |
| 95 | + case "A market": |
| 96 | + print("Check data against your trading strategy. For example, the bid is: ", market["bid"] ) |
| 97 | + # Implement calculations for your trading logic |
| 98 | + # If they are positive, place an order: For example: |
| 99 | + # self.bitvavo_socket.placeOrder("A market", |
| 100 | + # 'buy', |
| 101 | + # 'limit', |
| 102 | + # { 'amount': '1', 'price': '00001' }, |
| 103 | + # self.order_placed_callback) |
| 104 | + case "a different market": |
| 105 | + print("Implement a different strategy for this market") |
| 106 | + |
| 107 | + |
| 108 | + def order_placed_callback(self, response): |
| 109 | + print("Order placed:", json.dumps(response, indent=2)) |
| 110 | + # Add your business logic to handle orders |
| 111 | + |
| 112 | + |
| 113 | + # Sockets are fast, but asynchronous. Keep the socket open while you are |
| 114 | + # trading. |
| 115 | + def wait_and_close(self): |
| 116 | + limit = self.bitvavo_engine.getRemainingLimit() |
| 117 | + try: |
| 118 | + while (limit > 0): |
| 119 | + time.sleep(0.5) |
| 120 | + limit = self.bitvavo_engine.getRemainingLimit() |
| 121 | + except KeyboardInterrupt: |
| 122 | + self.bitvavo_socket.closeSocket() |
| 123 | + |
| 124 | + |
| 125 | + # Shall I re-explain main? Naaaaaaaaaa. |
| 126 | + if __name__ == '__main__': |
| 127 | + bvavo = bitvavo_implementation() |
| 128 | + bvavo.a_trading_strategy() |
| 129 | + bvavo.wait_and_close() |
110 | 130 | ``` |
111 | 131 | 1. **Add security information** |
112 | 132 |
|
113 | 133 | Replace the values of `api_key` and `api_secret` with your credentials from [Bitvavo Dashboard](https://account.bitvavo.com/user/api). |
| 134 | + |
| 135 | + You must supply your security information to trade on Bitvavo and see your account details. You can retrieve public information such as available markets, assets and current market without |
| 136 | + supplying your key and secret. However, Bitvavo returns an error. |
114 | 137 |
|
115 | 138 | 1. **Run your app** |
116 | 139 |
|
117 | 140 | - Command line warriors: `python3 <filename>`. |
118 | 141 | - IDE heroes: press the big green button. |
119 | 142 | |
120 | | -Your app connects to Bitvavo and returns a list of the current market prices. |
| 143 | +Your app connects to Bitvavo and returns a list the latest trade price for each market. The callback cycles through the |
| 144 | +market data so you can implement your trading logic. |
121 | 145 |
|
122 | 146 | # Python Bitvavo Api |
123 | 147 | This is the python wrapper for the Bitvavo API. This project can be used to build your own projects which interact with the Bitvavo platform. Every function available on the API can be called through a REST request or over websockets. For info on the specifics of every parameter consult the [Bitvavo API documentation](https://docs.bitvavo.com/) |
|
0 commit comments