Exemple #1
0
        private void Close(IList trades)
        {
            if (trades == null || trades.Count == 0)
            {
                return;
            }

            var closedTrades = new List <Trade>();

            foreach (Trade trade in trades)
            {
                //Already closed or can't close -> skip it
                if (!trade.Open)
                {
                    continue;
                }

                //first load up the collections, needed for the IsClosable() check.
                Context.Entry(trade).Collection(x => x.Orders).Load();
                Context.Entry(trade).Collection(x => x.CashTransactions).Load();
                Context.Entry(trade).Collection(x => x.FXTransactions).Load();

                //this needs to be done after loading the orders
                if (!trade.IsClosable())
                {
                    continue;
                }

                trade.Open = false;
                closedTrades.Add(trade);
            }

            //Update the stats of the trades we closed
            Task.Run(() =>
            {
                foreach (Trade trade in closedTrades)
                {
                    //we can skip collection load since it's done a few lines up
                    TradesRepository.UpdateStats(trade, skipCollectionLoad: true);
                }
                Context.SaveChanges();
            });
        }
        private void UpdateTradeStats(IList trades)
        {
            if (trades == null || trades.Count == 0)
            {
                return;
            }

            foreach (Trade t in trades)
            {
                TradesRepository.UpdateStats(t);
            }
            Context.SaveChanges();
        }
Exemple #3
0
        private async Task UpdateTradeStats(IList trades)
        {
            if (trades == null || trades.Count == 0)
            {
                return;
            }

            foreach (Trade t in trades)
            {
                await TradesRepository.UpdateStats(t);
            }

            await TradesRepository.UpdateTrade(trades : trades.Cast <Trade>());
        }
Exemple #4
0
        /// <summary>
        /// Opens/closes a trade
        /// </summary>
        public async Task ChangeOpenState(bool newOpenState, Trade trade)
        {
            if (newOpenState == false && !trade.IsClosable())
            {
                return;
            }

            using (var dbContext = _contextFactory.Get())
            {
                trade.Open = newOpenState;
                await TradesRepository.UpdateStats(trade);

                await TradesRepository.UpdateTrade(trade);
            }
        }
Exemple #5
0
        private void TradesGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            if (_tradesGridIsCellEditEnding)
            {
                return;
            }

            if ((string)e.Column.Header == "Tags")
            {
                var trade = (Trade)TradesGrid.SelectedItem;

                if (e.EditAction == DataGridEditAction.Commit)
                {
                    //save the new tag configuration
                    foreach (CheckListItem <Tag> item in TagPickerPopupListBox.Items)
                    {
                        if (item.IsChecked && !trade.Tags.Contains(item.Item))
                        {
                            trade.Tags.Add(item.Item);
                        }
                        else if (!item.IsChecked && trade.Tags.Contains(item.Item))
                        {
                            trade.Tags.Remove(item.Item);
                        }
                    }

                    Context.SaveChanges();
                    trade.TagStringUpdated();
                }

                TagPickerPopup.IsOpen = false;
            }
            else if ((string)e.Column.Header == "Open")
            {
                var trade = (Trade)TradesGrid.SelectedItem;

                bool originalOpen = (bool)Context.Entry(trade).OriginalValues["Open"];
                bool?newOpen      = ((CheckBox)e.EditingElement).IsChecked;

                if (newOpen.HasValue && newOpen.Value != originalOpen)
                {
                    //The user has opened or closed the trade,
                    //so we do a stats update to make sure the numbers are right
                    //and set the proper closing time

                    //first load up the collections, needed for the IsClosable() check.
                    Context.Entry(trade).Collection(x => x.Orders).Load();
                    Context.Entry(trade).Collection(x => x.CashTransactions).Load();
                    Context.Entry(trade).Collection(x => x.FXTransactions).Load();

                    //if we're closing the trade, make sure it's closable first
                    if (newOpen.Value == false && !trade.IsClosable())
                    {
                        e.Cancel = true;
                        _tradesGridIsCellEditEnding = true;
                        ((DataGrid)sender).CancelEdit(DataGridEditingUnit.Cell);
                        _tradesGridIsCellEditEnding = false;
                        return;
                    }

                    trade.Open = newOpen.Value;
                    Task.Run(() =>
                    {
                        TradesRepository.UpdateStats(trade, skipCollectionLoad: true);     //we can skip collection load since it's done a few lines up
                        Context.SaveChanges();
                    });
                }
            }
        }