Skip to content

Repository files navigation

Deriv Api Client

TestscodecovSchema UpdatedGo Report CardGo ReferenceLicense: MIT

This is unofficial library for working with Deriv API

DerivAPI is a Go package that provides a client for the Deriv API. It allows you to connect to the Deriv API and make requests to retrieve data and perform actions on your or your client's Deriv account.

Installation

To use DerivAPI in your Go project, you can install it using go get:

go get github.com/ksysoev/deriv-api

Usage

Tick stream

Here's an example of how to use DerivAPI to connect to the Deriv API and subscribe on market data updates:

import (
"context""fmt""log""time""github.com/ksysoev/deriv-api""github.com/ksysoev/deriv-api/schema"
)
api, err:=deriv.NewDerivAPI("wss://ws.binaryws.com/websockets/v3", 1, "en", "http://example.com/")
iferr!=nil {
log.Fatal(err)
}
deferapi.Disconnect()
ctx, cancel:=context.WithTimeout(context.Background(), 30*time.Second)
defercancel()
resp, sub, err:=api.SubscribeTicks(ctx, schema.Ticks{Ticks: "R_50"})
iferr!=nil {
log.Fatal(err)
return
}
fmt.Println("Symbol: ", *resp.Tick.Symbol, "Quote: ", *resp.Tick.Quote)
fortick:=rangesub.Stream {
fmt.Println("Symbol: ", *tick.Tick.Symbol, "Quote: ", *tick.Tick.Quote)
}

Buying a contract

Here's another example of how to use DerivAPI to buy contract and listen for updates for this contract.

import (
"context""log""time""github.com/ksysoev/deriv-api""github.com/ksysoev/deriv-api/schema"
)
apiToken:="YOU_API_TOKEN_HERE"api, err:=deriv.NewDerivAPI("wss://ws.binaryws.com/websockets/v3", 1, "en", "https://www.binary.com")
iferr!=nil {
log.Fatal(err)
}
deferapi.Disconnect()
ctx, cancel:=context.WithTimeout(context.Background(), 30*time.Second)
defercancel()
// First, we need to authorize the connectionreqAuth:= schema.Authorize{Authorize: apiToken}
_, err=api.Authorize(ctx, reqAuth)
iferr!=nil {
log.Fatal(err)
}
amount:=100.0barrier:="+0.001"duration:=5basis:=schema.ProposalBasisPayoutreqProp:= schema.Proposal{
Proposal: 1,
Amount: &amount,
Barrier: &barrier,
Basis: &basis,
ContractType: deriv.ProposalContractTypeCALL,
Currency: "USD",
Duration: &duration,
DurationUnit: deriv.ProposalDurationUnitT,
Symbol: "R_50",
}
// Send a proposal requestproposal, err:=api.Proposal(ctx, reqProp)
iferr!=nil {
log.Fatal(err)
}
buyReq:= schema.Buy{
Buy: proposal.Proposal.Id,
Price: 100.0,
}
_, buySub, err:=api.SubscribeBuy(context.Background(), buyReq)
iferr!=nil {
log.Fatal(err)
}
forproposalOpenContract:=rangebuySub.Stream {
log.Println("Current Spot: ", *proposalOpenContract.ProposalOpenContract.CurrentSpot)
if*proposalOpenContract.ProposalOpenContract.IsSold==1 {
log.Println("Contract Result: ", proposalOpenContract.ProposalOpenContract.Status.Value)
buySub.Forget()
break
}
}

About

Golang Deriv API client

Topics

Resources

Stars

20 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages