Skip to content

Commit

Permalink
add oi to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptohazard committed Oct 26, 2022
1 parent fd17fab commit b621be0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 42 deletions.
5 changes: 3 additions & 2 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func printTable(options []rainbow.Option) {
red := color.FgRed.Render
magenta := color.FgMagenta.Render
blue := color.FgCyan.Render
darkBlue := color.FgBlue.Render

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
Expand All @@ -51,7 +52,7 @@ func printTable(options []rainbow.Option) {

t.AppendHeader(table.Row{
"Provider", "Asset", "Type", "Size", green(" Bid"), "Strike", blue("MarketIV"),
red(" Ask"), "Size", "Instrument", magenta("url"),
red(" Ask"), "Size", darkBlue("Open Interest"), "Instrument", magenta("url"),
})

align := api.NewAlign()
Expand All @@ -62,7 +63,7 @@ func printTable(options []rainbow.Option) {
highlight(o.Provider), o.Asset, o.Type,
bestBidQty, green(bestBidPx), math.Round(o.Strike*100) / 100,
blue(math.Round(o.MarketIV*100) / 100),
red(bestAskPx), bestAskQty, o.Name, magenta(o.URL),
red(bestAskPx), bestAskQty, darkBlue(math.Round(o.OpenInterest*100) / 100), o.Name, magenta(o.URL),
}})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/provider/zetamarkets/anchor/anchor.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type Option struct {
ZG *zeta.ZetaGroup
Product *zeta.Product
Greek *zeta.ProductGreeks
expiry uint64
Expiry uint64
// in ZetaGroup the Product & ProductPaddings are by packet of 23 = 11 calls + 11 puts + 1 future.
// To find the corresponding expiry(padding), we need its index in the array, and divide by 23
// the quotient is the index of the expiry of interest
Expand Down Expand Up @@ -172,7 +172,7 @@ func (o Option) Name() string {
const ContractSize = 1000

func (o Option) Expiration() string {
seconds := int64(o.expiry)
seconds := int64(o.Expiry)
expiryTime := time.Unix(seconds, 0).UTC()
return expiryTime.Format("2006-01-02 15:04:05")
}
Expand Down
95 changes: 57 additions & 38 deletions pkg/provider/zetamarkets/zeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ package zetamarkets

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"net/http"
"strconv"
"strings"
"time"

"github.com/davecgh/go-spew/spew"
Expand Down Expand Up @@ -39,8 +44,8 @@ func (p Provider) Options() ([]rainbow.Option, error) {
}
//spew.Dump(m)
//spew.Dump(&m)
oi := OpenInterestMap(m)
spew.Dump(oi)
zoi := OpenInterestMap(m)
//spew.Dump(zoi)

client := rpc.NewClient(anchor.SolanaRPC)

Expand All @@ -67,7 +72,7 @@ func (p Provider) Options() ([]rainbow.Option, error) {
return nil, err
}

options = append(options, rainbow.Option{
o := rainbow.Option{
Name: i.Name(),
Type: i.OptionType(),
Asset: i.Asset(),
Expand All @@ -82,7 +87,9 @@ func (p Provider) Options() ([]rainbow.Option, error) {
Ask: asks,
MarketIV: i.Vol(),
URL: "https://dex.zeta.markets/",
})
}
o.OpenInterest = OpenInterest(&i, zoi)
options = append(options, o)
}

if len(options) == 0 {
Expand Down Expand Up @@ -146,51 +153,63 @@ func normalizeOrders(
return orders, nil
}

func OpenInterestMap(m map[string][]uint64) map[string]ZetaAPI {
func OpenInterestMap(m map[string][]uint64) ZetaOI {
//if error fail with just a log
//let's keep it like that for now because evyrything shouldn't fail if zeta api is down
oi := make(map[string]ZetaAPI)
/*
for asset, expiries := range m {
for _, e := range expiries {
url := ZetaAPIUrl + asset + "?expiry=" + strconv.FormatUint(e, 10)
spew.Dump(url)
resp, err := http.Get(url)
if err != nil {
log.Warnf("zeta open interest GET %s: %w", url, err)
return nil
}
defer resp.Body.Close()
//spew.Dump(resp.Body)
var result struct {
Result []ZetaAPI `json:"result"`
}
if err = gg.DecodeJSONResponse(resp, &result.Result); err != nil {
//return Quote{}, fmt.Errorf("quote from Ox: %w", err)
return nil
}
json.NewDecoder(resp.Body).Decode(&result)
//json.Unmarshal(resp.Body, &result)
spew.Dump(result)
//oi := make(map[string]ZetaAPI)
var oi ZetaOI

for asset, expiries := range m {

for _, e := range expiries {
url := ZetaAPIUrl + asset + "?expiry=" + strconv.FormatUint(e, 10)
spew.Dump(url)
resp, err := http.Get(url)
if err != nil {
log.Warnf("zeta open interest GET %s: %w", url, err)
return nil
}
defer resp.Body.Close()
//spew.Dump(resp.Body)

/*var result struct {
Result []ZetaAPI `json:"result"`
}
if err = gg.DecodeJSONResponse(resp, &result.Result); err != nil {
//return Quote{}, fmt.Errorf("quote from Ox: %w", err)
return nil
}
json.NewDecoder(resp.Body).Decode(&result)
//json.Unmarshal(resp.Body, &result)
spew.Dump(result)*/
data, _ := io.ReadAll(resp.Body)
json.Unmarshal(data, &oi)
//spew.Dump(oi)
}
*/

}
//spew.Dump(oi)

return oi

}

type ZetaAPI struct {
OI ZetaOI
}
type ZetaOI struct {
// type ZetaAPI struct {
// OI ZetaOI
// }
type ZetaOI map[string]struct {
Timestamp int64 `json:"timestamp"`
OpenInterest int `json:"open_interest"`
OpenInterest float64 `json:"open_interest"`
ImpliedVolatility float64 `json:"implied_volatility"`
Delta int `json:"delta"`
Vega int `json:"vega"`
Delta float64 `json:"delta"`
Vega float64 `json:"vega"`
Theo float64 `json:"theo"`
}

func OpenInterest(o *anchor.Option, zoi ZetaOI) float64 {
key := o.Asset() + `#` + strconv.FormatUint(o.Expiry, 10) +
`#` + strings.ToLower(o.OptionType()) + `#` + fmt.Sprintf("%.1f", o.Strike())
//spew.Dump(key)
//spew.Dump(zoi[key])
return zoi[key].OpenInterest
}

0 comments on commit b621be0

Please sign in to comment.