vadiminshakov / exmo

Golang EXMO client API lib

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Golang lib for EXMO.com (exmo.me) cryptocurrency exchange

Usage
Testing
API


Usage

Import package into your code:

import "github.com/vadiminshakov/exmo"

Call fabric function for api instance:

var api = exmo.Api(key, secret)

(you can find key and secret in your profile settings)

Now you can use api features, for example:

package main
    import (
        "github.com/vadiminshakov/exmo" 
        ...
        )   
  func main(){
    var api = exmo.Api("K-92fds9df9ew0sfg9df9sf", "S-293r9dfsjvnef3n31lmr")
    
    // Getting information about user's account
    resultUserInfo, err := api.GetUserInfo()
    	if err != nil {
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		for key, value := range resultUserInfo {
    			if key == "balances" {
    				fmt.Println("\n-- balances:")
    				for k, v := range value.(map[string]interface{}) {
    					fmt.Println(k, v)
    				}
    			}
    			if key == "reserved" {
    				fmt.Println("\n-- reserved:")
    				for k, v := range value.(map[string]interface{}) {
    					fmt.Println(k, v)
    				}
    			}
    		}
    	}
    
    // Buy BTC for RUB
    order, err := api.Buy("BTC_RUB", "0.001", "50096")
    	if err != nil {
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		fmt.Println("Creating order...")
    		for key, value := range order {
    			if key == "result" && value != true {
    				fmt.Println("\nError")
    			}
    			if key == "error" && value != "" {
    				fmt.Println(value)
    			}
    			if key == "order_id" && value != nil {
    				fmt.Printf("Order id: %d\n", int(value.(float64)))
    				val := strconv.Itoa(int(value.(float64)))
    				orderId = val
    				fmt.Printf("Order id: %s\n", orderId)
    			}
    		}
    	}
  }

Testing



Set environment variables:

export EXMO_PUBLIC="your public key"
export EXMO_SECRET="your secret key"

Test specific method:

go test -run <method name>

Example:

go test -run GetPairSettings

Or run all tests:

(ATTENTION! Some test tasks will create buy and sell orders using your account)

go test

API


You can check the official EXMO API for details


GetTrades(arg string)

List of the deals on currency pairs

arg - one or various currency pairs separated by commas (example: BTC_USD,BTC_EUR)

resultTrades, err := api.GetTrades("BTC_RUB")
    	if err != nil {
    		fmt.Errorf("api error: %s\n", err)
    	} else {
    		for _, v := range resultTrades {
    			for k, val := range v.([]interface{}) {
    				tmpindex := 0
    				for key, value := range val.(map[string]interface{}) {
    					if tmpindex != k {
    						fmt.Printf("\n\nindex: %d \n", k)
    						tmpindex = k
    					}
    					if key == "trade_id" {
    						fmt.Println(key, big.NewFloat(value.(float64)).String())
    					} else if key == "date" {
    						fmt.Println(key, time.Unix(int64(value.(float64)), 0))
    					} else {
    						fmt.Println(key, value)
    					}
    				}
    			}
    		}
    	}

GetOrderBook(pair string, limit int)

The book of current orders on the currency pair

pair - one or various currency pairs separated by commas (example: BTC_USD,BTC_EUR)

limit - the number of returned deals (default: 100, мmaximum: 10 000)

    resultBook, err := api.GetOrderBook("BTC_RUB", 200)
    	if err != nil {
    		fmt.Errorf("api error: %s\n", err)
    	} else {
    		for _, v := range resultBook {
    			for key, value := range v.(map[string]interface{}) {
    				if key == "bid" || key == "ask" {
    					for _, val := range value.([]interface{}) {
    						fmt.Printf("%s: ", key)
    						for index, valnested := range val.([]interface{}) {
    							switch index {
    							case 0:
    								fmt.Printf("price %s, ", valnested.(string))
    
    							case 1:
    								fmt.Printf("quantity %s, ", valnested.(string))
    							case 2:
    								fmt.Printf("total %s \n", valnested.(string))
    							}
    						}
    					}
    				} else {
    					fmt.Println(key, value)
    				}
    			}
    
    		}
    	}

Ticker()

Statistics on prices and volume of trades by currency pairs

    ticker, err := api.Ticker()
    	if err != nil {
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		for pair, pairvalue := range ticker {
    			fmt.Printf("\n\n%s:\n", pair)
    			for key, value := range pairvalue.(map[string]interface{}) {
    				fmt.Println(key, value)
    			}
    		}
    	}

GetPairSettings()

Currency pairs settings

    resultPairSettings, err := api.GetPairSettings()
    	if err != nil {
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		for pair, pairvalue := range resultPairSettings {
    			fmt.Printf("\n\n%s:\n", pair)
    			for key, value := range pairvalue.(map[string]interface{}) {
    				fmt.Println(key, value)
    			}
    		}
    	}

GetCurrency()

Currencies list

    resultCurrency, err := api.GetCurrency()
    	if err != nil {
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		fmt.Println("\nCurrencies:")
    		for _, pair := range resultCurrency {
    			fmt.Println(pair)
    		}
    	}

GetUserInfo()

Getting information about user's account

    resultUserInfo, err := api.GetUserInfo()
    	if err != nil {
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		for key, value := range resultUserInfo {
    			if key == "balances" {
    				fmt.Println("\n-- balances:")
    				for k, v := range value.(map[string]interface{}) {
    					fmt.Println(k, v)
    				}
    			}
    			if key == "reserved" {
    				fmt.Println("\n-- reserved:")
    				for k, v := range value.(map[string]interface{}) {
    					fmt.Println(k, v)
    				}
    			}
    		}
    
    	}

GetUserTrades(pair string)

Getting the list of user’s deals

pair - one or various currency pairs separated by commas (example: BTC_USD,BTC_EUR) limit - limit the number of displayed positions (default: 100, max: 1000) offset - last deal offset (default: 0)

    usertrades, err := api.GetUserTrades("BTC_RUB")
    	if err != nil {s
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		fmt.Println("User trades")
    		for pair, val := range usertrades {
    			fmt.Printf("\n\n %s", pair)
    			for _, interfacevalue := range val.([]interface{}) {
    				fmt.Printf("\n\n***\n")
    				for k, v := range interfacevalue.(map[string]interface{}) {
    					fmt.Println(k, v)
    				}
    			}
    		}
    	}

Buy(pair string, quantity string, price string)

Creation of an order to buy the currency

pair - currency pair

quantity - quantity for the order

price - price for the order

    order, err := api.Buy("BTC_RUB", "0.001", "50096")
    	if err != nil {
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		fmt.Println("Creating order...")
    		for key, value := range order {
    			if key == "result" && value != true {
    				fmt.Println("\nError")
    			}
    			if key == "error" && value != "" {
    				fmt.Println(value)
    			}
    			if key == "order_id" && value != nil {
    				fmt.Printf("Order id: %d\n", int(value.(float64)))
    				val := strconv.Itoa(int(value.(float64)))
    				orderId = val
    				fmt.Printf("Order id: %s\n", orderId)
    			}
    		}
    	}

MarketBuy(pair string, quantity string)

Creation of an order to buy the currency at a market price

pair - currency pair

quantity - quantity for the order

    marketOrder, err := api.MarketBuy("BTC_RUB", "0.001")
    	if err != nil {
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		fmt.Println("Creating order...")
    		for key, value := range marketOrder {
    			if key == "result" && value != true {
    				fmt.Println("\nError")
    			}
    			if key == "error" && value != "" {
    				fmt.Println(value)
    			}
    			if key == "order_id" && value != nil {
    				val := strconv.Itoa(int(value.(float64)))
    				orderId = val
    				fmt.Printf("Order id: %s", orderId)
    			}
    		}
    	}

Sell(pair string, quantity string, price string)

Creation of an order to sell the currency

pair - currency pair

quantity - quantity for the order

price - price for the order

    orderSell, err := api.Sell("BTC_RUB", "0.001", "800000")
    	if err != nil {
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		fmt.Println("Creating order...")
    		for key, value := range orderSell {
    			if key == "result" && value != true {
    				fmt.Println("\nError")
    			}
    			if key == "error" && value != "" {
    				fmt.Println(value)
    			}
    			if key == "order_id" && value != nil {
    				val := strconv.Itoa(int(value.(float64)))
    				orderId = val
    				fmt.Printf("Order id: %f", orderId)
    			}
    		}
    	}

MarketSell(pair string, quantity string)

Creation of an order to sell the currency at a market price

pair - currency pair

quantity - quantity for the order

    orderSellMarket, err := api.MarketSell("BTC_RUB", "0.0005")
    	if err != nil {
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		fmt.Println("Creating order...")
    		for key, value := range orderSellMarket {
    			if key == "result" && value != true {
    				fmt.Println("\nError")
    			}
    			if key == "error" && value != "" {
    				fmt.Println(value)
    			}
    			if key == "order_id" && value != nil {
    				val := strconv.Itoa(int(value.(float64)))
    				orderId = val
    				fmt.Printf("Order id: %s", orderId)
    			}
    		}
    	}

OrderCancel(orderId string)

Cancels the open order

orderId - id of the order to cancel

    orderCancel, err := api.OrderCancel(orderId)
    	if err != nil {
    		fmt.Printf("api error: %s\n", err)
    	} else {
    		fmt.Printf("\nCancel order %s \n", orderId)
    		for key, value := range orderCancel {
    			if key == "result" && value != true {
    				fmt.Println("\nError")
    			}
    			if key == "error" && value != "" {
    				fmt.Println(value)
    			}
    		}
    	}

GetUserOpenOrders()

Getting the list of user’s active orders

    resultUserOpenOrders, err := api.GetUserOpenOrders()
    	if err != nil {
    		fmt.Errorf("api error: %s\n", err)
    	} else {
    		for _, v := range resultUserOpenOrders {
    			for _, val := range v.([]interface{}) {
    				for key, value := range val.(map[string]interface{}) {
    					fmt.Println(key, value)
    				}
    			}
    		}
    	}

GetUserCancelledOrders(offset uint, limit uint)

Getting the list of user’s cancelled orders

offset - last deal offset (default: 0)

limit - the number of returned deals (default: 100, мmaximum: 10 000)

    resultUserCancelledOrders, err := api.GetUserCancelledOrders(0, 100)
    	if err != nil {
    		fmt.Errorf("api error: %s\n", err)
    	} else {
    		for _, v := range resultUserCancelledOrders {
    			for key, val := range v.(map[string]interface{}) {
    				if key == "pair" {
    					fmt.Printf("\n%s\n", val)
    				} else {
    					fmt.Println(key, val)
    				}
    			}
    		}
    	}

GetOrderTrades(orderId string)

Getting the history of deals with the order

orderId - order identifier

    resultOrderTrades, err := api.GetOrderTrades(orderId)
    	if err != nil {
    		fmt.Errorf("api error: %s\n", err)
    	} else {
    		for k, v := range resultOrderTrades {
    			fmt.Println(k, v)
    		}
    	}

GetRequiredAmount(pair string, quantity string)

Calculating the sum of buying a certain amount of currency for the particular currency pair

pair - currency pair

quantity - quantity to buy

    resultRequiredAmount, err := api.GetRequiredAmount("BTC_RUB", "0.01")
    	if err != nil {
    		fmt.Errorf("api error: %s\n", err)
    	} else {
    		for k, v := range resultRequiredAmount {
    			fmt.Println(k, v)
    		}
    	}

GetDepositAddress()

Getting the list of addresses for cryptocurrency deposit

    resultDepositAddress, err := api.GetDepositAddress()
    	if err != nil {
    		fmt.Errorf("api error: %s\n", err)
    	} else {
    		for k, v := range resultDepositAddress {
    			fmt.Println(k, v)
    		}
    	}

GetWalletHistory(date time.Time)

Get history of wallet

date - timestamp of the day (if empty got current day)

    date := time.Date(2019, 10, 4, 0, 0, 0, 0, time.UTC)
    	subdate := 10*time.Hour
    
    	resultWalletHistory, err := api.GetWalletHistory(date.Truncate(subdate))
    
    	if err != nil {
    		fmt.Errorf("api error: %s\n", err)
    	} else {
    		for k, v := range resultWalletHistory {
    			if k == "history" {
    				fmt.Println(k, v)
    				for key, val := range v.([]interface{}) {
    					fmt.Println(key, val)
    				}
    			}
    		}
    	}

About

Golang EXMO client API lib

License:Apache License 2.0


Languages

Language:Go 100.0%