Skip to content

Commit 2d45a65

Browse files
committed
add Example project
1 parent 16b4fde commit 2d45a65

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

DigitexConnectorCSharp.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1010
README.md = README.md
1111
EndProjectSection
1212
EndProject
13+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{1CE9B672-B2A5-4FC1-8B83-3AFC46CB00C9}"
14+
ProjectSection(ProjectDependencies) = postProject
15+
{5BDAF922-88F8-4B73-8848-221469768845} = {5BDAF922-88F8-4B73-8848-221469768845}
16+
EndProjectSection
17+
EndProject
1318
Global
1419
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1520
Debug|Any CPU = Debug|Any CPU
@@ -20,6 +25,10 @@ Global
2025
{5BDAF922-88F8-4B73-8848-221469768845}.Debug|Any CPU.Build.0 = Debug|Any CPU
2126
{5BDAF922-88F8-4B73-8848-221469768845}.Release|Any CPU.ActiveCfg = Release|Any CPU
2227
{5BDAF922-88F8-4B73-8848-221469768845}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{1CE9B672-B2A5-4FC1-8B83-3AFC46CB00C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{1CE9B672-B2A5-4FC1-8B83-3AFC46CB00C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{1CE9B672-B2A5-4FC1-8B83-3AFC46CB00C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{1CE9B672-B2A5-4FC1-8B83-3AFC46CB00C9}.Release|Any CPU.Build.0 = Release|Any CPU
2332
EndGlobalSection
2433
GlobalSection(SolutionProperties) = preSolution
2534
HideSolutionNode = FALSE

Example/Example.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\DigitexConnector\DigitexConnector.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

Example/IntervalAlgorithm.cs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using DigitexConnector;
2+
using DigitexConnector.EngineAPI;
3+
using DigitexConnector.Orders;
4+
using DigitexConnector.Trading;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Threading;
8+
9+
namespace Example
10+
{
11+
class IntervalAlgorithm : TradingAlgorithm
12+
{
13+
private int Interval = 10;
14+
private string SymbolName = "BTC/USD1";
15+
private Symbol Smbl;
16+
private OrderBook OBook;
17+
18+
private Timer TradingTimer;
19+
20+
public IntervalAlgorithm(string host, string token, bool wss)
21+
: base(host, token, wss)
22+
{ }
23+
24+
public override void Prepare()
25+
{
26+
Smbl = SymbolsContainer.GetSymbol(SymbolName);
27+
SpotPriceUpdated += SpotPriceHandler;
28+
OrderBookUpdated += OrderBookHandler;
29+
OBook = TrackSymbol(Smbl);
30+
TradingTimer = new Timer((object obj) => PlaceOrder(), null, Interval * 1000, Interval * 1000);
31+
}
32+
33+
private void SpotPriceHandler(OrderBook oBook)
34+
{
35+
if (oBook.Equals(OBook))
36+
{
37+
// Do something for handle spot price changing.
38+
}
39+
}
40+
41+
private void OrderBookHandler(OrderBook oBook)
42+
{
43+
if (oBook.Equals(OBook))
44+
{
45+
// Do something for handle market depth changing.
46+
}
47+
}
48+
49+
private void PlaceOrder()
50+
{
51+
bool positivePosition = Account.GetTraderInfo(Smbl).PositionType == DigitexWire.OrderPosition.Long;
52+
decimal price;
53+
DigitexWire.OrderSide side;
54+
55+
if (positivePosition)
56+
{
57+
price = OBook.GetBestAskPrice() ?? OBook.AdjustedSpotPrice;
58+
side = DigitexWire.OrderSide.Sell;
59+
}
60+
else
61+
{
62+
price = OBook.GetBestBidPrice() ?? OBook.AdjustedSpotPrice;
63+
side = DigitexWire.OrderSide.Buy;
64+
}
65+
66+
OrderBase order = PlaceOrderLimit(Smbl, side, 1, price, OrderStatusHandler, ErrorHandler);
67+
68+
Console.WriteLine($"Place {order?.OrigClientId} - {(order is null ? "fail" : "success")}");
69+
}
70+
71+
private void OrderStatusHandler(OrderBase order)
72+
{
73+
// Do something for handle order status changing.
74+
75+
Console.WriteLine($"Order {order.OrigClientId} is {order.Status}");
76+
77+
// Use OrigClientId for comparison of orders.
78+
}
79+
80+
private void ErrorHandler(OrderBase order, ErrorCodes code)
81+
{
82+
// Do something for handle order errors.
83+
84+
Console.WriteLine($"Error {code}, order {order.OrigClientId}");
85+
}
86+
87+
public override List<ModuleState> GetParams()
88+
{
89+
return new List<ModuleState>()
90+
{
91+
new ModuleState("interval", "10")
92+
};
93+
}
94+
95+
public override void SetAlgoArguments(Dictionary<string, string> algoArguments)
96+
{
97+
if (algoArguments.ContainsKey("interval")) { Interval = int.Parse(algoArguments["interval"]); }
98+
}
99+
100+
public override void Stop()
101+
{
102+
TradingTimer.Dispose();
103+
}
104+
105+
public override void OnDispose()
106+
{
107+
TradingTimer?.Dispose();
108+
}
109+
}
110+
}

Example/Program.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace Example
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
// for mainnet set host to "ws.mainnet.digitexfutures.com"
10+
string host = "ws.testnet.digitexfutures.com";
11+
bool wss = true;
12+
string token = "d030b17c9b993107399eb19121daea0f04e4b476";
13+
14+
IntervalAlgorithm algorithm = new IntervalAlgorithm(host, token, wss);
15+
algorithm.Prepare();
16+
algorithm.Connect();
17+
18+
while (Console.ReadKey().Key != ConsoleKey.Q) { }
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)