-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
twobeeb
committed
Jan 31, 2014
1 parent
c3384e1
commit 88a45a9
Showing
33 changed files
with
9,689 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 11.00 | ||
# Visual Studio 2010 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitfinexApi", "BitfinexApi\BitfinexApi.csproj", "{6E7AFCF2-068B-4385-AEF5-0192DEFBD8D3}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|x86 = Debug|x86 | ||
Release|x86 = Release|x86 | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{6E7AFCF2-068B-4385-AEF5-0192DEFBD8D3}.Debug|x86.ActiveCfg = Debug|x86 | ||
{6E7AFCF2-068B-4385-AEF5-0192DEFBD8D3}.Debug|x86.Build.0 = Debug|x86 | ||
{6E7AFCF2-068B-4385-AEF5-0192DEFBD8D3}.Release|x86.ActiveCfg = Release|x86 | ||
{6E7AFCF2-068B-4385-AEF5-0192DEFBD8D3}.Release|x86.Build.0 = Release|x86 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using System.Text; | ||
|
||
namespace BitfinexApi | ||
{ | ||
public class ActiveOrdersRequest:GenericRequest | ||
{ | ||
public ActiveOrdersRequest(string nonce) | ||
{ | ||
this.nonce = nonce; | ||
this.request = "/v1/orders"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace BitfinexApi | ||
{ | ||
public class ActiveOrdersResponse | ||
{ | ||
public List<OrderStatusResponse> orders; | ||
|
||
public static ActiveOrdersResponse FromJSON(string response) | ||
{ | ||
List<OrderStatusResponse> orders = JsonConvert.DeserializeObject<List<OrderStatusResponse>>(response); | ||
return new ActiveOrdersResponse(orders); | ||
} | ||
private ActiveOrdersResponse(List<OrderStatusResponse> orders) | ||
{ | ||
this.orders = orders; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace BitfinexApi | ||
{ | ||
public class ActivePositionsRequest:GenericRequest | ||
{ | ||
public ActivePositionsRequest(string nonce) | ||
{ | ||
this.nonce = nonce; | ||
this.request = "/v1/positions"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
namespace BitfinexApi | ||
{ | ||
public class ActivePositionItem | ||
{ | ||
public string id; | ||
public string symbol; | ||
public string status; | ||
public string _base;//base reserved word | ||
public string amount; | ||
public string timestamp; | ||
public string swap; | ||
public string pl; | ||
} | ||
public class ActivePositionsResponse | ||
{ | ||
public List<ActivePositionItem> positions; | ||
public static ActivePositionsResponse FromJSON(string response) | ||
{ | ||
response = response.Replace("base", "_base"); | ||
List<ActivePositionItem> items = JsonConvert.DeserializeObject<List<ActivePositionItem>>(response); | ||
return new ActivePositionsResponse(items); | ||
} | ||
private ActivePositionsResponse(List<ActivePositionItem> positions) | ||
{ | ||
this.positions = positions; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using System.Text; | ||
|
||
namespace BitfinexApi | ||
{ | ||
public class BalancesRequest:GenericRequest | ||
{ | ||
public BalancesRequest(string nonce) | ||
{ | ||
this.nonce = nonce; | ||
this.request = "/v1/balances"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using System.Text; | ||
using Newtonsoft.Json; | ||
using System.Globalization; | ||
|
||
namespace BitfinexApi | ||
{ | ||
public class BalanceResponseItem | ||
{ | ||
public string type; | ||
public string currency; | ||
public string amount; | ||
public string available; | ||
} | ||
public class BalancesResponse | ||
{ | ||
public Balance trading; | ||
public Balance deposit; | ||
public Balance exchange; | ||
public decimal totalUSD = 0; | ||
public decimal totalAvailableUSD = 0; | ||
public decimal totalBTC = 0; | ||
public decimal totalAvailableBTC = 0; | ||
|
||
public static BalancesResponse FromJSON(string response) | ||
{ | ||
List<BalanceResponseItem> items = JsonConvert.DeserializeObject<List<BalanceResponseItem>>(response); | ||
return new BalancesResponse(items); | ||
} | ||
private BalancesResponse(List<BalanceResponseItem> items) | ||
{ | ||
trading = new Balance(); | ||
deposit = new Balance(); | ||
exchange = new Balance(); | ||
|
||
Balance cur = null; | ||
decimal tmp; | ||
foreach (BalanceResponseItem item in items) | ||
{ | ||
|
||
switch (item.type) | ||
{ | ||
case "trading": | ||
cur = trading; | ||
break; | ||
case "deposit": | ||
cur = deposit; | ||
break; | ||
case "exchange": | ||
cur = exchange; | ||
break; | ||
} | ||
switch (item.currency) | ||
{ | ||
case "usd": | ||
tmp = decimal.Parse(item.available, CultureInfo.InvariantCulture); | ||
cur.availableUSD = tmp; | ||
totalAvailableUSD += tmp; | ||
tmp = decimal.Parse(item.amount, CultureInfo.InvariantCulture); | ||
cur.USD = tmp; | ||
totalUSD += tmp; | ||
break; | ||
case "btc": | ||
tmp = decimal.Parse(item.available, CultureInfo.InvariantCulture); | ||
cur.availableBTC = tmp; | ||
totalAvailableBTC += tmp; | ||
tmp=decimal.Parse(item.amount, CultureInfo.InvariantCulture); | ||
cur.BTC = tmp; | ||
totalBTC += tmp; | ||
break; | ||
|
||
} | ||
} | ||
} | ||
} | ||
public class Balance | ||
{ | ||
public decimal USD = 0; | ||
public decimal BTC = 0; | ||
public decimal availableUSD = 0; | ||
public decimal availableBTC = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using System.Text; | ||
using System.Security.Cryptography; | ||
using Newtonsoft.Json; | ||
using System.Net; | ||
using System.IO; | ||
|
||
namespace BitfinexApi | ||
{ | ||
public class BitfinexApiV1 | ||
{ | ||
private DateTime epoch = new DateTime(1970, 1, 1); | ||
|
||
private HMACSHA384 hashMaker; | ||
private string Key; | ||
private int nonce = 0; | ||
private string Nonce | ||
{ | ||
get | ||
{ | ||
if (nonce == 0) | ||
{ | ||
nonce = (int)(DateTime.UtcNow - epoch).TotalSeconds; | ||
} | ||
return (nonce++).ToString(); | ||
} | ||
} | ||
public BitfinexApiV1(string key, string secret) | ||
{ | ||
hashMaker = new HMACSHA384(Encoding.UTF8.GetBytes(secret)); | ||
this.Key = key; | ||
} | ||
|
||
private String GetHexString(byte[] bytes) | ||
{ | ||
StringBuilder sb = new StringBuilder(bytes.Length * 2); | ||
foreach (byte b in bytes) | ||
{ | ||
sb.Append(String.Format("{0:x2}", b)); | ||
} | ||
return sb.ToString(); | ||
} | ||
public BalancesResponse GetBalances() | ||
{ | ||
BalancesRequest req = new BalancesRequest(Nonce); | ||
string response = SendRequest(req,"GET"); | ||
BalancesResponse resp = BalancesResponse.FromJSON(response); | ||
|
||
return resp; | ||
} | ||
public CancelOrderResponse CancelOrder(int order_id) | ||
{ | ||
CancelOrderRequest req = new CancelOrderRequest(Nonce, order_id); | ||
string response = SendRequest(req,"POST"); | ||
CancelOrderResponse resp = CancelOrderResponse.FromJSON(response); | ||
return resp; | ||
} | ||
public CancelAllOrdersResponse CancelAllOrders() | ||
{ | ||
CancelAllOrdersRequest req = new CancelAllOrdersRequest(Nonce); | ||
string response = SendRequest(req,"GET"); | ||
return new CancelAllOrdersResponse(response); | ||
} | ||
public OrderStatusResponse GetOrderStatus(int order_id) | ||
{ | ||
OrderStatusRequest req = new OrderStatusRequest(Nonce, order_id); | ||
string response = SendRequest(req, "POST"); | ||
return OrderStatusResponse.FromJSON(response); | ||
} | ||
public ActiveOrdersResponse GetActiveOrders() | ||
{ | ||
ActiveOrdersRequest req = new ActiveOrdersRequest(Nonce); | ||
string response = SendRequest(req, "POST"); | ||
return ActiveOrdersResponse.FromJSON(response); | ||
} | ||
public ActivePositionsResponse GetActivePositions() | ||
{ | ||
ActivePositionsRequest req = new ActivePositionsRequest(Nonce); | ||
string response = SendRequest(req, "POST"); | ||
return ActivePositionsResponse.FromJSON(response); | ||
} | ||
|
||
public NewOrderResponse ExecuteBuyOrderBTC(decimal amount, decimal price, OrderExchange exchange, OrderType type) | ||
{ | ||
return ExecuteOrder(OrderSymbol.BTCUSD, amount, price, exchange, OrderSide.Buy, type); | ||
} | ||
public NewOrderResponse ExecuteSellOrderBTC(decimal amount, decimal price, OrderExchange exchange, OrderType type) | ||
{ | ||
return ExecuteOrder(OrderSymbol.BTCUSD, amount, price, exchange, OrderSide.Sell, type); | ||
} | ||
public NewOrderResponse ExecuteOrder(OrderSymbol symbol, decimal amount, decimal price, OrderExchange exchange, OrderSide side, OrderType type) | ||
{ | ||
NewOrderRequest req = new NewOrderRequest(Nonce, symbol, amount, price, exchange, side, type); | ||
string response = SendRequest(req,"POST"); | ||
NewOrderResponse resp = NewOrderResponse.FromJSON(response); | ||
return resp; | ||
} | ||
|
||
private string SendRequest(GenericRequest request,string httpMethod) | ||
{ | ||
string json = JsonConvert.SerializeObject(request); | ||
string json64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(json)); | ||
byte[] data = Encoding.UTF8.GetBytes(json64); | ||
byte[] hash = hashMaker.ComputeHash(data); | ||
string signature = GetHexString(hash); | ||
|
||
HttpWebRequest wr = WebRequest.Create("https://api.bitfinex.com"+request.request) as HttpWebRequest; | ||
wr.Headers.Add("X-BFX-APIKEY", Key); | ||
wr.Headers.Add("X-BFX-PAYLOAD", json64); | ||
wr.Headers.Add("X-BFX-SIGNATURE", signature); | ||
wr.Method = httpMethod; | ||
|
||
string response = null; | ||
try | ||
{ | ||
HttpWebResponse resp = wr.GetResponse() as HttpWebResponse; | ||
StreamReader sr = new StreamReader(resp.GetResponseStream()); | ||
response = sr.ReadToEnd(); | ||
sr.Close(); | ||
} | ||
catch (WebException ex) | ||
{ | ||
StreamReader sr = new StreamReader(ex.Response.GetResponseStream()); | ||
response = sr.ReadToEnd(); | ||
sr.Close(); | ||
throw new BitfinexException(ex, response); | ||
} | ||
return response; | ||
} | ||
} | ||
} |
Oops, something went wrong.