/// <summary>
        /// Strategy enter/exit/filtering rules
        /// </summary>
        public override void OnNewBar()
        {
            if (this.GetOpenPosition() == 0)
            {
                int buySignal = (int)this.GetInputParameter("Stochastic %D Buy signal trigger level");

                if (stochasticIndicator.GetD()[1] <= buySignal && stochasticIndicator.GetD()[0] > buySignal)
                {
                    Order buyOrder = new MarketOrder(OrderSide.Buy, 1, "Entry long");
                    limitTakeProfitOrder = new LimitOrder(OrderSide.Sell, 1, Bars.Close[0] + stdDevIndicator.GetStdDev()[0], "Exit long (take profit stop)");

                    this.InsertOrder(buyOrder);
                    this.InsertOrder(limitTakeProfitOrder);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Strategy enter/exit/filtering rules
        /// </summary>
        public override void OnNewBar()
        {
            double buySignal  = (double)this.GetInputParameter("Trend-following buy signal");
            double sellSignal = (double)this.GetInputParameter("Trend-following sell signal");

            double stopMargin   = (int)this.GetInputParameter("Trailing Stop Loss ticks distance") * this.GetMainChart().Symbol.TickSize;
            double profitMargin = (int)this.GetInputParameter("Profit Target ticks distance") * this.GetMainChart().Symbol.TickSize;

            bool longTradingEnabled  = false;
            bool shortTradingEnabled = false;

            // Day-of-week filter
            if (IsDayEnabledForTrading(this.Bars.Time[0].DayOfWeek))
            {
                // Time-of-day filter
                if (IsTimeEnabledForTrading(this.Bars.Time[0]))
                {
                    // Volatility filter
                    if (CalculateVolatilityRange() > (double)this.GetInputParameter("Minimum Range Filter"))
                    {
                        // ADX minimum level and current trending filters
                        if (this.GetOpenPosition() == 0 && IsADXEnabledForLongEntry() && IsBullishUnderlyingTrend())
                        {
                            longTradingEnabled = true;
                        }
                        else if (this.GetOpenPosition() == 0 && IsADXEnabledForShortEntry() && IsBearishUnderlyingTrend())
                        {
                            shortTradingEnabled = true;
                        }
                    }
                }
            }

            if (longTradingEnabled && stochasticIndicator.GetD()[1] <= buySignal && stochasticIndicator.GetD()[0] > buySignal)
            {
                // BUY SIGNAL: Stochastic %D crosses above "buy signal" level
                MarketOrder buyOrder = new MarketOrder(OrderSide.Buy, 1, "Enter long position");
                this.InsertOrder(buyOrder);

                trailingStopOrder = new StopOrder(OrderSide.Sell, 1, this.Bars.Close[0] - stopMargin, "Catastrophic stop long exit");
                this.InsertOrder(trailingStopOrder);

                profitOrder = new LimitOrder(OrderSide.Sell, 1, this.Bars.Close[0] + profitMargin, "Profit stop long exit");
                this.InsertOrder(profitOrder);

                // Linking Stop and Limit orders: when one is executed, the other is cancelled
                trailingStopOrder.IsChildOf = profitOrder;
                profitOrder.IsChildOf       = trailingStopOrder;

                // Setting the initial acceleration for the trailing stop and the furthest (the most extreme) close price
                acceleration  = (double)this.GetInputParameter("Trailing Stop acceleration");
                furthestClose = this.Bars.Close[0];
            }
            else if (shortTradingEnabled && stochasticIndicator.GetD()[1] >= sellSignal && stochasticIndicator.GetD()[0] < sellSignal)
            {
                // SELL SIGNAL: Stochastic %D crosses below "sell signal" level
                MarketOrder sellOrder = new MarketOrder(OrderSide.Sell, 1, "Enter short position");
                this.InsertOrder(sellOrder);

                trailingStopOrder = new StopOrder(OrderSide.Buy, 1, this.Bars.Close[0] + stopMargin, "Catastrophic stop short exit");
                this.InsertOrder(trailingStopOrder);

                profitOrder = new LimitOrder(OrderSide.Buy, 1, this.Bars.Close[0] - profitMargin, "Profit stop short exit");
                this.InsertOrder(profitOrder);

                // Linking Stop and Limit orders: when one is executed, the other is cancelled
                trailingStopOrder.IsChildOf = profitOrder;
                profitOrder.IsChildOf       = trailingStopOrder;

                // Setting the initial acceleration for the trailing stop and the furthest (the most extreme) close price
                acceleration  = (double)this.GetInputParameter("Trailing Stop acceleration");
                furthestClose = this.Bars.Close[0];
            }
            else if (this.GetOpenPosition() == 1 && this.Bars.Close[0] > furthestClose)
            {
                // We're long and the price has moved in our favour

                furthestClose = this.Bars.Close[0];

                // Increasing acceleration
                acceleration = acceleration * (furthestClose - trailingStopOrder.Price);

                // Checking if trailing the stop order would exceed the current market price
                if (trailingStopOrder.Price + acceleration < this.Bars.Close[0])
                {
                    // Setting the new price for the trailing stop
                    trailingStopOrder.Price = trailingStopOrder.Price + acceleration;
                    trailingStopOrder.Label = "Trailing stop long exit";
                    this.ModifyOrder(trailingStopOrder);
                }
                else
                {
                    // Cancelling the order and closing the position
                    this.CancelOrder(trailingStopOrder);
                    this.CancelOrder(profitOrder);

                    MarketOrder exitLongOrder = new MarketOrder(OrderSide.Sell, 1, "Exit long position");
                    this.InsertOrder(exitLongOrder);
                }
            }
            else if (this.GetOpenPosition() == -1 && this.Bars.Close[0] < furthestClose)
            {
                // We're short and the price has moved in our favour

                furthestClose = this.Bars.Close[0];

                // Increasing acceleration
                acceleration = acceleration * Math.Abs(trailingStopOrder.Price - furthestClose);

                // Checking if trailing the stop order would exceed the current market price
                if (trailingStopOrder.Price - acceleration > this.Bars.Close[0])
                {
                    // Setting the new price for the trailing stop
                    trailingStopOrder.Price = trailingStopOrder.Price - acceleration;
                    trailingStopOrder.Label = "Trailing stop short exit";
                    this.ModifyOrder(trailingStopOrder);
                }
                else
                {
                    // Cancelling the order and closing the position
                    this.CancelOrder(trailingStopOrder);
                    this.CancelOrder(profitOrder);

                    MarketOrder exitShortOrder = new MarketOrder(OrderSide.Buy, 1, "Exit short position");
                    this.InsertOrder(exitShortOrder);
                }
            }
        }