trades

package
v0.0.0-...-ab5b903 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package trades provides an SQLite based trades database.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB is a database of stock trades.

Example
package main

import (
	"fmt"
	"math/rand"
	"time"

	"github.com/ardanlabs/python-go/sqlite/trades"
)

func main() {
	dbFile := "/tmp/db-test" + time.Now().Format(time.RFC3339)
	db, err := trades.NewDB(dbFile)
	if err != nil {
		fmt.Println("ERROR: create -", err)
		return
	}
	defer db.Close()

	const count = 10_000
	for i := 0; i < count; i++ {
		trade := trades.Trade{
			Time:   time.Now(),
			Symbol: "AAPL",
			Price:  rand.Float64() * 200,
			IsBuy:  i%2 == 0,
		}
		if err := db.Add(trade); err != nil {
			fmt.Println("ERROR: insert - ", err)
			return
		}
	}

	fmt.Printf("inserted %d records\n", count)
}
Output:
inserted 10000 records

func NewDB

func NewDB(dbFile string) (*DB, error)

NewDB constructs a Trades value for managing stock trades in a SQLite database. This API is not thread safe.

func (*DB) Add

func (db *DB) Add(trade Trade) error

Add stores a trade into the buffer. Once the buffer is full, the trades are flushed to the database.

func (*DB) Close

func (db *DB) Close() (err error)

Close flushes all trades to the database and prevents any future trading.

func (*DB) Flush

func (db *DB) Flush() error

Flush inserts pending trades into the database.

type Trade

type Trade struct {
	Time   time.Time
	Symbol string
	Price  float64
	IsBuy  bool
}

Trade is a buy/sell trade for symbol.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL