int sendOrder(Parameters& params, std::string direction, double quantity, double price) { // define limit price to be sure to be executed double limPrice; if (direction.compare("buy") == 0) { limPrice = getLimitPrice(params, quantity, false); } else if (direction.compare("sell") == 0) { limPrice = getLimitPrice(params, quantity, true); } // signature std::ostringstream oss; oss << "amount=" << quantity << "&api_key=" << params.okCoinApi << "&price=" << limPrice << "&symbol=btc_usd&type=" << direction << "&secret_key=" << params.okCoinSecret; std::string signature = oss.str(); oss.clear(); oss.str(""); // content oss << "amount=" << quantity << "&api_key=" << params.okCoinApi << "&price=" << limPrice << "&symbol=btc_usd&type=" << direction; std::string content = oss.str(); *params.logFile << "<OKCoin> Trying to send a \"" << direction << "\" limit order: " << quantity << "@$" << limPrice << "..." << std::endl; json_t *root = authRequest(params, "https://www.okcoin.com/api/v1/trade.do", signature, content); int orderId = json_integer_value(json_object_get(root, "order_id")); *params.logFile << "<OKCoin> Done (order ID: " << orderId << ")\n" << std::endl; json_decref(root); return orderId; }
int sendOrder(Parameters& params, std::string direction, double quantity, double price) { double limPrice; // define limit price to be sure to be executed if (direction.compare("buy") == 0) { limPrice = getLimitPrice(params, quantity, false); } else if (direction.compare("sell") == 0) { limPrice = getLimitPrice(params, quantity, true); } *params.logFile << "<Bitstamp> Trying to send a \"" << direction << "\" limit order: " << quantity << "@$" << limPrice << "..." << std::endl; std::ostringstream oss; oss << "https://www.bitstamp.net/api/" << direction << "/"; std::string url = oss.str(); oss.clear(); oss.str(""); oss << "amount=" << quantity << "&price=" << std::fixed << std::setprecision(2) << limPrice; std::string options = oss.str(); json_t* root = authRequest(params, url, options); int orderId = json_integer_value(json_object_get(root, "id")); if (orderId == 0) { *params.logFile << "<Bitstamp> Order ID = 0. Message: " << json_dumps(root, 0) << std::endl; } *params.logFile << "<Bitstamp> Done (order ID: " << orderId << ")\n" << std::endl; json_decref(root); return orderId; }
/*To send order to bitstamp - direction is buy or sell*/ int sendOrder(CURL *curl, Parameters params, std::string direction, double quantity, double price) { /*Before sending order to bitstamp API, first get limitprice of the buy or sell order for the specific quantity*/ double limPrice; // define limit price to be sure to be executed if (direction.compare("buy") == 0) { limPrice = getLimitPrice(curl, quantity, false); } else if (direction.compare("sell") == 0) { limPrice = getLimitPrice(curl, quantity, true); } std::cout << "<Bitstamp> Trying to send a \"" << direction << "\" limit order: " << quantity << "@$" << limPrice << "..." << std::endl; /*class std::ostringstream : Output stream class to operate on strings. Objects of this class use a string buffer that contains a sequence of characters. This sequence of characters can be accessed directly as a string object, using member str.*/ std::ostringstream oss; oss << "https://www.bitstamp.net/api/" << direction << "/"; /* public member function std::ostringstream::str 1.string str() const; 2.void str (const string& s); Get/set content The first form (1) returns a string object with a copy of the current contents of the stream. The second form (2) sets s as the contents of the stream, discarding any previous contents. The object preserves its open mode: if this includes ios_base::ate, the writing position is moved to the end of the new sequence. */ std::string url = oss.str(); oss.clear(); oss.str(""); /*This operator (<<) applied to an output stream is known as insertion operator. It will insert objects into the output stream*/ oss << "amount=" << quantity << "&price=" << std::fixed << std::setprecision(2) << limPrice; std::string options = oss.str(); /*oss will return amount=&price=<limPrice value with precision = 2>*/ /*url used as argument to function below stores the https://www.bitstamp.net/api/" << direction API address*/ json_t *root = authRequest(curl, params, url, options);/*root will get assigned with this authRequest*/ /*"id" is a key in root JSON object. We are fetching its value*/ int orderId = json_integer_value(json_object_get(root, "id")); if (orderId == 0) { std::cout << "<Bitstamp> Order ID = 0. Message: " << json_dumps(root, 0) /*json_dumps returns the JSON form of root. 0 is a used for indentation*/ << std::endl; } std::cout << "<Bitstamp> Done (order ID: " << orderId << ")\n" << std::endl; json_decref(root); return orderId; }
int sendOrder(Parameters& params, std::string direction, double quantity, double price){ double limPrice; // define limit price to be sure to be executed if (direction.compare("buy") == 0) { limPrice = getLimitPrice(params, quantity, false); } else if (direction.compare("sell") == 0) { limPrice = getLimitPrice(params, quantity, true); } *params.logFile << "<SevenNintySix> Trying to send a \"" << direction << "\" limit order: " << quantity << "@$" << limPrice << "..." << std::endl; std::ostringstream oss; oss << "\"symbol\":\"btcusd\", \"amount\":\"" << quantity << "\", \"price\":\"" << limPrice << "\", \"exchange\":\"bitfinex\", \"side\":\"" << direction << "\", \"type\":\"limit\""; std::string options = oss.str(); json_t* root= authRequest(params, "https://796.com/v2/weeklyfutures/orders", "order/new", options); int orderId = json_integer_value(json_object_get(root, "id")); *params.logFile << "<SevenNintySix> Done (order ID: " << orderId << ")\n" << std::endl; json_decref(root); return orderId; }
int sendOrder(Parameters& params, std::string direction, double quantity, double price) { if (direction.compare("buy") != 0 && direction.compare("sell") != 0) { *params.logFile << "Error: Neither \"buy\" nor \"sell\" selected" << std::endl; return 0; } double limPrice; // define limit price to be sure to be executed if (direction.compare("buy") == 0) { limPrice = getLimitPrice(params, quantity, false); } else { limPrice = getLimitPrice(params, quantity, true); } *params.logFile << "<Kraken> Trying to send a \"" << direction << "\" limit order: " << quantity << " @ $" << limPrice << "..." << std::endl; std::string pair = "XXBTZUSD"; std::string type = direction; std::string ordertype = "limit"; std::string pricelimit = patch::to_string(limPrice); std::string volume = patch::to_string(quantity); std::string options = "pair=" + pair + "&type=" + type + "&ordertype=" + ordertype + "&price=" + pricelimit + "&volume=" + volume; json_t* res = authRequest(params, "https://api.kraken.com", "/0/private/AddOrder", options); json_t* root = json_object_get(res, "result"); if (json_is_object(root) == 0) { *params.logFile << json_dumps(res, 0) << std::endl; exit(0); } std::string txid = json_string_value(json_array_get(json_object_get(root, "txid"), 0)); int max_id = id_to_transaction->size(); (*id_to_transaction)[max_id] = txid; *params.logFile << "<Kraken> Done (transaction ID: " << txid << ")\n" << std::endl; json_decref(root); return max_id; }
int sendOrder(CURL *curl, Parameters params, std::string direction, double quantity, double price) { // define limit price to be sure to be executed double limPrice; if (direction.compare("buy") == 0) { limPrice = getLimitPrice(curl, quantity, false); } else if (direction.compare("sell") == 0) { limPrice = getLimitPrice(curl, quantity, true); } std::cout << "<Bitfinex> Trying to send a \"" << direction << "\" limit order: " << quantity << "@$" << limPrice << "..." << std::endl; std::ostringstream oss; oss << "\"symbol\":\"btcusd\", \"amount\":\"" << quantity << "\", \"price\":\"" << limPrice << "\", \"exchange\":\"bitfinex\", \"side\":\"" << direction << "\", \"type\":\"limit\""; std::string options = oss.str(); json_t *root = authRequest(curl, params, "https://api.bitfinex.com/v1/order/new", "order/new", options); int orderId = json_integer_value(json_object_get(root, "order_id")); std::cout << "<Bitfinex> Done (order ID: " << orderId << ")\n" << std::endl; json_decref(root); return orderId; }