Beispiel #1
0
func main() {
	flags = initFlags()

	if flags.exchange == "bitstamp" {
		client = bitstamp.NewClient(
			flags.clientId,
			flags.apiKey,
			flags.apiSecret)
	} else if flags.exchange == "btce" {
		client = btce.NewClient(
			flags.apiKey,
			flags.apiSecret)
	} else if flags.exchange == "bitfinex" {
		client = bitfinex.NewClient(
			flags.apiKey,
			flags.apiSecret)
	} else if flags.exchange == "kraken" {
		client = kraken.NewClient(
			flags.apiKey,
			flags.apiSecret)
	}
	client.SetDryRun(flags.dryRun)

	switch flags.c {
	case "last":
		last()
	case "ob":
		fallthrough
	case "order_book":
		orderBook()
	case "t":
		fallthrough
	case "transactions":
		transactions()
	case "b":
		fallthrough
	case "balance":
		balance()
	case "ut":
		fallthrough
	case "user_transactions":
		userTransactions()
	case "oo":
		fallthrough
	case "open_orders":
		openOrders()
	case "cancel":
		fallthrough
	case "cancel_order":
		cancelOrder()
	case "buy":
		buy()
	case "sell":
		sell()
	default:
		fmt.Printf("Command must be one of %s\n", COMMANDS)
		os.Exit(1)
	}
}
Beispiel #2
0
func main() {
	initFlags()
	var client bitcoin.Client
	if flagExchange == "bitstamp" {
		client = bitstamp.NewClient(flagClientId, flagApiKey, flagApiSecret)
	} else if flagExchange == "bitfinex" {
		client = bitfinex.NewClient(flagApiKey, flagApiSecret)
	} else if flagExchange == "kraken" {
		client = kraken.NewClient(flagApiKey, flagApiSecret)
	} else {
		fmt.Printf("Unknown exchange: %v\n", flagExchange)
		os.Exit(1)
	}
	client.SetDryRun(flagTest)

	openOrders, err := client.OpenOrders()
	if err != nil {
		fmt.Printf("Error open orders: %v\n", err)
		return
	}
	if flagTest {
		fmt.Printf("%v open orders:\n", len(openOrders))
		for _, order := range openOrders {
			fmt.Printf("%v\n", order)
		}
	} else {
		if len(openOrders) == 4 {
			return
		}
	}
	orderMap := make(map[string]*StatusOrder)
	for _, order := range openOrders {
		orderMap[order.String()] = &StatusOrder{order, ORDER_REMOVE}
	}

	A, err := client.Balance(bitcoin.FIAT)
	if err != nil {
		fmt.Printf("Error balance: %v\n", err)
		return
	}
	b, err := client.Balance(bitcoin.BTC)
	if err != nil {
		fmt.Printf("Error balance: %v\n", err)
		return
	}
	A += flagOffsetUsd
	b += flagOffsetBtc
	R := flagBtcRatio / (1 - flagBtcRatio)
	F, err := client.Fee()
	if err != nil {
		fmt.Printf("Error fee: %v\n", err)
		return
	}
	if flagSpread < 200*F {
		fmt.Printf(
			"spread (%.2f%%) must be at least twice the fee (%.2f%%) "+
				"not to make a loss.\n", flagSpread, 100*F)
		return
	}
	s := 1 + (flagSpread / 100)

	previousRate := R * A / b

	fmt.Printf("Rate = %.2f\n", previousRate)
	fmt.Printf("Fiat = %v\n", A)
	fmt.Printf("BTC = %v\n", b)
	fmt.Printf("Fee = %v\n", F)

	placeBuyOrders(A, b, R, F, s, orderMap)
	placeSellOrders(A, b, R, F, s, orderMap)

	for _, order := range orderMap {
		err := order.Execute(client)
		if err != nil {
			fmt.Printf("Error executing order: %v: %v\n", order, err)
		}
	}
}