Skip to content

Commit

Permalink
[Feature] Add support for stop limit orders (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
BraveSirAndrew authored and dougdellolio committed Apr 28, 2018
1 parent 3a10822 commit 2f98ee0
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,31 @@ public static string CreateStopOrder()
return json;
}

public static string CreateLimitOrderMany(OrderStatus orderStatus = OrderStatus.Pending)
public static string CreateStopLimitOrder()
{
var json = @"
{
""id"": ""d0c5340b-6d6c-49d9-b567-48c4bfca13d2"",
""price"": ""0.10000000"",
""size"": ""0.01000000"",
""product_id"": ""BTC-USD"",
""side"": ""buy"",
""stp"": ""dc"",
""type"": ""limit"",
""time_in_force"": ""GTC"",
""post_only"": false,
""created_at"": ""2016-12-08T24:00:00Z"",
""fill_fees"": ""0.0000000000000000"",
""filled_size"": ""0.00000000"",
""executed_value"": ""0.0000000000000000"",
""status"": ""pending"",
""settled"": false
}";

return json;
}

public static string CreateLimitOrderMany(OrderStatus orderStatus = OrderStatus.Pending)
{
var json = $@"
[
Expand Down
29 changes: 29 additions & 0 deletions GDAXSharp.Specs/Services/Orders/OrdersServiceSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,35 @@ class when_placing_a_stop_order
};
}

class when_placing_a_stop_limit_order
{
Establish context = () =>
The<IHttpClient>().WhenToldTo(p => p.ReadAsStringAsync(Param.IsAny<HttpResponseMessage>()))
.Return(Task.FromResult(OrderResponseFixture.CreateStopLimitOrder()));

Because of = () =>
order_response_result = Subject.PlaceStopLimitOrderAsync(OrderSide.Buy, ProductType.BtcUsd, .01M, .1M, .1M, false).Result;

It should_have_correct_order_information = () =>
{
order_response_result.Id.ShouldEqual(new Guid("d0c5340b-6d6c-49d9-b567-48c4bfca13d2"));
order_response_result.Price.ShouldEqual(0.10000000M);
order_response_result.Size.ShouldEqual(0.01000000M);
order_response_result.ProductId.ShouldEqual(ProductType.BtcUsd);
order_response_result.Side.ShouldEqual(OrderSide.Buy);
order_response_result.Stp.ShouldEqual("dc");
order_response_result.OrderType.ShouldEqual(OrderType.Limit);
order_response_result.TimeInForce.ShouldEqual(TimeInForce.Gtc);
order_response_result.PostOnly.ShouldBeFalse();
order_response_result.CreatedAt.ShouldEqual(new DateTime(2016, 12, 9));
order_response_result.FillFees.ShouldEqual(0.0000000000000000M);
order_response_result.FilledSize.ShouldEqual(0.00000000M);
order_response_result.ExecutedValue.ShouldEqual(0.0000000000000000M);
order_response_result.Status.ShouldEqual(OrderStatus.Pending);
order_response_result.Settled.ShouldBeFalse();
};
}

class when_cancelling_all_orders
{
Establish context = () =>
Expand Down
1 change: 1 addition & 0 deletions GDAXSharp/GDAXSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="GDAXClient.cs" />
<Compile Include="Network\HttpClient\HttpClient.cs" />
<Compile Include="Network\HttpClient\IHttpClient.cs" />
<Compile Include="Services\Orders\Types\StopType.cs" />
<Compile Include="Shared\ApiUris.cs" />
<Compile Include="Shared\Utilities\JsonConfig.cs" />
<Compile Include="WebSocket\Models\Response\Done.cs" />
Expand Down
7 changes: 7 additions & 0 deletions GDAXSharp/Services/Orders/Models/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ public class Order

public decimal Price { get; set; }

[JsonProperty("stop")]
[JsonConverter(typeof(StringEnumConverter))]
public StopType StopType { get; set; }

[JsonProperty("stop_price")]
public decimal StopPrice { get; set; }

[JsonProperty("type")]
[JsonConverter(typeof(StringEnumConverter))]
public OrderType OrderType { get; set; }
Expand Down
27 changes: 27 additions & 0 deletions GDAXSharp/Services/Orders/OrdersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,33 @@ public async Task<OrderResponse> PlaceLimitOrderAsync(
return await PlaceOrderAsync(order);
}

public async Task<OrderResponse> PlaceStopLimitOrderAsync(
OrderSide side,
ProductType productId,
decimal size,
decimal stopPrice,
decimal limitPrice,
bool postOnly = true,
Guid? clientOid = null)
{
var order = new Order
{
Side = side,
ProductId = productId,
OrderType = OrderType.Limit,
Price = limitPrice,
Size = size,
StopType = side == OrderSide.Buy
? StopType.Entry
: StopType.Loss,
StopPrice = stopPrice,
ClientOid = clientOid,
PostOnly = postOnly,
};

return await PlaceOrderAsync(order);
}

public async Task<OrderResponse> PlaceStopOrderAsync(
OrderSide side,
ProductType productId,
Expand Down
12 changes: 12 additions & 0 deletions GDAXSharp/Services/Orders/Types/StopType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Runtime.Serialization;

namespace GDAXSharp.Services.Orders.Types
{
public enum StopType
{
[EnumMember(Value = "loss")]
Loss,
[EnumMember(Value = "entry")]
Entry
}
}

0 comments on commit 2f98ee0

Please sign in to comment.