Added MtApi5 sample: HistoryOrder

This commit is contained in:
Viacheslav Demydiuk
2024-08-23 11:35:00 +03:00
parent 05de6f4995
commit d9a9d88318
3 changed files with 109 additions and 0 deletions
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Reference Include="MtApi5">
<HintPath>..\..\..\build\products\Release\net8.0\MtApi5.dll</HintPath>
</Reference>
<Reference Include="MtClient">
<HintPath>..\..\..\build\products\Release\net8.0\MtClient.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\build\products\Release\net8.0\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
+62
View File
@@ -0,0 +1,62 @@
using MtApi5;
static DateTime UnixTimeStampToDateTime(long unixTimeStamp)
{
// Unix timestamp is seconds past epoch
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dateTime;
}
const string HOST = "localhost";
const int PORT = 8228;
const int HISTORY_DAYS = 20;
var client = new MtApi5Client();
Console.WriteLine("Start history test");
try
{
client.Connect(HOST, PORT).Wait();
}
catch (Exception e)
{
Console.WriteLine($"Connection failed: {e.Message}");
return;
}
Console.WriteLine($"Connected to {HOST}:{PORT}");
var to = DateTime.Now;
var from = to.AddDays(-HISTORY_DAYS);
if (client.HistorySelect(from, to))
{
var orders = client.HistoryOrdersTotal();
Console.WriteLine($"Order count: {orders}");
for (int i = 0; i < orders; i++)
{
var ticket = client.HistoryOrderGetTicket(i);
var order_type = (ENUM_ORDER_TYPE) client.HistoryOrderGetInteger(ticket, ENUM_ORDER_PROPERTY_INTEGER.ORDER_TYPE);
long time_setup_msc = client.HistoryOrderGetInteger(ticket, ENUM_ORDER_PROPERTY_INTEGER.ORDER_TIME_SETUP_MSC);
long time_done_msc = client.HistoryOrderGetInteger(ticket, ENUM_ORDER_PROPERTY_INTEGER.ORDER_TIME_DONE_MSC);
double order_open_price = client.HistoryOrderGetDouble(ticket, ENUM_ORDER_PROPERTY_DOUBLE.ORDER_PRICE_OPEN);
double order_volume = client.HistoryOrderGetDouble(ticket, ENUM_ORDER_PROPERTY_DOUBLE.ORDER_VOLUME_INITIAL);
Console.WriteLine($"{i + 1} - Order #{ticket} type = {order_type}" +
$" ORDER_TIME_SETUP_MSC={time_setup_msc} => {UnixTimeStampToDateTime(time_setup_msc / 1000)}," +
$" ORDER_TIME_DONE_MSC={time_done_msc} => {UnixTimeStampToDateTime(time_done_msc / 1000)}," +
$" Open price = {order_open_price}, Volume = {order_volume}");
}
}
else
{
var last_error = client.GetLastError();
Console.WriteLine($"Failed to select order history. ErrorCode = {last_error}");
}
Console.WriteLine("History test completed.");
client.Disconnect();
+25
View File
@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34622.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HistoryOrderSample", "HistoryOrderSample\HistoryOrderSample.csproj", "{AE42346D-347D-43E4-B0C8-A0BF7E506A5E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AE42346D-347D-43E4-B0C8-A0BF7E506A5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AE42346D-347D-43E4-B0C8-A0BF7E506A5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AE42346D-347D-43E4-B0C8-A0BF7E506A5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AE42346D-347D-43E4-B0C8-A0BF7E506A5E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3C1DF1F1-13BF-4CE2-9F74-F39548D21ABE}
EndGlobalSection
EndGlobal