Skip to content

Commit

Permalink
Merge branch 'add-native-denomination'
Browse files Browse the repository at this point in the history
  • Loading branch information
jppade committed Sep 25, 2023
2 parents fa1edc1 + 5f77a7b commit 365c53a
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 128 deletions.
1 change: 1 addition & 0 deletions cmd/http/graphqlServer/schema/quotation.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Query {
StartTime: Time
EndTime: Time
TradeVolumeThreshold: Float
NativeDenomination: Boolean
FeedSelection: [FeedSelection!]
): [FilterPointExtended]

Expand Down
53 changes: 32 additions & 21 deletions internal/pkg/filtersBlockService/FilterMA.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,34 @@ import (
// FilterMA is the struct for a moving average filter implementing
// the Filter interface.
type FilterMA struct {
asset dia.Asset
exchange string
currentTime time.Time
prices []float64
volumes []float64
lastTrade dia.Trade
memory int
value float64
modified bool
filterName string
asset dia.Asset
exchange string
currentTime time.Time
prices []float64
volumes []float64
lastTrade dia.Trade
memory int
value float64
modified bool
filterName string
nativeDenomination bool
//max float64
min float64
}

// NewFilterMA returns a moving average filter.
// @currentTime is the begin time of the filtersBlock.
func NewFilterMA(asset dia.Asset, exchange string, currentTime time.Time, memory int) *FilterMA {
func NewFilterMA(asset dia.Asset, exchange string, currentTime time.Time, memory int, nativeDenomination bool) *FilterMA {
filter := &FilterMA{
asset: asset,
exchange: exchange,
prices: []float64{},
volumes: []float64{},
currentTime: currentTime,
memory: memory,
filterName: "MA" + strconv.Itoa(memory),
min: -1,
asset: asset,
exchange: exchange,
prices: []float64{},
volumes: []float64{},
currentTime: currentTime,
memory: memory,
filterName: "MA" + strconv.Itoa(memory),
min: -1,
nativeDenomination: nativeDenomination,
}
return filter
}
Expand Down Expand Up @@ -88,7 +90,12 @@ func (filter *FilterMA) processDataPoint(trade dia.Trade) {
filter.prices = filter.prices[0 : filter.memory-1]
filter.volumes = filter.volumes[0 : filter.memory-1]
}
filter.prices = append([]float64{trade.EstimatedUSDPrice}, filter.prices...)
if !filter.nativeDenomination {
filter.prices = append([]float64{trade.EstimatedUSDPrice}, filter.prices...)
} else {
filter.prices = append([]float64{trade.Price}, filter.prices...)
}

filter.volumes = append([]float64{trade.Volume}, filter.volumes...)
}

Expand Down Expand Up @@ -118,7 +125,11 @@ func (filter *FilterMA) finalCompute(t time.Time) float64 {

}
if len(filter.prices) > 0 && len(filter.volumes) > 0 {
filter.prices = []float64{filter.lastTrade.EstimatedUSDPrice}
if !filter.nativeDenomination {
filter.prices = []float64{filter.lastTrade.EstimatedUSDPrice}
} else {
filter.prices = []float64{filter.lastTrade.Price}
}
filter.volumes = []float64{filter.lastTrade.Volume}
}
return filter.value
Expand Down
51 changes: 31 additions & 20 deletions internal/pkg/filtersBlockService/FilterMAIR.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,30 @@ import (
// Outliers are eliminated using interquartile range.
// see: https://en.wikipedia.org/wiki/Interquartile_range
type FilterMAIR struct {
asset dia.Asset
exchange string
currentTime time.Time
prices []float64
volumes []float64
lastTrade dia.Trade
memory int
value float64
filterName string
modified bool
asset dia.Asset
exchange string
currentTime time.Time
prices []float64
volumes []float64
lastTrade dia.Trade
memory int
value float64
filterName string
nativeDenomination bool
modified bool
}

// NewFilterMAIR returns a FilterMAIR
func NewFilterMAIR(asset dia.Asset, exchange string, currentTime time.Time, memory int) *FilterMAIR {
func NewFilterMAIR(asset dia.Asset, exchange string, currentTime time.Time, memory int, nativeDenomination bool) *FilterMAIR {
filter := &FilterMAIR{
asset: asset,
exchange: exchange,
prices: []float64{},
volumes: []float64{},
currentTime: currentTime,
memory: memory,
filterName: "MAIR" + strconv.Itoa(memory),
asset: asset,
exchange: exchange,
prices: []float64{},
volumes: []float64{},
currentTime: currentTime,
memory: memory,
filterName: "MAIR" + strconv.Itoa(memory),
nativeDenomination: nativeDenomination,
}
return filter
}
Expand Down Expand Up @@ -84,7 +86,12 @@ func (filter *FilterMAIR) processDataPoint(trade dia.Trade) {
filter.prices = filter.prices[0 : filter.memory-1]
filter.volumes = filter.volumes[0 : filter.memory-1]
}
filter.prices = append([]float64{trade.EstimatedUSDPrice}, filter.prices...)
if !filter.nativeDenomination {
filter.prices = append([]float64{trade.EstimatedUSDPrice}, filter.prices...)
} else {
filter.prices = append([]float64{trade.Price}, filter.prices...)
}

filter.volumes = append([]float64{trade.Volume}, filter.volumes...)
}

Expand Down Expand Up @@ -113,7 +120,11 @@ func (filter *FilterMAIR) finalCompute(t time.Time) float64 {
filter.value = mean
// Reduce the filter values to the last recorded value for the next tradesblock.
if len(filter.prices) > 0 && len(filter.volumes) > 0 {
filter.prices = []float64{filter.lastTrade.EstimatedUSDPrice}
if !filter.nativeDenomination {
filter.prices = []float64{filter.lastTrade.EstimatedUSDPrice}
} else {
filter.prices = []float64{filter.lastTrade.Price}
}
filter.volumes = []float64{filter.lastTrade.Volume}
}
return filter.value
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/filtersBlockService/FilterMAIR_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestFilterMAIRIgnore(t *testing.T) {
Symbol: "XRP",
Name: "XRP",
}
f := NewFilterMAIR(assetXRP, "", d, filterParam)
f := NewFilterMAIR(assetXRP, "", d, filterParam, false)
p := firstPrice
priceIncrements := 1.0
for i := 0; i <= steps; i++ {
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestFilterMAIRAverageCleanOutliers(t *testing.T) {
Symbol: "XRP",
Name: "XRP",
}
f := NewFilterMAIR(assetXRP, "", d, memory)
f := NewFilterMAIR(assetXRP, "", d, memory, false)
for _, p := range c.samples {
f.compute(dia.Trade{EstimatedUSDPrice: p, Time: d})
d = d.Add(time.Second)
Expand Down
42 changes: 24 additions & 18 deletions internal/pkg/filtersBlockService/FilterMEDIR.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@ import (
// It implements a trimmed median. Outliers are eliminated using interquartile range
// see: https://en.wikipedia.org/wiki/Interquartile_range
type FilterMEDIR struct {
asset dia.Asset
exchange string
currentTime time.Time
prices []float64
lastTrade dia.Trade
memory int
value float64
filterName string
modified bool
asset dia.Asset
exchange string
currentTime time.Time
prices []float64
lastTrade dia.Trade
memory int
value float64
filterName string
modified bool
nativeDenomination bool
}

//NewFilterMEDIR creates a FilterMEDIR
func NewFilterMEDIR(asset dia.Asset, exchange string, currentTime time.Time, memory int) *FilterMEDIR {
// NewFilterMEDIR creates a FilterMEDIR
func NewFilterMEDIR(asset dia.Asset, exchange string, currentTime time.Time, memory int, nativeDenomination bool) *FilterMEDIR {
filter := &FilterMEDIR{
asset: asset,
exchange: exchange,
prices: []float64{},
currentTime: currentTime,
memory: memory,
filterName: "MEDIR" + strconv.Itoa(memory),
asset: asset,
exchange: exchange,
prices: []float64{},
currentTime: currentTime,
memory: memory,
filterName: "MEDIR" + strconv.Itoa(memory),
nativeDenomination: nativeDenomination,
}
return filter
}
Expand Down Expand Up @@ -62,7 +64,11 @@ func (filter *FilterMEDIR) processDataPoint(trade dia.Trade) {
if len(filter.prices) >= filter.memory {
filter.prices = filter.prices[0 : filter.memory-1]
}
filter.prices = append([]float64{trade.EstimatedUSDPrice}, filter.prices...)
if !filter.nativeDenomination {
filter.prices = append([]float64{trade.EstimatedUSDPrice}, filter.prices...)
} else {
filter.prices = append([]float64{trade.Price}, filter.prices...)
}
}

func (filter *FilterMEDIR) finalCompute(t time.Time) float64 {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/filtersBlockService/FilterMEDIR_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestFilterMEDIRMedianCleanOutliers(t *testing.T) {
Symbol: "XRP",
Name: "XRP",
}
f := NewFilterMEDIR(assetXRP, "", d, memory)
f := NewFilterMEDIR(assetXRP, "", d, memory, false)
for _, p := range c.samples {
f.compute(dia.Trade{EstimatedUSDPrice: p, Time: d})
d = d.Add(time.Second)
Expand Down
44 changes: 25 additions & 19 deletions internal/pkg/filtersBlockService/FilterVWAP.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,30 @@ import (

// FilterVWAP ...
type FilterVWAP struct {
asset dia.Asset
exchange string
currentTime time.Time
prices []float64
volumes []float64
lastTrade dia.Trade
param int
value float64
filterName string
modified bool
asset dia.Asset
exchange string
currentTime time.Time
prices []float64
volumes []float64
lastTrade dia.Trade
param int
value float64
filterName string
nativeDenomination bool
modified bool
}

// NewFilterVWAP ...
func NewFilterVWAP(asset dia.Asset, exchange string, currentTime time.Time, param int) *FilterVWAP {
func NewFilterVWAP(asset dia.Asset, exchange string, currentTime time.Time, param int, nativeDenomination bool) *FilterVWAP {
s := &FilterVWAP{
asset: asset,
exchange: exchange,
prices: []float64{},
volumes: []float64{},
currentTime: currentTime,
param: param,
filterName: "VWAP" + strconv.Itoa(param),
asset: asset,
exchange: exchange,
prices: []float64{},
volumes: []float64{},
currentTime: currentTime,
param: param,
filterName: "VWAP" + strconv.Itoa(param),
nativeDenomination: nativeDenomination,
}
return s
}
Expand Down Expand Up @@ -62,7 +64,11 @@ func (filter *FilterVWAP) fill(trade dia.Trade) {
}

func (filter *FilterVWAP) processDataPoint(trade dia.Trade) {
filter.prices = append([]float64{trade.EstimatedUSDPrice}, filter.prices...)
if !filter.nativeDenomination {
filter.prices = append([]float64{trade.EstimatedUSDPrice}, filter.prices...)
} else {
filter.prices = append([]float64{trade.Price}, filter.prices...)
}
filter.volumes = append([]float64{trade.Volume}, filter.volumes...)
}

Expand Down
44 changes: 25 additions & 19 deletions internal/pkg/filtersBlockService/FilterVWAPIR.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,29 @@ import (

// FilterVWAP ...
type FilterVWAPIR struct {
exchange string
currentTime time.Time
prices []float64
volumes []float64
lastTrade dia.Trade
param int
value float64
modified bool
filterName string
asset dia.Asset
exchange string
currentTime time.Time
prices []float64
volumes []float64
lastTrade dia.Trade
param int
value float64
modified bool
filterName string
asset dia.Asset
nativeDenomination bool
}

func NewFilterVWAPIR(asset dia.Asset, exchange string, currentTime time.Time, param int) *FilterVWAPIR {
func NewFilterVWAPIR(asset dia.Asset, exchange string, currentTime time.Time, param int, nativeDenomination bool) *FilterVWAPIR {
s := &FilterVWAPIR{
asset: asset,
exchange: exchange,
prices: []float64{},
volumes: []float64{},
currentTime: currentTime,
param: param,
filterName: "VWAPIR" + strconv.Itoa(param),
asset: asset,
exchange: exchange,
prices: []float64{},
volumes: []float64{},
currentTime: currentTime,
param: param,
filterName: "VWAPIR" + strconv.Itoa(param),
nativeDenomination: nativeDenomination,
}
return s
}
Expand Down Expand Up @@ -61,7 +63,11 @@ func (filter *FilterVWAPIR) fill(trade dia.Trade) {
}

func (filter *FilterVWAPIR) processDataPoint(trade dia.Trade) {
filter.prices = append([]float64{trade.EstimatedUSDPrice}, filter.prices...)
if !filter.nativeDenomination {
filter.prices = append([]float64{trade.EstimatedUSDPrice}, filter.prices...)
} else {
filter.prices = append([]float64{trade.Price}, filter.prices...)
}
filter.volumes = append([]float64{trade.Volume}, filter.volumes...)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/filtersBlockService/FilterVWAPIR_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestVWAPIR(t *testing.T) {
var filterPoints []dia.FilterPoint
trades := getTrades()
maFilter := NewFilterVWAPIR(dia.Asset{}, "Binance", trades[len(trades)-1].Time, dia.BlockSizeSeconds)
maFilter := NewFilterVWAPIR(dia.Asset{}, "Binance", trades[len(trades)-1].Time, dia.BlockSizeSeconds, false)

for _, trade := range trades {

Expand All @@ -32,7 +32,7 @@ func BenchmarkVWAPIR(b *testing.B) {

trades := getTrades()
for n := 0; n < b.N; n++ {
maFilter := NewFilterVWAPIR(dia.Asset{}, "Binance", trades[len(trades)-1].Time, dia.BlockSizeSeconds)
maFilter := NewFilterVWAPIR(dia.Asset{}, "Binance", trades[len(trades)-1].Time, dia.BlockSizeSeconds, false)

for _, trade := range trades {

Expand Down
Loading

0 comments on commit 365c53a

Please sign in to comment.