Skip to content

Commit

Permalink
intentionally leaking DataHandler abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
victorl2 committed Apr 20, 2021
1 parent dc2ddf7 commit 7590f45
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
20 changes: 10 additions & 10 deletions kate/data_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

//DataHandler is a wrapper that packages the required data for running backtesting simulation.
type DataHandler struct {
counter, windowSize int
prices []DataPoint
Counter, WindowSize int
Prices []DataPoint
}

//Position is the representation of a traded position
Expand Down Expand Up @@ -43,29 +43,29 @@ var csvColumns = []string{"open", "high", "low", "close", "volume"}
//newDataHandler creates and initializes a DataHandler with pricing data and executes the required setup
func newDataHandler(prices []DataPoint, windowSize int) *DataHandler {
return &DataHandler{
windowSize: windowSize,
prices: prices,
counter: windowSize,
WindowSize: windowSize,
Prices: prices,
Counter: windowSize,
}
}

//NextValues returns AggregatedDataPoints with the next values in the stream of datapoints (containing the lastest windowSize of values).
//a nil return denotes the end for the stream
func (handler *DataHandler) nextValues() *AggregatedDataPoints {
if handler.counter < len(handler.prices) {
if handler.Counter < len(handler.Prices) {
data := &AggregatedDataPoints{
datapoints: handler.prices[handler.counter-handler.windowSize : handler.counter],
datapoints: handler.Prices[handler.Counter-handler.WindowSize : handler.Counter],
}
handler.counter++
handler.Counter++
return data
}
return nil
}

//SetWindow defines the range of values that will be avaliable on each step through the data points
func (handler *DataHandler) SetWindow(windowSize int) {
handler.windowSize = windowSize
handler.counter = windowSize
handler.WindowSize = windowSize
handler.Counter = windowSize
}

//PricesFromCSV reads all csv data in the OHLCV format to the DataHandler and returns if a error occurred
Expand Down
16 changes: 8 additions & 8 deletions kate/data_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
func TestLoadCSVData(t *testing.T) {
handler, _ := PricesFromCSV("../testdata/simple_example.csv")

if len(handler.prices) != 100 {
t.Errorf("The amount of prices is %v the expected amount is 100", len(handler.prices))
if len(handler.Prices) != 100 {
t.Errorf("The amount of prices is %v the expected amount is 100", len(handler.Prices))
}

firstPrice := DataPoint{
Expand All @@ -23,8 +23,8 @@ func TestLoadCSVData(t *testing.T) {
close: 746.95,
volume: 1045532,
}
if !reflect.DeepEqual(handler.prices[0], firstPrice) {
t.Errorf("The first price in the csv is %v the expected was %v", handler.prices[0], firstPrice)
if !reflect.DeepEqual(handler.Prices[0], firstPrice) {
t.Errorf("The first price in the csv is %v the expected was %v", handler.Prices[0], firstPrice)
}

lastPrice := DataPoint{
Expand All @@ -35,8 +35,8 @@ func TestLoadCSVData(t *testing.T) {
volume: 80597,
}

if !reflect.DeepEqual(handler.prices[len(handler.prices)-1], lastPrice) {
t.Errorf("The last price in the csv is %v the expected was %v", handler.prices[len(handler.prices)-1], lastPrice)
if !reflect.DeepEqual(handler.Prices[len(handler.Prices)-1], lastPrice) {
t.Errorf("The last price in the csv is %v the expected was %v", handler.Prices[len(handler.Prices)-1], lastPrice)
}

middlePrice := DataPoint{
Expand All @@ -46,8 +46,8 @@ func TestLoadCSVData(t *testing.T) {
close: 746.45,
volume: 5306,
}
if !reflect.DeepEqual(handler.prices[66], middlePrice) {
t.Errorf("The middle price in the csv is %v the expected was %v", handler.prices[66], middlePrice)
if !reflect.DeepEqual(handler.Prices[66], middlePrice) {
t.Errorf("The middle price in the csv is %v the expected was %v", handler.Prices[66], middlePrice)
}

}
Expand Down
2 changes: 1 addition & 1 deletion kate/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (bt *Backtester) calculateStatistics(initialBalance float64) *Statistics {
TotalTrades: len(tradeHistory),
WinRate: float64(wins) / float64(len(tradeHistory)),
MaxDrawdown: (peakProfit - bottomProfit) / peakProfit,
TotalDataPoints: len(bt.dataHandler.prices),
TotalDataPoints: len(bt.dataHandler.Prices),
}

stats.SharpeRatio = sharpe(stats.NetProfit, 0.0, stdDev(balanceHistory))
Expand Down

0 comments on commit 7590f45

Please sign in to comment.