Example #1
0
func InterfaceToFloat64(iv interface{}) (retV float64) {
	//logger.Debugf("iv=%v\n", iv)

	switch ivTo := iv.(type) {
	case float64:
		//	logger.Traceln(ivTo, "is float64")
		retV = ivTo
	case string:
		//	logger.Traceln(ivTo, "is string")
		{
			var err error
			retV, err = strconv.ParseFloat(ivTo, 64)
			if err != nil {
				logger.Fatalln("convert failed, crash!")
				return 0
			}
		}
	default:
		logger.Fatalln(ivTo)
		logger.Fatalln("don't know the type, crash!")
		return 0
	}

	return retV
}
Example #2
0
func parse_buy_sell(sells_buys []interface{}, sells_buys_data *[10]SellBuy) bool {
	for k, v := range sells_buys {
		switch vt := v.(type) {
		case map[string]interface{}:
			logger.Debugln(k, " is a map:")
			logger.Debugf("sells/buys[%d]\n", k)

			for ik, iv := range vt {
				switch ik {
				case "price":
					sells_buys_data[k].Price = util.InterfaceToFloat64(iv)
				case "level":
					sells_buys_data[k].Level = util.InterfaceToFloat64(iv)
				case "amount":
					sells_buys_data[k].Amount = util.InterfaceToFloat64(iv)
				}
			}
		default:
			logger.Errorln(k, v)
			logger.Fatalln("don't know the type, crash!")
			return false
		}
	}

	return true
}
Example #3
0
func parse_trade(trades []interface{}, trades_data *[60]Trade) bool {
	for k, v := range trades {
		switch vt := v.(type) {
		case map[string]interface{}:
			logger.Debugln(k, " is a map:")
			logger.Debugf("trades[%d]\n", k)

			for ik, iv := range vt {
				switch ik {
				case "time":
					trades_data[k].Time = iv.(string)
				case "price":
					trades_data[k].Price = util.InterfaceToFloat64(iv)
				case "amount":
					trades_data[k].Amount = util.InterfaceToFloat64(iv)
				case "type":
					trades_data[k].Type = iv.(string)
				}
			}
		default:
			logger.Errorln(k, v)
			logger.Fatalln("don't know the type, crash!")
			return false
		}
	}
	return true
}
Example #4
0
func (w *OkcoinTrade) check_json_result(body string) (errorMsg ErrorMsg, ret bool) {
	if strings.Contains(body, "result") != true {
		ret = false
		return
	}

	doc := json.NewDecoder(strings.NewReader(body))

	if err := doc.Decode(&errorMsg); err == io.EOF {
		logger.Traceln(err)
	} else if err != nil {
		logger.Fatal(err)
		logger.Fatalln(body)
	}

	if errorMsg.Result != true {
		logger.Errorln(errorMsg)
		SetLastError(errorMsg.ErrorCode)
		ret = false
		return
	}

	ret = true
	return
}
Example #5
0
func parse_trade(trades []interface{}, trades_data *[15]Trade) {
	for k, v := range trades {
		switch vt := v.(type) {
		case map[string]interface{}:
			logger.Traceln(k, " is a map:")
			logger.Tracef("trades[%d]\n", k)

			for ik, iv := range vt {
				switch ik {
				case "time":
					trades_data[k].time = iv.(string)
				case "price":
					trades_data[k].price = InterfaceToFloat64(iv)
				case "amount":
					trades_data[k].amount = InterfaceToFloat64(iv)
				case "type":
					trades_data[k].type_ = iv.(string)
				}
			}
		default:
			logger.Errorln(k, v)
			logger.Fatalln("don't know the type, crash!")
		}
	}
}
Example #6
0
func parse_topsell(topsells map[string]interface{}, topsells_data *[5]Top_buy_sell) {
	index := 4
	for k, v := range topsells {
		switch vt := v.(type) {
		case map[string]interface{}:
			logger.Traceln(k, " is a map:")
			logger.Tracef("topsells[%s]\n", k)

			for ik, iv := range vt {
				logger.Traceln(ik, iv)
				switch ik {
				case "price":
					topsells_data[index].Price = InterfaceToFloat64(iv)
				case "amount":
					topsells_data[index].amount = InterfaceToFloat64(iv)
				case "level":
					topsells_data[index].level = InterfaceToFloat64(iv)
				case "accu":
					topsells_data[index].accu = InterfaceToFloat64(iv)
				}
			}

			index--

		default:
			logger.Errorln(k, v)
			logger.Fatalln("don't know the type, crash!")
		}
	}
}
Example #7
0
// just f**k the huobi old shit bug
func parse_topsell(topsells map[string]interface{}, topsells_data *[5]Top_buy_sell) bool {
	index := 4
	for k, v := range topsells {
		switch vt := v.(type) {
		case map[string]interface{}:
			logger.Debugln(k, " is a map:")
			logger.Debugf("topsells[%s]\n", k)

			for ik, iv := range vt {
				logger.Debugln(ik, iv)
				switch ik {
				case "price":
					topsells_data[index].Price = util.InterfaceToFloat64(iv)
				case "amount":
					topsells_data[index].Amount = util.InterfaceToFloat64(iv)
				case "level":
					topsells_data[index].Level = util.InterfaceToFloat64(iv)
				case "accu":
					topsells_data[index].Accu = util.InterfaceToFloat64(iv)
				}
			}

			index--

		default:
			logger.Errorln(k, v)
			logger.Fatalln("don't know the type, crash!")
			return false
		}
	}
	return true
}
Example #8
0
func stringt2float64(in string) float64 {
	out, err := strconv.ParseFloat(in, 64)
	if err != nil {
		logger.Fatalln("don't know the type, crash!")
		logger.Traceln("config item amount is not number, get from buy price")
	}

	return out
}
Example #9
0
func tradeAPI() (tradeAPI TradeAPI) {
	if Option["tradecenter"] == "huobi" {
		tradeAPI = huobi.NewHuobi()
	} else if Option["tradecenter"] == "okcoin" {
		tradeAPI = okcoin.NewOkcoin()
	} else {
		logger.Fatalln("Please config the tradecenter firstly...")

	}
	return
}
Example #10
0
func marketAPI() (marketAPI MarketAPI) {
	if Option["datacenter"] == "huobi" {
		marketAPI = huobi.NewHuobi()
	} else if Option["datacenter"] == "okcoin" {
		marketAPI = okcoin.NewOkcoin()
	} else {
		logger.Fatalln("Please config the datacenter firstly...")

	}
	return
}
Example #11
0
func RobotWorker() {
	fmt.Println("env", Config["env"])
	if Config["env"] == "dev" {
		fmt.Println("test working...")
		var tradeAPI common.TradeAPI
		tradeAPI = huobi.NewHuobi()
		tradeAPI.Get_account_info()
		symbol := "btc_cny"
		tradeAPI.GetOrderBook(symbol)

		//testHuobiAPI()
		//testOkcoinLTCAPI()
		return
	}

	ticker := time.NewTicker(2 * time.Second) //2s
	defer ticker.Stop()

	var tradeAPI common.TradeAPI
	tradeAPI = huobi.NewHuobi()
	tradeAPI.Get_account_info()

	tradeAPI = okcoin.NewOkcoin()
	tradeAPI.Get_account_info()

	if Option["tradecenter"] == "huobi" {
		tradeAPI = huobi.NewHuobi()
	} else if Option["tradecenter"] == "okcoin" {
		tradeAPI = okcoin.NewOkcoin()
	} else {
		logger.Fatalln("Please config the tradecenter firstly...")
		return
	}
	peroid, _ := strconv.Atoi(Option["tick_interval"])
	totalHour, _ := strconv.ParseInt(Option["totalHour"], 0, 64)
	if totalHour < 1 {
		totalHour = 1
	}

	fmt.Println("robot working...")

	go func() {
		for _ = range ticker.C {
			tradeAPI.AnalyzeKLine(peroid)
		}
	}()

	logger.Infof("程序将持续运行%d小时后停止", time.Duration(totalHour))

	time.Sleep(time.Duration(totalHour) * time.Hour)

	logger.Infof("程序到达设定时长%d小时,停止运行。", time.Duration(totalHour))
}
Example #12
0
func parseMinuteCSV(filename string) (MinuteRecords []MinuteRecord) {

	// convert to standard csv file
	data2csv(filename, 3)

	file, err := os.Open(filename + ".csv")
	if err != nil {
		fmt.Println("ParseMinuteCSV Error:", err)
		return
	}
	defer file.Close()
	reader := csv.NewReader(file)
	/*
		record, err := reader.ReadAll()
		fmt.Println(record)
		return
	*/

	for {
		record, err := reader.Read()
		if err == io.EOF {
			break
		} else if err != nil {
			fmt.Println("Error:", err)
			return
		}

		if len(record) < 4 {
			fmt.Println("Error:", "record is zero, maybe it is not a cvs format!!!")
			return
		}

		var minRecord MinuteRecord
		minRecord.Time = record[0]
		minRecord.Price, err = strconv.ParseFloat(record[1], 64)
		if err != nil {
			logger.Fatalln(record)
			logger.Fatal("ParseMinuteCSV item price is not number")
		}
		minRecord.Volumn, err = strconv.ParseFloat(record[2], 64)
		if err != nil {
			logger.Fatal("ParseMinuteCSV item Volumn is not number")
		}
		minRecord.Amount, err = strconv.ParseFloat(record[3], 64)
		if err != nil {
			logger.Fatal("ParseMinuteCSV item Amount is not number")
		}

		MinuteRecords = append(MinuteRecords, minRecord)
	}

	return
}
Example #13
0
func InterfaceToFloat64(iv interface{}) (retV float64) {
	switch ivTo := iv.(type) {
	case float64:
		retV = ivTo
	case string:
		{
			var err error
			retV, err = strconv.ParseFloat(ivTo, 64)
			if err != nil {
				logger.Fatalln("convert failed, crash!")
				return 0
			}
		}
	default:
		logger.Fatalln(ivTo)
		logger.Fatalln("don't know the type, crash!")
		return 0
	}

	return retV
}
Example #14
0
func marketAPI() (marketAPI MarketAPI) {
	if Option["datacenter"] == "huobi" {
		marketAPI = huobi.NewHuobi()
	} else if Option["datacenter"] == "okcoin" {
		marketAPI = okcoin.NewOkcoin()
	} else if Option["datacenter"] == "peatio" {
		marketAPI = peatio.NewPeatio()
	} else if Option["datacenter"] == "bittrex" {
		marketAPI = Bittrex.Manager()
	} else {
		logger.Fatalln("Please config the market center...")
	}
	return
}
Example #15
0
func tradeAPI() (tradeAPI TradeAPI) {
	if Option["tradecenter"] == "huobi" {
		tradeAPI = huobi.NewHuobi()
	} else if Option["tradecenter"] == "okcoin" {
		tradeAPI = okcoin.NewOkcoin()
	} else if Option["tradecenter"] == "bitvc" {
		tradeAPI = bitvc.NewBitvc()
	} else if Option["tradecenter"] == "peatio" {
		tradeAPI = peatio.NewPeatio()
	} else {
		logger.Fatalln("Please config the exchange center...")
	}
	return
}
Example #16
0
func tradeAPI() (tradeAPI TradeAPI) {
	if Option["tradecenter"] == "huobi" {
		tradeAPI = huobi.NewHuobi()
	} else if Option["tradecenter"] == "okcoin" {
		tradeAPI = okcoin.NewOkcoin()
	} else if Option["tradecenter"] == "bitvc" {
		tradeAPI = bitvc.NewBitvc()
	} else if Option["tradecenter"] == "peatio" {
		tradeAPI = peatio.NewPeatio()
	} else if Option["tradecenter"] == "bittrex" {
		tradeAPI = Bittrex.Manager()
	} else if Option["tradecenter"] == "simulate" {
		tradeAPI = simulate.NewSimulate()
	} else {
		logger.Fatalln("Please config the exchange center...")
		panic(0)
	}
	return
}
Example #17
0
func parse_buy_sell(sells_buys []interface{}, sells_buys_data *[10]SellBuy) {
	for k, v := range sells_buys {
		switch vt := v.(type) {
		case map[string]interface{}:
			logger.Traceln(k, " is a map:")
			logger.Tracef("sells/buys[%d]\n", k)

			for ik, iv := range vt {
				switch ik {
				case "price":
					sells_buys_data[k].price = InterfaceToFloat64(iv)
				case "level":
					sells_buys_data[k].level = InterfaceToFloat64(iv)
				case "amount":
					sells_buys_data[k].amount = InterfaceToFloat64(iv)
				}
			}
		default:
			logger.Errorln(k, v)
			logger.Fatalln("don't know the type, crash!")
		}
	}
}