forked from QuantConnect/Lean.Brokerages.Bitfinex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitfinexBrokerageTests.cs
More file actions
175 lines (152 loc) · 6.62 KB
/
Copy pathBitfinexBrokerageTests.cs
File metadata and controls
175 lines (152 loc) · 6.62 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using QuantConnect.Interfaces;
using QuantConnect.Securities;
using NUnit.Framework;
using QuantConnect.Brokerages.Bitfinex;
using QuantConnect.Configuration;
using Moq;
using QuantConnect.Brokerages;
using QuantConnect.Tests.Common.Securities;
using QuantConnect.Lean.Engine.DataFeeds;
using QuantConnect.Orders;
namespace QuantConnect.Tests.Brokerages.Bitfinex
{
[TestFixture]
public partial class BitfinexBrokerageTests : BrokerageTests
{
/// <summary>
/// Creates the brokerage under test and connects it
/// </summary>
/// <param name="orderProvider"></param>
/// <param name="securityProvider"></param>
/// <returns></returns>
protected override IBrokerage CreateBrokerage(IOrderProvider orderProvider, ISecurityProvider securityProvider)
{
var security = securityProvider.GetSecurity(Symbol);
var securities = new SecurityManager(new TimeKeeper(DateTime.UtcNow, TimeZones.NewYork))
{
{Symbol, security}
};
var transactions = new SecurityTransactionManager(null, securities);
transactions.SetOrderProcessor(new FakeOrderProcessor());
var algorithmSettings = new AlgorithmSettings();
var algorithm = new Mock<IAlgorithm>();
algorithm.Setup(a => a.Transactions).Returns(transactions);
algorithm.Setup(a => a.BrokerageModel).Returns(new BitfinexBrokerageModel(AccountType.Cash));
algorithm.Setup(a => a.Portfolio).Returns(new SecurityPortfolioManager(securities, transactions, algorithmSettings));
algorithm.Setup(a => a.Securities).Returns(securities);
return new BitfinexBrokerage(
Config.Get("bitfinex-api-key"),
Config.Get("bitfinex-api-secret"),
algorithm.Object,
new AggregationManager(),
null
);
}
/// <summary>
/// Gets the symbol to be traded, must be shortable
/// </summary>
protected override Symbol Symbol => StaticSymbol;
private static Symbol StaticSymbol => Symbol.Create("TESTBTCTESTUSD", SecurityType.Crypto, Market.Bitfinex);
/// <summary>
/// Gets the security type associated with the <see cref="BrokerageTests.Symbol" />
/// </summary>
protected override SecurityType SecurityType => SecurityType.Crypto;
private static TestCaseData[] OrderParameters =>
[
new TestCaseData(new MarketOrderTestParameters(StaticSymbol)),
new TestCaseData(new LimitOrderTestParameters(StaticSymbol, 1000m, 100m)),
new TestCaseData(new StopMarketOrderTestParameters(StaticSymbol, 1000m, 100m)),
new TestCaseData(new StopLimitOrderTestParameters(StaticSymbol, 1000m, 100m)),
];
/// <summary>
/// Gets the current market price of the specified security
/// </summary>
protected override decimal GetAskPrice(Symbol symbol)
{
var tick = ((BitfinexBrokerage)Brokerage).GetTick(symbol);
return tick.AskPrice;
}
/// <summary>
/// Returns whether or not the brokers order methods implementation are async
/// </summary>
protected override bool IsAsync() => true;
/// <summary>
/// Returns whether or not the brokers order cancel method implementation is async
/// </summary>
protected override bool IsCancelAsync() => true;
/// <summary>
/// Gets the default order quantity
/// </summary>
protected override decimal GetDefaultQuantity() => 0.04m;
[TestCaseSource(nameof(OrderParameters))]
public override void CancelOrders(OrderTestParameters parameters)
{
base.CancelOrders(parameters);
}
[TestCaseSource(nameof(OrderParameters))]
public override void LongFromZero(OrderTestParameters parameters)
{
base.LongFromZero(parameters);
if (parameters is not MarketOrderTestParameters)
{
// We expect the orders to be open
var openOrders = Brokerage.GetOpenOrders();
Assert.AreEqual(1, openOrders.Count);
var expectedOrderType = parameters switch
{
LimitOrderTestParameters _ => typeof(LimitOrder),
StopMarketOrderTestParameters _ => typeof(StopMarketOrder),
StopLimitOrderTestParameters _ => typeof(StopLimitOrder),
_ => throw new ArgumentException("Unsupported order type for this test", nameof(parameters))
};
// Check that the order type matches the expected type
Assert.IsInstanceOf(expectedOrderType, openOrders[0]);
}
}
[TestCaseSource(nameof(OrderParameters))]
public override void CloseFromLong(OrderTestParameters parameters)
{
base.CloseFromLong(parameters);
}
[TestCaseSource(nameof(OrderParameters))]
public override void ShortFromZero(OrderTestParameters parameters)
{
base.ShortFromZero(parameters);
}
[TestCaseSource(nameof(OrderParameters))]
public override void CloseFromShort(OrderTestParameters parameters)
{
base.CloseFromShort(parameters);
}
[TestCaseSource(nameof(OrderParameters))]
public override void ShortFromLong(OrderTestParameters parameters)
{
base.ShortFromLong(parameters);
}
[TestCaseSource(nameof(OrderParameters))]
public override void LongFromShort(OrderTestParameters parameters)
{
base.LongFromShort(parameters);
}
[Test]
public override void GetAccountHoldings()
{
Assert.IsEmpty(Brokerage.GetAccountHoldings());
}
}
}