-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
ExampleOption.cs
65 lines (57 loc) · 2 KB
/
ExampleOption.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
using System;
using System.Threading.Tasks;
using CommandLine;
using ExchangeSharp;
using ExchangeSharpConsole.Options.Interfaces;
namespace ExchangeSharpConsole.Options
{
[Verb(
"example",
HelpText = "Simple example showing how to create an API instance and get the ticker, and place an order."
)]
public class ExampleOption : BaseOption, IOptionWithKey
{
public override async Task RunCommand()
{
using var api = await ExchangeAPI.GetExchangeAPIAsync(ExchangeName.Kraken);
var ticker = await api.GetTickerAsync("XXBTZUSD");
Console.WriteLine("On the Kraken exchange, 1 bitcoin is worth {0} USD.", ticker.Bid);
try
{
// load API keys created from ExchangeSharpConsole.exe keys mode=create path=keys.bin keylist=public_key,private_key
api.LoadAPIKeys(KeyPath);
}
catch (ArgumentException)
{
Console.Error.WriteLine(
"Invalid key file.\n" + "Try generating a key file with the \"keys\" utility."
);
Environment.Exit(Program.ExitCodeError);
return;
}
// place limit order for 0.01 bitcoin at ticker.Ask USD
var result = await api.PlaceOrderAsync(
new ExchangeOrderRequest
{
Amount = 0.01m,
IsBuy = true,
Price = ticker.Ask,
MarketSymbol = "XXBTZUSD"
}
);
// Kraken is a bit funny in that they don't return the order details in the initial request, so you have to follow up with an order details request
// if you want to know more info about the order - most other exchanges don't return until they have the order details for you.
// I've also found that Kraken tends to fail if you follow up too quickly with an order details request, so sleep a bit to give them time to get
// their house in order.
await Task.Delay(500);
result = await api.GetOrderDetailsAsync(result.OrderId);
Console.WriteLine(
"Placed an order on Kraken for 0.01 bitcoin at {0} USD. Status is {1}. Order id is {2}.",
ticker.Ask,
result.Result,
result.OrderId
);
}
public string KeyPath { get; set; }
}
}