Esempio n. 1
0
        /// <summary>
        /// Runs the strategy over the series.
        /// <para>
        /// </para>
        /// </summary>
        /// <param name="strategy"> the trading strategy </param>
        /// <param name="orderType"> the <seealso cref="OrderType"/> used to open the trades </param>
        /// <param name="amount"> the amount used to open/close the trades </param>
        /// <returns> the trading record coming from the run </returns>
        public virtual TradingRecord Run(Strategy strategy, OrderType orderType, Decimal amount)
        {
            _logger?.LogTrace("Running strategy: {0} (starting with {1})", strategy, orderType);

            var tradingRecord = new TradingRecord(orderType);

            for (var i = _beginIndex; i <= _endIndex; i++)
            {
                // For each tick in the sub-series...
                if (strategy.ShouldOperate(i, tradingRecord))
                {
                    tradingRecord.Operate(i, _ticks[i].ClosePrice, amount);
                }
            }

            if (!tradingRecord.Closed)
            {
                // If the last trade is still opened, we search out of the end index.
                // May works if the current series is a sub-series (but not the last sub-series).
                for (var i = _endIndex + 1; i < _ticks.Count; i++)
                {
                    // For each tick out of sub-series bound...
                    // --> Trying to close the last trade
                    if (strategy.ShouldOperate(i, tradingRecord))
                    {
                        tradingRecord.Operate(i, _ticks[i].ClosePrice, amount);
                        break;
                    }
                }
            }
            return(tradingRecord);
        }
Esempio n. 2
0
        /// <param name="index"> the tick index </param>
        /// <param name="tradingRecord"> the potentially needed trading history </param>
        /// <returns> true to recommend to exit, false otherwise </returns>
        public bool ShouldExit(int index, TradingRecord tradingRecord)
        {
            if (IsUnstableAt(index))
            {
                return(false);
            }

            var exit = _exitRule.IsSatisfied(index, tradingRecord);

            TraceShouldExit(index, exit);
            return(exit);
        }
Esempio n. 3
0
        /// <param name="index"> the tick index </param>
        /// <param name="tradingRecord"> the potentially needed trading history </param>
        /// <returns> true to recommend to enter, false otherwise </returns>
        public bool ShouldEnter(int index, TradingRecord tradingRecord)
        {
            if (IsUnstableAt(index))
            {
                return(false);
            }

            var enter = _entryRule.IsSatisfied(index, tradingRecord);

            TraceShouldEnter(index, enter);
            return(enter);
        }
Esempio n. 4
0
        /// <param name="index"> the tick index </param>
        /// <param name="tradingRecord"> the potentially needed trading history </param>
        /// <returns> true to recommend an order, false otherwise (no recommendation) </returns>
        public bool ShouldOperate(int index, TradingRecord tradingRecord)
        {
            var trade = tradingRecord.CurrentTrade;

            if (trade.New)
            {
                return(ShouldEnter(index, tradingRecord));
            }
            if (trade.Opened)
            {
                return(ShouldExit(index, tradingRecord));
            }
            return(false);
        }