Skip to content

Instantly share code, notes, and snippets.

@nj4x
Created September 26, 2018 14:38
Show Gist options
  • Save nj4x/ef534e27f12ca2ad332c6878d90809c6 to your computer and use it in GitHub Desktop.
Save nj4x/ef534e27f12ca2ad332c6878d90809c6 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Threading;
using nj4x.Metatrader;
using NUnit.Framework;
namespace nj4x.Appliance.Tests
{
public class Account : IDisposable
{
public int AccountNo { get; set; }
public string Server { get; set; }
public string Password { get; set; }
private Strategy _connection;
private DateTime _lastOrderCloseDate;
private readonly HashSet<long> _processedTickets = new HashSet<long>();
public void Dispose()
{
_connection?.Dispose();
}
public void LoadHistoricalTrades()
{
if (_connection == null)
{
_lastOrderCloseDate = DateTime.MinValue; // load from DB
//
_connection = new Strategy();
_connection
.WithHistoryPeriod(_lastOrderCloseDate == DateTime.MinValue
? Strategy.HistoryPeriod.ALL_HISTORY
: Strategy.HistoryPeriod.LAST_3_MONTHS)
.Connect("127.0.0.1", 7788, new Broker(Server), AccountNo.ToString(), Password);
}
//
var orders = _lastOrderCloseDate == DateTime.MinValue
? _connection.OrderGetAll(SelectionPool.MODE_HISTORY)
: _connection.OrderGetAll(SelectionPool.MODE_HISTORY, _lastOrderCloseDate, DateTime.Now);
//
foreach (var o in orders.Values)
{
IOrderInfo order = (IOrderInfo) o;
if (!_processedTickets.Contains(order.Ticket()))
{
_lastOrderCloseDate = order.GetCloseTime() > _lastOrderCloseDate
? order.GetCloseTime()
: _lastOrderCloseDate;
//
Console.WriteLine(order); // dump order to DB
_processedTickets.Add(order.Ticket());
}
}
}
}
[TestFixture]
public class HistoryLoader
{
private static List<Account> GetAccounts()
{
// load accounts from configs or DB
return new List<Account>
{
new Account {Server = "demo1-amsterdam.fxpro.com", AccountNo = 6796186, Password = "dpkk4hg"},
new Account {Server = "Oanda-V20 Practice-2", AccountNo = 3019935, Password = "Trading_NJ4X"}
//...
};
}
[Test]
public void Main()
{
var accounts = GetAccounts();
//
while (true)
{
foreach (var account in accounts)
{
account.LoadHistoricalTrades();
}
//
Thread.Sleep(5000); // check history every 5 seconds
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment