func testKLineAPI(done chan bool) { ticker := time.NewTicker(time.Millisecond * 2000) huobi := huobiapi.NewHuobi() huobi.Peroid, _ = strconv.Atoi(Option["tick_interval"]) slippage, err := strconv.ParseFloat(Config["slippage"], 64) if err != nil { logger.Debugln("config item slippage is not float") slippage = 0 } huobi.Slippage = slippage go func() { for _ = range ticker.C { if huobi.Peroid == 1 { huobi.TradeKLineMinute() } else { huobi.TradeKLinePeroid(huobi.Peroid) } } }() time.Sleep(time.Millisecond * 24 * 60 * 60 * 1000) ticker.Stop() fmt.Println("Ticker stopped") done <- true }
func TradeHandler(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) msgtype := vars["msgtype"] if req.Method != "POST" || msgtype == "" { // 获取用户信息 err := config.LoadTrade() if err != nil { logger.Errorln(err) fmt.Fprint(rw, `{"errno": 1, "msg":"`, "读取Trade配置数据失败", `"}`) return } // 设置模板数据 filter.SetData(req, map[string]interface{}{"trade": config.TradeOption}) req.Form.Set(filter.CONTENT_TPL_KEY, "/template/trade/trade.html") return } else if req.Method == "POST" { if msgtype == "dobuy" { config.TradeOption["buyprice"] = req.FormValue("buyprice") config.TradeOption["buyamount"] = req.FormValue("buyamount") } else if msgtype == "dosell" { config.TradeOption["sellprice"] = req.FormValue("sellprice") config.TradeOption["sellamount"] = req.FormValue("sellamount") } else { fmt.Fprint(rw, `{"errno": 1, "msg":"`, "无效的POST请求", `"}`) return } // 更新个人信息 err := config.SaveTrade() if err != nil { fmt.Fprint(rw, `{"errno": 1, "msg":"`, "写入Trade配置数据失败", `"}`) return } huobi := huobiapi.NewHuobi() var ret bool if msgtype == "dobuy" { ret = huobi.Do_buy(config.TradeOption["buyprice"], config.TradeOption["buyamount"]) } else if msgtype == "dosell" { ret = huobi.Do_sell(config.TradeOption["sellprice"], config.TradeOption["sellamount"]) } if ret != true { fmt.Fprint(rw, `{"errno": 1, "msg":"`, "交易委托失败", `"}`) return } else { fmt.Fprint(rw, `{"errno": 1, "msg":"`, "交易委托成功", `"}`) return } } }
func doTradeDelegation() { huobi := huobiapi.NewHuobi() logger.Infoln("doTradeDelegation start....") if huobi.Login() == true { logger.Debugln("Login successfully.") huobi.TradeDelegation() } else { logger.Debugln("Login failed.") } logger.Infoln("doTradeDelegation end-----") }
func doTradeDelegation() { huobi := huobiapi.NewHuobi() logger.Infoln("doTradeDelegation start....") if huobi.Login() == true { console.SetColor(console.FOREGROUND_GREEN) logger.Debugln("Login successfully.") huobi.TradeDelegation() } else { logger.Debugln("Login failed.") } logger.Infoln("doTradeDelegation end-----") }
func backtesting(done chan bool) { fmt.Println("back testing begin...") huobi := huobiapi.NewHuobi() peroids := []int{1, 5, 15, 30, 60, 100} for _, v := range peroids { huobi.Peroid = v if huobi.TradeKLinePeroid(huobi.Peroid) == true { } else { logger.Errorln("TradeKLine failed.") } } fmt.Println("back testing end ...") done <- true }
func backtesting() { fmt.Println("back testing begin...") huobi := huobiapi.NewHuobi() huobi.Disable_trading = 1 peroids := []int{1, 5, 15, 30, 60, 100} for _, v := range peroids { huobi.Peroid = v if huobi.TradeKLinePeroid(huobi.Peroid) == true { } else { logger.Errorln("TradeKLine failed.") } } fmt.Println("生成 1/5/15/30/60分钟及1天 周期的后向测试报告于log/reportxxx.log文件中,请查看") fmt.Println("back testing end ...") }
func testKLineAPI(done chan bool) { ticker := time.NewTicker(2000 * time.Millisecond) //2s huobi := huobiapi.NewHuobi() huobi.Peroid, _ = strconv.Atoi(Option["tick_interval"]) totalHour, _ := strconv.ParseInt(Option["totalHour"], 0, 64) if totalHour < 1 { totalHour = 1 } slippage, err := strconv.ParseFloat(Config["slippage"], 64) if err != nil { logger.Debugln("config item slippage is not float") slippage = 0 } huobi.Slippage = slippage huobi.Disable_trading = 0 go func() { for _ = range ticker.C { if huobi.Peroid == 1 { huobi.TradeKLineMinute() } else { huobi.TradeKLinePeroid(huobi.Peroid) } } }() oneHour := 60 * 60 * 1000 * time.Millisecond logger.Infof("程序将持续运行%d小时后停止", time.Duration(totalHour)) time.Sleep(time.Duration(totalHour) * oneHour) ticker.Stop() fmt.Println("程序到达设定时长%d小时,停止运行。", time.Duration(totalHour)) done <- true }