Exemple #1
0
        public void Write(T data, TickTime time)
        {
            Cleanup();

            Buffer.Add(time.Copy(), data, true);
            _TickerWrite.SetNow();
        }
Exemple #2
0
        /// <summary>
        /// Creates a longrun task, and invokes action within
        /// </summary>
        /// <param name="method"></param>
        /// <param name="ID"></param>
        /// <param name="wait"></param>
        /// <returns></returns>
        public bool Run(Expression <Action> method, string ID, bool wait)
        {
            if (method == null)
            {
                return(false);
            }

            if (ID.IsNullOrEmpty())
            {
                ID = Expressions.nameofFull(method);
            }



            if (ID.IsNullOrEmpty())
            {
                return(false);
            }

            var task = Data.TryGetValue(ID, null);

            if (task != null && task.IsRunning)
            {
                if (wait)
                {
                    task.Wait();
                }
                else
                {
                    return(false);
                }
            }


            this.Cleanup();

            if (this.IsFull)
            {
                return(false);
            }

            lock (locker)
            {
                var task_new = Data.TryGetValue(ID, null);
                if (task_new != null && task_new.IsRunning)
                {
                    return(false);
                }

                Action     action     = method.Compile();
                TaskObject taskObject = new TaskObject(action);

                Data.Add(ID, taskObject, true);

                Data[ID].Run();
                return(true);
            }
        }
        public void Write(T data, TickTime time)
        {
            if (Size > 0 && Buffer.Count > Size)
            {
                this.Cleanup();
            }

            Buffer.Add(time.Copy(), data);
            _TickerWrite.SetNow();
        }
        public void UpdateAccount()
        {
            if (!ForexAuthentication.Connected || !ForexRates.Loaded || (DataAccount.IsValid && DataAccount.UpdateSpan < 1000))
            {
                return;
            }

            Margin marg = null;

            try
            {
                marg = CEDTS_TradingService.GetMarginBlotter(ForexAuthentication.Token).Output[0];
            }
            catch (Exception e)
            {
                Exceptions.Add(e);
                return;
            }

            Account current = AccountInfo.ToAccount(marg);

            current.MarginOrigin = TickTime.Now;

            if (DataAccount.Count <= 0)
            {
                if (current.OpenPosition == 0)
                {
                    current.ClosedBalance = current.MarginBalance;
                    current.OriginBalance = current.MarginBalance;
                }
                else
                {
                    throw new Exception("Archive log must contain at least one closed record !");
                }
            }
            else
            {
                current.ClosedBalance = this.GetClosedMarginBalance();
                current.OriginBalance = this.GetOriginMarginBalance();
            }

            if (DataAccount.Count <= 0 || (current.OpenPosition == 0 && !AccountInfo.EqualsPosition(current, AccountLog)))
            {
                lock (DataAccount)
                {
                    DataAccount.IsValid = false;
                    DataAccount.Add(current.MarginOrigin, current); //only closed position should be saved
                    DataAccount.SaveFile(false);
                }
            }

            current.LiveProfit  = this.GetLiveProfit();
            Account             = current; //Account.LiveOrigin = TickTime.Now;
            DataAccount.IsValid = true;
        }
Exemple #5
0
        public static double Ln(double x)
        {
            double var;

            if (ThreadedNaturalLogarithm.ContainsKey(x))
            {
                var = ThreadedNaturalLogarithm[x];
            }
            else
            {
                var = Math.Log(x);
                ThreadedNaturalLogarithm.Add(x, var);
            }

            return(var);
        }
Exemple #6
0
        public void Save(DealRequest DRequest)
        {
            lock (DataDealRequest)
            {
                if (DataDealRequest.ContainsKey(DRequest.CreationTime))
                {
                    DataDealRequest[DRequest.CreationTime] = DRequest;
                }
                else
                {
                    DataDealRequest.Add(DRequest.CreationTime, DRequest);
                }
            }

            this.SaveDataRequest(DRequest.CreationTime.ToString());
        }
Exemple #7
0
        public void TimrBalance()
        {
            if (!TimeoutBalance.IsTriggered)
            {
                return;
            }

            TimeoutBalance.Forced = true;

            Balance[] balances = this.GetBalance();

            if (balances == null)
            {
                return;
            }

            //set balance of missing assets to 0
            foreach (string key in Balances.Keys)
            {
                if (balances.Length <= 0)
                {
                    Balances[key].BalanceAmount = 0;
                    continue;
                }

                if (!balances.Any(balance => balance.AssetName == key))
                {
                    Balances[key].BalanceAmount = 0;
                    continue;
                }
            }

            //set current assets balances
            foreach (Balance balance in balances)
            {
                if (Balances.ContainsKey(balance.AssetName))
                {
                    Balances.Add(balance.AssetName, balance);
                }
                else
                {
                    Balances[balance.AssetName] = balance;
                }
            }

            TimeoutBalance.Reset();
        }
        //public Settings Settings { get; private set; }

        public void GetDeals()
        {
            //Wait at least one minute to check
            if (!ForexAuthentication.Connected || (DataDeals.IsValid && DataDeals.UpdateSpan < 10 * 1000))
            {
                return;
            }

            Deal[] ADeals;
            //CEDTS_TradingService.get
            try
            {
                ADeals = CEDTS_TradingService.GetOpenDealBlotter(ForexAuthentication.Token).Output;
            }
            catch (Exception e)
            {
                Exceptions.Add(e);
                return;
            }

            DataDeals.IsValid = false;

            lock (DataDeals)
            {
                DataDeals.Clear();

                foreach (Deal deal in ADeals)
                {
                    if (deal != null)
                    {
                        DataDeals.Add(deal.ConfirmationNumber, deal);
                    }
                }
            }

            DataDeals.IsValid = true;

            if (DataDeals.Count == 0)
            {
                DataDeals.IsValid = true;
                return;
            }
        }
Exemple #9
0
        public void MonteCarlo(double[] data, string key, int span, int index)
        {
            double[] daSubData = data;

            if (index != data.Length - 1)
            {
                daSubData = data.Take(index + 1).ToArray();
            }


            //if (!MonteCarloData.ContainsKey(key))
            MonteCarloData.Add(key, new ThreadedList <double[]>());

            try
            {
                for (int i = 0; i < 1000000; i++)
                {
                    MonteCarloData[key].Add(MonteCarloSimulation.Next(daSubData, span).ToArray());


                    while (Performance.CPU > 50)
                    {
                        Thread.Sleep(1);
                        if (StopSimulations)
                        {
                            return;
                        }
                    }

                    //while (Performance.CPU > 50 && Performance.CPU < 100)
                    //    if (!this.Sleep(Indexes.Length)) return;

                    if (StopSimulations)
                    {
                        return;
                    }
                    //
                }
            }
            catch { }
        }