public int executeBotBuy(int index) { if (setting.pauseBuy) { return(0); } if (setting.top <= index) { return(0); } if (holdKRW - setting.limit < setting.krw * 1.0005d) { return(0); } string coinName = coinList[index]; if (state.Tables[coinName].Rows.Count >= setting.time && setting.time != 0) { return(0); } int ret; if ((ret = buyDecision(index)) < 1) { return(ret); } Dictionary <string, string> par = new Dictionary <string, string>(); par.Add("market", "KRW-" + coinName); par.Add("side", "bid"); par.Add("price", setting.krw.ToString()); par.Add("ord_type", "price"); JObject jObject = apiData.order(par); if (jObject == null) { executionStr.Add(new Output(0, "Bot Execution", "Fail to buy " + coinName + " (NULL)")); return(-2); } DataRow row = order.NewRow(); row["coinName"] = coinName; row["uuid"] = jObject["uuid"]; order.Rows.Add(row); lastTrade[coinName] = DateTime.Now; return(1); }
// execute 'coinName' trading // when is not place trade, price does not need public JObject executeDeal (bool isBuy, bool isPlace, string coinName, double units, double price, double total) { Dictionary <string, string> par = new Dictionary <string, string>(); par.Add("market", "KRW-" + coinName); if (isPlace) { if (isBuy) { par.Add("side", "bid"); par.Add("volume", units.ToString("0.########")); par.Add("price", price.ToString("0.########")); par.Add("ord_type", "limit"); return(apiData.order(par)); } else { par.Add("side", "ask"); par.Add("volume", units.ToString("0.########")); par.Add("price", price.ToString("0.########")); par.Add("ord_type", "limit"); return(apiData.order(par)); } } else { if (isBuy) { par.Add("side", "bid"); par.Add("price", total.ToString("0.########")); par.Add("ord_type", "price"); return(apiData.order(par)); } else { par.Add("side", "ask"); par.Add("volume", units.ToString("0.########")); par.Add("ord_type", "market"); return(apiData.order(par)); } } }
// execute macro setting public int executeMacro(int index) { if (index >= setting.Count) { return(-1); } string coinName = setting[index].coinName; DataTable buyCandle; DataTable sellCandle = min30_candle.Tables[coinName]; if (setting[index].week_from > -1500d) { buyCandle = week_candle.Tables[coinName]; } else if (setting[index].day_from > -1500d) { buyCandle = day_candle.Tables[coinName]; } else if (setting[index].hour4_from > -1500d) { buyCandle = hour4_candle.Tables[coinName]; } else if (setting[index].hour1_from > -1500d) { buyCandle = hour1_candle.Tables[coinName]; } else if (setting[index].min30_from > -1500d) { buyCandle = min30_candle.Tables[coinName]; } else { buyCandle = null; } if (buyCandle == null || sellCandle == null) { executionStr.Add(new output(2, "Macro setting error : " + coinName + " candle data")); return(-1); } if (buyCandle.Rows.Count < 28) { executionStr.Add(new output(2, "Macro buy candle data error : " + coinName + " candle data is not enouph")); return(-1); } if (sellCandle.Rows.Count < 28) { executionStr.Add(new output(2, "Macro sell candle data error : " + coinName + " candle data is not enouph")); return(-1); } double sellOpen = (double)sellCandle.Rows[0]["open"]; double sellClose = (double)sellCandle.Rows[0]["close"]; double sellLastAverage = (sellOpen + sellClose) / 2; double buyOpen = (double)buyCandle.Rows[0]["open"]; double buyclose = (double)buyCandle.Rows[0]["close"]; double buyLastAverage = (buyOpen + buyclose) / 2; //top price == sell if (lastPrice[coinName] < sellLastAverage) { if (sellOpen >= sellClose) { return(0); } if (state.Tables[coinName].Rows.Count < 1) { return(0); } double unit = 0d; double minPrice = 1000000000d; for (int i = 0; i < state.Tables[coinName].Rows.Count; i++) { if (minPrice > (double)state.Tables[coinName].Rows[i]["price"]) { unit = (double)state.Tables[coinName].Rows[i]["unit"]; minPrice = (double)state.Tables[coinName].Rows[i]["price"]; } } minPrice *= (100d + setting[index].yield) / 100d; if (lastPrice[coinName] < minPrice) { return(0); } if (setting[index].week_to > -1500d && getBollingerResult(coinName, ac.CANDLE_WEEK, setting[index].week_to, lastPrice[coinName]) < 0) { return(0); } if (setting[index].day_to > -1500d && getBollingerResult(coinName, ac.CANDLE_DAY, setting[index].day_to, lastPrice[coinName]) < 0) { return(0); } if (setting[index].hour4_to > -1500d && getBollingerResult(coinName, ac.CANDLE_HOUR4, setting[index].hour4_to, lastPrice[coinName]) < 0) { return(0); } if (setting[index].hour1_to > -1500d && getBollingerResult(coinName, ac.CANDLE_HOUR1, setting[index].hour1_to, lastPrice[coinName]) < 0) { return(0); } if (setting[index].min30_to > -1500d && getBollingerResult(coinName, ac.CANDLE_MIN30, setting[index].min30_to, lastPrice[coinName]) < 0) { return(0); } Dictionary <string, string> par = new Dictionary <string, string>(); par.Add("market", "KRW-" + coinName); par.Add("side", "ask"); par.Add("volume", unit.ToString("0.########")); par.Add("ord_type", "market"); JObject jObject = apiData.order(par); if (jObject == null) { executionStr.Add(new output(2, "Macro sell " + coinName + " error : NULL return")); return(-2); } DataRow row = order.NewRow(); row["coinName"] = coinName; row["uuid"] = jObject["uuid"]; order.Rows.Add(row); return(1); } //bot price == buy if (lastPrice[coinName] > buyLastAverage) { if (buyOpen <= buyclose) { return(0); } if (holdKRW < setting[index].krw * 1.0005) { return(0); } if (state.Tables[coinName].Rows.Count >= setting[index].time && setting[index].time != 0) { return(0); } DateTime lastBuyDate = DateTime.Now.AddYears(-1); for (int i = 0; i < state.Tables[coinName].Rows.Count; i++) { if (DateTime.Compare(lastBuyDate, (DateTime)state.Tables[coinName].Rows[i]["date"]) < 0) { lastBuyDate = (DateTime)state.Tables[coinName].Rows[i]["date"]; } } if (setting[index].min30_from > -1500d) { if (DateTime.Compare(DateTime.Now, lastBuyDate.AddMinutes(30)) <= 0) { return(0); } } else if (setting[index].hour1_from > -1500d) { if (DateTime.Compare(DateTime.Now, lastBuyDate.AddHours(1)) <= 0) { return(0); } } else if (setting[index].hour4_from > -1500d) { if (DateTime.Compare(DateTime.Now, lastBuyDate.AddHours(4)) <= 0) { return(0); } } else if (setting[index].day_from > -1500d) { if (DateTime.Compare(DateTime.Now, lastBuyDate.AddDays(1)) <= 0) { return(0); } } else if (setting[index].week_from > -1500d) { if (DateTime.Compare(DateTime.Now, lastBuyDate.AddDays(7)) <= 0) { return(0); } } if (setting[index].week_from > -1500d && getBollingerResult(coinName, ac.CANDLE_WEEK, setting[index].week_from, lastPrice[coinName]) > 0) { return(0); } if (setting[index].day_from > -1500d && getBollingerResult(coinName, ac.CANDLE_DAY, setting[index].day_from, lastPrice[coinName]) > 0) { return(0); } if (setting[index].hour4_from > -1500d && getBollingerResult(coinName, ac.CANDLE_HOUR4, setting[index].hour4_from, lastPrice[coinName]) > 0) { return(0); } if (setting[index].hour1_from > -1500d && getBollingerResult(coinName, ac.CANDLE_HOUR1, setting[index].hour1_from, lastPrice[coinName]) > 0) { return(0); } if (setting[index].min30_from > -1500d && getBollingerResult(coinName, ac.CANDLE_MIN30, setting[index].min30_from, lastPrice[coinName]) > 0) { return(0); } Dictionary <string, string> par = new Dictionary <string, string>(); par.Add("market", "KRW-" + coinName); par.Add("side", "bid"); par.Add("price", setting[index].krw.ToString()); par.Add("ord_type", "price"); JObject jObject = apiData.order(par); if (jObject == null) { executionStr.Add(new output(2, "Macro buy " + coinName + " error : NULL return")); return(-2); } DataRow row = order.NewRow(); row["coinName"] = coinName; row["uuid"] = jObject["uuid"]; order.Rows.Add(row); return(2); } return(0); }