예제 #1
0
        public DealInfo toDTO(DBDeals deal)
        {
            DealInfo result = new DealInfo();

            //result.ClosePrice = deal;
            if (deal.Closetime.HasValue)
            {
                result.CloseTime = deal.Closetime.Value.ToString(xtradeConstants.MTDATETIMEFORMAT);
            }
            result.Comment    = deal.Comment;
            result.Commission = (double)deal.Commission;
            result.Lots       = (double)deal.Volume;
            if (deal.Adviser != null)
            {
                result.Magic = deal.Adviser.Id;
            }

            result.OpenPrice = (double)deal.Price;
            result.OpenTime  = deal.Opentime.ToString(xtradeConstants.MTDATETIMEFORMAT);
            result.Profit    = (double)deal.Profit;
            if (deal.Terminal != null)
            {
                result.Account     = deal.Terminal.Accountnumber.Value;
                result.AccountName = deal.Terminal.Broker;
            }

            result.SwapValue = (double)deal.Swap;
            if (deal.Symbol != null)
            {
                result.Symbol = deal.Symbol.Name;
            }
            if (deal.Orderid.HasValue)
            {
                result.Ticket = deal.Orderid.Value;
            }
            result.Type = (sbyte)deal.Typ;
            return(result);
        }
예제 #2
0
        public void SaveDeals(List <DealInfo> deals)
        {
            if (deals == null)
            {
                return;
            }
            if (deals.Count() <= 0)
            {
                return;
            }
            try
            {
                lock (lockDeals)
                {
                    int i = 0;
                    using (ISession Session = ConnectionHelper.CreateNewSession())
                    {
                        foreach (var deal in deals.OrderBy(x => x.CloseTime))
                        {
                            var sym = getSymbolByName(deal.Symbol);
                            if (sym == null)
                            {
                                continue;
                            }
                            DBDeals dbDeal = Session.Get <DBDeals>((int)deal.Ticket);
                            if (dbDeal == null)
                            {
                                if (getDealById(Session, deal.Ticket) != null)
                                {
                                    continue;
                                }
                                try
                                {
                                    using (ITransaction Transaction = Session.BeginTransaction())
                                    {
                                        dbDeal          = new DBDeals();
                                        dbDeal.Dealid   = (int)deal.Ticket;
                                        dbDeal.Symbol   = getSymbolByName(deal.Symbol);
                                        dbDeal.Terminal = getBDTerminalByNumber(Session, deal.Account);
                                        dbDeal.Adviser  = getAdviserByMagicNumber(Session, deal.Magic);
                                        dbDeal.Id       = (int)deal.Ticket;
                                        DateTime closeTime;
                                        if (DateTime.TryParse(deal.CloseTime, out closeTime))
                                        {
                                            dbDeal.Closetime = DateTime.Parse(deal.CloseTime);
                                        }
                                        dbDeal.Comment    = deal.Comment;
                                        dbDeal.Commission = (decimal)deal.Commission;
                                        DateTime openTime;
                                        if (DateTime.TryParse(deal.OpenTime, out openTime))
                                        {
                                            dbDeal.Opentime = DateTime.Parse(deal.OpenTime);
                                        }
                                        dbDeal.Orderid = (int)deal.OrderId;
                                        dbDeal.Profit  = (decimal)deal.Profit;
                                        dbDeal.Price   = (decimal)deal.ClosePrice;
                                        dbDeal.Swap    = (decimal)deal.SwapValue;
                                        dbDeal.Typ     = deal.Type;
                                        dbDeal.Volume  = (decimal)deal.Lots;
                                        Session.Save(dbDeal);
                                        Transaction.Commit();
                                        i++;
                                    }
                                }
                                catch (Exception)
                                {
                                    log.Log($"Deal {deal.Ticket}:{deal.Symbol} failed to be saved in database");
                                }
                            }
                        }
                    }

                    if (i > 0)
                    {
                        log.Log($"Saved {i} history deals in database");
                    }
                }
            }
            catch (Exception e)
            {
                string message = "Error: DataService.SaveDeals: " + e;
                log.Error(message);
                log.Log(message);
            }
        }