Exemple #1
0
        public override bool IsSatisfied(int index, ITradingRecord tradingRecord)
        {
            bool satisfied = false;

            // No trading history or no trade opened, no gain
            if (tradingRecord != null)
            {
                Trade currentTrade = tradingRecord.GetCurrentTrade();
                if (currentTrade.IsOpened())
                {
                    decimal entryPrice   = currentTrade.GetEntry().getPrice();
                    decimal currentPrice = _closePrice.GetValue(index);
                    decimal threshold    = entryPrice.MultipliedBy(_gainRatioThreshold);
                    if (currentTrade.GetEntry().isBuy())
                    {
                        satisfied = currentPrice.IsGreaterThanOrEqual(threshold);
                    }
                    else
                    {
                        satisfied = currentPrice.IsLessThanOrEqual(threshold);
                    }
                }
            }
            traceIsSatisfied(index, satisfied);
            return(satisfied);
        }
Exemple #2
0
        public override decimal Calculate(ITimeSeries series, ITradingRecord tradingRecord)
        {
            decimal totalCosts   = 0M;
            decimal tradedAmount = _initialAmount;

            foreach (Trade trade in tradingRecord.Trades)
            {
                decimal tradeCost = getTradeCost(series, trade, tradedAmount);
                totalCosts += tradeCost;
                // To Calculate the new traded amount:
                //    - Remove the cost of the *first* order
                //    - Multiply by the profit ratio
                //    - Remove the cost of the *second* order
                tradedAmount  = (tradedAmount - getOrderCost(trade.GetEntry(), tradedAmount));
                tradedAmount *= _profit.Calculate(series, trade);
                tradedAmount -= getOrderCost(trade.GetExit(), tradedAmount);
            }

            // Special case: if the current trade is open
            Trade currentTrade = tradingRecord.GetCurrentTrade();

            if (currentTrade.IsOpened())
            {
                totalCosts += getOrderCost(currentTrade.GetEntry(), tradedAmount);
            }

            return(totalCosts);
        }
Exemple #3
0
        /**
         * @param index the bar index
         * @param tradingRecord the potentially needed trading history
         * @return true to recommend an order, false otherwise (no recommendation)
         */
        public bool ShouldOperate(int index, ITradingRecord tradingRecord)
        {
            Trade trade = tradingRecord.GetCurrentTrade();

            if (trade.IsNew())
            {
                return(ShouldEnter(index, tradingRecord));
            }
            else if (trade.IsOpened())
            {
                return(ShouldExit(index, tradingRecord));
            }
            return(false);
        }
 public void GetCurrentTrade()
 {
     Assert.IsTrue(emptyRecord.GetCurrentTrade().IsNew());
     Assert.IsTrue(openedRecord.GetCurrentTrade().IsOpened());
     Assert.IsTrue(closedRecord.GetCurrentTrade().IsNew());
 }