Skip to content

Commit

Permalink
Restored console app
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad Noman Musleh committed Jan 26, 2022
1 parent 95566be commit e452789
Showing 1 changed file with 67 additions and 100 deletions.
167 changes: 67 additions & 100 deletions src/Console.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleDemo
Expand All @@ -23,53 +22,41 @@ internal class Program

private static async Task Main()
{
//Console.Write("Enter App ID: ");
Console.Write("Enter App ID: ");

//var appId = Console.ReadLine();
var appId = Console.ReadLine();

var appId = "699_9UIX3RJWkl3BwGfKi30xzfiyCaMkEA1FLKD020gy57i4e3XplL";
Console.Write("Enter App Secret: ");

//Console.Write("Enter App Secret: ");
var appSecret = Console.ReadLine();

//var appSecret = Console.ReadLine();
Console.Write("Enter Client Type (WebSocket Or TCP): ");

var appSecret = "dfJVd3Ud1HkLcQJaLPx5fmEqR8iUkmLYeCBikQUa6J3bJH2Jce";

//Console.Write("Enter Client Type (WebSocket Or TCP): ");

//var useWebScoket = Console.ReadLine().ToLowerInvariant() switch
//{
// "websocket" => true,
// _ => false
//};

var useWebScoket = false;

//Console.Write("Enter Connection Mode (Live or Demo): ");

//var modeString = Console.ReadLine();
var useWebScoket = Console.ReadLine().ToLowerInvariant() switch
{
"websocket" => true,
_ => false
};

//var mode = (Mode)Enum.Parse(typeof(Mode), modeString, true);
Console.Write("Enter Connection Mode (Live or Demo): ");

var mode = Mode.Demo;
var modeString = Console.ReadLine();

//Console.Write("Do you have an access token (Y/N): ");
var mode = (Mode)Enum.Parse(typeof(Mode), modeString, true);

//var isTokenAvailable = Console.ReadLine().ToLowerInvariant() switch
//{
// "y" => true,
// _ => false
//};
Console.Write("Do you have an access token (Y/N): ");

var isTokenAvailable = true;
var isTokenAvailable = Console.ReadLine().ToLowerInvariant() switch
{
"y" => true,
_ => false
};

if (isTokenAvailable)
{
//Console.Write("Your Access Token: ");

//var accessToken = Console.ReadLine();
Console.Write("Your Access Token: ");

var accessToken = "nC-FYGFwxsl_ZBCepA897H7bdoGTg_8GkVxAWRKflo4";
var accessToken = Console.ReadLine();

_token = new Token
{
Expand Down Expand Up @@ -150,52 +137,19 @@ private static async Task Main()

await _client.SendMessage(applicationAuthReq);

await Task.Delay(3000);
await Task.Delay(5000);

Console.WriteLine("You should see the application auth response message before entering any command");

Console.WriteLine("For commands list and description use 'help' command");

ShowDashLine();

AccountAuthRequest(new string[] { "accountauth", "16608956" });

await Task.Delay(2000);

TickDataRequest(new string[] { "tickdata", "16608956", "1", "bid", "1" });

GetCommand();
}

private static void OnMessageReceived(IMessage message)
{
if (message is ProtoOAGetTickDataRes response)
{
try
{
long previousTickTime = 0;

var stringBuilder = new StringBuilder();

stringBuilder.AppendLine("Tick,Time");

foreach (var tick in response.TickData)
{
previousTickTime += tick.Timestamp;

stringBuilder.AppendLine($"{tick.Tick},{DateTimeOffset.FromUnixTimeMilliseconds(previousTickTime)}");
}

System.Diagnostics.Trace.WriteLine(stringBuilder.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}

return;
}

Console.WriteLine($"\nMessage Received:\n{message}");

Console.WriteLine();
Expand Down Expand Up @@ -255,6 +209,8 @@ private static void ProcessCommand(string command)

Console.WriteLine("\nFor getting tick data: tickdata {Account ID} {Symbol ID} {Type (bid/ask)} {Number of Hours}\n");

Console.WriteLine("For getting profile: profile\n");

Console.WriteLine("To refresh access token, type: refreshtoken\n");

Console.WriteLine("To exit the app and disconnect the client type: disconnect\n");
Expand Down Expand Up @@ -287,6 +243,10 @@ private static void ProcessCommand(string command)
TickDataRequest(commandSplit);
break;

case "profile":
ProfileRequest();
break;

case "refreshtoken":
RefreshToken();
break;
Expand Down Expand Up @@ -338,97 +298,97 @@ private static void ProcessSubscriptionCommand(string[] commandSplit)

private static async void RefreshToken()
{
Console.WriteLine("Refreshing access token...");
Console.WriteLine("Sending ProtoOARefreshTokenReq...");

var refreshTokenReq = new ProtoOARefreshTokenReq
var request = new ProtoOARefreshTokenReq
{
RefreshToken = _token.RefreshToken
};

await _client.SendMessage(refreshTokenReq);
await _client.SendMessage(request);
}

private static async void SubscribeToSymbolTrendBar(string[] commandSplit)
{
Console.WriteLine("Subscribing to symbol trend bar event...");
Console.WriteLine("Sending ProtoOASubscribeLiveTrendbarReq...");

var subscribeLiveTrendbarReq = new ProtoOASubscribeLiveTrendbarReq()
var request = new ProtoOASubscribeLiveTrendbarReq()
{
Period = (ProtoOATrendbarPeriod)Enum.Parse(typeof(ProtoOATrendbarPeriod), commandSplit[2], true),
CtidTraderAccountId = long.Parse(commandSplit[3]),
SymbolId = long.Parse(commandSplit[4]),
};

await _client.SendMessage(subscribeLiveTrendbarReq);
await _client.SendMessage(request);
}

private static async void SubscribeToSymbolSpot(string[] commandSplit)
{
Console.WriteLine("Subscribing to symbol spot event...");
Console.WriteLine("Sending ProtoOASubscribeSpotsReq...");

var subscribeSpotsReq = new ProtoOASubscribeSpotsReq()
var request = new ProtoOASubscribeSpotsReq()
{
CtidTraderAccountId = long.Parse(commandSplit[2]),
};

subscribeSpotsReq.SymbolId.AddRange(commandSplit.Skip(3).Select(iSymbolId => long.Parse(iSymbolId)));
request.SymbolId.AddRange(commandSplit.Skip(3).Select(iSymbolId => long.Parse(iSymbolId)));

await _client.SendMessage(subscribeSpotsReq);
await _client.SendMessage(request);
}

private static async void SymbolListRequest(string[] commandSplit)
{
var accountId = long.Parse(commandSplit[1]);

Console.WriteLine("Sending symbols list req...");
Console.WriteLine("Sending ProtoOASymbolsListReq...");

var symbolsListReq = new ProtoOASymbolsListReq
var request = new ProtoOASymbolsListReq
{
CtidTraderAccountId = accountId,
};

await _client.SendMessage(symbolsListReq);
await _client.SendMessage(request);
}

private static async void ReconcileRequest(string[] commandSplit)
{
var accountId = long.Parse(commandSplit[1]);

Console.WriteLine("Sending reconcile req...");
Console.WriteLine("Sending ProtoOAReconcileReq...");

var reconcileReq = new ProtoOAReconcileReq
var request = new ProtoOAReconcileReq
{
CtidTraderAccountId = accountId,
};

await _client.SendMessage(reconcileReq);
await _client.SendMessage(request);
}

private static async void AccountListRequest()
{
Console.WriteLine("Sending account list req...");
Console.WriteLine("Sending ProtoOAGetAccountListByAccessTokenReq...");

var accountListByAccessTokenReq = new ProtoOAGetAccountListByAccessTokenReq
var request = new ProtoOAGetAccountListByAccessTokenReq
{
AccessToken = _token.AccessToken,
};

await _client.SendMessage(accountListByAccessTokenReq);
await _client.SendMessage(request);
}

private static async void AccountAuthRequest(string[] commandSplit)
{
var accountId = long.Parse(commandSplit[1]);

Console.WriteLine("Sending account auth req...");
Console.WriteLine("Sending ProtoOAAccountAuthReq...");

var accountAuthReq = new ProtoOAAccountAuthReq
var request = new ProtoOAAccountAuthReq
{
CtidTraderAccountId = accountId,
AccessToken = _token.AccessToken
};

await _client.SendMessage(accountAuthReq);
await _client.SendMessage(request);
}

private static async void TickDataRequest(string[] commandSplit)
Expand All @@ -443,23 +403,30 @@ private static async void TickDataRequest(string[] commandSplit)

var hours = long.Parse(commandSplit[4]);

Console.WriteLine("Sending tick data req...");
Console.WriteLine("Sending ProtoOAGetTickDataReq...");

var from = new DateTimeOffset(2022, 1, 20, 20, 3, 26, TimeSpan.FromHours(0));
var to = new DateTimeOffset(2022, 1, 20, 21, 3, 26, TimeSpan.FromHours(0));

var tickDataReq = new ProtoOAGetTickDataReq
var request = new ProtoOAGetTickDataReq
{
CtidTraderAccountId = accountId,
SymbolId = symbolId,
//FromTimestamp = DateTimeOffset.UtcNow.AddHours(-hours).ToUnixTimeMilliseconds(),
//ToTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
FromTimestamp = from.ToUnixTimeMilliseconds(),
ToTimestamp = to.ToUnixTimeMilliseconds(),
FromTimestamp = DateTimeOffset.UtcNow.AddHours(-hours).ToUnixTimeMilliseconds(),
ToTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
Type = type
};

await _client.SendMessage(tickDataReq);
await _client.SendMessage(request);
}

private static async void ProfileRequest()
{
Console.WriteLine("Sending ProtoOAGetCtidProfileByTokenReq...");

var request = new ProtoOAGetCtidProfileByTokenReq
{
AccessToken = _token.AccessToken
};

await _client.SendMessage(request);
}

private static void GetCommand()
Expand Down

0 comments on commit e452789

Please sign in to comment.