public static void Init(this FinancialCalculator calculator, DataFeed dataFeed)
        {
            if (calculator == null)
            {
                throw new ArgumentNullException("calculator");
            }

            if (dataFeed == null)
            {
                throw new ArgumentNullException("dataFeed");
            }


            var symbols = new SymbolInfo[0];

            try
            {
                calculator.Currencies.Clear();
                calculator.Symbols.Clear();

                symbols = dataFeed.Server.GetSymbols();

                dataFeed.Server.SubscribeToQuotes(symbols.Select(s => s.Name), 1);
                InitializeCalculator(calculator, dataFeed, symbols);
            }
            finally
            {
                dataFeed.Server.UnsubscribeQuotes(symbols.Select(symbol => symbol.Name));
            }
        }
        /// <summary>
        ///  Converts volume in currency X to currency Z (margin currency to deposit currency)
        /// </summary>
        /// <param name="calculator">Financial calculator.</param>
        /// <param name="amount">Volume.</param>
        /// <param name="symbol">Symbol X/Y.</param>
        /// <param name="depositCurrency">Deposit currency.</param>
        /// <returns>Rate or null if rate cannot be calculated.</returns>
        public static double?ConvertXToZ(this FinancialCalculator calculator, double amount, string symbol, string depositCurrency)
        {
            if (calculator == null)
            {
                throw new ArgumentNullException(nameof(calculator));
            }
            if (symbol == null)
            {
                throw new ArgumentNullException(nameof(symbol));
            }
            if (depositCurrency == null)
            {
                throw new ArgumentNullException(nameof(depositCurrency));
            }

            if (calculator.MarketState == null)
            {
                calculator.Calculate();
            }

            try
            {
                double rate = (double)calculator.MarketState.ConversionMap.GetMarginConversion(symbol, depositCurrency).Value;

                return(rate * amount);
            }
            catch (BusinessLogicException)
            {
                return(null);
            }
        }
Example #3
0
 public NewSymbolDialog(ListBox symbols, FinancialCalculator calcualtor)
 {
     this.symbols    = symbols;
     this.calculator = calcualtor;
     this.InitializeComponent();
     this.UpdatePreview();
 }
Example #4
0
        internal FinancialCalculator CreateCalculator()
        {
            var result = new FinancialCalculator();

            //{
            //    MarginMode = this.MarginMode
            //};

            foreach (var element in this.Prices)
            {
                result.Prices.Update(element.Symbol, element.Bid, element.Ask);
            }

            foreach (var element in this.Symbols)
            {
                var entry = element.CreateEntry(result);
                result.Symbols.Add(entry);
            }

            foreach (var element in this.Accounts)
            {
                var entry = element.CreateEntry(result);
                result.Accounts.Add(entry);
            }

            foreach (var element in this.Currencies)
            {
                var entry = new CurrencyEntry(result, element, 2, 0);
                result.Currencies.Add(entry);
            }

            return(result);
        }
Example #5
0
        internal FinancialCalculator CreateCalculator()
        {
            var result = new FinancialCalculator();
            //{
            //    MarginMode = this.MarginMode
            //};

            foreach (var element in this.Prices)
            {
                result.Prices.Update(element.Symbol, element.Bid, element.Ask);
            }

            foreach (var element in this.Symbols)
            {
                var entry = element.CreateEntry(result);
                result.Symbols.Add(entry);
            }

            foreach (var element in this.Accounts)
            {
                var entry = element.CreateEntry(result);
                result.Accounts.Add(entry);
            }

            foreach (var element in this.Currencies)
            {
                result.Currencies.Add(element);
            }

            return result;
        }
Example #6
0
 public NewCurrencyDialog(ListBox currencies, FinancialCalculator calcualtor)
 {
     this.currencies = currencies;
     this.calculator = calcualtor;
     this.InitializeComponent();
     this.UpdateState();
 }
Example #7
0
 public static void RegisterToFeed(DataFeed feed, FinancialCalculator calculator)
 {
     feed.Tick += (object sender, TickEventArgs e) =>
     {
         calculator.Prices.Update(e.Tick.Symbol, e.Tick.Bid, e.Tick.Ask);
     };
     feed.Server.SubscribeToQuotes(Symbols.Select(symbol => symbol.Name), 1);
 }
Example #8
0
        public static int ConnectToFdk(string address, string login, string password, string path)
        {
            Calculator = null;
            Console.WriteLine("Connecting ... ");
            var result = FdkHelper.ConnectToFdk(address, login, password, path);

            Console.WriteLine("Done");
            return(result);
        }
Example #9
0
        public static double CalculatePriceBid(SymbolInfo symbol)
        {
            FinancialCalculator financialCalculator = FdkStatic.Calculator;
            double?rateK = financialCalculator.CalculateAssetRate(1, symbol.SettlementCurrency, "USD");

            if (!rateK.HasValue)
            {
                return(double.NaN);
            }
            return(rateK.Value);
        }
Example #10
0
        public static double CalculatePriceAsk(SymbolInfo symbol)
        {
            FinancialCalculator financialCalculator = FdkStatic.Calculator;
            PriceEntry?         priceEntry          = financialCalculator.Prices.TryGetPriceEntry(symbol.Name);

            if (!priceEntry.HasValue)
            {
                return(double.NaN);
            }
            return(priceEntry.Value.Ask);
        }
Example #11
0
    public static void Main()
    {
        decimal[] prices = { 1m, 2m, 3m };

        Type                calcType  = typeof(FinancialCalculator);
        MethodInfo          sumMethod = calcType.GetMethod("Sum");
        FinancialCalculator calc      = (FinancialCalculator)Activator.CreateInstance(calcType);
        decimal             sum       = (decimal)sumMethod.Invoke(calc, new object[] { prices });

        Console.WriteLine($"Sum: {sum}\nPress any key to continue.");
        Console.ReadKey();
    }
Example #12
0
 static string FormatTextFromCalculator(FinancialCalculator calc)
 {
     using (var stream = new MemoryStream())
     {
         calc.Save(stream);
         stream.Position = 0;
         using (var reader = new StreamReader(stream))
         {
             var result = reader.ReadToEnd();
             return(result);
         }
     }
 }
Example #13
0
        internal SymbolEntry CreateEntry(FinancialCalculator owner)
        {
            var result = new SymbolEntry(owner, this.Symbol, this.From, this.To)
            {
                Tag = this.Tag,
                ContractSize = this.ContractSize,
                Hedging = this.Hedging,
                MarginFactorOfPositions = this.MarginFactorOfPositions,
                MarginFactorOfLimitOrders = this.MarginFactorOfLimitOrders,
                MarginFactorOfStopOrders = this.MarginFactorOfStopOrders
            };

            return result;
        }
Example #14
0
        static FinancialCalculator CreateCalculatorFromText(string text)
        {
            using (var stream = new MemoryStream(text.Length))
            {
                using (var writer = new StreamWriter(stream))
                {
                    writer.Write(text);
                    writer.Flush();
                    stream.Position = 0;

                    var result = FinancialCalculator.Load(stream);
                    return(result);
                }
            }
        }
Example #15
0
        public static double CalculatePipsValue(SymbolInfo symbol)
        {
            FinancialCalculator financialCalculator = FdkStatic.Calculator;
            int    decimals = symbol.Precision;
            double?amountZ  = financialCalculator.ConvertYToZ(Math.Pow(10, -decimals) * symbol.RoundLot, symbol.Name, "USD");

            if (!amountZ.HasValue)
            {
                Log.WarnFormat("No rate for currency pair: {0}/USD", symbol.Name);
                return(double.NaN);
                //   throw new InvalidOperationException(
                //     string.Format("No rate for currency pair: {0}/USD", symbol.Name));
            }
            return(amountZ.Value);
        }
Example #16
0
        public static double CalculatePipsValue(SymbolInfo symbol)
        {
            FinancialCalculator financialCalculator = FdkStatic.Calculator;
            int    decimals     = symbol.Precision;
            double contractSize = symbol.ContractMultiplier;
            double?rateK        = financialCalculator.CalculateAssetRate(1, symbol.SettlementCurrency, "USD");

            if (!rateK.HasValue)
            {
                throw new InvalidOperationException(
                          string.Format("No rate for currency pair: {0}/USD", symbol.SettlementCurrency));
            }
            double formula = Math.Pow(10, -decimals) * contractSize * rateK.Value;

            return(formula);
        }
Example #17
0
        internal SymbolEntry CreateEntry(FinancialCalculator owner)
        {
            var result = new SymbolEntry(owner, this.Symbol, this.From, this.To)
            {
                Tag                             = this.Tag,
                ContractSize                    = this.ContractSize,
                Hedging                         = this.Hedging,
                MarginFactorOfPositions         = this.MarginFactorOfPositions,
                MarginFactorOfLimitOrders       = this.MarginFactorOfLimitOrders,
                MarginFactorOfStopOrders        = this.MarginFactorOfStopOrders,
                StopOrderMarginReduction        = this.StopOrderMarginReduction,
                HiddenLimitOrderMarginReduction = this.HiddenLimitOrderMarginReduction
            };

            return(result);
        }
Example #18
0
        internal CalculatorData(FinancialCalculator calculator)
            : this()
        {
            if (calculator == null)
                throw new ArgumentNullException(nameof(calculator));

            //this.MarginMode = calculator.MarginMode;

            this.Prices.AddRange(calculator.Prices.Select(o => new PriceData(o)));

            this.Symbols.AddRange(calculator.Symbols.Select(o => new SymbolData(o)));

            this.Accounts.AddRange(calculator.Accounts.Select(o => new AccountData(o)));

            this.Currencies.AddRange(calculator.Currencies);
        }
        private static void InitializeCalculator(FinancialCalculator calculator, DataFeed dataFeed, SymbolInfo[] symbols)
        {
            var dtUtcNow          = DateTime.UtcNow;
            var currenciesHashSet = new HashSet <string>();

            symbols.ToList().ForEach(s =>
            {
                var symbolEntry = new SymbolEntry(calculator, s.Name, s.SettlementCurrency, s.Currency)
                {
                    ContractSize = s.RoundLot,
                    MarginFactor = s.MarginFactor,
                    Hedging      = s.MarginHedge
                };
                calculator.Symbols.Add(symbolEntry);

                if (currenciesHashSet.Add(s.Currency))
                {
                    calculator.Currencies.Add(s.Currency);
                }

                if (currenciesHashSet.Add(s.SettlementCurrency))
                {
                    calculator.Currencies.Add(s.SettlementCurrency);
                }
                try
                {
                    double priceBid = 0;
                    double priceAsk = 0;
                    TryGetBidAsk(dataFeed, s.Name, ref priceBid, ref priceAsk);

                    try
                    {
                        calculator.Prices.Update(s.Name, priceBid, priceAsk);
                    }
                    catch (Exception ex)
                    {
                        Log.Error(string.Format("Failed to update calculator for symbol: {0} exception: {1}", s.Name, ex));
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(string.Format("Failed to get bid/ask for symbol {0}", s.Name));
                }
            });

            currenciesHashSet.Clear();
        }
Example #20
0
        internal CalculatorData(FinancialCalculator calculator)
            : this()
        {
            if (calculator == null)
            {
                throw new ArgumentNullException(nameof(calculator));
            }

            //this.MarginMode = calculator.MarginMode;

            this.Prices.AddRange(calculator.Prices.Select(o => new PriceData(o)));

            this.Symbols.AddRange(calculator.Symbols.Select(o => new SymbolData(o)));

            this.Accounts.AddRange(calculator.Accounts.Select(o => new AccountData(o)));

            this.Currencies.AddRange(calculator.Currencies.Select(o => o.Name));
        }
Example #21
0
        void OnOpen(object sender, EventArgs e)
        {
            var result = this.m_openFileDialog.ShowDialog();

            if (result != DialogResult.OK)
            {
                return;
            }

            var path = this.m_openFileDialog.FileName;

            try
            {
                this.calculator = FinancialCalculator.Load(path);
                this.RefreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #22
0
        public SetRatesOfCurrentTime(List <SymbolInfo> symbolInfoDic, FinancialCalculator calculator)
        {
            _symbolInfoDic = symbolInfoDic;
            _calculator    = calculator;
            _currencies    = new HashSet <string>();
            Symbols        = new Dictionary <string, SymbolInfo>();
            foreach (var sym in symbolInfoDic)
            {
                _currencies.Add(sym.Currency);
                _currencies.Add(sym.SettlementCurrency);
                Symbols[sym.Name] = sym;
            }

            foreach (var curr in _calculator.Currencies)
            {
                _currencies.Remove(curr);
            }

            foreach (var sym in _calculator.Symbols)
            {
                Symbols.Remove(sym.Symbol);
            }
        }
Example #23
0
        internal AccountEntry CreateEntry(FinancialCalculator owner)
        {
            var result = new AccountEntry(owner)
            {
                Tag = this.Tag,
                Type = this.Type,
                Leverage = this.Leverage,
                Balance = this.Balance,
                Currency = this.Currency,
                Profit = this.Profit,
                ProfitStatus = this.ProfitStatus,
                Margin = this.Margin,
                MarginStatus = this.MarginStatus
            };

            foreach (var element in this.Trades)
            {
                var entry = element.CreateEntry(result);
                result.Trades.Add(entry);
            }

            return result;
        }
Example #24
0
        internal AccountEntry CreateEntry(FinancialCalculator owner)
        {
            var result = new AccountEntry(owner)
            {
                Tag          = this.Tag,
                Type         = this.Type,
                Leverage     = this.Leverage,
                Balance      = this.Balance,
                Currency     = this.Currency,
                Profit       = this.Profit,
                ProfitStatus = this.ProfitStatus,
                Margin       = this.Margin,
                MarginStatus = this.MarginStatus
            };

            foreach (var element in this.Trades)
            {
                var entry = element.CreateEntry(result);
                result.Trades.Add(entry);
            }

            return(result);
        }
        public static double?CalculateAssetRate(this FinancialCalculator calculator, double asset, string assetCurrency, string currency)
        {
            if (calculator == null)
            {
                throw new ArgumentNullException(nameof(calculator));
            }
            if (assetCurrency == null)
            {
                throw new ArgumentNullException(nameof(assetCurrency));
            }
            if (currency == null)
            {
                throw new ArgumentNullException(nameof(currency));
            }

            if (calculator.MarketState == null)
            {
                calculator.Calculate();
            }

            try
            {
                if (asset >= 0)
                {
                    return((double)calculator.MarketState.ConversionMap.GetPositiveAssetConversion(assetCurrency, currency).Value);
                }
                else
                {
                    return((double)calculator.MarketState.ConversionMap.GetNegativeAssetConversion(assetCurrency, currency).Value);
                }
            }
            catch (BusinessLogicException)
            {
                return(null);
            }
        }
Example #26
0
 static FdkStatic()
 {
     Calculator = new FinancialCalculator();
     SetupLog4Net();
 }
Example #27
0
        internal CurrencyEntry CreateEntry(FinancialCalculator owner)
        {
            var result = new CurrencyEntry(owner, this.Name, this.Precision, this.SortOrder);

            return(result);
        }
Example #28
0
 public static void Disconnect()
 {
     Calculator = null;
     FdkHelper.Disconnect();
     FdkVars.ClearAll();
 }
Example #29
0
 public void Setup()
 {
     _financialCalculator = new FinancialCalculator();
 }