func (w *Huobi) doEMA(Time []string, Price []float64, Volumn []float64) { if len(Price) == 0 { logger.Errorln("no data is prepared!") return } //read config shortEMA, _ := strconv.Atoi(Option["shortEMA"]) longEMA, _ := strconv.Atoi(Option["longEMA"]) stopPoints, err := strconv.ParseFloat(Config["stopPoints"], 64) if err != nil { logger.Debugln("config item stopPoints is not float") return } EMAMinThreshold, err := strconv.ParseFloat(Config["EMAMinThreshold"], 64) if err != nil { logger.Debugln("config item EMAMinThreshold is not float") return } tradeOnlyAfterSwitch, _ := strconv.Atoi(Config["tradeOnlyAfterSwitch"]) TresholdLevel, _ := strconv.Atoi(Config["TresholdLevel"]) _, err = strconv.ParseFloat(Option["tradeAmount"], 64) if err != nil { logger.Debugln("config item tradeAmount is not float") return } tradeAmount := Option["tradeAmount"] f_tradeAmount, err := strconv.ParseFloat(Option["tradeAmount"], 64) if err != nil { logger.Debugln("config item tradeAmount is not float") return } MACDtradeAmount := fmt.Sprintf("%0.02f", f_tradeAmount/2.0) //compute the indictor emaShort := EMA(Price, shortEMA) emaLong := EMA(Price, longEMA) EMAdif := getEMAdif(emaShort, emaLong) length := len(Price) //check indictor using history data, loop through data, get history samples logger.OverrideStart(w.Peroid) logger.Overridef("EMA 收益率分析[%d:[s=%d/l=%d],stopPoints:%0.0f]\n", w.Peroid, shortEMA, longEMA, stopPoints) var profit float64 var times int var lastTrade float64 var entryPrice float64 var totaltimes int if w.Disable_trading != 1 { if Config["env"] == "dev" { logger.Infof("%s\t%-6.2f\t%-6.2f\n", Time[length-1], Price[length-1], Volumn[length-1]) } } //EMA cross for i := 1; i < length; i++ { if EMAdif[i-1] < 0 && EMAdif[i] > 0 { //up cross w.lastAction = "buyIn" totaltimes++ logger.Overrideln(totaltimes) if times == 0 { entryPrice = Price[i] } times++ profit -= Price[i] lastTrade = Price[i] w.lastBuyprice = Price[i] var samplesBegin int if i > longEMA { samplesBegin = i - longEMA } else { samplesBegin = 0 } periodArr := Price[samplesBegin:i] w.lastLowestprice = arrayLowest(periodArr) if EMAdif[i] >= EMAMinThreshold { logger.Overrideln("++ buyIn", i, Time[i], Price[i], fmt.Sprintf("%0.04f", EMAdif[i]), w.lastLowestprice, 2*w.lastBuyprice-w.lastLowestprice) } else { logger.Overrideln(" + buyIn", i, Time[i], Price[i], fmt.Sprintf("%0.04f", EMAdif[i]), w.lastLowestprice, 2*w.lastBuyprice-w.lastLowestprice) } if w.checkException(Price[i-1], Price[i], Volumn[i]) == false { logger.Infoln("detect exception data in huobi.com", Price[i-1], Price[i], Volumn[i]) continue } if i == length-1 && w.latestMACDTrend != 1 { if w.Disable_trading != 1 { logger.Infoln("EMA up cross, 买入buyIn", w.getTradePrice("")) go service.TriggerTrender("EMA up cross, 买入buyIn") } if Option["disable_trading"] != "1" && w.Disable_trading != 1 { w.latestMACDTrend = 1 w.Do_buy(w.getTradePrice("buy"), tradeAmount) } w.backup(Time[i]) } } else if (w.lastAction != "sellOut" || w.lastAction != "stop") && EMAdif[i-1] > 0 && EMAdif[i] < 0 { //down cross w.lastAction = "sellOut" if EMAdif[i] <= -EMAMinThreshold { logger.Overrideln("-- sellOut", i, Time[i], Price[i], fmt.Sprintf("%0.04f", EMAdif[i])) } else { logger.Overrideln(" - sellOut", i, Time[i], Price[i], fmt.Sprintf("%0.04f", EMAdif[i])) } if w.checkException(Price[i-1], Price[i], Volumn[i]) == false { logger.Infoln("detect exception data in huobi.com", Price[i-1], Price[i], Volumn[i]) continue } if i == length-1 && w.latestMACDTrend != -1 { if w.Disable_trading != 1 { logger.Infoln("EMA down cross, 卖出sellOut", w.getTradePrice("")) go service.TriggerTrender("EMA down cross, 卖出sellOut") } if Option["disable_trading"] != "1" && w.Disable_trading != 1 { w.latestMACDTrend = -1 ret := w.Do_sell(w.getTradePrice("sell"), tradeAmount) if ret == false { w.Do_sell(w.getTradePrice("sell"), MACDtradeAmount) } } w.backup(Time[i]) } if times != 0 { times++ profit += Price[i] lastTrade = Price[i] if times != 0 && times%2 == 0 { logger.Overridef("profit=%0.02f, rate=%0.02f%%\n", Price[i]-w.lastBuyprice, 100*(Price[i]-w.lastBuyprice)/w.lastBuyprice) } } } /* else if (w.lastAction != "exit" || w.lastAction != "stop") && Price[i] < emaMiddle[i]-stopPoints { //stop w.lastAction = "stop" logger.Overrideln("-- stop", i, Time[i], Price[i], fmt.Sprintf("%0.04f", emaMiddle[i])) if i == length-1 && w.latestMACDTrend != -1 { logger.Infoln("保守止损位", w.getTradePrice("")) go service.TriggerTrender("保守止损位") ret := w.do_sell(w.getTradePrice("sell"), tradeAmount) if ret == false { w.do_sell(w.getTradePrice("sell"), MACDtradeAmount) } } if times != 0 { times++ profit += Price[i] lastTrade = Price[i] if times != 0 && times%2 == 0 { logger.Overridef("profit=%0.02f, rate=%0.02f%%\n", Price[i]-w.lastBuyprice, 100*(Price[i]-w.lastBuyprice)/w.lastBuyprice) } } }*/ } if times%2 != 0 { profit += lastTrade totaltimes-- } logger.Overridef("totaltimes[%d] profit=%0.02f, entryPrice=%0.02f, rate=%0.02f%%\n", totaltimes, profit, entryPrice, 100*profit/entryPrice) if false { //current trend //trade according trend indictor if w.latestSolidTrend == 0 { w.findLatestSolidTrend(emaShort, emaLong, EMAMinThreshold, TresholdLevel, length) } w.trade(emaShort, emaLong, EMAMinThreshold, TresholdLevel, length, tradeOnlyAfterSwitch, tradeAmount) } }
func (w *Huobi) doMACD(Time []string, Price []float64) { if len(Price) == 0 { return } //read config shortEMA, _ := strconv.Atoi(Config["shortEMA"]) middleEMA, _ := strconv.Atoi(Config["middleEMA"]) longEMA, _ := strconv.Atoi(Config["longEMA"]) signalPeriod, _ := strconv.Atoi(Config["signalPeriod"]) MACDMinThreshold, err := strconv.ParseFloat(Config["MACDMinThreshold"], 64) if err != nil { logger.Debugln("config item MACDMinThreshold is not float") return } tradeAmount := Config["MACDtradeAmount"] //compute the indictor emaShort := EMA(Price, shortEMA) emaMiddle := EMA(Price, middleEMA) emaLong := EMA(Price, longEMA) MACDdif := getMACDdif(emaShort, emaLong) MACDSignal := getMACDSignal(MACDdif, signalPeriod) MACDHistogram := getMACDHistogram(MACDdif, MACDSignal) length := len(Price) logger.OverrideStart(w.Peroid) logger.Overridef("macd cross分析[%d/%d]\n", shortEMA, longEMA) //macd cross for i := 1; i < length; i++ { if MACDHistogram[i-1] < 0 && MACDHistogram[i] > 0 { if MACDHistogram[i]-MACDHistogram[i-1] >= 0 && Price[i] > emaMiddle[i] { logger.Overrideln("++", i, Time[i], Price[i], fmt.Sprintf("%0.02f", MACDSignal[i])) } else { logger.Overrideln(" +", i, Time[i], Price[i], fmt.Sprintf("%0.02f", MACDSignal[i])) } if i == length-1 && w.latestMACDTrend != 1 && MACDSignal[i] < -MACDMinThreshold { w.latestMACDTrend = 1 logger.Infoln("MACD has switched, 探测到买入点", w.getTradePrice("buy")) go service.TriggerTrender("MACD has switched, 探测到买入点") w.Do_buy(w.getTradePrice("buy"), tradeAmount) } } else if MACDHistogram[i-1] > 0 && MACDHistogram[i] < 0 { if MACDHistogram[i]-MACDHistogram[i-1] <= 0 && Price[i] < emaMiddle[i] { logger.Overrideln("--", i, Time[i], Price[i], fmt.Sprintf("%0.02f", MACDSignal[i])) } else { logger.Overrideln(" -", i, Time[i], Price[i], fmt.Sprintf("%0.02f", MACDSignal[i])) } if i == length-1 && w.latestMACDTrend != -1 && MACDSignal[i] > MACDMinThreshold { w.latestMACDTrend = -1 logger.Infoln("MACD has switched, 探测到卖出点", w.getTradePrice("sell")) go service.TriggerTrender("MACD has switched, 探测到卖出点") w.Do_sell(w.getTradePrice("sell"), tradeAmount) } } } }