Esempio n. 1
0
        protected override void OnBarUpdate()
        {
            if (BarsInProgress == 0 && CurrentBar >= this.BarsRequiredToTrade)
            {
                metBarsInProgress = true;
            }

            if (!metBarsInProgress)
            {
                return;
            }

            if (BarsInProgress == 0 && disableManager.IsValidTime(Time[0]))
            {
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    //PrintValues(Time[0], CountIf(() => stochasticsFast_14_3.D[0] > 80 && stochasticsFast_40_4.D[0] > 80 && stochasticsFast_9_3.D[0] > 80, 5) > 0);
                    //EnterShort(1, 1, ENTER_SHORT);
                    //stopLoss = MAX(High, 3)[0] + TickSize;
                    //Draw.VerticalLine(this, String.Format("vl_{0}", CurrentBar), Time[0], Brushes.White);
                }
            }
            else if (BarsInProgress == 1 && Position.MarketPosition != MarketPosition.Flat)
            {
                if (Position.MarketPosition == MarketPosition.Long && High[0] >= profitTaker)
                {
                    ExitLong(1, 1, PROFIT_TAKER, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && Low[0] <= profitTaker)
                {
                    ExitShort(1, 1, PROFIT_TAKER, ENTER_SHORT);
                }
                if (Position.MarketPosition == MarketPosition.Long && Low[0] <= stopLoss)
                {
                    ExitLong(1, 1, STOP_LOSS, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && High[0] >= stopLoss)
                {
                    ExitShort(1, 1, STOP_LOSS, ENTER_SHORT);
                }
            }
        }
        protected override void OnBarUpdate()
        {
            if (BarsInProgress == 0 && CurrentBar >= this.BarsRequiredToTrade)
            {
                metBarsInProgress = true;
            }

            if (!metBarsInProgress)
            {
                return;
            }

            if (BarsInProgress == 0 && disableManager.IsValidTime(Time[0]))
            {
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                }
            }
            else if (BarsInProgress == 1 && Position.MarketPosition != MarketPosition.Flat)
            {
                if (Position.MarketPosition == MarketPosition.Long && High[0] >= profitTaker)
                {
                    ExitLong(1, 1, PROFIT_TAKER, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && Low[0] <= profitTaker)
                {
                    ExitShort(1, 1, PROFIT_TAKER, ENTER_SHORT);
                }
                if (Position.MarketPosition == MarketPosition.Long && Low[0] <= stopLoss)
                {
                    ExitLong(1, 1, STOP_LOSS, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && High[0] >= stopLoss)
                {
                    ExitShort(1, 1, STOP_LOSS, ENTER_SHORT);
                }
            }
        }
Esempio n. 3
0
        protected override void OnBarUpdate()
        {
            if (BarsInProgress == 0 && CurrentBar >= this.BarsRequiredToTrade)
            {
                metBarsInProgress = true;
            }

            if (!metBarsInProgress)
            {
                return;
            }

            if (BarsInProgress == 0)
            {
                //Draw.ArrowDown(this, "UpTrendBreak" + unique + lineCount.ToString(), true, barsAgo, High[barsAgo] + TickSize, DownTrendColor);

                bool bullTrendStarted = autoTrend.TrendStarted[1] != 1 && autoTrend.TrendStarted[0] == 1 &&
                                        CountIf(() => autoTrend.Signal[0] == -1, this.TrendStrength * 2) == 0;

                if (bullTrendStarted && Position.MarketPosition == MarketPosition.Flat)
                {
                    currentTrend   = 1;
                    enterDirection = MarketPosition.Long;
                    enterLimit     = Close[0];
                    stopLoss       = swing.SwingLow[0];
                }
                else if (bullTrendStarted && Position.MarketPosition == MarketPosition.Short)
                {
                    //liquidate = true;
                }

                bool bullTrendBroken = currentTrend == 1 && autoTrend.Signal[1] != -1 && autoTrend.Signal[0] == -1;

                if (bullTrendBroken)
                {
                    currentTrend   = 0;
                    enterDirection = MarketPosition.Flat;
                    enterLimit     = 0;
                }

                bool bearTrendStarted = autoTrend.TrendStarted[1] != -1 && autoTrend.TrendStarted[0] == -1 &&
                                        CountIf(() => autoTrend.Signal[0] == 1, this.TrendStrength * 2) == 0;

                if (bearTrendStarted && Position.MarketPosition == MarketPosition.Flat)
                {
                    currentTrend   = -1;
                    enterDirection = MarketPosition.Short;
                    enterLimit     = Close[0];
                    stopLoss       = swing.SwingHigh[0];
                }
                else if (bearTrendStarted && Position.MarketPosition == MarketPosition.Long)
                {
                    //liquidate = true;
                }

                bool bearTrendBroken = currentTrend == -1 && autoTrend.Signal[1] != 1 && autoTrend.Signal[0] == 1;

                if (bearTrendBroken)
                {
                    currentTrend   = 0;
                    enterDirection = MarketPosition.Flat;
                    enterLimit     = 0;
                }

                if (Position.MarketPosition == MarketPosition.Long && Position.Quantity == this.LetItRideQuantity)
                {
                    stopLoss = GetStop(stopLoss, MIN(Low, TrailingBarsStop)[0] - TickSize, MarketPosition.Long);
                }
                else if (Position.MarketPosition == MarketPosition.Long && Position.Quantity == this.LetItRideQuantity)
                {
                    stopLoss = GetStop(stopLoss, MAX(High, TrailingBarsStop)[0] + TickSize, MarketPosition.Short);
                }
            }
            else if (BarsInProgress == 1 && Position.MarketPosition == MarketPosition.Flat && disableManager.IsValidTime(Time[0]))
            {
                if (enterDirection == MarketPosition.Long && enterLimit > 0 && Close[0] > enterLimit)
                {
                    enterDirection = MarketPosition.Flat;
                    enterLimit     = 0;
                    EnterLong(this.InitialQuantity, ENTER_LONG);
                    PrintValues(Time[0], String.Format("{0:N2}", autoTrend.ChangePerBar[0]), momentum[0]);
                }
                else if (enterDirection == MarketPosition.Short && enterLimit > 0 && Close[0] < enterLimit)
                {
                    enterDirection = MarketPosition.Flat;
                    enterLimit     = 0;
                    EnterShort(this.InitialQuantity, ENTER_SHORT);
                    PrintValues(Time[0], String.Format("{0:N2}", autoTrend.ChangePerBar[0]), momentum[0]);
                }
            }
            else if (BarsInProgress == 1 && Position.MarketPosition != MarketPosition.Flat)
            {
                if (liquidate && Position.MarketPosition == MarketPosition.Long)
                {
                    ExitLong(Position.Quantity, STOP_LOSS, ENTER_LONG);
                }
                else if (liquidate && Position.MarketPosition == MarketPosition.Short)
                {
                    ExitShort(Position.Quantity, STOP_LOSS, ENTER_SHORT);
                }
                else if (Position.MarketPosition == MarketPosition.Long && profitTaker > 0 && High[0] >= profitTaker)
                {
                    stopLoss    = GetStop(Position.AveragePrice, MIN(Lows[0], TrailingBarsStop)[0] - TickSize, MarketPosition.Long);
                    profitTaker = 0;
                    ExitLong(this.InitialQuantity - this.LetItRideQuantity, PROFIT_TAKER, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && profitTaker > 0 && Low[0] <= profitTaker)
                {
                    stopLoss    = GetStop(Position.AveragePrice, MAX(Highs[0], TrailingBarsStop)[0] + TickSize, MarketPosition.Short);
                    profitTaker = 0;
                    ExitShort(this.InitialQuantity - this.LetItRideQuantity, PROFIT_TAKER, ENTER_SHORT);
                }
                if (Position.MarketPosition == MarketPosition.Long && Low[0] <= stopLoss)
                {
                    ExitLong(Position.Quantity, STOP_LOSS, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && High[0] >= stopLoss)
                {
                    ExitShort(Position.Quantity, STOP_LOSS, ENTER_SHORT);
                }
            }
        }
        protected override void OnBarUpdate()
        {
            //Print(CurrentBar);
            if (BarsInProgress == 0 && CurrentBar < BarsRequiredToTrade)
            {
                return;
            }

            if (BarsInProgress == 0)
            {
                //			double fastMa = fastMaIndicator[0];
                //            double slowMa = slowMaIndicator[0];

                //            double maDiff = fastMa - slowMa;
                //			Brush trendBrush = (maDiff < MinMaDiff && maDiff > -MinMaDiff) ? NeutralBrush : fastMa < slowMa ? BearishBrush : BullishBrush;
                //            fastMaIndicator.PlotBrushes[0][0] = trendBrush;

                DateTime debugTime = new DateTime(2018, 6, 20, 9, 42, 0);
                DateTime blahTime  = Time[0];

                bool adxBullTrend = adxIndicator.DmiPlusPlot[0] > adxIndicator.DmiMinusPlot[0];
                bool adxBearTrend = adxIndicator.DmiPlusPlot[0] < adxIndicator.DmiMinusPlot[0];

                int  enterPeriodMinusOne = EnterPeriod - 2;
                bool enterSignal         = adxIndicator[0] > MinAdx;

                if (adxBullTrend)
                {
                    enterSignal = enterSignal && (CountIf(() => Close[0] > Open[0], EnterBarsLength) == EnterBarsLength);
                }
                else if (adxBearTrend)
                {
                    enterSignal = enterSignal && (CountIf(() => Close[0] < Open[0], EnterBarsLength) == EnterBarsLength);
                }


                if (Contrarian)
                {
                    enterSignal = enterSignal && enterAdxSlopeIndicator[0] < EnterSlopeThreshold;
                }
                else
                {
                    enterSignal = enterSignal && enterAdxSlopeIndicator[0] > EnterSlopeThreshold;
                }

                bool exitBarSignal = false;

                //Bullish exit
                if (this.openPosition && this.guerillaTraderBridge.ot_tradeType == (int)TradeTypes.LongFuture)
                {
                    if (trendBarAppeared)
                    {
                        //exit if red
                        exitBarSignal = Close[0] < Open[0];
                    }
                    else if ((CurrentBar - enterBarNumber) >= MinTrendLength)
                    {
                        //true if green
                        //trendBarAppeared = Close[0] > Open[0];
                        trendBarAppeared = (CountIf(() => Close[0] > Open[0], MinTrendLength) == MinTrendLength);
                    }
                }
                //bearish exit
                else if (this.openPosition && this.guerillaTraderBridge.ot_tradeType == (int)TradeTypes.ShortFuture)
                {
                    if (trendBarAppeared)
                    {
                        //exit if green
                        exitBarSignal = Close[0] > Open[0];
                    }
                    else if ((CurrentBar - enterBarNumber) >= MinTrendLength)
                    {
                        //true if red
                        //trendBarAppeared = Close[0] < Open[0];
                        trendBarAppeared = (CountIf(() => Close[0] < Open[0], MinTrendLength) == MinTrendLength);
                    }
                }

                //exitBarSignal = false;

                DateTime start = Time[0].Date.AddHours(StartHour).AddMinutes(StartMinute);
                DateTime end   = Time[0].Date.AddHours(EndHour).AddMinutes(EndMinute);
                //bool validTime = !UseBoundaryHours || (start <= Time[0]) && (end >= Time[0]);
                bool validTime = disableManager.IsValidTime(Time[0]);

                Double priceOffset = Math.Round(rangeIndicator[0] / 2, MidpointRounding.AwayFromZero) * TickSize;

                if (!this.openPosition && validTime && (enterSignal) && adxBullTrend && !targetHit)
                {
                    this.enterBarNumber = CurrentBar;
                    if (!this.Contrarian)
                    {
                        EnterLong(0, Quantity, ENTER_LONG);
                    }
                    else
                    {
                        EnterShort(0, Quantity, ENTER_SHORT);
                    }
                }
                else if (!this.openPosition && validTime && (enterSignal) && adxBearTrend && !targetHit)
                {
                    this.enterBarNumber = CurrentBar;
                    if (!this.Contrarian)
                    {
                        EnterShort(0, Quantity, ENTER_SHORT);
                    }
                    else
                    {
                        EnterLong(0, Quantity, ENTER_LONG);
                    }
                }
                else if (this.openPosition && !closing && (exitBarSignal) && this.guerillaTraderBridge.ot_tradeType == 1)
                {
                    closing = true;

                    Double exitLimitPrice = GetCurrentBid() + (TickSize * ExitLimitBuffer);
                    if (exitLimitPrice <= Close[0])
                    {
                        exitLimitPrice = Close[0] + TickSize;
                    }

                    ExitLongLimit(0, true, Quantity, exitLimitPrice, EXIT, ENTER_LONG);

                    Double exitStopPrice = GetCurrentBid() - (TickSize * ExitStopBuffer);
                    if (exitStopPrice >= Close[0])
                    {
                        exitStopPrice = Close[0] - TickSize;
                    }

                    ExitLongStopMarket(0, true, Quantity, exitStopPrice, EXIT, ENTER_LONG);
                }
                else if (this.openPosition && !closing && (exitBarSignal) && this.guerillaTraderBridge.ot_tradeType == 2)
                {
                    closing = true;

                    Double exitLimitPrice = GetCurrentAsk() - (TickSize * ExitLimitBuffer);
                    if (exitLimitPrice >= Close[0])
                    {
                        exitLimitPrice = Close[0] - TickSize;
                    }

                    ExitShortLimit(0, true, Quantity, exitLimitPrice, EXIT, ENTER_SHORT);

                    Double exitStopPrice = GetCurrentAsk() + (TickSize * ExitStopBuffer);
                    if (exitStopPrice <= Close[0])
                    {
                        exitStopPrice = Close[0] + TickSize;
                    }

                    ExitShortStopMarket(0, true, Quantity, exitStopPrice, EXIT, ENTER_SHORT);
                }
            }
            else if (BarsInProgress == 1)
            {
                if (this.openPosition && !closing && this.guerillaTraderBridge.ot_tradeType == 1)
                {
                    Double pl = ((Close[0] - this.guerillaTraderBridge.ot_entryPrice) * Instrument.MasterInstrument.PointValue) * this.guerillaTraderBridge.ot_size;
                    if (pl > this.TrailingStopMinDollars)
                    {
                        int divided = (int)(pl / TrailingStopDivisorDollars);

                        if (divided > this.maxDivided)
                        {
                            this.maxDivided = divided;
                            int    stopTicks     = (int)(TrailingStopCushionDollars / Instrument.MasterInstrument.PointValue / TickSize / this.guerillaTraderBridge.ot_size);
                            Double exitStopPrice = GetCurrentBid() - (TickSize * stopTicks);

                            ExitLongStopMarket(0, true, Quantity, exitStopPrice, EXIT, ENTER_LONG);
                        }
                    }
                }
                else if (this.openPosition && !closing && this.guerillaTraderBridge.ot_tradeType == 2)
                {
                    Double pl = ((this.guerillaTraderBridge.ot_entryPrice - Close[0]) * Instrument.MasterInstrument.PointValue) * this.guerillaTraderBridge.ot_size;
                    if (pl > this.TrailingStopMinDollars)
                    {
                        int divided = (int)(pl / TrailingStopDivisorDollars);

                        if (divided > this.maxDivided)
                        {
                            this.maxDivided = divided;
                            int    stopTicks     = (int)(TrailingStopCushionDollars / Instrument.MasterInstrument.PointValue / TickSize / this.guerillaTraderBridge.ot_size);
                            Double exitStopPrice = GetCurrentAsk() + (TickSize * ExitStopBuffer);

                            ExitShortStopMarket(0, true, Quantity, exitStopPrice, EXIT, ENTER_SHORT);
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        protected override void OnBarUpdate()
        {
            //Print(CurrentBar);
            if (CurrentBar < BarsRequiredToTrade)
            {
                return;
            }

//			double fastMa = fastMaIndicator[0];
//            double slowMa = slowMaIndicator[0];

//            double maDiff = fastMa - slowMa;
//			Brush trendBrush = (maDiff < MinMaDiff && maDiff > -MinMaDiff) ? NeutralBrush : fastMa < slowMa ? BearishBrush : BullishBrush;
//            fastMaIndicator.PlotBrushes[0][0] = trendBrush;

            bool adxCrossAbove = CrossAbove(adxIndicator, MinAdx, 1);
            bool adxCrossBelow = CrossBelow(adxIndicator, MinAdx, 1);
            bool adxBullTrend  = adxIndicator.DmiPlusPlot[0] > adxIndicator.DmiMinusPlot[0];
            bool adxBearTrend  = adxIndicator.DmiPlusPlot[0] < adxIndicator.DmiMinusPlot[0];

            int  enterPeriodMinusOne = EnterPeriod - 2;
            bool enterSignal         = EnterPeriod < 2 ? false :
                                       ((enterAdxSlopeIndicator[0] > EnterSlopeThreshold) &&
                                        (CountIf(() => adxIndicator[0] > MinAdx, enterPeriodMinusOne) == enterPeriodMinusOne));

            if (adxBullTrend)
            {
                enterSignal = enterSignal && (CountIf(() => Close[0] > Open[0], EnterBarsLength) == EnterBarsLength);
            }
            else if (adxBearTrend)
            {
                enterSignal = enterSignal && (CountIf(() => Close[0] < Open[0], EnterBarsLength) == EnterBarsLength);
            }

            bool exitSlopeSignal = ExitPeriod < 2 ? false :
                                   exitAdxSlopeIndicator[0] < ExitSlopeThreshold;

            bool exitBarSignal = false;

            if ((!this.Contrarian && this.guerillaTraderBridge.ot_tradeType == 1) || (this.Contrarian && this.guerillaTraderBridge.ot_tradeType == 2))
            {
                exitBarSignal = (CountIf(() => Close[0] < Open[0], ExitBarsLength) == ExitBarsLength);
            }
            else if ((!this.Contrarian && this.guerillaTraderBridge.ot_tradeType == 2) || (this.Contrarian && this.guerillaTraderBridge.ot_tradeType == 1))
            {
                exitBarSignal = (CountIf(() => Close[0] > Open[0], ExitBarsLength) == ExitBarsLength);
            }

            //exitBarSignal = false;

            DateTime start = Time[0].Date.AddHours(StartHour).AddMinutes(StartMinute);
            DateTime end   = Time[0].Date.AddHours(EndHour).AddMinutes(EndMinute);
            //bool validTime = !UseBoundaryHours || (start <= Time[0]) && (end >= Time[0]);
            bool validTime = disableManager.IsValidTime(Time[0]);

            Double priceOffset = Math.Round(rangeIndicator[0] / 2, MidpointRounding.AwayFromZero) * TickSize;

            if (!this.openPosition && validTime && (enterSignal) && adxBullTrend)
            {
                if (!this.Contrarian)
                {
//					if(State == State.Historical){
                    EnterLong(0, Quantity, ENTER_LONG);
//					}
//					else{
//						EnterLong(ENTER_LONG);
//					}
                }
                else
                {
                    EnterShort(0, Quantity, ENTER_SHORT);
                }

//				if(EnterViaStop){
//					EnterLongStopLimit(0, false, 1, GetCurrentAsk() + priceOffset, GetCurrentBid() + priceOffset, ENTER_LONG);
//				}
                //EnterLong();
            }
            else if (!this.openPosition && validTime && (enterSignal) && adxBearTrend)
            {
                if (!this.Contrarian)
                {
//					if(State == State.Historical){
                    EnterShort(0, Quantity, ENTER_SHORT);
//					}
//					else{
//						EnterShort(ENTER_SHORT);
//					}
                }
                else
                {
                    EnterLong(0, Quantity, ENTER_LONG);
                }

//				if(EnterViaStop){
//					EnterShortStopLimit(0, false, 1, GetCurrentBid() - priceOffset, GetCurrentAsk() - priceOffset, ENTER_SHORT);
//				}
                //EnterShort();
            }
            else if (this.openPosition && !closing && this.guerillaTraderBridge.ot_tradeType == 1)
            {
                closing = true;
                ExitLong(0, Quantity, EXIT, ENTER_LONG);
            }
            else if (this.openPosition && !closing && this.guerillaTraderBridge.ot_tradeType == 2)
            {
                closing = true;
                ExitShort(0, Quantity, EXIT, ENTER_SHORT);
            }
        }
Esempio n. 6
0
        protected override void OnBarUpdate()
        {
            DateTime debugDate = new DateTime(2017, 12, 13, 20, 45, 0);

            if (debugDate == Time[0] && BarsInProgress == 1)
            {
                int d = 0;
            }

            bool canGoLong  = Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Short;
            bool canGoShort = Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Long;

            if (BarsInProgress == 0 && Position.MarketPosition == MarketPosition.Flat)
            {
                miniBarIndex = 0;
            }
            else if (BarsInProgress == 1)
            {
                if (miniBarIndex >= 0 && miniBarIndex <= 1)
                {
                    double body      = Math.Abs(Closes[0][0] - Opens[0][0]);
                    bool   smallBody = body <= 1;

                    if (this.guerillaTickProfile.WithinThreshold[0])
                    {
                        bool enterLong  = false;
                        bool enterShort = false;
                        bool exitLong   = false;
                        bool exitShort  = false;
                        bool canEnter   = disableManager.IsValidTime(Time[0]);

                        if (miniBarIndex == 0 && this.Trade0Period)
                        {
                            enterLong = (this.TradeTails && this.fiftyHammerStick[0] > 0 && (this.greenBarStick[0] > 0 || smallBody)) ||
                                        (this.TradeBigBars && this.greenBarStick[0] > 0 && this.bigBarStick[0] > 0);
                            enterShort = (this.TradeTails && this.fiftyManStick[0] > 0 && (this.redBarStick[0] > 0 || smallBody)) ||
                                         (this.TradeBigBars && this.redBarStick[0] > 0 && this.bigBarStick[0] > 0);
                        }
                        else if (miniBarIndex == 1 && this.Trade1Period)
                        {
                            bool isGreen = Close[0] > Open[0];
                            bool isRed   = Close[0] < Open[0];

                            enterLong = isGreen &&
                                        ((this.TradeTails && this.fiftyHammerStick[0] > 0) ||
                                         (this.TradeBigBars && this.bigBarStick[0] > 0 && this.redBarStick[0] > 0)
                                        );

                            enterShort = isRed &&
                                         ((this.TradeTails && this.fiftyManStick[0] > 0) ||
                                          (this.TradeBigBars && this.bigBarStick[0] > 0 && this.greenBarStick[0] > 0)
                                         );
                        }

                        if (Position.MarketPosition == MarketPosition.Flat && canEnter)
                        {
                            if (enterLong)
                            {
                                currentStop = 0;
                                EnterLong(2, 1, ENTER_LONG);
                            }
                            else if (enterShort)
                            {
                                currentStop = 0;
                                EnterShort(2, 1, ENTER_SHORT);
                            }
                        }
                        else if (Position.MarketPosition == MarketPosition.Long)
                        {
                            if (enterShort && canEnter)
                            {
                                //EnterShort(2, 1, ENTER_SHORT);
                            }
                            else if (exitLong || enterShort)
                            {
                                //ExitLong(2, 1, EXIT, ENTER_LONG);
                            }
                        }
                        else if (Position.MarketPosition == MarketPosition.Short)
                        {
                            if (enterLong && canEnter)
                            {
                                //EnterLong(2, 1, ENTER_LONG);
                            }
                            else if (exitShort || enterLong)
                            {
                                //ExitShort(2, 1, EXIT, ENTER_SHORT);
                            }
                        }
                    }
                }

                if (miniBarIndex > -1)
                {
                    miniBarIndex += 1;
                }
            }
            else if (BarsInProgress == 2 && Position.MarketPosition != MarketPosition.Flat)
            {
                //double pl = 0;
                //double stopPrice = 0;

                //if (Position.MarketPosition == MarketPosition.Long)
                //{
                //    pl = (Close[0] - Position.AveragePrice);
                //}
                //else if (Position.MarketPosition == MarketPosition.Short)
                //{
                //    pl = Position.AveragePrice - Close[0];
                //}

                if (Position.MarketPosition == MarketPosition.Long && High[0] >= profitTaker)
                {
                    ExitLong(2, 1, PROFIT_TAKER, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && Low[0] <= profitTaker)
                {
                    ExitShort(2, 1, PROFIT_TAKER, ENTER_SHORT);
                }
                else if (Position.MarketPosition == MarketPosition.Long && Low[0] <= stopLoss)
                {
                    ExitLong(2, 1, STOP_LOSS, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && High[0] >= stopLoss)
                {
                    ExitShort(2, 1, STOP_LOSS, ENTER_SHORT);
                }
            }
        }
Esempio n. 7
0
        protected override void OnBarUpdate()
        {
            if (CurrentBar < BarsRequiredToTrade)
            {
                return;
            }

            //props
            int previousTrendLookback     = 4;
            int previousTrendBarLookback  = 4;
            int previousRequiredTrendBars = 2;
            int requiredTrendBars         = 1;
            int jumpInTrendPeriod         = 5;

            int  indecisionPeriod   = 5;
            bool previousIndecision = this.GuerillaCountIf(bullishTrendBarIndicator, x => x == 1, 2, indecisionPeriod, x => x == 0) &&
                                      this.GuerillaCountIf(bearishTrendBarIndicator, x => x == 1, 2, indecisionPeriod, x => x == 0);

            //Bearish
            List <bool> bearishEntrySetups = new List <bool>();
            bool        overbought         = this.StochasticsFilter ? this.GuerillaCountIf(stochastics, x => x >= 70, 0, 4, x => x > 0) : true;

            //Shooting star reversal
            bearishEntrySetups.Add(this.GuerillaCountIf(greenBarIndicator, x => x == 1, 2, previousTrendLookback, x => x >= previousTrendLookback - 1) &&
                                   this.GuerillaCountIf(bullishTrendBarIndicator, x => x == 1, 2, previousTrendBarLookback, x => x >= previousRequiredTrendBars) &&
                                   shootingStarIndicator[1] == 1 &&
                                   bearishTrendBarIndicator[0] == 1 &&
                                   overbought);

            if (bearishEntrySetups.Last())
            {
                initialStopLoss = High[1] + TickSize;
            }

            //Doji entries
            if (!bearishEntrySetups.Any(x => x))
            {
                bearishEntrySetups.Add(this.GuerillaCountIf(dojiIndicator, x => x == 1, 2, 2, x => x >= 1) &&
                                       this.GuerillaCountIf(bearishTrendBarIndicator, x => x == 1, 0, 2, x => x >= 1) &&
                                       overbought);

                if (bearishEntrySetups.Last())
                {
                    int dojiIndex = dojiIndicator[2] == 1 ? 2 : 3;
                    initialStopLoss = High[dojiIndex] + TickSize;
                }
            }
            else
            {
                bearishEntrySetups.Add(false);
            }

            if (!bearishEntrySetups.Any(x => x))
            {
                bearishEntrySetups.Add(dojiIndicator[1] == 1 &&
                                       bearishTrendBarIndicator[0] == 1 &&
                                       overbought);

                if (bearishEntrySetups.Last())
                {
                    initialStopLoss = High[1] + TickSize;
                }
            }
            else
            {
                bearishEntrySetups.Add(false);
            }

            //Trend bar reversal
            if (!bearishEntrySetups.Any(x => x))
            {
                bearishEntrySetups.Add(this.GuerillaCountIf(greenBarIndicator, x => x == 1, 2, previousTrendLookback, x => x >= previousTrendLookback - 1) &&
                                       this.GuerillaCountIf(bullishTrendBarIndicator, x => x == 1, 2, previousTrendBarLookback, x => x >= previousRequiredTrendBars) &&
                                       this.GuerillaCountIf(bearishTrendBarIndicator, x => x == 1, 0, 2, x => x == requiredTrendBars) &&
                                       overbought);

                if (bearishEntrySetups.Last())
                {
                    initialStopLoss = High[2] + TickSize;
                }
            }
            else
            {
                bearishEntrySetups.Add(false);
            }

            //Indecision breakout
            if (!bearishEntrySetups.Any(x => x) && !this.Conservative)
            {
                bearishEntrySetups.Add(previousIndecision &&
                                       this.GuerillaCountIf(bearishTrendBarIndicator, x => x == 1, 0, 2, x => x == requiredTrendBars));

                if (bearishEntrySetups.Last())
                {
                    double high = 0;
                    for (int i = 0; i < indecisionPeriod; i++)
                    {
                        if (High[i + 2] > high)
                        {
                            high = High[i + 2];
                        }
                    }

                    initialStopLoss = high + TickSize;
                }
            }
            else
            {
                bearishEntrySetups.Add(false);
            }

            //Jump in trend
            if (!bearishEntrySetups.Any(x => x) && !this.Contrarian && !this.Conservative)
            {
                bearishEntrySetups.Add(this.GuerillaCountIf(bearishTrendBarIndicator, x => x == 1, 0, jumpInTrendPeriod, x => x == jumpInTrendPeriod));

                if (bearishEntrySetups.Last())
                {
                    initialStopLoss = MAX(High, jumpInTrendPeriod)[0];
                }
            }
            else
            {
                bearishEntrySetups.Add(false);
            }

            this.bearishSetupsString = String.Join(",", bearishEntrySetups.Select(x => x.ToString().ToLower()));

            bool bearishEntrySignal = bearishEntrySetups.Any(x => x);

            //Bullish
            List <bool> bullishEntrySetups = new List <bool>();
            bool        oversold           = this.StochasticsFilter ? this.GuerillaCountIf(stochastics, x => x <= 30, 0, 4, x => x > 0) : true;

            //Shooting star reversal
            bullishEntrySetups.Add(this.GuerillaCountIf(redBarIndicator, x => x == 1, 2, previousTrendLookback, x => x >= previousTrendLookback - 1) &&
                                   this.GuerillaCountIf(bearishTrendBarIndicator, x => x == 1, 2, previousTrendBarLookback, x => x >= previousRequiredTrendBars) &&
                                   hammerIndicator[1] == 1 &&
                                   bullishTrendBarIndicator[0] == 1 &&
                                   oversold);

            if (bullishEntrySetups.Last())
            {
                initialStopLoss = Low[1] - TickSize;
            }

            //Doji entries
            if (!bullishEntrySetups.Any(x => x))
            {
                bullishEntrySetups.Add(this.GuerillaCountIf(dojiIndicator, x => x == 1, 2, 2, x => x >= 1) &&
                                       this.GuerillaCountIf(bullishTrendBarIndicator, x => x == 1, 0, 2, x => x >= 1) &&
                                       oversold);

                if (bullishEntrySetups.Last())
                {
                    int dojiIndex = dojiIndicator[2] == 1 ? 2 : 3;
                    initialStopLoss = Low[dojiIndex] - TickSize;
                }
            }
            else
            {
                bullishEntrySetups.Add(false);
            }

            if (!bullishEntrySetups.Any(x => x))
            {
                bullishEntrySetups.Add(dojiIndicator[1] == 1 && bullishTrendBarIndicator[0] == 1 &&
                                       oversold);

                if (bullishEntrySetups.Last())
                {
                    initialStopLoss = Low[1] - TickSize;
                }
            }
            else
            {
                bullishEntrySetups.Add(false);
            }

            //Trend bar reversal
            if (!bullishEntrySetups.Any(x => x))
            {
                bullishEntrySetups.Add(this.GuerillaCountIf(redBarIndicator, x => x == 1, 2, previousTrendLookback, x => x >= previousTrendLookback - 1) &&
                                       this.GuerillaCountIf(bearishTrendBarIndicator, x => x == 1, 2, previousTrendBarLookback, x => x >= previousRequiredTrendBars) &&
                                       this.GuerillaCountIf(bullishTrendBarIndicator, x => x == 1, 0, 2, x => x == requiredTrendBars) &&
                                       oversold);

                if (bullishEntrySetups.Last())
                {
                    initialStopLoss = Low[2] - TickSize;
                }
            }
            else
            {
                bullishEntrySetups.Add(false);
            }

            //Indecision breakout
            if (!bullishEntrySetups.Any(x => x) && !this.Conservative)
            {
                bullishEntrySetups.Add(previousIndecision &&
                                       this.GuerillaCountIf(bullishTrendBarIndicator, x => x == 1, 0, 2, x => x == requiredTrendBars));

                if (bearishEntrySetups.Last())
                {
                    double low = 0;
                    for (int i = 0; i < indecisionPeriod; i++)
                    {
                        if (High[i + 2] > low)
                        {
                            low = Low[i + 2];
                        }
                    }

                    initialStopLoss = low - TickSize;
                }
            }
            else
            {
                bullishEntrySetups.Add(false);
            }

            //Jump in trend
            if (!bullishEntrySetups.Any(x => x) && !this.Contrarian && !this.Conservative)
            {
                bullishEntrySetups.Add(this.GuerillaCountIf(bullishTrendBarIndicator, x => x == 1, 0, jumpInTrendPeriod, x => x == jumpInTrendPeriod));

                if (bullishEntrySetups.Last())
                {
                    initialStopLoss = MIN(Low, jumpInTrendPeriod)[0];
                }
            }
            else
            {
                bullishEntrySetups.Add(false);
            }

            this.bullishSetupsString = String.Join(",", bullishEntrySetups.Select(x => x.ToString().ToLower()));

            bool bullishEntrySignal = bullishEntrySetups.Any(x => x);

            //Exit
            bool bullishExitSignal = this.GuerillaCountIf(indecisionBarIndicator, x => x == 1, 0, 2, x => x == 2) ||
                                     dojiIndicator[0] == 1 ||
                                     bearishTrendBarIndicator[0] == 1 ||
                                     bearishEntrySignal;

            bool bearishExitSignal = this.GuerillaCountIf(indecisionBarIndicator, x => x == 1, 0, 2, x => x == 2) ||
                                     dojiIndicator[0] == 1 ||
                                     bullishTrendBarIndicator[0] == 1 ||
                                     bullishEntrySignal;

            bool validTime = disableManager.IsValidTime(Time[0]);

            if (!Contrarian)
            {
                if (this.Position.MarketPosition == MarketPosition.Flat && validTime && bearishEntrySignal)
                {
                    EnterShort(0, Quantity, ENTER_SHORT);
                }
                else if (this.Position.MarketPosition == MarketPosition.Flat && validTime && bullishEntrySignal)
                {
                    EnterLong(0, Quantity, ENTER_LONG);
                }
                else if (this.Position.MarketPosition == MarketPosition.Long && bullishExitSignal)
                {
                    if (bearishEntrySignal)
                    {
                        EnterShort(0, this.Position.Quantity, ENTER_SHORT);
                    }
                    else if (!this.Conservative)
                    {
                        ExitLong(0, this.Position.Quantity, EXIT, ENTER_LONG);
                    }
                }
                else if (this.Position.MarketPosition == MarketPosition.Short && bearishExitSignal)
                {
                    if (bullishEntrySignal)
                    {
                        EnterLong(0, this.Position.Quantity, ENTER_LONG);
                    }
                    else if (!this.Conservative)
                    {
                        ExitShort(0, this.Position.Quantity, EXIT, ENTER_SHORT);
                    }
                }
            }
            else
            {
                if (this.Position.MarketPosition == MarketPosition.Flat && validTime && bullishEntrySignal)
                {
                    EnterShort(0, this.Quantity, ENTER_SHORT);
                }
                else if (this.Position.MarketPosition == MarketPosition.Flat && validTime && bearishEntrySignal)
                {
                    EnterLong(0, this.Quantity, ENTER_LONG);
                }
                else if (this.Position.MarketPosition == MarketPosition.Long && bearishExitSignal)
                {
                    if (bullishEntrySignal)
                    {
                        EnterShort(0, this.Position.Quantity, ENTER_SHORT);
                    }
                    else if (!this.Conservative)
                    {
                        ExitLong(0, this.Position.Quantity, EXIT, ENTER_LONG);
                    }
                }
                else if (this.Position.MarketPosition == MarketPosition.Short && bullishExitSignal)
                {
                    if (bearishEntrySignal)
                    {
                        EnterLong(0, this.Position.Quantity, ENTER_LONG);
                    }
                    else if (!this.Conservative)
                    {
                        ExitShort(0, this.Position.Quantity, EXIT, ENTER_SHORT);
                    }
                }
            }
        }
        protected override void OnBarUpdate()
        {
            DateTime debugDate = new DateTime(2017, 12, 13, 20, 45, 0);

            if (debugDate == Time[0] && BarsInProgress == 1)
            {
                int d = 0;
            }

            Double minutes     = Time[0].Minute + (Time[0].Second == 30 ? .5 : 0);
            double heldPeriods = (minutes % longTimePeriod) / shortTimePeriod;

            if (BarsInProgress == 0 && Position.MarketPosition == MarketPosition.Flat && heldPeriods == 0)
            {
                firstPrimaryBar = true;
            }

            else if (firstPrimaryBar && BarsInProgress == 1 && heldPeriods == 1)
            {
                double body      = Math.Abs(Closes[0][0] - Opens[0][0]);
                bool   smallBody = body <= 1;

                if (this.guerillaTickProfile.WithinThreshold[0])
                {
                    bool enterLong  = false;
                    bool enterShort = false;
                    bool canEnter   = disableManager.IsValidTime(Time[0]);

                    if (heldPeriods == 1)
                    {
                        enterLong  = this.secondaryGreenBarStick[0] > 0;
                        enterShort = this.secondaryRedBarStick[0] > 0;
                    }

                    if (Position.MarketPosition == MarketPosition.Flat && canEnter)
                    {
                        if (enterLong)
                        {
                            EnterLong(2, 1, ENTER_LONG);
                        }
                        else if (enterShort)
                        {
                            EnterShort(2, 1, ENTER_SHORT);
                        }
                    }
                }
            }
            else if (BarsInProgress == 2 && Position.MarketPosition != MarketPosition.Flat)
            {
                if (Position.MarketPosition == MarketPosition.Long && High[0] >= profitTaker)
                {
                    ExitLong(2, 1, PROFIT_TAKER, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && Low[0] <= profitTaker)
                {
                    ExitShort(2, 1, PROFIT_TAKER, ENTER_SHORT);
                }
                else if (Position.MarketPosition == MarketPosition.Long && Low[0] <= stopLoss)
                {
                    ExitLong(2, 1, STOP_LOSS, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && High[0] >= stopLoss)
                {
                    ExitShort(2, 1, STOP_LOSS, ENTER_SHORT);
                }
            }
        }
Esempio n. 9
0
        protected override void OnBarUpdate()
        {
            if (CurrentBar < BarsRequiredToTrade)
            {
                return;
            }

            if (BarsInProgress == 0)
            {
                DateTime debugTime = new DateTime(2018, 6, 22, 4, 42, 0);
                DateTime blahTime  = Time[0];

                bool adxBullTrend = adxIndicator.DmiPlusPlot[0] > adxIndicator.DmiMinusPlot[0];
                bool adxBearTrend = adxIndicator.DmiPlusPlot[0] < adxIndicator.DmiMinusPlot[0];
                bool greenBar     = Close[0] > Open[0];
                bool redBar       = Close[0] < Open[0];

                int  enterPeriodMinusOne = EnterPeriod - 2;
                bool bullishEnterSignal  = adxIndicator[0] > MinAdx;
                bool bearishEnterSignal  = adxIndicator[0] > MinAdx;

                bullishEnterSignal = bullishEnterSignal && adxBullTrend && (CountIf(() => Close[0] > Open[0], EnterBarsLength) == EnterBarsLength);
                bearishEnterSignal = bearishEnterSignal && adxBearTrend && (CountIf(() => Close[0] < Open[0], EnterBarsLength) == EnterBarsLength);

                bullishEnterSignal = bullishEnterSignal && enterAdxSlopeIndicator[0] < EnterSlopeThreshold;
                bearishEnterSignal = bearishEnterSignal && enterAdxSlopeIndicator[0] < EnterSlopeThreshold;

                bool exitBarSignal = false;

                if (!this.openPosition)
                {
                    if (bullishSignalBarNumber > 0)
                    {
                        int barDiff = bullishSignalBarNumber - CurrentBar;
                        if (barDiff > BsWindowLength)
                        {
                            bullishSignalBarNumber = 0;
                            bearishSignalBarNumber = 0;
                        }
                    }

                    if (bearishSignalBarNumber > 0)
                    {
                        int barDiff = bearishSignalBarNumber - CurrentBar;
                        if (barDiff > BsWindowLength)
                        {
                            bullishSignalBarNumber = 0;
                            bearishSignalBarNumber = 0;
                        }
                    }

                    if (bullishEnterSignal)
                    {
                        bullishSignalBarNumber = CurrentBar;
                        bearishSignalBarNumber = 0;
                        return;
                    }

                    if (bearishEnterSignal)
                    {
                        bearishSignalBarNumber = CurrentBar;
                        bullishSignalBarNumber = 0;
                        return;
                    }

                    bullishEnterSignal = false;
                    bearishEnterSignal = false;

                    if (bullishSignalBarNumber > 0 && redBar)
                    {
                        bullishEnterSignal = true;
                        bearishEnterSignal = false;
                    }
                    else if (bearishSignalBarNumber > 0 && greenBar)
                    {
                        bullishEnterSignal = false;
                        bearishEnterSignal = true;
                    }
                }

                //Bullish exit
                if (this.openPosition && this.guerillaTraderBridge.ot_tradeType == (int)TradeTypes.LongFuture)
                {
                    if ((CurrentBar - enterBarNumber) == 1 && redBar)
                    {
                        exitBarSignal = true;
                    }
                    else if (trendBarAppeared)
                    {
                        //exit if red
                        exitBarSignal = redBar;
                    }
                    else if ((CurrentBar - enterBarNumber) >= MinTrendLength)
                    {
                        //true if green
                        //trendBarAppeared = Close[0] > Open[0];
                        trendBarAppeared = (CountIf(() => Close[0] > Open[0], MinTrendLength) == MinTrendLength);
                    }
                }
                //bearish exit
                else if (this.openPosition && this.guerillaTraderBridge.ot_tradeType == (int)TradeTypes.ShortFuture)
                {
                    if ((CurrentBar - enterBarNumber) == 1 && greenBar)
                    {
                        exitBarSignal = true;
                    }
                    else if (trendBarAppeared)
                    {
                        //exit if green
                        exitBarSignal = greenBar;
                    }
                    else if ((CurrentBar - enterBarNumber) >= MinTrendLength)
                    {
                        //true if red
                        //trendBarAppeared = Close[0] < Open[0];
                        trendBarAppeared = (CountIf(() => Close[0] < Open[0], MinTrendLength) == MinTrendLength);
                    }
                }


                DateTime start     = Time[0].Date.AddHours(StartHour).AddMinutes(StartMinute);
                DateTime end       = Time[0].Date.AddHours(EndHour).AddMinutes(EndMinute);
                bool     validTime = disableManager.IsValidTime(Time[0]);

                if (!this.openPosition && validTime && bullishEnterSignal && !targetHit)
                {
                    this.enterBarNumber         = CurrentBar;
                    this.bullishSignalBarNumber = 0;
                    this.bearishSignalBarNumber = 0;
                    this.tickRange = rangeIndicator[0];
                    this.maSlope   = Slope(fastMaIndicator, 5, 0) * 10;
                    this.maDiff    = fastMaIndicator[0] - slowMaIndicator[0];
                    EnterShort(0, Quantity, ENTER_SHORT);
                }
                else if (!this.openPosition && validTime && bearishEnterSignal && !targetHit)
                {
                    this.enterBarNumber         = CurrentBar;
                    this.bullishSignalBarNumber = 0;
                    this.bearishSignalBarNumber = 0;
                    this.tickRange = rangeIndicator[0];
                    this.maSlope   = Slope(fastMaIndicator, 3, 0) * 10;
                    this.maDiff    = fastMaIndicator[0] - slowMaIndicator[0];
                    EnterLong(0, Quantity, ENTER_LONG);
                }
                else if (this.openPosition && !closing && (exitBarSignal) && this.guerillaTraderBridge.ot_tradeType == 1)
                {
                    closing = true;

                    //Double exitLimitPrice = GetCurrentBid() + (TickSize * ExitLimitBuffer);
                    //if (exitLimitPrice <= Close[0]) exitLimitPrice = Close[0] + TickSize;

                    //ExitLongLimit(0, true, Quantity, exitLimitPrice, EXIT, ENTER_LONG);

                    //Double exitStopPrice = GetCurrentBid() - (TickSize * ExitStopBuffer);
                    //if (exitStopPrice >= Close[0]) exitStopPrice = Close[0] - TickSize;

                    //ExitLongStopMarket(0, true, Quantity, exitStopPrice, EXIT, ENTER_LONG);
                    ExitLong(0, Quantity, EXIT, ENTER_LONG);
                }
                else if (this.openPosition && !closing && (exitBarSignal) && this.guerillaTraderBridge.ot_tradeType == 2)
                {
                    closing = true;

                    //Double exitLimitPrice = GetCurrentAsk() - (TickSize * ExitLimitBuffer);
                    //if (exitLimitPrice >= Close[0]) exitLimitPrice = Close[0] - TickSize;

                    //ExitShortLimit(0, true, Quantity, exitLimitPrice, EXIT, ENTER_SHORT);

                    //Double exitStopPrice = GetCurrentAsk() + (TickSize * ExitStopBuffer);
                    //if (exitStopPrice <= Close[0]) exitStopPrice = Close[0] + TickSize;

                    //ExitShortStopMarket(0, true, Quantity, exitStopPrice, EXIT, ENTER_SHORT);
                    ExitShort(0, Quantity, EXIT, ENTER_SHORT);
                }
            }
            else if (BarsInProgress == 1)
            {
                if (this.openPosition && !closing && this.guerillaTraderBridge.ot_tradeType == 1)
                {
                    Double pl = ((Close[0] - this.guerillaTraderBridge.ot_entryPrice) * Instrument.MasterInstrument.PointValue) * this.guerillaTraderBridge.ot_size;
                    if (pl > this.TrailingStopMinDollars)
                    {
                        int divided = (int)(pl / TrailingStopDivisorDollars);

                        if (divided > this.maxDivided)
                        {
                            this.maxDivided = divided;
                            int    stopTicks     = (int)(TrailingStopCushionDollars / Instrument.MasterInstrument.PointValue / TickSize / this.guerillaTraderBridge.ot_size);
                            Double exitStopPrice = GetCurrentBid() - (TickSize * stopTicks);

                            ExitLongStopMarket(0, true, Quantity, exitStopPrice, EXIT, ENTER_LONG);
                        }
                    }
                }
                else if (this.openPosition && !closing && this.guerillaTraderBridge.ot_tradeType == 2)
                {
                    Double pl = ((this.guerillaTraderBridge.ot_entryPrice - Close[0]) * Instrument.MasterInstrument.PointValue) * this.guerillaTraderBridge.ot_size;
                    if (pl > this.TrailingStopMinDollars)
                    {
                        int divided = (int)(pl / TrailingStopDivisorDollars);

                        if (divided > this.maxDivided)
                        {
                            this.maxDivided = divided;
                            int    stopTicks     = (int)(TrailingStopCushionDollars / Instrument.MasterInstrument.PointValue / TickSize / this.guerillaTraderBridge.ot_size);
                            Double exitStopPrice = GetCurrentAsk() + (TickSize * ExitStopBuffer);

                            ExitShortStopMarket(0, true, Quantity, exitStopPrice, EXIT, ENTER_SHORT);
                        }
                    }
                }
            }
        }
        protected override void OnBarUpdate()
        {
            if (BarsInProgress == 0 && CurrentBar >= this.BarsRequiredToTrade)
            {
                metBarsInProgress = true;
            }

            if (!metBarsInProgress)
            {
                return;
            }

            if (BarsInProgress == 0 && disableManager.IsValidTime(Time[0]))
            {
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    double trendStartPrice = 0;
                    double trendEndPrice   = 0;

                    //Break of bullish trend line
                    //Check for diverging stochastic


                    for (int i = 0; i < 5; i++)
                    {
                        if (stochasticsFast_14_3.TrendStartPrice[i] > 0)
                        {
                            trendStartPrice = stochasticsFast_14_3.TrendStartPrice[i];
                            trendEndPrice   = stochasticsFast_14_3.TrendEndPrice[i];
                        }
                    }

                    if (
                        autoTrend.Signal[0] == -1 &&
                        trendStartPrice > trendEndPrice &&
                        trendStartPrice > 80
                        //&& trendEndPrice > 65
                        && this.redBarStick[0] > 0 &&
                        this.bigBarStick[0] > 0
                        )
                    {
                        PrintValues(Time[0], CountIf(() => stochasticsFast_14_3.D[0] > 80 && stochasticsFast_40_4.D[0] > 80 && stochasticsFast_9_3.D[0] > 80, 5) > 0);
                        EnterShort(1, 1, ENTER_SHORT);
                        stopLoss = MAX(High, 3)[0] + TickSize;
                        Draw.VerticalLine(this, String.Format("vl_{0}", CurrentBar), Time[0], Brushes.White);
                    }
                }
                //else if (Position.MarketPosition == MarketPosition.Short)
                //{
                //    if (stochasticsFast_14_3.CrossAboveLowerThreshold[0])
                //    {
                //        ExitShort(1, 1, PROFIT_TAKER, ENTER_SHORT);
                //    }
                //}
            }
            else if (BarsInProgress == 1 && Position.MarketPosition != MarketPosition.Flat)
            {
                if (Position.MarketPosition == MarketPosition.Long && High[0] >= profitTaker)
                {
                    ExitLong(1, 1, PROFIT_TAKER, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && Low[0] <= profitTaker)
                {
                    ExitShort(1, 1, PROFIT_TAKER, ENTER_SHORT);
                }
                if (Position.MarketPosition == MarketPosition.Long && Low[0] <= stopLoss)
                {
                    ExitLong(1, 1, STOP_LOSS, ENTER_LONG);
                }
                else if (Position.MarketPosition == MarketPosition.Short && High[0] >= stopLoss)
                {
                    ExitShort(1, 1, STOP_LOSS, ENTER_SHORT);
                }
            }
        }
Esempio n. 11
0
        protected override void OnBarUpdate()
        {
            DateTime debugDate = new DateTime(2017, 12, 13, 20, 45, 0);

            if (debugDate == Time[0] && BarsInProgress == 1)
            {
                int d = 0;
            }

            Double minutes     = Time[0].Minute + (Time[0].Second == 30 ? .5 : 0);
            double heldPeriods = (minutes % longTimePeriod) / shortTimePeriod;

            bool canGoLong  = Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Short;
            bool canGoShort = Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Long;

            if (BarsInProgress == 0 && Position.MarketPosition == MarketPosition.Flat && heldPeriods == 0)
            {
                firstPrimaryBar = true;
            }
            else if (firstPrimaryBar && BarsInProgress == 1 && heldPeriods <= 1)
            {
                double body      = Math.Abs(Closes[0][0] - Opens[0][0]);
                bool   smallBody = body <= 1;

                if (this.guerillaTickProfile.WithinThreshold[0])
                {
                    bool enterLong  = false;
                    bool enterShort = false;
                    bool exitLong   = false;
                    bool exitShort  = false;
                    bool canEnter   = disableManager.IsValidTime(Time[0]);

                    if (heldPeriods == 0 && this.Trade0Period)
                    {
                        enterLong = (this.TradeTails && this.fiftyHammerStick[0] > 0 && (this.greenBarStick[0] > 0 || smallBody)) ||
                                    (this.TradeBigBars && this.greenBarStick[0] > 0 && this.bigBarStick[0] > 0);
                        enterShort = (this.TradeTails && this.fiftyManStick[0] > 0 && (this.redBarStick[0] > 0 || smallBody)) ||
                                     (this.TradeBigBars && this.redBarStick[0] > 0 && this.bigBarStick[0] > 0);
                    }
                    else if (heldPeriods == 1 && this.Trade1Period)
                    {
                        bool isGreen = Close[0] > Open[0];
                        bool isRed   = Close[0] < Open[0];

                        enterLong = isGreen &&
                                    ((this.TradeTails && this.fiftyHammerStick[0] > 0) ||
                                     (this.TradeBigBars && this.bigBarStick[0] > 0 && this.redBarStick[0] > 0)
                                    );

                        enterShort = isRed &&
                                     ((this.TradeTails && this.fiftyManStick[0] > 0) ||
                                      (this.TradeBigBars && this.bigBarStick[0] > 0 && this.greenBarStick[0] > 0)
                                     );
                    }

                    if (Position.MarketPosition == MarketPosition.Flat && canEnter)
                    {
                        if (enterLong)
                        {
                            currentStop = 0;
                            EnterLong(2, 1, ENTER_LONG);
                        }
                        else if (enterShort)
                        {
                            currentStop = 0;
                            EnterShort(2, 1, ENTER_SHORT);
                        }
                    }
                    else if (Position.MarketPosition == MarketPosition.Long)
                    {
                        if (enterShort && canEnter)
                        {
                            //EnterShort(2, 1, ENTER_SHORT);
                        }
                        else if (exitLong || enterShort)
                        {
                            //ExitLong(2, 1, EXIT, ENTER_LONG);
                        }
                    }
                    else if (Position.MarketPosition == MarketPosition.Short)
                    {
                        if (enterLong && canEnter)
                        {
                            //EnterLong(2, 1, ENTER_LONG);
                        }
                        else if (exitShort || enterLong)
                        {
                            //ExitShort(2, 1, EXIT, ENTER_SHORT);
                        }
                    }
                }
            }
            //else if (BarsInProgress == 2 && Position.MarketPosition != MarketPosition.Flat)
            //{
            //    double newStop = trailingStopManager.GetStop(Position.MarketPosition, Position.AveragePrice, Close[0], TickSize);

            //    if (Position.MarketPosition == MarketPosition.Long && newStop > currentStop && currentStop == 0)
            //    {
            //        currentStop = newStop;
            //        ExitLongStopMarket(2, true, 1, currentStop, EXIT, ENTER_LONG);
            //    }
            //    else if (Position.MarketPosition == MarketPosition.Short && newStop < currentStop && currentStop == 0)
            //    {
            //        currentStop = newStop;
            //        ExitShortStopMarket(2, true, 1, currentStop, EXIT, ENTER_SHORT);
            //    }
            //}
        }
        protected override void OnBarUpdate()
        {
            if (Time[0].Hour == 6 && Time[0].Minute < 45)
            {
                return;
            }

            DateTime debugDate = new DateTime(2018, 10, 18, 12, 0, 0);

            if (debugDate == Time[0] && BarsInProgress == 1)
            {
                int d = 0;
            }

            Double minutes     = Time[0].Minute + (Time[0].Second == 30 ? .5 : 0);
            double heldPeriods = (minutes % longTimePeriod) / shortTimePeriod;

            bool canGoLong  = Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Short;
            bool canGoShort = Position.MarketPosition == MarketPosition.Flat || Position.MarketPosition == MarketPosition.Long;

            if (BarsInProgress == 0 && Position.MarketPosition == MarketPosition.Flat && heldPeriods == 0)
            {
            }
            else if (BarsInProgress == 1 && heldPeriods <= 1)
            {
                DateTime startTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, this.StartTradingHour, this.StartTradingMinute, 0);
                DateTime stopTime  = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, this.StopTradingHour, this.StopTradingMinute, 0);
                double   body      = Math.Abs(Closes[0][0] - Opens[0][0]);
                bool     smallBody = body <= 1;

                if (this.guerillaTickProfile.WithinThreshold[0])
                {
                    bool enterLong  = false;
                    bool enterShort = false;
                    bool exitLong   = false;
                    bool exitShort  = false;
                    bool canEnter   = disableManager.IsValidTime(Time[0]);

                    if (heldPeriods == 0)
                    {
                        enterLong  = (this.fiftyHammerStick[0] > 0 && (this.greenBarStick[0] > 0 || smallBody));
                        enterShort = (this.fiftyManStick[0] > 0 && (this.redBarStick[0] > 0 || smallBody));
                        exitLong   = (this.fiftyManStick[0] > 0 || (this.bigBarStick[0] > 0 && this.redBarStick[0] > 0));
                        exitShort  = (this.fiftyHammerStick[0] > 0 || (this.bigBarStick[0] > 0 && this.greenBarStick[0] > 0));
                    }
                    else if (heldPeriods == 1)
                    {
                        bool isGreen = Close[0] > Open[0];
                        bool isRed   = Close[0] < Open[0];

                        enterLong  = isGreen && this.redBarStick[0] > 0 && this.fiftyHammerStick[0] > 0;
                        enterShort = isRed && this.greenBarStick[0] > 0 && this.fiftyManStick[0] > 0;
                    }

                    if (Position.MarketPosition == MarketPosition.Flat && canEnter)
                    {
                        if (enterLong)
                        {
                            EnterLong(2, 1, ENTER_LONG);
                        }
                        else if (enterShort)
                        {
                            EnterShort(2, 1, ENTER_SHORT);
                        }
                    }
                    else if (Position.MarketPosition == MarketPosition.Long)
                    {
                        if (enterShort && canEnter)
                        {
                            EnterShort(2, 1, ENTER_SHORT);
                        }
                        else if (exitLong || enterShort)
                        {
                            ExitLong(2, 1, EXIT, ENTER_LONG);
                        }
                    }
                    else if (Position.MarketPosition == MarketPosition.Short)
                    {
                        if (enterLong && canEnter)
                        {
                            EnterLong(2, 1, ENTER_LONG);
                        }
                        else if (exitShort || enterLong)
                        {
                            ExitShort(2, 1, EXIT, ENTER_SHORT);
                        }
                    }
                }
            }
        }