Exemple #1
0
        public JsonResult GetMonthBase([FromBody] Params param)
        {
            //Get History,Account
            var account   = _context.FrxAccount.SingleOrDefault(x => x.AccountId == param.AccountId);
            var positions = Position.GetPositions(param.ApiUrl, param.AccountId.ToString(), param.AccessToken);
            var histories = _context.FrxHistory.Where(x => x.AccountId == param.AccountId).OrderByDescending(x => x.ClosingTimestamp).ToList();
            var cashflow  = _context.FrxCashflow.Where(x => x.AccountId == param.AccountId);

            #region Get MonthBaseData
            //Get XData
            var          lastHistory      = histories[0];
            var          lastHisTimestamp = lastHistory.ClosingTimestamp;
            var          lastHisTime      = ConvertJson.StampToDateTime(lastHistory.ClosingTimestamp);
            var          lastHisTimeYear  = lastHisTime.Year;
            var          lastHisTimeMonth = lastHisTime.Month;
            List <XData> xDatas           = new List <XData>();
            var          xTime            = new DateTime(lastHisTimeYear, lastHisTimeMonth, 1);
            //Add Months
            for (int i = 0; i < 12; i++)
            {
                var   xt    = xTime.AddMonths(i - 11);
                var   xn    = xt.Month.ToString() + "月份";
                XData xData = new XData(xn, xt);
                xDatas.Add(xData);
            }
            //Add Year,Total
            var yearBeginTime  = new DateTime(lastHisTimeYear, 1, 1);
            var totalBeginTime = ConvertJson.StampToDateTime(account.TraderRegistrationTimestamp);
            var yearXData      = new XData("全年", yearBeginTime);
            var totalXData     = new XData("总计", totalBeginTime);
            xDatas.Add(yearXData);
            xDatas.Add(totalXData);
            //Get MonthBaseData
            List <MonthBaseData> monthBaseData = new List <MonthBaseData>();
            for (int i = 0; i < 14; i++)
            {
                MonthBaseData mdata   = new MonthBaseData();
                var           tempHis = new List <FrxHistory>();
                long          endTimestamp;
                mdata.XData = xDatas[i];
                if (i < 12)
                {
                    endTimestamp = ConvertJson.DateTimeToStamp(mdata.XData.XTime.AddMonths(1));
                }
                else
                {
                    endTimestamp = lastHisTimestamp + 86400000;
                }
                tempHis = histories.Where(x => x.ClosingTimestamp > ConvertJson.DateTimeToStamp(mdata.XData.XTime) && x.ClosingTimestamp < endTimestamp).OrderBy(y => y.ClosingTimestamp).ToList();
                if (tempHis.Count() != 0)
                {
                    var list_bsData = tempHis.GroupBy(g => new
                    {
                        g.SymbolCode
                    }).Select(s => new BuySellData
                    {
                        SymbolCode = s.Key.SymbolCode,
                        Count      = s.Count(),
                        Lots       = s.Sum(a => a.Lot),
                        Pips       = s.Sum(a => a.Pips * a.Lot) / s.Sum(a => a.Lot),
                        Profit     = s.Sum(a => a.NetProfit),
                        BuyCount   = s.Where(a => a.TradeType == TradeType.Buy).Count(),
                        BuyLots    = s.Where(a => a.TradeType == TradeType.Buy).Sum(a => a.Lot),
                        BuyPips    = s.Where(a => a.TradeType == TradeType.Buy).Sum(a => a.Pips * a.Lot) / s.Where(a => a.TradeType == TradeType.Buy).Sum(a => a.Lot),
                        BuyProfit  = s.Where(a => a.TradeType == TradeType.Buy).Sum(a => a.NetProfit),
                        BuyRate    = s.Where(t => t.TradeType == TradeType.Buy).Count() == 0 ? 0
                        : (s.Where(t => t.TradeType == TradeType.Sell).Count() == 0 ? 1
                        : Math.Round(s.Where(t => t.TradeType == TradeType.Buy).Sum(a => a.Lot) / s.Sum(b => b.Lot), 4)),
                        SellCount  = s.Where(a => a.TradeType == TradeType.Sell).Count(),
                        SellLots   = s.Where(a => a.TradeType == TradeType.Sell).Sum(a => a.Lot),
                        SellPips   = s.Where(a => a.TradeType == TradeType.Sell).Sum(a => a.Pips * a.Lot) / s.Where(a => a.TradeType == TradeType.Sell).Sum(a => a.Lot),
                        SellProfit = s.Where(a => a.TradeType == TradeType.Sell).Sum(a => a.NetProfit),
                        SellRate   = s.Where(t => t.TradeType == TradeType.Buy).Count() == 0 ? 1
                        : (s.Where(t => t.TradeType == TradeType.Sell).Count() == 0 ? 0
                        : (1 - Math.Round(s.Where(t => t.TradeType == TradeType.Buy).Sum(a => a.Lot) / s.Sum(b => b.Lot), 4))),
                        WinCount = s.Where(a => a.NetProfit > 0).Count(),
                        WinRate  = s.Where(a => a.NetProfit > 0).Count() == 0 ? 0
                        : (s.Where(a => a.NetProfit <= 0).Count() == 0 ? 1
                        : Math.Round((double)s.Where(a => a.NetProfit > 0).Count() / (double)s.Count(), 4)),
                        LossCount = s.Where(a => a.NetProfit <= 0).Count(),
                        LossRate  = s.Where(a => a.NetProfit > 0).Count() == 0 ? 1
                        : (s.Where(a => a.NetProfit <= 0).Count() == 0 ? 0
                        : (1 - Math.Round((double)s.Where(a => a.NetProfit > 0).Count() / (double)s.Count(), 4))),
                    }).ToList();
                    var initBalance = tempHis.ToList()[0].Balance - tempHis.ToList()[0].NetProfit;
                    var net         = tempHis.Select(x => x.NetProfit).Sum();
                    var gain        = net / initBalance;
                    var swap        = tempHis.Select(x => x.Swap).Sum();
                    var pips        = tempHis.Select(x => x.Pips).Sum();
                    var lots        = tempHis.Select(x => x.Lot).Sum();
                    mdata.BuySellData = list_bsData;
                    mdata.Net         = Math.Round(net, 2);
                    mdata.Gain        = Math.Round(gain, 4);
                    mdata.Swap        = Math.Round(swap, 2);
                    mdata.Pips        = Math.Round(pips, 2);
                    mdata.Lots        = Math.Round(lots, 2);
                    monthBaseData.Add(mdata);
                }
                else
                {
                    mdata.BuySellData = new List <BuySellData>();
                    mdata.Net         = 0;
                    mdata.Gain        = 0;
                    mdata.Swap        = 0;
                    mdata.Pips        = 0;
                    mdata.Lots        = 0;
                    monthBaseData.Add(mdata);
                }
            }
            #endregion
            #region GetAccountInfo
            var deposit  = 0.00;
            var withdraw = 0.00;
            foreach (var c in cashflow)
            {
                if (c.Type == "DEPOSIT")
                {
                    deposit += c.Delta;
                }
                if (c.Type == "WITHDRAW")
                {
                    withdraw += c.Delta;
                }
            }
            var unrnet = 0.00;
            if (positions.Count != 0)
            {
                unrnet = (double)positions.Sum(x => x.Commission * 2 + x.Swap + x.Profit) / 100;
            }
            var  maxbalance       = histories.Select(x => x.Balance).Max();
            long maxtimestamp     = histories.FirstOrDefault(x => x.Balance == maxbalance).ClosingTimestamp;
            var  maxdrawdown      = histories.Select(x => Math.Round((x.Balance - x.Equity) / x.Balance, 4)).Max();
            long maxdrawtimestamp = histories.FirstOrDefault(x => Math.Round((x.Balance - x.Equity) / x.Balance, 4) == maxdrawdown).ClosingTimestamp;
            var  totalswap        = histories.Select(x => x.Swap).Sum();
            var  twr = 1.00;
            foreach (var m in monthBaseData)
            {
                if (m.XData.XName != "全年" && m.XData.XName != "总计")
                {
                    twr = (1 + m.Gain) * twr;
                }
            }
            long lasttradetimestamp;
            if (histories.Count() > 0)
            {
                lasttradetimestamp = lastHisTimestamp;
            }
            else
            {
                lasttradetimestamp = account.TraderRegistrationTimestamp;
            }
            var accountinfo = new AccountInfo
            {
                Gain                  = Math.Round(twr - 1, 4) * 100,
                AbsGain               = Math.Round((account.Balance - deposit + withdraw) / deposit, 4) * 100,
                MaxDraw               = maxdrawdown * 100,
                MaxDrawTimestamp      = maxdrawtimestamp,
                Deposit               = deposit,
                Withdraw              = withdraw,
                Balance               = account.Balance,
                Equity                = account.Balance + (double)unrnet,
                MaxBalance            = maxbalance,
                MaxBalanceTimestamp   = maxtimestamp,
                TotalProfit           = account.Balance - deposit + withdraw,
                TotalSwap             = Math.Round(totalswap, 2),
                LastTradeTimestamp    = lasttradetimestamp,
                RigistrationTimestamp = account.TraderRegistrationTimestamp
            };
            #endregion
            return(Json(new { monthBaseData, accountinfo }));
        }