|
| 1 | +# Copyright 2022 Webull |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# coding=utf-8 |
| 16 | + |
| 17 | +from webull.core.client import ApiClient |
| 18 | +from webull.data.data_client import DataClient |
| 19 | + |
| 20 | +optional_api_endpoint = "<api_endpoint>" |
| 21 | +your_app_key = "<your_app_key>" |
| 22 | +your_app_secret = "<your_app_secret>" |
| 23 | +region_id = "<region_id>" |
| 24 | + |
| 25 | +api_client = ApiClient(your_app_key, your_app_secret, region_id) |
| 26 | +api_client.add_endpoint(region_id, optional_api_endpoint) |
| 27 | + |
| 28 | + |
| 29 | +if __name__ == '__main__': |
| 30 | + data_client = DataClient(api_client) |
| 31 | + |
| 32 | + # Get all watchlists |
| 33 | + res = data_client.watchlist.get_watchlist() |
| 34 | + if res.status_code == 200: |
| 35 | + print('get_watchlist:', res.json()) |
| 36 | + |
| 37 | + # Create a new watchlist (max 20 watchlists, shared with retail) |
| 38 | + res = data_client.watchlist.create_watchlist(name="Tech Stocks", sort=1) |
| 39 | + if res.status_code == 200: |
| 40 | + print('create_watchlist:', res.json()) |
| 41 | + watchlist_id = res.json().get("watchlist_id") |
| 42 | + |
| 43 | + # Update the watchlist |
| 44 | + res = data_client.watchlist.update_watchlist( |
| 45 | + watchlist_id=watchlist_id, |
| 46 | + name="My Favorites", |
| 47 | + sort=2 |
| 48 | + ) |
| 49 | + if res.status_code == 200: |
| 50 | + print('update_watchlist:', res.json()) |
| 51 | + |
| 52 | + # Add instruments to the watchlist (max 1000 instruments total) |
| 53 | + instruments = [ |
| 54 | + {"symbol": "AAPL", "category": "US_STOCK", "sort": 1}, |
| 55 | + {"symbol": "TSLA", "category": "US_STOCK", "sort": 2} |
| 56 | + ] |
| 57 | + res = data_client.watchlist.add_instruments(watchlist_id, instruments) |
| 58 | + if res.status_code == 200: |
| 59 | + print('add_instruments:', res.json()) |
| 60 | + |
| 61 | + # Get instruments in the watchlist |
| 62 | + res = data_client.watchlist.get_instruments(watchlist_id) |
| 63 | + if res.status_code == 200: |
| 64 | + print('get_instruments:', res.json()) |
| 65 | + |
| 66 | + # Update instruments sort order |
| 67 | + instruments = [ |
| 68 | + {"symbol": "AAPL", "category": "US_STOCK", "sort": 3}, |
| 69 | + {"symbol": "TSLA", "category": "US_STOCK", "sort": 1} |
| 70 | + ] |
| 71 | + res = data_client.watchlist.update_instruments(watchlist_id, instruments) |
| 72 | + if res.status_code == 200: |
| 73 | + print('update_instruments:', res.json()) |
| 74 | + |
| 75 | + # Remove instruments from the watchlist |
| 76 | + instruments = [ |
| 77 | + {"symbol": "AAPL", "category": "US_STOCK"}, |
| 78 | + {"symbol": "TSLA", "category": "US_STOCK"} |
| 79 | + ] |
| 80 | + res = data_client.watchlist.remove_instruments(watchlist_id, instruments) |
| 81 | + if res.status_code == 200: |
| 82 | + print('remove_instruments:', res.json()) |
| 83 | + |
| 84 | + # Delete the watchlist (irreversible) |
| 85 | + res = data_client.watchlist.delete_watchlist(watchlist_id) |
| 86 | + if res.status_code == 200: |
| 87 | + print('delete_watchlist:', res.json()) |
0 commit comments