Skip to content

Optimization on Orders #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions InterReact.Tests/SystemTests/Orders/CompletedOrdersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ public class Completed : CollectionTestBase
public Completed(ITestOutputHelper output, TestFixture fixture) : base(output, fixture) { }


//[Fact]
//public async Task CompletedOrdersAsyncTest()
//{
// bool api = true;
[Fact]
public async Task CompletedOrdersAsyncTest()
{
bool api = true;

// IList<CompletedOrder> orders = await Client
// .Service
// .GetCompleteOrdersAsync(api);
IList<CompletedOrder> orders = await Client
.Service
.GetCompleteOrdersAsync(api);

// Write($"Complete orders found: {orders.Count}.");
Write($"Complete orders found: {orders.Count}.");

// foreach (CompletedOrder order in orders)
// Write(order.Stringify());
//}
foreach (CompletedOrder order in orders)
Write(order.Stringify());
}
}
52 changes: 40 additions & 12 deletions InterReact.Tests/SystemTests/Orders/OpenOrderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,48 @@ public class Open : CollectionTestBase
{
public Open(ITestOutputHelper output, TestFixture fixture) : base(output, fixture) { }

//[Fact(Skip = "Test may conflict when run together with order placement tests")]
//public async Task OpenOrdersAsyncTest()
//{
// await Task.Delay(3000);
[Fact]
public async Task OpenOrdersAsyncTest()
{
await Task.Delay(1000);

// IList<object> list = await Client
// .Service
// .GetOpenOrdersAsync(OpenOrdersRequestType.OpenOrders)
// .WaitAsync(TimeSpan.FromSeconds(10));
// 1 place order
// Place the order with orderId: orderId
var contract = new Contract()
{
SecurityType = ContractSecurityType.Stock,
Symbol = "AMZN",
Currency = "USD",
Exchange = "SMART"
};

// Write($"Open orders found: {list.Count}.");
int orderId = Client.Request.GetNextId();

// foreach (object item in list)
// Write(item.Stringify());
//}
var order = new Order()
{
Action = OrderAction.Buy,
TotalQuantity = 1,
OrderType = OrderTypes.Limit,
LimitPrice = 1,
TimeInForce = OrderTimeInForce.GoodUntilCancelled
};

OrderMonitor orderMonitor = await Client.Service.PlaceOrderAsync(order, contract);

//2 Request Open Order
IList<OpenOrder> openOrders = await Client
.Service
.RequestOpenOrdersAsync()
.WaitAsync(TimeSpan.FromSeconds(1));

Write($"Open orders found: {openOrders.Count}.");

foreach (Object item in openOrders)
Write(item.Stringify());

//3 cancel Order
orderMonitor.CancelOrder();
orderMonitor.Dispose();
}
}

87 changes: 87 additions & 0 deletions InterReact.Tests/SystemTests/Orders/OrderIntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using InterReact;
using Stringification;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;

namespace Orders
{
public class OrderIntegrationTests: CollectionTestBase
{

public OrderIntegrationTests(ITestOutputHelper output, TestFixture fixture) : base(output, fixture) { }


[Fact]
public async Task Place_GetOpen_Cancel_Order_Test()
{
// Place the order with orderId: orderId
var contract = new Contract()
{
SecurityType = ContractSecurityType.Stock,
Symbol = "AMZN",
Currency = "USD",
Exchange = "SMART"
};

int orderId = Client.Request.GetNextId();

var order = new Order()
{
Action = OrderAction.Buy,
TotalQuantity = 1,
OrderType = OrderTypes.Limit,
LimitPrice = 1,
TimeInForce = OrderTimeInForce.GoodUntilCancelled
};

OrderMonitor orderMonitor = await Client.Service.PlaceOrderAsync(order, contract);

orderMonitor.CancelOrder();

OrderStatusReport report = await orderMonitor
.Messages
.OfType<OrderStatusReport>()
.Take(TimeSpan.FromSeconds(3))
.FirstOrDefaultAsync();

//orderMonitor.Dispose();
Assert.True(report.Status == OrderStatus.Cancelled || report.Status == OrderStatus.ApiCancelled);


// 等待1秒
await Task.Delay(1000);


// Get the order, if contains orderId, succeed
Task<IList<Object>> task2 = Client
.Response
.Take(TimeSpan.FromSeconds(3))
.ToList<Object>()
.ToTask();

Client.Request.RequestOpenOrders();
var openOrder = await task2;
Assert.IsType<Order>(openOrder);

// 等待1秒
await Task.Delay(1000);


// cancel the order
Task<Execution?> task3 = Client
.Response
.WithOrderId(orderId)
.OfType<Execution>()
.Take(TimeSpan.FromSeconds(3))
.FirstOrDefaultAsync()
.ToTask();
Client.Request.CancelOrder(orderId);
Execution? execution3 = await task3;
Assert.NotNull(execution3);

await Task.Run(() => Console.WriteLine("3"));
}


}
}
6 changes: 3 additions & 3 deletions InterReact.Tests/SystemTests/Orders/OrderMonitorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task OrderMonitorTest()
OrderType = OrderTypes.Market
};

OrderMonitor orderMonitor = Client.Service.PlaceOrder(order, contract);
OrderMonitor orderMonitor = await Client.Service.PlaceOrderAsync(order, contract);

orderMonitor
.Messages
Expand Down Expand Up @@ -66,7 +66,7 @@ public async Task OrderMonitorCancellationTest()
OrderType = OrderTypes.Market
};

OrderMonitor orderMonitor = Client.Service.PlaceOrder(order, contract);
OrderMonitor orderMonitor = await Client.Service.PlaceOrderAsync(order, contract);

orderMonitor.CancelOrder();

Expand Down Expand Up @@ -109,7 +109,7 @@ public async Task OrderMonitorModificationTest()
};

// Place the order
OrderMonitor orderMonitor = Client.Service.PlaceOrder(order, contract);
OrderMonitor orderMonitor = await Client.Service.PlaceOrderAsync(order, contract);

//await Task.Delay(100);

Expand Down
1 change: 1 addition & 0 deletions InterReact/InterReact.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<EnablePackageValidation>true</EnablePackageValidation>
<AnalysisMode>All</AnalysisMode>
<NoWarn>CA1848;</NoWarn>
<Title>InterReact.IBKR</Title>
</PropertyGroup>

<ItemGroup>
Expand Down
39 changes: 37 additions & 2 deletions InterReact/Services/OrderMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Reactive.Subjects;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;

namespace InterReact;

Expand All @@ -7,8 +9,41 @@ public partial class Service
/// <summary>
/// Places an order and returns an OrderMonitor object (below) which can be used to monitor the order.
/// </summary>
public OrderMonitor PlaceOrder(Order order, Contract contract) =>
new(order, contract, Request, Response);
public async Task<OrderMonitor> PlaceOrderAsync(Order order, Contract contract)
{
return await Task.Run(() =>
{
return new OrderMonitor(order, contract, Request, Response);
});
}

public async Task<IList<OpenOrder>> RequestOpenOrdersAsync()
{
Task<IList<OpenOrder>> task =
Response
.OfType<OpenOrder>()
.Take(TimeSpan.FromMilliseconds(500))
.ToList()
.ToTask();
Request.RequestOpenOrders();
var orders = await task;
return orders;
}

public async Task<IList<CompletedOrder>> GetCompleteOrdersAsync(bool api)
{
Task<IList<CompletedOrder>> task =
Response
.OfType<CompletedOrder>()
.Take(TimeSpan.FromMilliseconds(500))
.ToList()
.ToTask();

Request.RequestCompletedOrders(api);

var completedOrders = await task;
return completedOrders;
}
}

/// <summary>
Expand Down