Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what is the api variable value in function loop(self, api, request) #6

Closed
pte1601 opened this issue Jul 24, 2020 · 2 comments
Closed

Comments

@pte1601
Copy link

pte1601 commented Jul 24, 2020

Hello James
I am trying to use your deribit ws api to retrieve market data. I am struggling what is the value of the api variable in the loop function? Maybe you have also a quick example to retrieve market data?
Thanks for your assistance. Greetings, Peter

@Jimmy-sha256
Copy link
Owner

Hi the api variable determines whether we are connecting to the public or private endpoint.

The function async def private_api(self, request): returns the variable containing the data for a private api connection and

async def public_api(self, request): returns the variable containing the data for a public api connection

I hope this makes sense, if you bear with me I will put an example together showing how to retrive market data.

@Jimmy-sha256
Copy link
Owner

Jimmy-sha256 commented Jul 28, 2020

A quick example of a single script that will fetch the index prices of test net eth / btc, hope this helps. Let me know if your still struggling or want an example doing for a particular end point.

import asyncio
import websockets
import json

client_id = 'enter_client_id_here'
client_secret = 'enter_client_secret_here'
client_url = 'wss://test.deribit.com/ws/api/v2'

# create a websocket object
class WS_Client(object):
    def __init__(self, client_id=None, client_secret=None):
        self.client_id = client_id
        self.client_secret = client_secret
        self.client_url = client_url
        self.json = {
            "jsonrpc" : "2.0",
            "id" : 1,
            "method" : None,
        }


# send a public method request
    async def public_api(self, request):
        async with websockets.connect(self.client_url) as websocket:
            await websocket.send(request)
            response = await websocket.recv()
            response = json.loads(response)
            return response


# create an asyncio event loop
    def loop(self, api, request):
        response = asyncio.get_event_loop().run_until_complete(
            api(json.dumps(request)))
        return response


# example get index function https://docs.deribit.com/?python#public-get_index

    def index(self, currency):
        options = {"currency" : currency}
        self.json["method"] = "public/get_index"
        self.json["params"] = options
        return self.loop(self.public_api, self.json)

client = WS_Client(client_id, client_secret)

btc_index = client.index('BTC')
eth_index = client.index('ETH')

print(btc_index)
print(eth_index)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants