Beispiel #1
0
        /*
         * private void exitTradeStrategy(Series<DateTime, MarketDataElement> series)
         * {
         *
         *  if (!execution.isPositionOnSignalEmpty())
         *  {
         *      return;
         *  }
         *
         *  double tickClose = calculateMid();
         *  if (tickClose.Equals(double.NaN))
         *      return;
         *
         *  double enterPrice = lastExecutePrice;
         *  double idealEnterPrice = lastSignalPrice;
         *  if (execution.CurrentMarketPosition > 0)
         *  {
         *      double profitTargetPt = enterPrice + profitTarget;
         *      double cutLossTarget = enterPrice - cutLoss;
         *      String reason = "[Strategy] : Exit with the {0} trade because exceed profit target: profitTargetPt={1}; tickClose={2}; idealEnterPrice={3}; enterPrice={4}";
         *      reason = reason.Replace("{0}", "SELL").Replace("{1}", profitTargetPt.ToString()).Replace("{2}", tickClose.ToString());
         *      reason = reason.Replace("{3}", idealEnterPrice.ToString()).Replace("{4}", enterPrice.ToString());
         *      log.Info("[Strategy] : Exit Sell Generated !!! ");
         *      log.Info(reason);
         *      if (sentOrder)
         *      {
         *          stgHelper.placeLimitTrade(AppConstant.SELL_SIGNAL, "Exit Random Trade", profitTargetPt, "", orderSize, 1);
         *          stgHelper.placeStopTrade(AppConstant.SELL_SIGNAL, "Cutloss Random Trade", cutLossTarget, "", orderSize, 2);
         *      }
         *          return;
         *  }
         *
         *  if (execution.CurrentMarketPosition < 0)
         *  {
         *      double profitTargetPt = enterPrice - profitTarget;
         *      double cutLossTarget = enterPrice + cutLoss;
         *      String reason = "[Strategy] : Exit with the {0} trade because lower than profit target: profitTargetPt={1}; tickClose={2}; idealEnterPrice={3}; enterPrice={4}";
         *      reason = reason.Replace("{0}", "BUY").Replace("{1}", profitTargetPt.ToString()).Replace("{2}", tickClose.ToString());
         *      reason = reason.Replace("{3}", idealEnterPrice.ToString()).Replace("{4}", enterPrice.ToString());
         *      log.Info("[Strategy] : Exit BUY Generated !!! ");
         *      log.Info(reason);
         *      if (sentOrder)
         *      {
         *          stgHelper.placeLimitTrade(AppConstant.BUY_SIGNAL, "Exit Random Trade", profitTargetPt, "", orderSize, 1);
         *          stgHelper.placeStopTrade(AppConstant.BUY_SIGNAL, "Cutloss Random Trade", cutLossTarget, "", orderSize, 2);
         *      }
         *      return;
         *  }
         * }*/

        private void exitTradeStrategy(Series <DateTime, MarketDataElement> series)
        {
            if (!execution.isPositionOnSignalEmpty())
            {
                return;
            }

            double tickClose = calculateMid();

            if (tickClose.Equals(double.NaN))
            {
                return;
            }

            double enterPrice      = lastExecutePrice;
            double idealEnterPrice = lastSignalPrice;

            if (execution.CurrentMarketPosition > 0)
            {
                double profitTargetPt = enterPrice + profitTarget;

                if (tickClose > profitTargetPt)
                {
                    String reason = "[Strategy] : Exit with the {0} trade because exceed profit target: profitTargetPt={1}; tickClose={2}; idealEnterPrice={3}; enterPrice={4}";
                    reason = reason.Replace("{0}", "SELL").Replace("{1}", profitTargetPt.ToString()).Replace("{2}", tickClose.ToString());
                    reason = reason.Replace("{3}", idealEnterPrice.ToString()).Replace("{4}", enterPrice.ToString());
                    log.Info("[Strategy] : Exit Sell Generated !!! ");
                    log.Info(reason);
                    if (sentOrder)
                    {
                        stgHelper.placeMarketTrade(AppConstant.SELL_SIGNAL, "Exit Random Trade", tickClose, "", orderSize, 1);
                        //stgHelper.placeExitTrade(TSAppConstant.SELL_SIGNAL, "Profit Target Rule", reason, tickClose, enterPrice, orderSize);
                    }
                    return;
                }
            }
            if (execution.CurrentMarketPosition < 0)
            {
                double profitTargetPt = enterPrice - profitTarget;
                if (tickClose < profitTargetPt)
                {
                    String reason = "[Strategy] : Exit with the {0} trade because lower than profit target: profitTargetPt={1}; tickClose={2}; idealEnterPrice={3}; enterPrice={4}";
                    reason = reason.Replace("{0}", "BUY").Replace("{1}", profitTargetPt.ToString()).Replace("{2}", tickClose.ToString());
                    reason = reason.Replace("{3}", idealEnterPrice.ToString()).Replace("{4}", enterPrice.ToString());
                    log.Info("[Strategy] : Exit BUY Generated !!! ");
                    log.Info(reason);
                    if (sentOrder)
                    {
                        stgHelper.placeMarketTrade(AppConstant.BUY_SIGNAL, "Exit Random Trade", tickClose, "", orderSize, 1);
                        //stgHelper.placeExitTrade(TSAppConstant.BUY_SIGNAL, "Profit Target Rule", reason, tickClose, enterPrice, orderSize);
                    }
                    return;
                }
            }
        }
        public void test_IsPositionOnSignalEmpty_Positive()
        {
            ISignalContext context = new SignalContext();

            context.CurrentMarketPosition = 1;
            Assert.AreEqual(true, context.isPositionOnSignalEmpty());
        }
        public void test_IsPositionOnSignalEmpty_Negative()
        {
            ISignalContext context = new SignalContext();

            context.CurrentMarketPosition = 1;
            context.setPendingSignal1(AppConstant.BUY_SIGNAL, 20000, createOrder_Buy1Contract_0Complete(), "Buy Order");
            Assert.AreEqual(false, context.isPositionOnSignalEmpty());

            context = new SignalContext();
            context.CurrentMarketPosition = 0;
            Assert.AreEqual(false, context.isPositionOnSignalEmpty());

            context = new SignalContext();
            context.CurrentMarketPosition = 1;
            context.setPendingSignal1(AppConstant.BUY_SIGNAL, 20000, createOrder_Buy1Contract_0Complete(), "Buy Order");
            context.setPendingSignal2(AppConstant.SELL_SIGNAL, 22000, createOrder_Sell1Contract_0Complete(), "Sell Order");
            Assert.AreEqual(false, context.isPositionOnSignalEmpty());
        }