private void TimerThread()
        {
            long nextNotification = 0;

            var microStopwatch = new MicroStopwatch();

            microStopwatch.Start();

            while (!_stopTimer)
            {
                nextNotification += _timerIntervalInMicroSec;
                long elapsedMicroseconds;

                while ((elapsedMicroseconds = microStopwatch.ElapsedMicroseconds) < nextNotification)
                {
                    Thread.SpinWait(10);
                }

                var timerLateBy = elapsedMicroseconds - nextNotification;

                _handler(elapsedMicroseconds, timerLateBy);
            }

            microStopwatch.Stop();
        }
Ejemplo n.º 2
0
        protected virtual void NotificationTimer(ref long timerIntervalInMicroSec,
                                                 ref long ignoreEventIfLateBy,
                                                 ref bool stopTimer)
        {
            int  timerCount       = 0;
            long nextNotification = 0;

            MicroStopwatch microStopwatch = new MicroStopwatch();

            microStopwatch.Start();


            while (!stopTimer)
            {
                long callbackFunctionExecutionTime =
                    microStopwatch.ElapsedMicroseconds - nextNotification;

                long timerIntervalInMicroSecCurrent =
                    System.Threading.Interlocked.Read(ref timerIntervalInMicroSec);
                long ignoreEventIfLateByCurrent =
                    System.Threading.Interlocked.Read(ref ignoreEventIfLateBy);

                nextNotification += timerIntervalInMicroSecCurrent;
                timerCount++;
                long elapsedMicroseconds = 0;

                while ((elapsedMicroseconds = microStopwatch.ElapsedMicroseconds)
                       < nextNotification)
                {
                    System.Threading.Thread.SpinWait(10);
                }

                long timerLateBy = elapsedMicroseconds - nextNotification;

                if (timerLateBy >= ignoreEventIfLateByCurrent)
                {
                    continue;
                }

                MicroTimerEventArgs microTimerEventArgs =
                    new MicroTimerEventArgs(timerCount,
                                            elapsedMicroseconds,
                                            timerLateBy,
                                            callbackFunctionExecutionTime);
                MicroTimerElapsed(this, microTimerEventArgs);
            }


            microStopwatch.Stop();
        }
Ejemplo n.º 3
0
        private List <TradeSignal> Evaluate(Dictionary <Selection, IEnumerable <Bar> > marketData,
                                            IEnumerable <object> parameterItem,
                                            Selection triggerInstrument = null,
                                            IEnumerable <Tick> ticks    = null)
        {
            /* Evaluate supplied data bars using provided parameters
             * and return a collection of trades on successful evaluation
             * Hint: you can pass these bars to your IndicatorBase instance in its Calculate() method
             * and you can use current parameter values as that IndicatorBase parameters */

            var dataTickframes = marketData.Keys.Where(p => p.Timeframe == Timeframe.Tick);
            Dictionary <Selection, IEnumerable <Bar> > trigInstrData = new Dictionary <Selection, IEnumerable <Bar> >();

            #region Internal Backtest

            if (_execTradesParam.EvalCount % 10 == 0 && _internalBacktest == true)
            {
                _internalBacktest = false;

                var backtestSet = new BacktestSettings
                {
                    InitialBalance   = 10000,
                    TransactionCosts = 0,
                    Risk             = 0,
                    BarsBack         = 5,
                };

                Alert("----------------------------------");
                Alert("START Internal Backtest");
                Alert("----------------------------------");

                var res        = Backtest(false);
                var tradeCount = res?[0].Summaries?.Select(i => i.NumberOfTradeSignals).DefaultIfEmpty(0)?.Sum() ?? 0;
                _internalBacktest = true;

                Alert("Evaluate(): Internal Backtest Trades: " + tradeCount);
                Alert("----------------------------------");
                Alert("STOP Internal Backtest");
                Alert("----------------------------------");
            }

            #endregion

            #region Prepare marketdata and pass it to trading logic for processing

            if (StartMethod == StartMethod.NewBar && triggerInstrument != null)
            {
                trigInstrData.Clear();
                trigInstrData.Add(triggerInstrument, DataProvider.GetBars(triggerInstrument));
            }

            if (State == SignalState.Backtesting) // && dataTickframes.Count() > 0)
            {
                var timer = new MicroStopwatch();
                timer.Start();

                var trades = new List <TradeSignal>();
                trades = BacktestPriceSegmentation.BacktestPriceSegmentProcessor(this, marketData, _execTradesParam,
                                                                                 backtestPriceConst, Calculate, trigInstrData, ticks);

                timer.Stop();
                Alert($"Init instrumentData: ExecutionTime = {timer.ElapsedMicroseconds:#,0} µs");

                return(trades);
            }

            try
            {
                return(Calculate(marketData));
            }

            catch (Exception e)
            {
                Alert($"Evaluate(): Failed to Run on Usercode: {e.Message}");
                return(new List <TradeSignal>());
            }

            #endregion
        }
Ejemplo n.º 4
0
 public override void Dispose()
 {
     mTimer.Stop();
 }
Ejemplo n.º 5
0
        void NotificationTimer(ref long timerIntervalInMicroSec,
                               ref long ignoreEventIfLateBy,
                               ref bool stopTimer)
        {
            var timerCount = 0;
            long nextNotification = 0;

            var microStopwatch = new MicroStopwatch();
            microStopwatch.Start();

            while (!stopTimer) {
                var callbackFunctionExecutionTime =
                    microStopwatch.ElapsedMicroseconds - nextNotification;

                var timerIntervalInMicroSecCurrent =
                    Interlocked.Read(ref timerIntervalInMicroSec);
                var ignoreEventIfLateByCurrent =
                    Interlocked.Read(ref ignoreEventIfLateBy);

                nextNotification += timerIntervalInMicroSecCurrent;
                timerCount++;
                long elapsedMicroseconds;

                while ((elapsedMicroseconds = microStopwatch.ElapsedMicroseconds)
                        < nextNotification) {
                    Thread.SpinWait(10);
                }

                var timerLateBy = elapsedMicroseconds - nextNotification;

                if (timerLateBy >= ignoreEventIfLateByCurrent) {
                    continue;
                }

                MicroTimerEventArgs microTimerEventArgs =
                     new MicroTimerEventArgs(timerCount,
                                             elapsedMicroseconds,
                                             timerLateBy,
                                             callbackFunctionExecutionTime);
                MicroTimerElapsed?.Invoke(this, microTimerEventArgs);
            }

            microStopwatch.Stop();
        }