Skip to content

Commit

Permalink
GetProductHistoryAsync + Limit Orders additional parameters (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
quin810 authored and dougdellolio committed Jan 6, 2018
1 parent 5aea483 commit 68a89a9
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 5 deletions.
1 change: 1 addition & 0 deletions GDAXClient.Specs/GDAXClient.Specs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="JsonFixtures\Payments\PaymentMethodsResponseFixture.cs" />
<Compile Include="JsonFixtures\Products\ProductsOrderBookResponseFixture.cs" />
<Compile Include="JsonFixtures\Products\ProductsResponseFixture.cs" />
<Compile Include="JsonFixtures\Products\ProductHistoryFixture.cs" />
<Compile Include="JsonFixtures\Products\ProductStatsFixture.cs" />
<Compile Include="JsonFixtures\Products\ProductTickerFixture.cs" />
<Compile Include="JsonFixtures\Withdrawals\CoinbaseWithdrawalResponseFixture.cs" />
Expand Down
39 changes: 39 additions & 0 deletions GDAXClient.Specs/JsonFixtures/Products/ProductHistoryFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace GDAXClient.Specs.JsonFixtures.Products
{
public static class ProductHistoryFixture
{
public static string Create()
{

var json = @"
[
[
1512691200,
16777,
17777.69,
17390.01,
17210.99,
7650.386033540894
],
[
1512633600,
14487.8,
19697,
14487.8,
17390.01,
65581.82529800163
],
[
1512576000,
13500,
14499.89,
14056.78,
14487.8,
12303.76923928093
]
]";

return json;
}
}
}
2 changes: 1 addition & 1 deletion GDAXClient.Specs/Services/Orders/OrdersServiceSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class when_placing_a_limit_order
};

Because of = () =>
order_response_result = Subject.PlaceLimitOrderAsync(OrderSide.Buy, ProductType.BtcUsd, .01M, 0.1M).Result;
order_response_result = Subject.PlaceLimitOrderAsync(OrderSide.Buy, ProductType.BtcUsd, .01M, 0.1M, DateTime.Today.Date.ToUniversalTime()).Result;

It should_have_correct_account_information = () =>
{
Expand Down
48 changes: 47 additions & 1 deletion GDAXClient.Specs/Services/Products/ProductsServiceSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GDAXClient.Authentication;
using System;
using GDAXClient.Authentication;
using GDAXClient.HttpClient;
using GDAXClient.Products;
using GDAXClient.Services.HttpRequest;
Expand All @@ -10,6 +11,7 @@
using Machine.Fakes;
using Machine.Specifications;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -25,6 +27,8 @@ public class ProductsServiceSpecs : WithSubject<ProductsService>

static ProductsOrderBookResponse product_order_books_response;

static IEnumerable<object[]> product_history_response;

static ProductTicker product_ticker_result;

static ProductStats product_stats_result;
Expand Down Expand Up @@ -146,5 +150,47 @@ class when_getting_product_stats
product_stats_result.Volume.ShouldEqual(2.41000000M);
};
}

class when_getting_product_history
{
Establish context = () =>
{
The<IHttpRequestMessageService>().WhenToldTo(p => p.CreateHttpRequestMessage(Param.IsAny<HttpMethod>(), Param.IsAny<Authenticator>(), Param.IsAny<string>(), Param.IsAny<string>()))
.Return(new HttpRequestMessage());

The<IHttpClient>().WhenToldTo(p => p.SendASync(Param.IsAny<HttpRequestMessage>()))
.Return(Task.FromResult(new HttpResponseMessage()));

The<IHttpClient>().WhenToldTo(p => p.ReadAsStringAsync(Param.IsAny<HttpResponseMessage>()))
.Return(Task.FromResult(ProductHistoryFixture.Create()));
};

Because of = () =>
product_history_response = Subject.GetProductHistoryAsync(ProductType.BtcUsd, DateTime.Now.AddDays(-1), DateTime.Now, 57600).Result;

It should_have_correct_product_stats = () =>
{
product_history_response.ToList()[0][0].ToString().ShouldEqual(1512691200.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[0][1].ToString().ShouldEqual(16777.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[0][2].ToString().ShouldEqual(17777.69.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[0][3].ToString().ShouldEqual(17390.01.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[0][4].ToString().ShouldEqual(17210.99.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[0][5].ToString().ShouldEqual(7650.386033540894.ToString(CultureInfo.InvariantCulture));

product_history_response.ToList()[1][0].ToString().ShouldEqual(1512633600.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[1][1].ToString().ShouldEqual(14487.8.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[1][2].ToString().ShouldEqual(19697.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[1][3].ToString().ShouldEqual(14487.8.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[1][4].ToString().ShouldEqual(17390.01.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[1][5].ToString().ShouldEqual(65581.82529800163.ToString(CultureInfo.InvariantCulture));

product_history_response.ToList()[2][0].ToString().ShouldEqual(1512576000.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[2][1].ToString().ShouldEqual(13500.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[2][2].ToString().ShouldEqual(14499.89.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[2][3].ToString().ShouldEqual(14056.78.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[2][4].ToString().ShouldEqual(14487.8.ToString(CultureInfo.InvariantCulture));
product_history_response.ToList()[2][5].ToString().ShouldEqual(12303.76923928093.ToString(CultureInfo.InvariantCulture));
};
}
}
}
1 change: 1 addition & 0 deletions GDAXClient/GDAXClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Compile Include="GDAXClient.cs" />
<Compile Include="HttpClient\HttpClient.cs" />
<Compile Include="HttpClient\IHttpClient.cs" />
<Compile Include="Services\Orders\TimeInForce.cs" />
<Compile Include="Services\Accounts\Models\AccountHistory.cs" />
<Compile Include="Services\Accounts\Models\AccountHold.cs" />
<Compile Include="Services\Currencies\CurrenciesService.cs" />
Expand Down
39 changes: 37 additions & 2 deletions GDAXClient/Services/Orders/OrdersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace GDAXClient.Services.Orders
Expand Down Expand Up @@ -48,7 +49,7 @@ public async Task<OrderResponse> PlaceMarketOrderAsync(OrderSide side, ProductTy
return orderResponse;
}

public async Task<OrderResponse> PlaceLimitOrderAsync(OrderSide side, ProductType productId, decimal size, decimal price)
public async Task<OrderResponse> PlaceLimitOrderAsync(OrderSide side, ProductType productId, decimal size, decimal price, TimeInForce timeInForce = TimeInForce.Gtc, bool postOnly = true)
{
var newOrder = JsonConvert.SerializeObject(new Order
{
Expand All @@ -59,13 +60,47 @@ public async Task<OrderResponse> PlaceLimitOrderAsync(OrderSide side, ProductTyp
size = size
});

var httpResponseMessage = await SendHttpRequestMessageAsync(HttpMethod.Post, authenticator, "/orders", newOrder);
var queryString = new StringBuilder("?");

queryString.Append("time_in_force=");
queryString.Append(timeInForce.ToString().ToUpperInvariant());

queryString.Append("&post_only=");
queryString.Append(postOnly.ToString().ToLower());

var httpResponseMessage = await SendHttpRequestMessageAsync(HttpMethod.Post, authenticator, "/orders" + queryString, newOrder).ConfigureAwait(false);
var contentBody = await httpClient.ReadAsStringAsync(httpResponseMessage).ConfigureAwait(false);
var orderResponse = JsonConvert.DeserializeObject<OrderResponse>(contentBody);

return orderResponse;
}

public async Task<OrderResponse> PlaceLimitOrderAsync(OrderSide side, ProductType productId, decimal size, decimal price, DateTime cancelAfter, bool postOnly = true)
{
var newOrder = JsonConvert.SerializeObject(new Order
{
side = side.ToString().ToLower(),
product_id = productId.ToDasherizedUpper(),
type = OrderType.Limit.ToString().ToLower(),
price = price,
size = size
});

var queryString = new StringBuilder("?");

queryString.Append("time_in_force=GTT");
queryString.Append("&cancel_after=");
queryString.Append(cancelAfter.Minute + "," + cancelAfter.Hour + "," + cancelAfter.Day);
queryString.Append("&post_only=");
queryString.Append(postOnly.ToString().ToLower());

var httpResponseMessage = await SendHttpRequestMessageAsync(HttpMethod.Post, authenticator, "/orders" + queryString, newOrder);
var contentBody = await httpClient.ReadAsStringAsync(httpResponseMessage).ConfigureAwait(false);
var orderResponse = JsonConvert.DeserializeObject<OrderResponse>(contentBody);

return orderResponse;
}

public async Task<CancelOrderResponse> CancelAllOrdersAsync()
{
var httpResponseMessage = await SendHttpRequestMessageAsync(HttpMethod.Delete, authenticator, "/orders");
Expand Down
9 changes: 9 additions & 0 deletions GDAXClient/Services/Orders/TimeInForce.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace GDAXClient.Services.Orders
{
public enum TimeInForce
{
Gtc,
Ioc,
Fok
}
}
16 changes: 15 additions & 1 deletion GDAXClient/Services/Products/ProductsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GDAXClient.HttpClient;
using System;
using GDAXClient.HttpClient;
using GDAXClient.Services;
using GDAXClient.Services.Accounts;
using GDAXClient.Services.HttpRequest;
Expand Down Expand Up @@ -68,5 +69,18 @@ public async Task<ProductStats> GetProductStatsAsync(ProductType productPair)

return productStatsResponse;
}


public async Task<IEnumerable<object[]>> GetProductHistoryAsync(ProductType productPair, DateTime start, DateTime end, int granularity)
{
var isoStart = start.ToString("s");
var isoEnd = end.ToString("s");

var httpResponseMessage = await SendHttpRequestMessageAsync(HttpMethod.Get, authenticator, $"/products/{productPair.ToDasherizedUpper()}/candles?start={isoStart}&end={isoEnd}&granularity={granularity}");
var contentBody = await httpClient.ReadAsStringAsync(httpResponseMessage).ConfigureAwait(false);
var productHistoryResponse = JsonConvert.DeserializeObject<IEnumerable<object[]>>(contentBody);

return productHistoryResponse;
}
}
}

0 comments on commit 68a89a9

Please sign in to comment.