forked from amittleider/AutoFinance.Broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwsControllerTests.cs
130 lines (103 loc) · 4.45 KB
/
TwsControllerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using AutoFinance.Broker.InteractiveBrokers;
using AutoFinance.Broker.InteractiveBrokers.Constants;
using AutoFinance.Broker.InteractiveBrokers.Controllers;
using FluentAssertions;
using IBApi;
using System;
using System.Threading.Tasks;
using Xunit;
namespace AutoFinance.Broker.IntegrationTests.InteractiveBrokers.Controllers
{
public class TwsControllerTests
{
[Fact]
public async Task Should_PlaceBracketOrderWithStopLimit()
{
TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1);
ITwsController twsController = twsObjectFactory.TwsController;
await twsController.EnsureConnectedAsync();
Contract contract = new Contract
{
SecType = TwsContractSecType.Stock,
Symbol = "MSFT",
Exchange = TwsExchange.Smart,
PrimaryExch = TwsExchange.Island,
Currency = TwsCurrency.Usd,
};
bool placed = await twsController.PlaceBracketOrder(contract, TwsOrderActions.Buy, 1, 10, 1000, 1, 0.9);
placed.Should().BeTrue();
await twsController.DisconnectAsync();
}
[Fact]
public async Task Should_PlaceBracketOrderWithStopLimit_ForGrwgContract()
{
TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1);
ITwsController twsController = twsObjectFactory.TwsController;
await twsController.EnsureConnectedAsync();
var orderId = await twsController.GetNextValidIdAsync();
var orderId2 = await twsController.GetNextValidIdAsync();
Contract contract = new Contract
{
SecType = TwsContractSecType.Stock,
Symbol = "GRWG",
Exchange = TwsExchange.Smart,
PrimaryExch = TwsExchange.Island,
Currency = TwsCurrency.Usd,
};
//Order order = new Order
//{
// Action = "BUY",
// OrderType = "MKT",
// TotalQuantity = 1
//};
string group = Guid.NewGuid().ToString();
Order order = new Order()
{
Action = "SELL",
OrderType = TwsOrderType.Limit,
TotalQuantity = 200,
LmtPrice = 10,
Tif = TwsTimeInForce.GoodTillClose,
OcaType = (int)TwsOcaType.CancelAllRemainingOrdersWithBlock,
OcaGroup = group,
Transmit = true,
};
Order order2 = new Order()
{
Action = "SELL",
OrderType = TwsOrderType.StopLimit,
TotalQuantity = 200,
AuxPrice = 4,
LmtPrice = 3.9,
Tif = TwsTimeInForce.GoodTillClose,
OcaType = (int)TwsOcaType.CancelAllRemainingOrdersWithBlock,
OcaGroup = group,
Transmit = true,
};
bool placed = await twsController.PlaceOrderAsync(orderId, contract, order);
bool placed2 = await twsController.PlaceOrderAsync(orderId2, contract, order2);
placed.Should().BeTrue();
await twsController.DisconnectAsync();
}
[Fact]
public async Task Should_PlaceBracketForExistingPosition()
{
TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1);
ITwsController twsController = twsObjectFactory.TwsController;
await twsController.EnsureConnectedAsync();
bool success = await twsController.PlaceBracketForExistingPosition("FOSL", "SMART", 2, 5, 5.1);
success.Should().BeTrue();
await twsController.DisconnectAsync();
}
[Fact]
public async Task Should_CancelAllOrders()
{
TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1);
ITwsController twsController = twsObjectFactory.TwsController;
await twsController.EnsureConnectedAsync();
bool success = await twsController.CancelAllOrders();
success.Should().BeTrue();
await twsController.DisconnectAsync();
}
}
}