Esempio n. 1
0
        public TradeDetailsViewModel(Trade trade, Action closeWindow)
        {
            _closeWindow = closeWindow;
            DependencyContainer.ComposeParts(this);

            Trade = trade;
            Date  = Trade.StartDateTimeLocal != null
                ? Trade.StartDateTimeLocal.Value.ToString("dd/MM/yy HH:mm")
                : DateTime.Now.ToString("dd/MM/yy HH:mm");

            RefreshDetails();

            _broker        = _brokersService.GetBroker(trade.Broker);
            _brokerAccount = _brokersService.AccountsLookup[_broker];

            AddLimitCommand              = new DelegateCommand(AddLimit);
            AddStopCommand               = new DelegateCommand(AddStop);
            RemoveLimitCommand           = new DelegateCommand(RemoveLimit);
            RemoveStopCommand            = new DelegateCommand(RemoveStop);
            SetOrderDateTimePriceCommand = new DelegateCommand(SetOrderDateTimePrice);
            DoneCommand = new DelegateCommand(o => Done());
        }
        private void UpdateChartData(List <Trade> trades)
        {
            var series = new XyDataSeries <DateTime, double>();

            Task.Run(() =>
            {
                var xvalues = new List <DateTime>();
                var yvalues = new List <double>();

                var startedTrades = trades.Where(t => t.EntryDateTime != null && !t.Ignore).ToList();

                if (startedTrades.Count > 0)
                {
                    var earliest = startedTrades.OrderBy(t => t.EntryDateTime).First().EntryDateTime.Value.Date;
                    var latest   = DateTime.UtcNow;
                    var broker   = _brokerService.GetBroker("FXCM");

                    for (var date = earliest; date <= latest; date = date.AddDays(1))
                    {
                        var balance       = 10000M;
                        var currentTrades = trades.Where(t => t.EntryDateTime <= date).ToList();
                        foreach (var t in currentTrades)
                        {
                            if (date >= t.CloseDateTime)
                            {
                                balance += (decimal)t.Profit.Value;
                            }
                            else
                            {
                                var risk   = t.RiskAmount.Value;
                                var candle =
                                    _brokersCandlesService.GetLastClosedCandle(t.Market, _brokerService.GetBroker(t.Broker), Timeframe.D1, date,
                                                                               false);
                                var price = (decimal)(t.TradeDirection == TradeDirection.Long
                                    ? candle.Value.CloseBid
                                    : candle.Value.CloseAsk);


                                /*var stopDist = t.InitialStop.Value - t.EntryPrice;
                                 * var profit = (((decimal)price - t.EntryPrice.Value) / stopDist) * risk;*/
                                var profit = price * t.EntryQuantity.Value - t.EntryPrice.Value * t.EntryQuantity.Value; //TODO Add commission
                                balance   += (decimal)profit;
                            }
                        }

                        xvalues.Add(date);
                        yvalues.Add((double)balance);
                    }

                    series.Append(xvalues, yvalues);
                }

                _dispatcher.Invoke(() =>
                {
                    var renderableSeries = new FastLineRenderableSeries
                    {
                        DataSeries      = series,
                        StrokeThickness = 2
                    };

                    SeriesList.Add(renderableSeries);
                });
            });
        }
Esempio n. 3
0
        public static void UpdateRMultiple(Trade trade)
        {
            if (trade.RiskAmount != null && trade.RiskAmount.Value != 0M && trade.Profit != null)
            {
                trade.RMultiple = trade.Profit / trade.RiskAmount;
            }
            else if (trade.EntryPrice != null && trade.EntryDateTime != null && trade.ClosePrice != null && trade.StopPrices.Count > 0)
            {
                // Get stop price at entry
                DatePrice entryStop = null;
                foreach (var stop in trade.StopPrices)
                {
                    if (entryStop == null || stop.Date <= trade.EntryDateTime.Value)
                    {
                        entryStop = stop;
                    }
                    else
                    {
                        break;
                    }
                }

                if (entryStop?.Price != null)
                {
                    var oneR = Math.Abs(trade.EntryPrice.Value - entryStop.Price.Value);
                    if (trade.TradeDirection == TradeDirection.Long)
                    {
                        trade.RMultiple = oneR != 0 ? (decimal?)(trade.ClosePrice.Value - trade.EntryPrice.Value) / oneR : null;
                    }
                    else if (oneR != 0)
                    {
                        trade.RMultiple = trade.EntryPrice.Value != trade.ClosePrice.Value ? (trade.EntryPrice.Value - trade.ClosePrice.Value) / oneR : 0;
                    }
                    else
                    {
                        trade.RMultiple = null;
                    }
                }
            }
            else if (trade.EntryPrice != null && trade.EntryDateTime != null && trade.ClosePrice == null && trade.InitialStop != null && trade.CalculateOptions.HasFlag(CalculateOptions.IncludeOpenTradesInRMultipleCalculation))
            {
                var stopPrice     = trade.InitialStop.Value;
                var risk          = Math.Abs(stopPrice - trade.EntryPrice.Value);
                var currentCandle = _candlesService.GetCandles(_brokersService.GetBroker(trade.Broker), trade.Market, Timeframe.D1, false, cacheData: false).Last();
                var currentClose  = trade.TradeDirection == TradeDirection.Long
                    ? (decimal)currentCandle.CloseBid
                    : (decimal)currentCandle.CloseAsk;

                // Get stop price at entry
                DatePrice entryStop = null;
                foreach (var stop in trade.StopPrices)
                {
                    if (entryStop == null || stop.Date <= trade.EntryDateTime.Value)
                    {
                        entryStop = stop;
                    }
                    else
                    {
                        break;
                    }
                }

                if (entryStop?.Price != null)
                {
                    var oneR = Math.Abs(trade.EntryPrice.Value - entryStop.Price.Value);
                    if (trade.TradeDirection == TradeDirection.Long)
                    {
                        trade.RMultiple = (currentClose - trade.EntryPrice.Value) / oneR;
                    }
                    else
                    {
                        trade.RMultiple = (trade.EntryPrice.Value - currentClose) / oneR;
                    }
                }
            }
            else
            {
                trade.RMultiple = null;
            }
        }