-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathBuyOption.cs
149 lines (122 loc) · 4.24 KB
/
BuyOption.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Threading.Tasks;
using CommandLine;
using ExchangeSharp;
using ExchangeSharpConsole.Options.Interfaces;
namespace ExchangeSharpConsole.Options
{
[Verb(
"buy",
HelpText = "Adds a buy order to a given exchange.\n"
+ "This sub-command will perform an action that can lead to loss of funds.\n"
+ "Be sure to test it first with a dry-run."
)]
public class BuyOption
: BaseOption,
IOptionPerExchange,
IOptionWithDryRun,
IOptionWithKey,
IOptionWithInterval,
IOptionWithWait,
IOptionWithOrderInfo
{
public override async Task RunCommand()
{
await AddOrder(true);
}
protected async Task AddOrder(bool isBuyOrder)
{
using var api = await GetExchangeInstanceAsync(ExchangeName);
var exchangeOrderRequest = GetExchangeOrderRequest(isBuyOrder, api);
if (IsDryRun)
{
DumpRequest(exchangeOrderRequest);
return;
}
api.LoadAPIKeys(KeyPath);
var result = await api.PlaceOrderAsync(exchangeOrderRequest);
if (Wait)
{
await WaitForOrder(result, api).ConfigureAwait(false);
}
else
{
DumpResponse(result);
}
}
private async Task WaitForOrder(ExchangeOrderResult order, IExchangeAPI api)
{
while (order.Result == ExchangeAPIOrderResult.Open)
{
Console.Clear();
Console.WriteLine(order);
await Task.Delay(IntervalMs).ConfigureAwait(false);
order = await api.GetOrderDetailsAsync(
order.OrderId,
marketSymbol: order.MarketSymbol
);
}
Console.Clear();
Console.WriteLine(order);
Console.WriteLine($"Your order changed the status to \"{order.Result}\"");
}
private ExchangeOrderRequest GetExchangeOrderRequest(bool isBuyOrder, IExchangeAPI api)
{
var exchangeOrderRequest = new ExchangeOrderRequest
{
Amount = Amount,
Price = Price,
IsBuy = isBuyOrder,
IsMargin = IsMargin,
MarketSymbol = api.NormalizeMarketSymbol(MarketSymbol),
OrderType = OrderType,
StopPrice = StopPrice,
ShouldRoundAmount = ShouldRoundAmount
};
Amount = exchangeOrderRequest.RoundAmount();
return exchangeOrderRequest;
}
private void DumpResponse(ExchangeOrderResult orderResult)
{
Console.WriteLine($"Order Id: {orderResult.OrderId}");
Console.WriteLine($"Trade Id: {orderResult.TradeId}");
Console.WriteLine($"Order Date: {orderResult.OrderDate:R}");
Console.WriteLine($"Completed Date: {orderResult.CompletedDate:R}");
Console.WriteLine($"Side: {(orderResult.IsBuy ? "Bid" : "Ask")}");
Console.WriteLine($"Market symbol: {orderResult.MarketSymbol}");
Console.WriteLine($"Status: {orderResult.Result}");
Console.WriteLine($"Price: {orderResult.Price:N}");
Console.WriteLine($"Amount: {orderResult.Amount:N}");
Console.WriteLine($"Amount Filled: {orderResult.AmountFilled:N}");
Console.WriteLine($"Fees: {orderResult.Fees:N}");
Console.WriteLine($"Fees currency: {orderResult.FeesCurrency}");
Console.WriteLine($"Message: {orderResult.Message}");
Console.WriteLine($"Average Price: {orderResult.AveragePrice:N}");
}
private void DumpRequest(ExchangeOrderRequest orderRequest)
{
Console.WriteLine($"Exchange: {ExchangeName}");
Console.WriteLine($"KeyPath: {KeyPath}");
Console.WriteLine("---");
Console.WriteLine($"Price: {orderRequest.Price:N}");
Console.WriteLine($"Amount: {orderRequest.Amount:N}");
Console.WriteLine($"StopPrice: {orderRequest.StopPrice:N}");
Console.WriteLine($"OrderType: {orderRequest.OrderType}");
Console.WriteLine($"IsMargin: {(orderRequest.IsMargin ? "Y" : "N")}");
Console.WriteLine($"ShouldRoundAmount: {(orderRequest.ShouldRoundAmount ? "Y" : "N")}");
Console.WriteLine($"MarketSymbol: {orderRequest.MarketSymbol}");
}
public string ExchangeName { get; set; }
public bool Wait { get; set; }
public bool IsDryRun { get; set; }
public string KeyPath { get; set; }
public decimal Price { get; set; }
public decimal Amount { get; set; }
public decimal StopPrice { get; set; }
public OrderType OrderType { get; set; }
public bool IsMargin { get; set; }
public bool ShouldRoundAmount { get; set; }
public string MarketSymbol { get; set; }
public int IntervalMs { get; set; }
}
}