private Task <string> InitCandles(string sec, string timeframe) { TaskCompletionSource <string> TCS = new TaskCompletionSource <string>(); IDictionary <Func <string, bool>, Action> actions = new Dictionary <Func <string, bool>, Action>(); actions.Add((frame) => { return(frame == "5"); }, () => { _candleRepository.GetHistory(sec, GetStartDate(timeframe, _sessions, _mode.GetDate()), "1").ContinueWith(t => { CandlesConverter converter = new CandlesConverter(() => { return(new Candle()); }); var work_data = converter.Convert(t.Result.ToList(), 1, 5); _candles[sec][timeframe] = work_data; TCS.SetResult("ok"); }); }); actions.Add((frame) => { return(frame == "60"); }, () => { _candleRepository.GetHistory(sec, GetStartDate(timeframe, _sessions, _mode.GetDate()), timeframe).ContinueWith(t => { _candles[sec][timeframe] = t.Result.ToList(); TCS.SetResult("ok"); }); }); actions.Single(a => a.Key.Invoke(timeframe)).Value.Invoke(); return(TCS.Task); }
public TradeModel(TradeSessionRepository TradeSessionRepository, CandleRepository CandleRepository, OrderRepository OrderRepository, PositionRepository PositionRepository, DealRepository DealRepository, WebApiClient client, ITradeMode mode, Mode modeProperties, object token) { _token = token; _tradeSessionRepository = TradeSessionRepository; _candleRepository = CandleRepository; _orderRepository = OrderRepository; _positionRepository = PositionRepository; _dealRepository = DealRepository; _apiClient = client; _mode = mode; _modeProperties = modeProperties; _candles = new Dictionary <string, IDictionary <string, IList <ICandle> > >(); Messenger.Default.Register <InitTradeModelMessage>(this, _token, async(msg) => { var temp_sessions = await _tradeSessionRepository.GetAll(); _sessions = temp_sessions.OrderBy(t => t.Date).ToList(); _mode.SetAction("init", () => { foreach (string sec in msg.securities) { _candles[sec] = new Dictionary <string, IList <ICandle> >(); foreach (string frame in msg.frames) { string res = InitCandles(sec, frame).Result; } } }); _mode.SetAction("initEmpty", () => { foreach (string sec in msg.securities) { _candles[sec] = new Dictionary <string, IList <ICandle> >(); foreach (string frame in msg.frames) { _candles[sec][frame] = new List <ICandle>(); } } }); _mode.SetAction("update", () => { IEnumerable <Candle> candles = _candleRepository.GetAll().Result; _positionlist = _positionRepository.GetAll().Result; foreach (string sec in msg.securities) { var tempData = candles.Where(c => c.Code == sec).OrderBy(c => c.begin); UpdateCadles(sec, tempData, msg.frames); } this._positions = GetPositions(_positionlist, msg.securities); }); _mode.SetAction("sendToRobo", () => { Messenger.Default.Send <GetCandlesResponseMessage>(new GetCandlesResponseMessage() { DateTime = mode.GetDate(), Сandles = _candles, Positions = _positions }, _token); }); _mode.SetAction("showData", () => { _dealRepository.GetAll().ContinueWith(t => { Messenger.Default.Send <ShowDataMessage>(new ShowDataMessage() { Сandles = CopyCandles(_candles), Deals = t.Result, Positions = _positionlist }); }); }); mode.Start(); }); Messenger.Default.Register <CreateOrderMessage>(this, msg => { Order order = new Order() { Code = msg.Code, OrderOperation = msg.OrderOperation, Account = _modeProperties.Account, Price = msg.Price, Count = msg.Count, Class = _modeProperties.Class, Client = _modeProperties.Client, Comment = _modeProperties.Client, Profit = msg.Profit, StopLoss = msg.StopLoss }; string result = _orderRepository.Create(order).Result; }); //Messenger.Default.Register<GetCandlesMessage>(this, (msg) => //{ // Messenger.Default.Send<GetCandlesResponseMessage>(new GetCandlesResponseMessage() // { // Сandles = _candles // }); //}); Messenger.Default.Register <ClosePositionMessage>(this, msg => { string result = _apiClient.GetData(string.Format("{0}admin/ClosePosition?sec={1}", this.ServerURL, msg.Code)).Result; }); }