Ejemplo n.º 1
0
func (order StatusOrder) Execute(client bitcoin.Client) (err error) {
	if order.status == ORDER_KEEP {
		fmt.Printf("Keep order %v\n", order)
		return
	}
	if order.status == ORDER_REMOVE {
		return client.CancelOrder(order.Order.Id)
	}
	if order.Type == bitcoin.BUY_ORDER {
		return client.Buy(order.Price, order.Amount)
	} else {
		return client.Sell(order.Price, order.Amount)
	}
}
Ejemplo n.º 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)
		}
	}
}