Created
September 4, 2017 04:51
-
-
Save wozz/328c9de43b821a6ded592cdb575b539a to your computer and use it in GitHub Desktop.
Script to buy small amounts every hour over a week
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "math" | |
| "os" | |
| "time" | |
| gdax "github.com/preichenberger/go-coinbase-exchange" | |
| ) | |
| func round(x float64) float64 { | |
| return math.Floor(x*float64(1000.00)) / float64(1000.00) | |
| } | |
| func Stringify(o gdax.Order) string { | |
| b, err := json.Marshal(o) | |
| if err != nil { | |
| panic(err) | |
| } | |
| return fmt.Sprintf("%s", b) | |
| } | |
| func main() { | |
| secret := os.Getenv("COINBASE_SECRET") | |
| key := os.Getenv("COINBASE_KEY") | |
| passphrase := os.Getenv("COINBASE_PASSPHRASE") | |
| totalToTrade := float64(100) | |
| client := gdax.NewClient(secret, key, passphrase) | |
| accounts, err := client.GetAccounts() | |
| if err != nil { | |
| panic(err) | |
| } | |
| amountToTrade := float64(0) | |
| for _, act := range accounts { | |
| if act.Currency == "USD" { | |
| amountToTrade = act.Available | |
| } | |
| } | |
| if amountToTrade < totalToTrade { | |
| panic("need more funds") | |
| } | |
| eachTrade := totalToTrade / float64(24*7) | |
| numTrades := 0 | |
| for { | |
| numTrades = numTrades + 1 | |
| if numTrades > (24 * 7) { | |
| break | |
| } | |
| fmt.Printf("buying $%f\n", round(eachTrade)) | |
| order := gdax.Order{ | |
| Type: "market", | |
| Side: "buy", | |
| ProductId: "BTC-USD", | |
| Funds: round(eachTrade), | |
| } | |
| createdOrder, err := client.CreateOrder(&order) | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Println(Stringify(createdOrder)) | |
| fmt.Println("wait an hour") | |
| time.Sleep(time.Hour) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment