Package deriv_api
python-deriv-api
A python implementation of deriv api library.
Go through api.deriv.com to know simple easy steps on how to register and get access. Use this all-in-one python library to set up and make your app running or you can extend it.
Requirement
Python (3.9.6 or higher is recommended) and pip3
Note: There is bug in 'websockets' package with python 3.9.7, hope that will be fixed in 3.9.8 as mentioned in https://github.com/aaugustin/websockets/issues/1051. Please exclude python 3.9.7.
Installation
python3 -m pip install python_deriv_api
Usage
This is basic deriv-api python library which helps to make websockets connection and deal the API calls (including subscription).
Import the module
from deriv_api import DerivAPI
Access
api = DerivAPI(endpoint='ws://...', app_id=1234);
response = await api.ping({'ping': 1})
print(response)
Creating a websockets connection and API instantiation
You can either create an instance of websockets and pass it as connection or pass the endpoint and app_id to the constructor to create the connection for you.
If you pass the connection it's up to you to reconnect in case the connection drops (cause API doesn't know how to create the same connection).
- Pass the arguments needed to create a connection:
api = DerivAPI(endpoint='ws://...', app_id=1234);
- create and use a previously opened connection:
connection = await websockets.connect('ws://...')
api = DerivAPI(connection=connection)
Documentation
API reference
The complete API reference is hosted here
Examples here
Development
git clone https://github.com/deriv-com/python-deriv-api
cd python-deriv-api
Setup environment
make setup
Setup environment and run test
make all
Run test
python setup.py pytest
or
pytest
or
make test
Generate documentations
Generate html version of the docs and publish it to gh-pages
make gh-pages
Build the package
make build
Run examples
set token and run example
export DERIV_TOKEN=xxxTokenxxx
PYTHONPATH=. python3 examples/simple_bot1.py
Usage examples
Short examples
from deriv_api import DerivAPI
api = DerivAPI(app_id=app_id)
Authenticate to an account using token
authorize = await api.authorize(api_token)
print(authorize)
Get Balance
account = await api.balance()
print(account)
Get all the assets info
assets = await api.asset_index({"asset_index": 1})
print(assets)
To get assets info from cache
assets = await api.cache.asset_index({"asset_index": 1})
print(assets)
Get all active symbols
active_symbols = await api.active_symbols({"active_symbols": "full"})
print(active_symbols)
To get active symbols from cache
active_symbols = await api.cache.active_symbols({"active_symbols": "full"})
print(active_symbols)
Get proposal
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)
subscribe the proposal stream
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))
Buy
proposal_id = proposal.get('proposal').get('id')
buy = await api.buy({"buy": proposal_id, "price": 100})
print(buy)
open contract detail
contract_id = buy.get('buy').get('contract_id')
poc = await api.proposal_open_contract(
{"proposal_open_contract": 1, "contract_id": contract_id })
print(poc)
subscribe the open contract stream
source_poc: Observable = await api.subscribe({"proposal_open_contract": 1, "contract_id": contract_id})
source_poc.subscribe(lambda poc: print(poc)
Sell
contract_id = buy.get('buy').get('contract_id')
sell = await api.sell({"sell": contract_id, "price": 40})
print(sell)
Profit table
profit_table = await api.profit_table({"profit_table": 1, "description": 1, "sort": "ASC"})
print(profit_table)
Transaction statement
statement = await api.statement({"statement": 1, "description": 1, "limit": 100, "offset": 25})
print(statement)
Subscribe a stream
We are using rxpy to maintain our deriv api subscriptions. Please distinguish api subscription from rxpy sequence subscription
# 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))
unsubscribe the rxpy sequence
seq_sub = source_tick_50.subscribe(lambda tick: print(tick))
seq_sub.dispose()
unsubscribe the deriv api stream
There are 2 ways to unsubscribe deriv api stream
- by
dispose
all sequence subscriptions
# 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
- by
forget
that deriv stream
# 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'])
print errors
api.sanity_errors.subscribe(lambda err: print(err))
do something when one type of message coming
async def print_hello_after_authorize():
auth_data = await api.expect_response('authorize')
print(f"Hello {auth_data['authorize']['fullname']}")
asyncio.create_task(print_hello_after_authorize())
api.authorize({'authorize': 'AVALIDTOKEN'})
Sub-modules
deriv_api.cache
deriv_api.deriv_api
deriv_api.deriv_api_calls
deriv_api.in_memory
deriv_api.middlewares
deriv_api.streams_list
deriv_api.subscription_manager