public COIN(string symbol, int precision, EXCHANGE exchange, bool Autobuysell, string auth) { scl = new Scorpion_LOG(); ex = exchange; Scorpion_Write.write_success("Starting coin " + symbol + " with precision " + precision); cs.buy_sell_type = Autobuysell; Scorpion_Write.write_success("Automatic buy/sells: " + cs.buy_sell_type); if (!Autobuysell) { Scorpion_Write.write_input("Please enter your buy price preference: "); cs.buy_sell_type_buy_at = Convert.ToDouble(Console.ReadLine()); Scorpion_Write.write_input("Please enter your sell price preference: "); cs.buy_sell_type_sell_at = Convert.ToDouble(Console.ReadLine()); } cs.precision = precision; cs.symbol = symbol; cs.period = "1"; cs.unit = "HOURS"; cs.signal = 0; cs.key = auth; cs.current_balance = 0; cs.EUR_balance = 0; //start_ticker(); return; }
private void kill() { Scorpion_Write.write_error("Aborting " + cs.symbol); market_thread.Dispose(); coin_thread.Abort(); return; }
private void start_ticker() { const int interval = 30000; Scorpion_Write.write_success("Running coin ticker/trader " + cs.symbol + " every: " + (interval / 1000) + " seconds"); market_thread = new Timer(coin_thread_start); market_thread.Change(0, interval); return; }
private void sell() { Scorpion_Write.write("Selling " + cs.symbol); cs.sell_id = order_id(); if (cs.current_balance > 0) { Scorpion_Write.write_success("Selling " + cs.current_balance + " of " + cs.symbol + " | ID: " + cs.sell_id); string json_sell = ex.xorder(ref cs.symbol, "SELL", "MARKET", convert_comma_dot(cs.current_balance.ToString()), cs.sell_id); Scorpion_Write.write_success(json_sell); } else { Scorpion_Write.write_error("Unable to sell not enough " + cs.symbol + " to sell at " + cs.current_balance); } scl.write("Signal:" + cs.signal + " Symbol:" + cs.symbol + " Current price:" + cs.current_price + " High:" + cs.high + " Low:" + cs.low + " Tax:" + calculate_tax()); return; }
private void trade() { //Do buysell operation if buy_sell_type not auto kill after transaction //Check and compare averages vs current price if (cs.signal == 2) { get_balance(); buy(); } else if (cs.signal == 1) { get_balance(); sell(); } else { Scorpion_Write.write_notice("Neutral on " + cs.symbol); } return; }
private void buy() { Scorpion_Write.write("Buying " + cs.symbol); get_min(); cs.buy_id = order_id(); Scorpion_Write.write("Got new order id: " + cs.buy_id); if (cs.EUR_balance > 0 && check_min_buy()) { //buy Scorpion_Write.write_success("Buying " + cs.EUR_balance + " " + ex.bfx_url.PREFFERED_FIAT + " | ID: " + cs.buy_id); string json_buy = ex.xorder(ref cs.symbol, "BUY", "MARKET", convert_comma_dot((cs.EUR_balance / cs.current_price).ToString()), cs.buy_id); Scorpion_Write.write_success(json_buy); } else { Scorpion_Write.write_error("Unable to buy " + cs.symbol + " not enough FIAT " + ex.bfx_url.PREFFERED_FIAT + ": " + cs.EUR_balance + ", Required minimum " + ex.bfx_url.PREFFERED_FIAT + ": " + cs.min); } scl.write("Signal:" + cs.signal + " Symbol:" + cs.symbol + " Current price:" + cs.current_price + " High:" + cs.high + " Low:" + cs.low); return; }
private void market() { clear_all(); cs.time_interval = scdt.process_datetime(); cs.CANDLE_JSON = ex.xcandles(cs.symbol, cs.unit, ex.bfx_url.period, cs.time_interval[0], cs.time_interval[1], cs.time_interval[2], cs.time_interval[3]); //Get current price string tickJSON = ex.xinstrumentticker(cs.symbol); JObject tickarr = json.jsontoobject(ref tickJSON); cs.current_price = tickarr.Value <double>("last_price"); //Get Week averages try { JArray jarr = json.jsontoarray(ref cs.CANDLE_JSON); //Create arrays on the fly in order to mitigat unwanted values cs.highs = new double[jarr.Count]; cs.lows = new double[jarr.Count]; int ndx = 0; foreach (JObject jobj in jarr) { cs.highs[ndx] = (jobj.Value <double>("high")); cs.lows[ndx] = (jobj.Value <double>("low")); ndx++; } cs.high_average = coin_average(cs.highs); cs.low_average = coin_average(cs.lows); cs.low = cs.lows.Min(); cs.high = cs.highs.Max(); cs.high_mid_average = coin_average(new double[2] { cs.high, cs.high_average }); cs.low_mid_average = coin_average(new double[2] { cs.low, cs.low_average }); //Get Current price and check against averages to create signal //Code here get_signal(); trade(); if (!cs.buy_sell_type) { Scorpion_Write.write_notice("Manual buy/sell active [Buy at: " + cs.buy_sell_type_buy_at + "] [Sell at: " + cs.buy_sell_type_sell_at + "]"); } Console.WriteLine("Decoded {0} on (Auto Buy/Sell: {6}) [Current price: {5}][High: {1}][Low: {2}][Average High: {3}][Average Low: {4}][Mid-Average High: {7}][Mid-Average Low: {8}]", cs.symbol, cs.high, cs.low, cs.high_average, cs.low_average, cs.current_price, cs.buy_sell_type, cs.high_mid_average, cs.low_mid_average); if (cs.failed == true) { cs.failed = false; Scorpion_Write.write_notice("Failure signals cleared"); } } catch (Exception e) { Scorpion_Write.write_error("Unable to decode " + cs.symbol + ". Aborting on next failure. Error[" + e.Message + "])\n"); if (cs.failed) { kill(); } cs.failed = true; } return; }
public string xorder(ref string symbol, string side, string type, string amount, string id) { Scorpion_Write.write_notice("Amount to " + type + " is: " + amount); return(json.JSON_post_auth(bfx_url.base_URL + bfx_url.orders, bfx_settings.key, new string[] { }, new string[] { }, true, "{\"instrument_code\" : \"" + symbol + "_EUR\", \"side\": \"" + side + "\",\"type\":\"" + type + "\", \"amount\":\"" + amount + "\"}"));//, \"client_id\":\"" + id + "\"}"); }