### Setup Development Environment Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This make command executes the setup target defined in the project's Makefile, typically used to install development dependencies and configure the environment. ```Shell make setup ``` -------------------------------- ### Setup Environment and Run Tests Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This make command executes the 'all' target, which usually performs setup tasks and then runs the project's test suite as part of the development workflow. ```Shell make all ``` -------------------------------- ### Getting Asset Index Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Fetches the asset index information from the API, providing details about available assets. Prints the full response containing asset information. ```python assets = await api.asset_index({"asset_index": 1}) print(assets) ``` -------------------------------- ### Run Example Script Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md These shell commands set the DERIV_TOKEN environment variable with your API token and then execute a specific example script using python3, ensuring the project's root directory is included in the Python path. ```Shell export DERIV_TOKEN=xxxTokenxxx PYTHONPATH=. python3 examples/simple_bot1.py ``` -------------------------------- ### Getting Contract Proposal Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Requests a contract proposal from the API based on specified trading parameters like amount, barrier, contract type, duration, and symbol. Prints the proposal details. ```python proposal = await api.proposal({"proposal": 1, "amount": 100, "barrier": "+0.1", "basis": "payout", "contract_type": "CALL", "currency": "USD", "duration": 60, "duration_unit": "s", "symbol": "R_100" }) print(proposal) ``` -------------------------------- ### Initializing Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Initializes the Deriv API client using a specified application ID. This is the first step before making any API calls. ```python from deriv_api import DerivAPI api = DerivAPI(app_id=app_id) ``` -------------------------------- ### Getting Active Symbols Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Fetches the list of currently active symbols available for trading from the API with full details. Prints the response containing symbol information. ```python active_symbols = await api.active_symbols({"active_symbols": "full"}) print(active_symbols) ``` -------------------------------- ### Install python-deriv-api via pip Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This command installs the python-deriv-api library using pip, the Python package installer. It is the standard way to add the library to your Python environment. ```Shell python3 -m pip install python_deriv_api ``` -------------------------------- ### Getting Transaction Statement Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Retrieves the transaction statement from the API, listing account transactions. Includes description and allows specifying limit and offset for pagination. ```python statement = await api.statement({"statement": 1, "description": 1, "limit": 100, "offset": 25}) print(statement) ``` -------------------------------- ### Getting Account Balance Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Retrieves the current account balance using the API client. This call fetches the latest balance information for the authenticated account and prints the result. ```python account = await api.balance() print(account) ``` -------------------------------- ### Getting Profit Table Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Retrieves the profit table history from the API, showing details of past contracts and their outcomes. Includes description and sorts results in ascending order. ```python profit_table = await api.profit_table({"profit_table": 1, "description": 1, "sort": "ASC"}) print(profit_table) ``` -------------------------------- ### Basic DerivAPI Usage with Endpoint and App ID Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This snippet demonstrates basic usage of the DerivAPI by instantiating it with an endpoint and app ID, establishing a connection internally, sending a 'ping' request, and printing the response. This is a common pattern for quick setup. ```Python api = DerivAPI(endpoint='ws://...', app_id=1234); response = await api.ping({'ping': 1}) print(response) ``` -------------------------------- ### Getting Open Contract Details Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Retrieves the details of an open contract using its contract ID. This provides current information about a contract that has been bought but not yet ended. ```python contract_id = buy.get('buy').get('contract_id') poc = await api.proposal_open_contract( {"proposal_open_contract": 1, "contract_id": contract_id }) print(poc) ``` -------------------------------- ### Buying Contract Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Executes a buy order for a contract using the provided proposal ID and desired price. Prints the confirmation details of the executed buy order. ```python proposal_id = proposal.get('proposal').get('id') buy = await api.buy({"buy": proposal_id, "price": 100}) print(buy) ``` -------------------------------- ### Authenticating with Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Authenticates the API client using a provided API token to gain access to account-specific information and actions. Prints the authorization response. ```python authorize = await api.authorize(api_token) print(authorize) ``` -------------------------------- ### Getting Asset Index from Cache Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Retrieves the asset index information from the API client's local cache instead of making a new API request. Useful for accessing frequently needed static data quickly. ```python assets = await api.cache.asset_index({"asset_index": 1}) print(assets) ``` -------------------------------- ### Run Tests via pytest Command Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This command directly invokes the pytest runner, which will discover and execute tests in the current environment. This assumes pytest is installed and accessible in the PATH. ```Shell pytest ``` -------------------------------- ### Getting Active Symbols from Cache Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Retrieves the list of active symbols from the API client's local cache. Provides quick access to active symbol data without an API call. ```python active_symbols = await api.cache.active_symbols({"active_symbols": "full"}) print(active_symbols) ``` -------------------------------- ### Subscribing to Proposal Stream Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Subscribes to the proposal stream to receive real-time updates for a contract proposal. Uses an RxPy observable to handle incoming data and prints each update. ```python source_proposal: Observable = await api.subscribe({"proposal": 1, "amount": 100, "barrier": "+0.1", "basis": "payout", "contract_type": "CALL", "currency": "USD", "duration": 160, "duration_unit": "s", "symbol": "R_100", "subscribe": 1 }) source_proposal.subscribe(lambda proposal: print(proposal)) ``` -------------------------------- ### Selling Contract Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Executes a sell order for an open contract using its contract ID and a specified price. This closes the contract position. ```python contract_id = buy.get('buy').get('contract_id') sell = await api.sell({"sell": contract_id, "price": 40}) print(sell) ``` -------------------------------- ### Subscribing to Open Contract Stream Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Subscribes to the stream for a specific open contract to receive real-time updates on its status and price changes. Uses an RxPy observable to process updates. ```python source_poc: Observable = await api.subscribe({"proposal_open_contract": 1, "contract_id": contract_id}) source_poc.subscribe(lambda poc: print(poc) ``` -------------------------------- ### Subscribing to Tick Stream Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Demonstrates subscribing to a real-time tick stream for a specific symbol ('R_50') using the API client. Creates an RxPy sequence object and subscribes a callback to print incoming ticks. ```python # creating a rxpy sequence object to represent deriv api streams source_tick_50 = await api.subscribe({'ticks': 'R_50'}) # subscribe the rxpy sequence with a callback function, # when the data received, the call back function will be called source_tick_50.subscribe(lambda tick: print(tick)) ``` -------------------------------- ### Unsubscribing RxPy Sequence Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Shows how to dispose of an individual RxPy sequence subscription. Disposing all sequence subscriptions for a given API stream will automatically unsubscribe the underlying API stream. ```python seq_sub = source_tick_50.subscribe(lambda tick: print(tick)) seq_sub.dispose() ``` -------------------------------- ### Waiting for Specific API Response Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Demonstrates using `expect_response` to wait for a specific API message type ('authorize') and execute a callback function once the response is received. Uses `asyncio.create_task` to run the waiting function concurrently. ```python async def print_hello_after_authorize(): auth_data = await api.expect_response('authorize') print(f"Hello {auth_data['authorize']['fullname']}") import asyncio asyncio.create_task(print_hello_after_authorize()) api.authorize({'authorize': 'AVALIDTOKEN'}) ``` -------------------------------- ### Handling API Errors Deriv API Python Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Subscribes to the API client's `sanity_errors` observable to print any errors that occur during API communication. This helps in debugging and monitoring API interactions. ```python api.sanity_errors.subscribe(lambda err: print(err)) ``` -------------------------------- ### Unsubscribing Deriv API Stream (Dispose All) Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Explains and demonstrates unsubscribing a Deriv API stream by disposing of all associated RxPy sequence subscriptions. The API stream is automatically unsubscribed when the last sequence subscription is disposed. ```python # creating a rxpy sequence object to represent deriv api streams source_tick_50 = await api.subscribe({'ticks': 'R_50'}) # subscribe the rxpy sequence with a callback function, # when the data received , the call back function will be called seq_sub1 = source_tick_50.subscribe(lambda tick: print(f"get tick from sub1 {tick}")) seq_sub2 = source_tick_50.subscribe(lambda tick: print(f"get tick from sub2 {tick}")) seq_sub1.dispose() seq_sub2.dispose() # When all seq subscriptions of one sequence are disposed. Then a `forget` will be called and that deriv api stream will be unsubscribed ``` -------------------------------- ### Unsubscribing Deriv API Stream (Forget) Source: https://github.com/deriv-com/python-deriv-api/blob/master/docs/usage_examples.md Demonstrates unsubscribing a Deriv API stream directly using the `forget` method with the stream's subscription ID. Requires obtaining the subscription ID from a received data point. ```python # get a datum first from rx import operators as op tick = await source_tick_50.pipe(op.first(), op.to_future) api.forget(tick['R_50']['subscription']['id']) ``` -------------------------------- ### Run Tests via setup.py Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This command uses the project's setup.py file with the pytest command to discover and run the test suite. It's one method for executing tests. ```Shell python setup.py pytest ``` -------------------------------- ### Generate and Publish Documentation Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This make command executes the 'gh-pages' target, which typically generates HTML documentation for the project and publishes it to the gh-pages branch for hosting. ```Shell make gh-pages ``` -------------------------------- ### Instantiate DerivAPI with Endpoint and App ID Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This code shows how to create an instance of DerivAPI by providing the websocket endpoint URL and your application ID. The library will handle establishing the websocket connection internally. ```Python api = DerivAPI(endpoint='ws://...', app_id=1234); ``` -------------------------------- ### Run Tests via Makefile Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This make command executes the 'test' target defined in the project's Makefile, providing a convenient shortcut to run the test suite. ```Shell make test ``` -------------------------------- ### Clone and Navigate to Repository Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md These shell commands are used to clone the python-deriv-api repository from GitHub and change the current directory into the newly cloned repository for development purposes. ```Shell git clone https://github.com/deriv-com/python-deriv-api cd python-deriv-api ``` -------------------------------- ### Instantiate DerivAPI with Existing Connection Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This snippet illustrates how to instantiate DerivAPI using a pre-existing websocket connection object. This gives the user more control over the connection lifecycle, but requires manual handling of reconnections. ```Python connection = await websockets.connect('ws://...') api = DerivAPI(connection=connection) ``` -------------------------------- ### Build Package Distribution Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This make command executes the 'build' target, which is used to create distribution packages (like wheels or source distributions) for the library. ```Shell make build ``` -------------------------------- ### Import DerivAPI Class Source: https://github.com/deriv-com/python-deriv-api/blob/master/README.md This Python statement imports the main DerivAPI class from the deriv_api module, making it available for use in your script. ```Python from deriv_api import DerivAPI ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.