Skip to content

Commit

Permalink
check current price if use-basis is enabled; adjust purchase amount b…
Browse files Browse the repository at this point in the history
…y multiplier
  • Loading branch information
swhite24 committed Apr 16, 2021
1 parent a39865c commit f59d52f
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/cmd/cbprobuy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ package cmd
import (
"fmt"
"os"
"strconv"
"time"

"github.com/preichenberger/go-coinbasepro/v2"
"github.com/spf13/cobra"
"github.com/swhite24/cbpro-buy/pkg/book"
"github.com/swhite24/cbpro-buy/pkg/config"
"github.com/swhite24/cbpro-buy/pkg/purchase"

basisconfig "github.com/swhite24/cbpro-cost-basis/pkg/config"
"github.com/swhite24/cbpro-cost-basis/pkg/costbasis"
)

var (
Expand All @@ -29,6 +35,42 @@ func init() {
Run: func(cmd *cobra.Command, args []string) {
cfg := config.InitializeConfig(cmd.Flags())
client := initializeClient(cfg)
fmt.Println(cfg)

// Determine basis if configured
if cfg.UseBasis {
// Calculate average cost
c := &basisconfig.Config{
Key: cfg.Key,
Passphrase: cfg.Passphrase,
Secret: cfg.Secret,
Product: fmt.Sprintf("%s-%s", cfg.Product, cfg.Currency),
StartDate: time.Now().Add(time.Duration(cfg.BasisWindowStart*-24) * time.Hour),
EndDate: time.Now(),
}
info, err := costbasis.Calculate(client, c)
if err != nil {
fmt.Println("failed to calculate basis")
fmt.Println(err)
os.Exit(1)
}
fmt.Println(c, info, err)

// Get current price
average, _ := strconv.ParseFloat(info.AverageCost, 64)
price, err := book.GetPrice(client, fmt.Sprintf("%s-%s", cfg.Product, cfg.Currency))
if err != nil {
fmt.Println("failed to calculate current price")
fmt.Println(err)
os.Exit(1)
}

// Update purchase amount if current price is less than average cost
if price < average {
cfg.Amount = cfg.Amount * cfg.BasisMultiplier
}
}

err := purchase.InitiatePurchase(client, cfg)
if err != nil {
fmt.Println("failed to purchase")
Expand Down

0 comments on commit f59d52f

Please sign in to comment.