-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSizeSampleCore.cs
More file actions
71 lines (60 loc) · 3.88 KB
/
SizeSampleCore.cs
File metadata and controls
71 lines (60 loc) · 3.88 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
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using WhalesSecret.ScriptApiLib.Samples.SharedLib;
using WhalesSecret.TradeScriptLib.API.TradingV1;
using WhalesSecret.TradeScriptLib.API.TradingV1.Orders;
using WhalesSecret.TradeScriptLib.Entities;
using WhalesSecret.TradeScriptLib.Entities.Orders;
using WhalesSecret.TradeScriptLib.Exceptions;
namespace WhalesSecret.ScriptApiLib.Samples.BasicSamples.Trading;
/// <summary>
/// Sample that demonstrates how to create orders. The sample creates a limit buy order with the price well under the current price. It is thus not expected that the order is
/// filled. Then the order is canceled. The cancellation of the order is then awaited.
/// <para>Private connections are necessary to create orders. Exchange API credentials have to be set.</para>
/// <para>Placing small orders does not need a valid license. Placing larger orders requires a valid license to be put into <see cref="License"/>.</para>
/// </summary>
/// <remarks>IMPORTANT: You have to change the keys and the secrets in <see cref="Credentials"/> to make the sample work.</remarks>
public static class SizeSampleCore
{
/// <summary>
/// Run the sample.
/// </summary>
/// <param name="exchangeMarket">Exchange market to connect to.</param>
/// <param name="useLargeOrder">
/// <c>true</c> to use large order, <c>false</c> to use small order. If set to <c>true</c>, <see cref="License"/> has to changed to contain a valid license.
/// </param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static async Task RunSampleAsync(ExchangeMarket exchangeMarket, bool useLargeOrder)
{
using CancellationTokenSource timeoutCts = new(TimeSpan.FromMinutes(2));
// In order to unlock large orders, a valid license has to be used.
CreateOptions createOptions = new(license: License.WsLicense);
await using ScriptApi scriptApi = await ScriptApi.CreateAsync(createOptions, timeoutCts.Token).ConfigureAwait(false);
await using OrderSampleHelper helper = await OrderSampleHelper.InitializeAsync(scriptApi, exchangeMarket, timeoutCts.Token).ConfigureAwait(false);
ITradeApiClient tradeClient = helper.TradeApiClient;
SymbolPair symbolPair = helper.SelectedSymbolPair;
// Compute a limit price so that the order is unlikely to fill.
decimal limitPrice = Math.Floor(helper.BestBid / 5 * 4);
// Buy a small amount of bitcoin.
decimal exchangeOrderSize = exchangeMarket switch
{
ExchangeMarket.BinanceSpot => useLargeOrder ? 25m : 6.0m,
ExchangeMarket.KucoinSpot => useLargeOrder ? 20m : 2.0m,
ExchangeMarket.KrakenSpot => useLargeOrder ? 20m : 5.0m,
_ => throw new SanityCheckException($"Invalid exchange market {exchangeMarket} provided."),
};
// Rounding is necessary to get accepted on exchanges.
decimal orderSize = Math.Round(exchangeOrderSize / limitPrice, decimals: helper.BaseVolumePrecision);
string clientOrderId = string.Create(CultureInfo.InvariantCulture, $"os-{DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond}");
ILiveLimitOrder liveOrder = await tradeClient.CreateLimitOrderAsync(clientOrderId, symbolPair, OrderSide.Buy, price: limitPrice, size: orderSize, timeoutCts.Token)
.ConfigureAwait(false);
Console.WriteLine($"Limit order '{liveOrder}' is live now. Cancel it.");
await tradeClient.CancelOrderAsync(liveOrder, timeoutCts.Token).ConfigureAwait(false);
// As the cancellation succeeded, this is just a sanity check that should complete almost instantly.
Console.WriteLine("Wait for the order to be terminated.");
_ = await liveOrder.WaitUntilClosedAsync(timeoutCts.Token).ConfigureAwait(false);
Console.WriteLine("Disposing trade API client and script API.");
}
}