Esempio n. 1
0
        public static PnLViewModel ToPnLViewModel(this PnL pnl)
        {
            if (pnl == null)
            {
                return(null);
            }

            return(new PnLViewModel(pnl.LastUpdate.LocalDateTime, pnl.PerCrossPnLs.ToPnLPerCrossViewModel(), pnl.TotalFees, pnl.TotalGrossRealized, pnl.TotalGrossUnrealized, pnl.TotalNetRealized, pnl.TotalTradesCount));
        }
Esempio n. 2
0
        public async Task <PnL> LoadPnL(DateTime day, bool refresh)
        {
            try
            {
                if (!pnlDict.ContainsKey(day) || pnlDict[day] == null || refresh)
                {
                    CancellationTokenSource cts = new CancellationTokenSource();
                    cts.CancelAfter(TimeSpan.FromMinutes(1));

                    var result = await pnlsConnector.GetForDay(day, cts.Token);

                    if (result.Success && result.PnLs != null)
                    {
                        // Combine all pnls (temporary solution)
                        var combinedPerCross = result.PnLs.Select(p => p.PerCrossPnLs.AsEnumerable()).Aggregate((cur, next) => cur.Concat(next)).GroupBy(p => p.Cross).Select(g => new PnLPerCross()
                        {
                            Cross             = g.Key,
                            GrossRealized     = g.Select(p => p.GrossRealized).Sum(),
                            GrossUnrealized   = g.Select(p => p.GrossUnrealized).Sum(),
                            PipsUnrealized    = g.Select(p => p.PipsUnrealized).Sum(),
                            PositionOpenPrice = g.Select(p => p.PositionOpenPrice)?.FirstOrDefault(),
                            PositionOpenTime  = g.Select(p => p.PositionOpenTime)?.FirstOrDefault(),
                            PositionSize      = g.Select(p => p.PositionSize).Sum(),
                            TotalFees         = g.Select(p => p.TotalFees).Sum(),
                            TradesCount       = g.Select(p => p.TradesCount).Sum() / g.Count() // TODO: find a nicer solution
                        }).ToList();

                        pnlDict[day] = new PnL()
                        {
                            Account      = result.PnLs.Select(p => p.Account).FirstOrDefault(a => a.Contains("F")) ?? result.PnLs.Select(p => p.Account).First(),
                            Broker       = Broker.IB,
                            LastUpdate   = result.PnLs.Select(p => p.LastUpdate).Max(),
                            PerCrossPnLs = combinedPerCross
                        };
                    }
                    else
                    {
                        pnlDict[day] = new PnL();
                    }
                }

                return(pnlDict[day]);
            }
            catch (OperationCanceledException)
            {
                Debug.WriteLine("Not retrieving pnl: operation cancelled");
                return(null);
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Sync error: {e.Message}");
                return(null);
            }
        }
Esempio n. 3
0
        /* Открываем семантику для выбранного товара */
        private void btn_Ok_Click(object sender, EventArgs e)
        {
            if (analyticsForm != null)          //если вызвали из analyticsForm
            {
                if (bySKU)
                {
                    PnL pnl = new PnL(this, dgv_Products.Rows[dgv_Products.CurrentCellAddress.Y].Cells[3].Value.ToString());
                    pnl.Show();
                    this.Hide();
                }
                else
                {
                    PnL pnl = new PnL(dgv_Products.Rows[dgv_Products.CurrentCellAddress.Y].Cells[2].Value.ToString(), this);
                    pnl.Show();
                    this.Hide();
                }
            }
            else if (mf != null)                //если вызвали из MainForm
            {
                ProductId = int.Parse(dgv_Products.Rows[dgv_Products.CurrentCellAddress.Y].Cells[0].Value.ToString());

                string     sqlSemanticsIds = "SELECT * FROM Semantics WHERE [ProductId] = " + ProductId;
                SqlCommand command         = new SqlCommand(sqlSemanticsIds, connection);

                try
                {
                    connection.Open();

                    SqlDataReader reader = command.ExecuteReader();

                    if (reader.HasRows)
                    {
                        //Semantics semantics = new Semantics(ProductId, mf);
                        OpenSuccess = true;
                        //semantics.Show();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("У данного товара пока что нет ни одной семантики", "Ошибка");
                    }
                    reader.Close();
                    connection.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Упс! Возникла проблема с подключением к БД :( Приложение будет закрыто", "Ошибка");
                    Environment.Exit(0);
                }
            }
        }
Esempio n. 4
0
 public void pnl(int reqId, double dailyPnL, double unrealizedPnL, double realizedPnL)
 {
     PnL?.Invoke(new PnLMessage(reqId, dailyPnL, unrealizedPnL, realizedPnL));
 }