Skip to content

ksysoev/deriv-api

Repository files navigation

Deriv Api Client

Tests codecov Schema Updated Go Report Card Go Reference License: 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/")

if err != nil {
    log.Fatal(err)
}

defer api.Disconnect()

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

resp, sub, err := api.SubscribeTicks(ctx, schema.Ticks{Ticks: "R_50"})

if err != nil {
    log.Fatal(err)
    return
}

fmt.Println("Symbol: ", *resp.Tick.Symbol, "Quote: ", *resp.Tick.Quote)

for tick := range sub.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")

if err != nil {
    log.Fatal(err)
}

defer api.Disconnect()

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// First, we need to authorize the connection
reqAuth := schema.Authorize{Authorize: apiToken}
_, err = api.Authorize(ctx, reqAuth)

if err != nil {
    log.Fatal(err)
}

amount := 100.0
barrier := "+0.001"
duration := 5
basis := schema.ProposalBasisPayout

reqProp := 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 request
proposal, err := api.Proposal(ctx, reqProp)

if err != nil {
    log.Fatal(err)
}

buyReq := schema.Buy{
    Buy:   proposal.Proposal.Id,
    Price: 100.0,
}

_, buySub, err := api.SubscribeBuy(context.Background(), buyReq)

if err != nil {
    log.Fatal(err)
}

for proposalOpenContract := range buySub.Stream {
    log.Println("Current Spot: ", *proposalOpenContract.ProposalOpenContract.CurrentSpot)
    if *proposalOpenContract.ProposalOpenContract.IsSold == 1 {
        log.Println("Contract Result: ", proposalOpenContract.ProposalOpenContract.Status.Value)
        buySub.Forget()
        break
    }
}