Ejemplo n.º 1
0
        /// <summary>
        /// Static helper.
        /// </summary>
        public static decimal?GetResult(ResultModeEnum mode, decimal?open, decimal?close, decimal volume, Symbol orderSymbol,
                                        OrderStateEnum state, OrderTypeEnum type, CurrencyConversionManager convertor, Symbol accountCurrency,
                                        decimal lotSize, int decimalPlaces, decimal?ask, decimal?bid)
        {
            if (/*ask.HasValue == false || bid.HasValue == false */
                string.IsNullOrEmpty(orderSymbol.Name))
            {
                return(null);
            }

            Decimal?currentRawResult = null;

            if (state == OrderStateEnum.Executed)
            {
                // Update result.
                currentRawResult = GetRawResult(open, volume, state, type, ask, bid, null, mode != ResultModeEnum.Pips);
            }
            else if (state == OrderStateEnum.Closed)
            {
                currentRawResult = GetRawResult(open, volume, state, type, null, null, close.Value, mode != ResultModeEnum.Pips);
            }

            if (currentRawResult.HasValue == false)
            {
                return(null);
            }

            if (mode == ResultModeEnum.Pips)
            {
                //if (state == OrderStateEnum.Closed)
                //{// When closed we need to compensate the
                //    if (OrderInfo.TypeIsBuy(type))
                //    {
                //        return (close - open) * (decimal)Math.Pow(10, decimalPlaces);
                //    }
                //    else
                //    {
                //        return (open - close) * (decimal)Math.Pow(10, decimalPlaces);
                //    }
                //}
                //else
                //{
                return(currentRawResult * (decimal)Math.Pow(10, decimalPlaces));
                //}
            }
            else if (mode == ResultModeEnum.Raw)
            {
                return(currentRawResult);
            }
            else if (mode == ResultModeEnum.Currency)
            {
                return(currentRawResult);
            }
            else if (mode == ResultModeEnum.AccountBaseCurrency)
            {
                if (string.IsNullOrEmpty(accountCurrency.Name) || convertor == null)
                {
                    SystemMonitor.Warning("Mode requires the Account Currency and Convertion Manager to be specified.");
                    return(null);
                }

                if (orderSymbol.IsForexPair)
                {// We have a forex pair and need to rebase to account base currency.
                    double?conversionRate = convertor.GetRate(orderSymbol.ForexCurrency2, accountCurrency.Name, TimeSpan.FromSeconds(1.5), true);
                    if (conversionRate.HasValue == false)
                    {
                        SystemMonitor.OperationError("Failed to establish conversion rate between [" + orderSymbol.ForexCurrency2 + "] and [" + accountCurrency.Name + "].");
                        return(null);
                    }
                }
                else
                {// All other symbols are by default in account base currency prices.
                    return(currentRawResult.Value);
                }
            }

            SystemMonitor.NotImplementedCritical("Mode not supported.");
            return(0);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Static helper.
        /// </summary>
        public static decimal? GetResult(ResultModeEnum mode, decimal? open, decimal? close, decimal volume, Symbol orderSymbol,
            OrderStateEnum state, OrderTypeEnum type, CurrencyConversionManager convertor, Symbol accountCurrency, 
            decimal lotSize, int decimalPlaces, decimal? ask, decimal? bid)
        {
            if (/*ask.HasValue == false || bid.HasValue == false */
                string.IsNullOrEmpty(orderSymbol.Name))
            {
                return null;
            }

            Decimal? currentRawResult = null;
            if (state == OrderStateEnum.Executed)
            {
                // Update result.
                currentRawResult = GetRawResult(open, volume, state, type, ask, bid, null, mode != ResultModeEnum.Pips);
            }
            else if (state == OrderStateEnum.Closed)
            {
                currentRawResult = GetRawResult(open, volume, state, type, null, null, close.Value, mode != ResultModeEnum.Pips);
            }

            if (currentRawResult.HasValue == false)
            {
                return null;
            }

            if (mode == ResultModeEnum.Pips)
            {
                //if (state == OrderStateEnum.Closed)
                //{// When closed we need to compensate the
                //    if (OrderInfo.TypeIsBuy(type))
                //    {
                //        return (close - open) * (decimal)Math.Pow(10, decimalPlaces);
                //    }
                //    else
                //    {
                //        return (open - close) * (decimal)Math.Pow(10, decimalPlaces);
                //    }
                //}
                //else
                //{
                    return currentRawResult * (decimal)Math.Pow(10, decimalPlaces);
                //}
            }
            else if (mode == ResultModeEnum.Raw)
            {
                return currentRawResult;
            }
            else if (mode == ResultModeEnum.Currency)
            {
                return currentRawResult;
            }
            else if (mode == ResultModeEnum.AccountBaseCurrency)
            {
                if (string.IsNullOrEmpty(accountCurrency.Name) || convertor == null)
                {
                    SystemMonitor.Warning("Mode requires the Account Currency and Convertion Manager to be specified.");
                    return null;
                }

                if (orderSymbol.IsForexPair)
                {// We have a forex pair and need to rebase to account base currency.
                    double? conversionRate = convertor.GetRate(orderSymbol.ForexCurrency2, accountCurrency.Name, TimeSpan.FromSeconds(1.5), true);
                    if (conversionRate.HasValue == false)
                    {
                        SystemMonitor.OperationError("Failed to establish conversion rate between [" + orderSymbol.ForexCurrency2 + "] and [" + accountCurrency.Name + "].");
                        return null;
                    }
                }
                else
                {// All other symbols are by default in account base currency prices.
                    return currentRawResult.Value;
                }
            }

            SystemMonitor.NotImplementedCritical("Mode not supported.");
            return 0;
        }