public interface IIntegrator { bool Connected {get; set;} void Initialize(); void Connect(string userName, string password); void Disconnect(); void Stop(); Action UpdateInfo { get; set; } bool AllowsHistory { get; } bool AllowsTesting { get; } bool AllowsTrading { get; } bool RequiresCredentials { get; } string[] AvaliableMarketCodes { get; } //Date, Open, High, Low, Close, Volume, Adj Close List<string[]> LoadHistory(string marketNameCode,string instrument,int period,DateTime dateFrom,DateTime dateTo); Helpers.Positions[] GetPositions(); double GetLastPrice(string code); int LimitSell(string briefCase, string ticker, string placeCode, int amount, double price, int timeOut, double? activateIfPriceHasGrown, double? activateIfPriceHasFallen); int LimitBuy(string briefCase, string ticker, string placeCode, int amount, double price, int timeOut, double? activateIfPriceHasGrown, double? activateIfPriceHasFallen); void DropOrder(int orderNo); }
public class Positions { public string Briefcase { get; set; } public string MarketCode { get; set; } public string Ticker { get; set; } public double Amount { get; set; } public double DailyPL { get; set; } }
public class AlfaDirectIntegrator : IIntegrator, INotifyPropertyChanged { public bool Connected { get { if (_terminal == null) return false; return _terminal.Connected; } set { _terminal.Connected = value; OnPropertyChanged("Connected"); if (UpdateInfo!=null) UpdateInfo(); } } public Action UpdateInfo { get; set; } private AlfaDirectClass _terminal; public bool AllowsHistory { get { return true; } } public bool AllowsTesting { get { return true; } } public bool AllowsTrading { get { return true; } } public bool RequiresCredentials { get { return true; } } // OnConnectionChanged // // private Timer _connectionStateChecker; private int _msToCheckConnectionState = 1000; // public void Initialize() { _terminal = new AlfaDirectClass(); _connectionStateChecker = new Timer(_msToCheckConnectionState); _connectionStateChecker.Elapsed += _connectionStateChecker_Elapsed; _terminal.OnConnectionChanged += (obj) => { _connectionStateChecker_Elapsed(null, null); }; _connectionStateChecker.Enabled = true; } void _connectionStateChecker_Elapsed(object sender, ElapsedEventArgs e) { OnPropertyChanged("Connected"); UpdateInfo(); } public void Connect(string userName, string password) { _terminal.UserName = userName; _terminal.Password = password; Connected = true; } public void Disconnect() { Connected = false; } public void Stop() { if (_connectionStateChecker != null) { _connectionStateChecker.Enabled = false; _connectionStateChecker.Elapsed -= _connectionStateChecker_Elapsed; _connectionStateChecker = null; } _terminal = null; } public string[] AvaliableMarketCodes { get { return MarketCodes.Keys.ToArray(); } } private Dictionary<string, string> MarketCodes = new Dictionary<string, string>() { { " ", "MICEX_SHR_T" }, { "", "FORTS" }, { " ", "MICEX_SHR"}, { "FOREX", "CURRENCY"}, { "DJ Indexes", "DJIA"}, { " ", "EBONDS"}, { "OTC EUROCLEAR", "EUROTRADE"}, { ". ", "INDEX"}, { ". ", "INDEX2"}, { "IPE", "IPE"}, { "LME", "LME"}, { "LSE", "LSE"}, { "LSE(delay)", "LSE_DL"}, { " ", "MICEX_BOND"}, { " ", "MICEX_EBND"}, { " ", "MICEX_SELT"}, { "NEWEX", "NEWEX"}, { "-", "NONMARKET"}, { "- ()", "NONMARKET_DCC"}, { "NYSE", "NYSE"}, { " ()", "OTC_NDC"}, { " ()", "RTS_GAZP"}, { " ", "RTS_SGK_R"}, { "", "RTS_SHR"}, { " .", "RTS_STANDARD"} }; public List<string[]> LoadHistory(string marketNameCode, string instrument, int period, DateTime dateFrom, DateTime dateTo) { // : 08.07.2010 15:22:21 // marketNameCode = MarketCodes[marketNameCode]; List<string[]> data = new List<string[]>(); var rawData = (string)_terminal.GetArchiveFinInfo(marketNameCode, instrument, period, dateFrom, dateTo.AddDays(1), 2, 100); if (_terminal.LastResult != StateCodes.stcSuccess) { System.Windows.MessageBox.Show(_terminal.LastResultMsg,"",System.Windows.MessageBoxButton.OK,System.Windows.MessageBoxImage.Error); return data; } string[] stringSeparators = new string[] { "\r\n" }; // var strings = rawData.Split(stringSeparators,StringSplitOptions.None).ToList(); foreach (var s in strings) if (s != "") { var values = s.Replace(',','.').Split('|'); data.Add(new string[] {values[0] , values[1], values[2], values[3], values[4], values[5], values[4] }); } // return data; } public double GetLastPrice(string code) { var message = _terminal.GetLocalDBData("FIN_INFO", "last_price", "p_code like \'" + code + "\'"); if (message == null) return 0; return Convert.ToDouble(message.Split('|')[0]); } public int LimitSell(string briefCase, string ticker, string placeCode, int amount, double price, int timeOut, double? activateIfPriceHasGrown, double? activateIfPriceHasFallen) { return _terminal.CreateLimitOrder(briefCase, placeCode, ticker, DateTime.Now.AddMinutes(1), " Trade Robot System", "RUR", "S", amount, price, activateIfPriceHasGrown, activateIfPriceHasFallen, null, null, null, 'Y', null, null, null, null, null, null, null, null, null, null, timeOut); } public int LimitBuy(string briefCase, string ticker, string placeCode, int amount, double price, int timeOut, double? activateIfPriceHasGrown, double? activateIfPriceHasFallen) { return _terminal.CreateLimitOrder(briefCase, placeCode, ticker, DateTime.Now.AddMinutes(1), " Trade Robot System", "RUR", "B", amount, price, activateIfPriceHasGrown, activateIfPriceHasFallen, null, null, null, 'Y', null, null, null, null, null, null, null, null, null, null, timeOut); } public void DropOrder(int orderNo) { _terminal.DropOrder(orderNo, null, null, null, null, null, 0); } public Helpers.Positions[] GetPositions() { var result = new List<Helpers.Positions>(); var message = _terminal.GetLocalDBData("balance", "acc_code, p_code, place_code, forword_rest, daily_pl", ""); if ((message == null)||(!Connected)) return result.ToArray(); string[] stringSeparators = new string[] { "\r\n" }; // var strings = message.Split(stringSeparators,StringSplitOptions.None).ToList(); foreach (var str in strings) if (str != "") { var fields = str.Split('|'); result.Add(new Helpers.Positions() { Briefcase = fields[0], Ticker = fields[1], MarketCode = fields[2], Amount = Convert.ToDouble(fields[3]), DailyPL = Convert.ToDouble(fields[4]) }); } return result.ToArray(); } #region PropertyChanged members public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion }
Source: https://habr.com/ru/post/253839/
All Articles