├── packages.config ├── README.md ├── .gitignore ├── TradingBot.sln ├── Run.cs ├── Globals.cs ├── TradingBot.csproj ├── Models.cs ├── Algorithm.cs ├── Helper.cs └── API.cs /packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TradingBot 2 | Learning C# by building a simple trading bot with the Binance API 3 | 4 | -A simple stock quantity maximizing technique is used for holding purposes -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #Xamarin stuff 2 | Components 3 | [Pp]ackages 4 | *.userprefs 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.sln.docstates 10 | 11 | # Build results 12 | [Dd]ebug/ 13 | [Dd]ebugPublic/ 14 | [Rr]elease/ 15 | x64/ 16 | build/ 17 | bld/ 18 | [Bb]in/ 19 | [Oo]bj/ 20 | 21 | *_i.c 22 | *_p.c 23 | *_i.h 24 | *.ilk 25 | *.meta 26 | *.obj 27 | *.pch 28 | *.pdb 29 | *.pgc 30 | *.pgd 31 | *.rsp 32 | *.sbr 33 | *.tlb 34 | *.tli 35 | *.tlh 36 | *.tmp 37 | *.tmp_proj 38 | *.log 39 | *.vspscc 40 | *.vssscc 41 | .builds 42 | *.pidb 43 | *.svclog 44 | *.scc 45 | 46 | node_modules/ 47 | .DS_Store 48 | *.userprefs 49 | readMe.txt 50 | -------------------------------------------------------------------------------- /TradingBot.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TradingBot", "TradingBot.csproj", "{C41A2807-ED33-4615-B8CE-F1335CBA9143}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|x86 = Debug|x86 9 | Release|x86 = Release|x86 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {C41A2807-ED33-4615-B8CE-F1335CBA9143}.Debug|x86.ActiveCfg = Debug|x86 13 | {C41A2807-ED33-4615-B8CE-F1335CBA9143}.Debug|x86.Build.0 = Debug|x86 14 | {C41A2807-ED33-4615-B8CE-F1335CBA9143}.Release|x86.ActiveCfg = Release|x86 15 | {C41A2807-ED33-4615-B8CE-F1335CBA9143}.Release|x86.Build.0 = Release|x86 16 | EndGlobalSection 17 | EndGlobal 18 | -------------------------------------------------------------------------------- /Run.cs: -------------------------------------------------------------------------------- 1 | using TradingBot.Models; 2 | using System; 3 | using System.Timers; 4 | 5 | 6 | 7 | namespace TradingBot 8 | { 9 | public class Run 10 | { 11 | static System.Threading.ManualResetEvent timerFired = new System.Threading.ManualResetEvent(false); 12 | 13 | public static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 14 | { 15 | Console.WriteLine("Timer Fired."); 16 | Algorithm.Bot(); 17 | } 18 | 19 | 20 | public static void Main(string[] args) 21 | { 22 | //Start Bot the first time, before timer 23 | Algorithm.Bot(); 24 | 25 | //Start Timer 26 | Timer timer = new Timer(); 27 | timer.Interval = Globals.timerInterval * 60 * 1000; // converts ms to minutes 28 | timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 29 | timer.Enabled = true; 30 | 31 | timerFired.WaitOne(); //https://stackoverflow.com/questions/34958759/timer-does-not-fire-before-application-ends-in-c-sharp?rq=1 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /Globals.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | 4 | namespace TradingBot 5 | { 6 | public static class Globals 7 | { 8 | 9 | public static string[] keys = System.IO.File.ReadAllLines(@"/Users/aditi/Desktop/Bot/binanceInfo/apiKeys.txt"); 10 | 11 | //Api Key 12 | public static string ApiKey { get; set; } = keys[0]; 13 | 14 | //Secret Key 15 | public static string SecretKey { get; set; } = keys[1]; 16 | 17 | //Currency1 18 | public static string C1 = "XRP"; 19 | 20 | //Currency2 - should be a trade option that exists for Currency1, such as BTC or ETH 21 | public static string C2 = "BTC"; 22 | 23 | //Minimum quantity relies on currency traded 24 | public static double quatityPerTrade = 5; 25 | 26 | //Percentage threshold for trade to execute 27 | public static double percentageChange = 0.5; 28 | 29 | //Interval after which algorithm should run 30 | public static double timerInterval = 0.3; //in minutes 31 | 32 | //Set to true to make a test order. 33 | //Console will write trade details of last real trade if test 34 | public static bool testCase = true; 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /TradingBot.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | x86 6 | {C41A2807-ED33-4615-B8CE-F1335CBA9143} 7 | Exe 8 | TradingBot 9 | TradingBot 10 | v4.5 11 | 12 | 13 | true 14 | full 15 | false 16 | bin\Debug 17 | DEBUG; 18 | prompt 19 | 4 20 | x86 21 | 22 | 23 | true 24 | bin\Release 25 | prompt 26 | 4 27 | x86 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Models.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace TradingBot.Models 4 | { 5 | public class ServerTime 6 | { 7 | public long Time { get; set; } 8 | } 9 | 10 | public class SymbolPrice 11 | { 12 | public string Symbol { get; set; } 13 | public double Price { get; set; } 14 | } 15 | 16 | public class SymbolTicker 17 | { 18 | public string Symbol { get; set; } 19 | public double BidPrice { get; set; } 20 | public double BidQty { get; set; } 21 | public double AskPrice { get; set; } 22 | public double AskQty { get; set; } 23 | } 24 | 25 | public class AccountInformation 26 | { 27 | public double MakerCommission { get; set; } 28 | public double TakerCommission { get; set; } 29 | public double BuyerCommission { get; set; } 30 | public double SellerCommission { get; set; } 31 | public bool CanTrade { get; set; } 32 | public bool CanWithdraw { get; set; } 33 | public bool CanDeposit { get; set; } 34 | public List Balances { get; set; } 35 | } 36 | 37 | public class AssetBalance 38 | { 39 | public string Asset { get; set; } 40 | public double Free { get; set; } 41 | public double Locked { get; set; } 42 | } 43 | 44 | public class Trades 45 | { 46 | public long Id { get; set; } 47 | public long OrderId { get; set; } 48 | public double Price { get; set; } 49 | public double Qty { get; set; } 50 | public string Commission { get; set; } 51 | public string commissionAsset { get; set; } 52 | public long Time { get; set; } 53 | public bool isBuyer { get; set; } 54 | public bool isMaker { get; set; } 55 | public bool isBestMatch { get; set; } 56 | } 57 | 58 | public class Order 59 | { 60 | public string Symbol { get; set; } 61 | public long OrderId { get; set; } 62 | public string ClientOrderid { get; set; } 63 | public double Price { get; set; } 64 | public double OrigQty { get; set; } 65 | public string ExecutedQty { get; set; } 66 | public OrderStatuses Status { get; set; } 67 | public TimesInForce TimeInForce { get; set; } 68 | public OrderTypes Type { get; set; } 69 | public OrderSides Side { get; set; } 70 | public double StopPrice { get; set; } 71 | public double IcebergQty { get; set; } 72 | public long Time { get; set; } 73 | } 74 | 75 | 76 | public enum OrderStatuses 77 | { 78 | NEW, 79 | PARTIALLY_FILLED, 80 | FILLED, 81 | CANCELED, 82 | PENDING_CANCEL, 83 | REJECTED, 84 | EXPIRED, 85 | } 86 | public enum OrderTypes 87 | { 88 | LIMIT, 89 | MARKET, 90 | STOP_LOSS, 91 | STOP_LOSS_LIMIT, 92 | TAKE_PROFIT, 93 | TAKE_PROFIT_LIMIT, 94 | LIMIT_MAKER 95 | } 96 | public enum OrderSides 97 | { 98 | BUY, 99 | SELL 100 | } 101 | public enum TimesInForce 102 | { 103 | GTC, 104 | IOC, 105 | FOK 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /Algorithm.cs: -------------------------------------------------------------------------------- 1 | 2 | using TradingBot.Models; 3 | using System; 4 | using System.Linq; 5 | 6 | 7 | 8 | namespace TradingBot 9 | { 10 | public class Algorithm 11 | { 12 | 13 | 14 | public static void Bot() 15 | { 16 | string symbol = Globals.C1 + Globals.C2; 17 | 18 | //GetAccountInformation 19 | var accInfo = API.GetAccountInformation(); 20 | 21 | //Check account funds for both currencies 22 | AssetBalance Currency1 = (from coin in accInfo.Balances where coin.Asset == Globals.C1 select coin).FirstOrDefault(); 23 | var freeCurrency1 = Currency1.Free; 24 | 25 | AssetBalance Currency2 = (from coin in accInfo.Balances where coin.Asset == Globals.C2 select coin).FirstOrDefault(); 26 | var freeCurrency2 = Currency2.Free; 27 | 28 | //Get Price of last BCCBTC trade 29 | var lastTrade = API.GetLastTrade(symbol); 30 | var lastTradePrice = 0.0; 31 | var lastTradeSide = OrderSides.SELL; //if lastTrade == null, this means buy Currency1 32 | 33 | 34 | //get price and OrderSide of last trade (if any) 35 | if (lastTrade != null) 36 | { 37 | lastTradePrice = lastTrade.Price; 38 | 39 | if (lastTrade.isBuyer == true) 40 | lastTradeSide = OrderSides.BUY; 41 | else 42 | lastTradeSide = OrderSides.SELL; 43 | } 44 | 45 | //Check current price 46 | var currentPrice = API.GetOnePrice(symbol); 47 | 48 | //Calculate actual price change percentage 49 | var priceChange = 100 * (currentPrice - lastTradePrice) / currentPrice; 50 | 51 | Console.WriteLine("Current Price is " + currentPrice); 52 | Console.WriteLine("Price Change is " + priceChange); 53 | 54 | //Create Order 55 | Order marketOrder = null; 56 | 57 | if (lastTradeSide == OrderSides.BUY && priceChange > Globals.percentageChange) 58 | { 59 | //if last order was buy, and price has increased 60 | //sell C1 61 | marketOrder = API.PlaceMarketOrder(symbol, OrderSides.SELL, Globals.quatityPerTrade); 62 | 63 | } 64 | else if (lastTradeSide == OrderSides.SELL && priceChange < -Globals.percentageChange) 65 | { 66 | //if last order was sell, and price has decreased 67 | //buy C1 68 | marketOrder = API.PlaceMarketOrder(symbol, OrderSides.BUY, Globals.quatityPerTrade); 69 | } 70 | 71 | 72 | //Statements 73 | if (marketOrder == null) 74 | { 75 | Console.WriteLine("No trade was made."); 76 | var actualLastTrade = API.GetLastTrade(symbol); 77 | if (actualLastTrade.isBuyer == true) 78 | { 79 | Console.WriteLine(actualLastTrade.Qty + " of " + Globals.C1 + " was previously bought for " + actualLastTrade.Price); 80 | } 81 | else if (actualLastTrade.isBuyer == false) 82 | { 83 | Console.WriteLine(actualLastTrade.Qty + " of " + Globals.C1 + " was previously sold for " + actualLastTrade.Price); 84 | } 85 | } 86 | else 87 | { 88 | var newLastTrade = API.GetLastTrade(symbol); 89 | if (marketOrder.Side == OrderSides.BUY) 90 | { 91 | Console.WriteLine(newLastTrade.Qty + " of " + Globals.C1 + " was bought for " + newLastTrade.Price); 92 | } 93 | else if (marketOrder.Side == OrderSides.SELL) 94 | { 95 | Console.WriteLine(newLastTrade.Qty + " of " + Globals.C1 + " was sold for " + newLastTrade.Price); 96 | } 97 | } 98 | 99 | } 100 | } 101 | } -------------------------------------------------------------------------------- /Helper.cs: -------------------------------------------------------------------------------- 1 | using TradingBot.Models; 2 | using System; 3 | using System.Net; 4 | using System.Text; 5 | using Newtonsoft.Json; 6 | using System.Security.Cryptography; 7 | 8 | 9 | namespace TradingBot 10 | { 11 | public static class Helper 12 | { 13 | /// 14 | /// Takes date and returns timestamp. 15 | /// 16 | /// timestamp in milliseconds 17 | /// Current date. 18 | /// Only works for adding timestamp to Url if timestamp entered is UTC 19 | /// Otherwise, recvWindow is required for other timestamps, less than UTC only! 20 | public static long DateToTimestamp(DateTime date) 21 | { 22 | var timeSt = (long)(date - new DateTime(1970, 1, 1)).TotalMilliseconds; 23 | return timeSt; 24 | } 25 | 26 | /// 27 | /// Gets the current time stamp for UTC 28 | /// 29 | /// Returns timestamp in milliseconds 30 | public static long getTimeStamp() 31 | { 32 | var timeSt = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds; 33 | return timeSt; 34 | } 35 | 36 | /// 37 | /// Test Connectivity to Binance Rest API and get current server time object 38 | /// 39 | /// Object of current time in milliseconds 40 | /// Should not be used for timestamp parameter in url, delay can be too long. 41 | public static ServerTime getServerTimeObject() 42 | { 43 | string apiRequestUrl = "https://www.binance.com/api/v1/time"; 44 | 45 | string response = webRequest(apiRequestUrl, "GET", null); 46 | var serverTime = JsonConvert.DeserializeObject(response); 47 | return serverTime; 48 | } 49 | 50 | /// 51 | /// Get current server time 52 | /// 53 | /// Long of current time in milliseconds 54 | public static long getServerTime() 55 | { 56 | var serverTime = getServerTimeObject(); 57 | return serverTime.Time; 58 | } 59 | 60 | 61 | /// 62 | /// Converts bytes to string. 63 | /// 64 | /// String 65 | /// Variable that holds bytes 66 | public static string ByteToString(byte[] buff) //from StackOverflow 67 | { 68 | string str = ""; 69 | for (int i = 0; i < buff.Length; i++) 70 | str += buff[i].ToString("X2"); 71 | return str; 72 | } 73 | 74 | 75 | public static string addReceiveWindow(string query) 76 | { 77 | query = $"{query}&recvWindow={5}"; 78 | return query; 79 | } 80 | 81 | /// 82 | /// Create HttpWebRequest with Signature if needed 83 | /// 84 | /// Url to be requested. String. 85 | /// Type of HTTP request. Eg; GET/POST. String. 86 | /// Public Key to be added to header if signed. String. 87 | /// HTTP Request Responce 88 | public static string webRequest(string requestUrl, string method, string ApiKey) 89 | { 90 | try 91 | { 92 | var request = (HttpWebRequest)WebRequest.Create(requestUrl); 93 | request.Method = method; 94 | request.Timeout = 5000; //very long response time from Singapore. Change in Boston accordingly. 95 | if (ApiKey != null) 96 | { 97 | request.Headers.Add("X-MBX-APIKEY", ApiKey); 98 | } 99 | 100 | var webResponse = (HttpWebResponse)request.GetResponse(); 101 | if (webResponse.StatusCode != HttpStatusCode.OK) 102 | { 103 | throw new Exception($"Did not return OK 200. Returned: {webResponse.StatusCode}"); 104 | } 105 | 106 | var encoding = ASCIIEncoding.ASCII; 107 | string responseText = null; 108 | 109 | using (var reader = new System.IO.StreamReader(webResponse.GetResponseStream(), encoding)) 110 | { 111 | responseText = reader.ReadToEnd(); 112 | } 113 | 114 | return responseText; 115 | } 116 | catch (WebException webEx) 117 | { 118 | if (webEx.Response != null) 119 | { 120 | Encoding encoding = ASCIIEncoding.ASCII; 121 | using (var reader = new System.IO.StreamReader(webEx.Response.GetResponseStream(), encoding)) 122 | { 123 | string responseText = reader.ReadToEnd(); 124 | throw new Exception(responseText); 125 | } 126 | } 127 | throw; 128 | } 129 | catch 130 | { 131 | return "Error"; 132 | } 133 | } 134 | 135 | /// 136 | /// Create Signature 137 | /// 138 | /// Secret Key for HMAC signature. String. 139 | /// Text to be signed. String. 140 | /// Signature 141 | public static string getSignature(string SecretKey, string query) 142 | { 143 | Encoding encoding = Encoding.UTF8; 144 | var keyByte = encoding.GetBytes(SecretKey); 145 | using (var hmacsha256 = new HMACSHA256(keyByte)) 146 | { 147 | hmacsha256.ComputeHash(encoding.GetBytes(query)); 148 | return ByteToString(hmacsha256.Hash); 149 | } 150 | 151 | } 152 | 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /API.cs: -------------------------------------------------------------------------------- 1 | using TradingBot.Models; 2 | using Newtonsoft.Json; 3 | using System; 4 | using System.Linq; 5 | using System.Timers; 6 | using System.ComponentModel; 7 | 8 | namespace TradingBot 9 | { 10 | public class API 11 | { 12 | /// 13 | /// The Bot takes a currency pair, checks previous trades for the pair 14 | /// Only the opposite of the last trade can be executed (maximising stock quantity technique) 15 | /// If the price has changed according to the desired percentage, a trade is executed. 16 | /// 17 | 18 | // Do not change from here 19 | public static string baseUrl { get; set; } = "https://www.binance.com/api/"; 20 | 21 | /// 22 | /// Test Connectivity to Binance Rest API 23 | /// 24 | /// Boolean value (True/False) based on success 25 | public static bool Ping() 26 | { 27 | string apiRequestUrl = $"{baseUrl}v1/ping"; 28 | 29 | string response = Helper.webRequest(apiRequestUrl, "GET", null); 30 | if (response == "{}") 31 | return true; 32 | else 33 | return false; 34 | } 35 | 36 | /// 37 | /// Test Connectivity to Binance Rest API and get current server time 38 | /// 39 | /// String of current time in milliseconds 40 | public static string Time() 41 | { 42 | string apiRequestUrl = $"{baseUrl}v1/time"; 43 | 44 | var response = Helper.webRequest(apiRequestUrl, "GET", null); 45 | return response; 46 | } 47 | 48 | 49 | /// 50 | /// Get prices for all symbols 51 | /// 52 | /// Current price for all symbols 53 | public static SymbolPrice[] GetAllPrices() 54 | { 55 | string apiRequestUrl = $"{baseUrl}v1/ticker/allPrices"; 56 | 57 | var response = Helper.webRequest(apiRequestUrl, "GET", null); 58 | var parsedResponse = JsonConvert.DeserializeObject(response); 59 | 60 | return parsedResponse; 61 | } 62 | 63 | 64 | /// 65 | /// Get the current price for the given symbol 66 | /// 67 | /// Asset symbol e.g.ETHBTC 68 | /// The price. Double. 69 | public static double GetOnePrice(string symbol) 70 | { 71 | var symbolPrices = GetAllPrices(); 72 | 73 | SymbolPrice symbolPrice = (from sym in symbolPrices where sym.Symbol == symbol select sym).FirstOrDefault(); 74 | if (symbolPrice == null) 75 | { 76 | throw new ApplicationException($"No symbol, {symbol}, exists"); 77 | } 78 | 79 | return symbolPrice.Price; 80 | } 81 | 82 | 83 | /// 84 | /// Get AccountInformation including all Asset Balances 85 | /// 86 | /// Interval (in milliseconds) in which the request must be processed within a certain number of milliseconds or be rejected by the server. Defaults to 5000 milliseconds 87 | /// AccountInformation object including current asset balances 88 | public static AccountInformation GetAccountInformation() 89 | { 90 | string apiRequestUrl = $"{baseUrl}v3/account"; 91 | 92 | string query = $""; 93 | query = $"{query}×tamp={Helper.getTimeStamp()}"; 94 | 95 | var signature = Helper.getSignature(Globals.SecretKey, query); 96 | query += "&signature=" + signature; 97 | 98 | apiRequestUrl += "?" + query; 99 | 100 | var response = Helper.webRequest(apiRequestUrl, "GET", Globals.ApiKey); 101 | 102 | var parsedResponse = JsonConvert.DeserializeObject(response); 103 | return parsedResponse; 104 | } 105 | 106 | /// 107 | /// Get your Open Orders for the given symbol 108 | /// 109 | /// Asset symbol e.g.ETHBTC 110 | /// A list of Order objects with the order data 111 | public static Order[] GetOpenOrders(string symbol) 112 | { 113 | string apiRequestUrl = $"{baseUrl}v3/openOrders"; 114 | 115 | string query = $"symbol={symbol}"; 116 | 117 | query = $"{query}×tamp={Helper.getTimeStamp()}"; 118 | 119 | var signature = Helper.getSignature(Globals.SecretKey, query); 120 | 121 | query += "&signature=" + signature; 122 | 123 | apiRequestUrl += "?" + query; 124 | 125 | var response = Helper.webRequest(apiRequestUrl, "GET", Globals.ApiKey); 126 | var parsedResponse = JsonConvert.DeserializeObject(response); 127 | return parsedResponse; 128 | } 129 | 130 | /// 131 | /// Get trades for a specific account and symbol 132 | /// 133 | /// Asset symbol e.g.ETHBTC 134 | /// A list of Trades 135 | public static Trades[] GetMyTrades(string symbol) 136 | { 137 | string apiRequestUrl = $"{baseUrl}v3/myTrades"; 138 | 139 | string query = $"symbol={symbol}"; 140 | 141 | query = $"{query}×tamp={Helper.getTimeStamp()}"; 142 | 143 | var signature = Helper.getSignature(Globals.SecretKey, query); 144 | query += "&signature=" + signature; 145 | 146 | apiRequestUrl += "?" + query; 147 | 148 | var response = Helper.webRequest(apiRequestUrl, "GET", Globals.ApiKey); 149 | var parsedResponse = JsonConvert.DeserializeObject(response); 150 | return parsedResponse; 151 | } 152 | 153 | /// 154 | /// Gets the last trade. 155 | /// 156 | /// The last trade. Trades object 157 | /// Symbol. 158 | public static Trades GetLastTrade(string symbol) 159 | { 160 | var parsedResponse = GetMyTrades(symbol); 161 | 162 | if (parsedResponse.Length != 0) 163 | return parsedResponse[parsedResponse.Length - 1]; 164 | else 165 | return null; 166 | } 167 | 168 | /// 169 | /// Places an order 170 | /// 171 | /// The order object 172 | /// Symbol of currencies to be traded, eg BCCETH 173 | /// Order Side, BUY or SELL 174 | /// Order Type, see Set.OrderTypes 175 | /// Time order will be active for. 176 | /// Amount to be traded 177 | /// Price to be bought at. 178 | 179 | public static Order PlaceOrder(string symbol, OrderSides side, OrderTypes type, TimesInForce timeInForce, double quantity, double price) 180 | { 181 | string apiRequestUrl = ""; 182 | 183 | if (Globals.testCase == true) 184 | apiRequestUrl = $"{baseUrl}v3/order/test"; 185 | else 186 | apiRequestUrl = $"{baseUrl}v3/order"; 187 | 188 | 189 | string query = $"symbol={symbol}&side={side}&type={type}&timeInForce={timeInForce}&quantity={quantity}&price={price}"; 190 | 191 | query = $"{query}×tamp={Helper.getTimeStamp()}"; 192 | 193 | var signature = Helper.getSignature(Globals.SecretKey, query); 194 | query += "&signature=" + signature; 195 | 196 | apiRequestUrl += "?" + query; 197 | var response = Helper.webRequest(apiRequestUrl, "POST", Globals.ApiKey); 198 | 199 | var parsedResponse = JsonConvert.DeserializeObject(response); 200 | return parsedResponse; 201 | } 202 | 203 | /// 204 | /// Places a market order. (Order at the current market price, needs no price or timeInForce params). 205 | /// 206 | /// The order object 207 | /// Symbol of currencies to be traded, eg BCCETH. 208 | /// Order Side, BUY or SELL. 209 | /// Amount to be traded. 210 | 211 | public static Order PlaceMarketOrder(string symbol, OrderSides side, double quantity) 212 | { 213 | string apiRequestUrl = ""; 214 | 215 | if (Globals.testCase == true) 216 | apiRequestUrl = $"{baseUrl}v3/order/test"; 217 | else 218 | apiRequestUrl = $"{baseUrl}v3/order"; 219 | 220 | string query = $"symbol={symbol}&side={side}&type={OrderTypes.MARKET}&quantity={quantity}"; 221 | query = $"{query}×tamp={Helper.getTimeStamp()}"; 222 | 223 | var signature = Helper.getSignature(Globals.SecretKey, query); 224 | query += "&signature=" + signature; 225 | 226 | apiRequestUrl += "?" + query; 227 | var response = Helper.webRequest(apiRequestUrl, "POST", Globals.ApiKey); 228 | 229 | var parsedResponse = JsonConvert.DeserializeObject(response); 230 | return parsedResponse; 231 | } 232 | 233 | 234 | /*********** 235 | 236 | //Ping 237 | var pingTest = Ping(); 238 | 239 | //Time 240 | var timeTest = Time(); 241 | 242 | ///Get your account Info 243 | //Returned as object, see Set.cs to parse through it 244 | var accountInfo = GetAccountInformation(); 245 | 246 | ///Get prices of all available currency pairs 247 | //Returned as list of objects, see Set.cs or GetOnePrice() function to parse through it 248 | var allPrices = GetAllPrices(); 249 | 250 | ///Get prices of a specific currency pair/symbol 251 | var onePrice = GetOnePrice("BCCETH"); 252 | 253 | ///Get all open orders on your account 254 | var openOrders = GetOpenOrders("BCCETH"); 255 | 256 | ///Get your trade history related to a specific currency pair/symbol 257 | //Returned as list of objects, see Set.cs or GetLastTrade() function to parse through it 258 | var trades = GetMyTrades("XRPETH"); 259 | 260 | ///Get your last trade of a specific currency pair/symbol 261 | //Returned as object, see Set.cs to parse through it 262 | var lastTrade = GetLastTrade("XRPETH"); 263 | 264 | ///Place any kind of acccepted order, set to "test" phase as not in use. 265 | //var order = PlaceOrder("BCCETH", OrderSides.SELL, OrderTypes.MARKET, TimesInForce.GTC, 0.01, 2.09); 266 | 267 | ///Place a Market order 268 | var marketOrder = PlaceMarketOrder("BCCETH", OrderSides.SELL, 0.01); 269 | 270 | **********/ 271 | 272 | } 273 | } 274 | --------------------------------------------------------------------------------