protected override void Execute()
        {
            var pctChange = ROC.Series(Close, 1);
            var rocUp     = SeriesIsAboveValue.Series(pctChange, 0, 5);
            var rocDown   = SeriesIsBelowValue.Series(pctChange, 3.0, 5);

            for (int bar = GetTradingLoopStartBar(1); bar< Bars.Count; bar++)
            {
                if (rocUp[bar] >= 5 & amp; & rocDown[bar] >= 5)
                {
                    AnnotateBar("|", bar, false, Color.Black);
                    AnnotateBar("|", bar, false, Color.Black);
                    AnnotateBar("|", bar, false, Color.Black);
                    AnnotateBar("5wQA", bar, false, Color.Black, Color.Yellow);
                }
            }

            var lh = LineStyle.Histogram;
            var ls = LineStyle.Solid;
            var pd = CreatePane(20, true, true);
            var pu = CreatePane(20, true, true);

            PlotSeries(pd, rocDown, Color.Red, lh, 1);
            PlotSeries(pu, rocUp, Color.Blue, lh, 1);
            DrawHorzLine(pd, 5, Color.Black, ls, 1);
            DrawHorzLine(pu, 5, Color.Black, ls, 1);
        }
Exemple #2
0
        public ConnorsRSI(DataSeries ds, int periodRSI, int periodStreak, int periodPR, string description)
            : base(ds, description)
        {
            base.FirstValidValue = Math.Max(Math.Max(periodRSI, periodStreak), periodPR);
            if (FirstValidValue <= 1)
            {
                return;
            }

            ConsecDaysDown cdd    = ConsecDaysDown.Series(ds, 0);
            ConsecDaysUp   cdu    = ConsecDaysUp.Series(ds, 0);
            DataSeries     streak = new DataSeries(ds, "streak");

            for (int bar = 0; bar < ds.Count; bar++)
            {
                streak[bar] = cdd[bar] > 0 ? -cdd[bar] : cdu[bar] > 0 ? cdu[bar] : 0;
            }

            RSI        rsi3       = RSI.Series(ds, periodRSI);
            RSI        rsiStreak  = RSI.Series(streak, periodStreak);
            ROC        ret        = ROC.Series(ds, 1);
            DataSeries pr         = PercentRank.Series(ret, periodPR) * 100.0;
            DataSeries connorsRSI = (rsi3 + rsiStreak + pr) / 3;

            for (int bar = base.FirstValidValue; bar < ds.Count; bar++)
            {
                base[bar] = connorsRSI[bar];
            }
        }
        public override Advice GetActionForBuckets(List <TradeBucket> tradeBuckets)
        {
            Advice advice = new Advice();

            advice.AlgorithmName = Name;
            advice.Confidence    = 0;
            advice.Group         = Group;
            advice.Time          = tradeBuckets[tradeBuckets.Count - 1].Time;

            List <TradeBucket> buckets = tradeBuckets;
            ROC rsi = new ROC();

            TIOutput output = rsi.GetCurrentValue(buckets);

            advice.Price      = buckets[buckets.Count - 1].Close;
            advice.Confidence = (decimal)output.Value;

            if (output.Value > 5)
            {
                advice.Action = TradeAction.Sell;
            }
            else if (output.Value < -5)
            {
                advice.Action = TradeAction.Buy;
            }
            else
            {
                advice.Action = TradeAction.Hold;
            }

            return(advice);
        }
Exemple #4
0
        public Coppock(DataSeries ds, int ROCPeriod1, int ROCPeriod2, int MAPeriod, CoppockCalculation option, string description)
            : base(ds, description)
        {
            base.FirstValidValue = Math.Max(MAPeriod, Math.Max(ROCPeriod1, ROCPeriod2));

            if (FirstValidValue > ds.Count || FirstValidValue < 0)
            {
                FirstValidValue = ds.Count;
            }
            if (ds.Count < Math.Max(ROCPeriod1, ROCPeriod2))
            {
                return;
            }

            DataSeries Coppock  = WMA.Series((ROC.Series(ds, ROCPeriod1) + ROC.Series(ds, ROCPeriod2)), MAPeriod);
            DataSeries Coppock2 = WMA.Series((Community.Indicators.FastSMA.Series(ds, 22) / Community.Indicators.FastSMA.Series(ds >> 250, 22) - 1), 150);

            for (int bar = FirstValidValue; bar < ds.Count; bar++)
            {
                if (option == CoppockCalculation.Option1)
                {
                    base[bar] = Coppock[bar];
                }
                else
                {
                    base[bar] = Coppock2[bar];
                }
            }
        }
Exemple #5
0
        public DVCFE(Bars bars, int period1, int period2, int rankPeriod, string description)
            : base(bars, description)
        {
            if (FirstValidValue > bars.Count || FirstValidValue < 0)
            {
                FirstValidValue = bars.Count;
            }
            if (bars.Count < Math.Max(rankPeriod, Math.Max(period1, period2)))
            {
                return;
            }

            //Step 1
            DataSeries PriceChange = DataSeries.Abs(ROC.Series(bars.Close, 1));
            //Step 2
            DataSeries TenDayPriceChange = bars.Close - (bars.Close >> period1);
            DataSeries FirstRatio        = (TenDayPriceChange / Sum.Series(PriceChange, period1)) * -1;
            //Step 3
            DataSeries TwoFiftyDayPriceChange = bars.Close - (bars.Close >> period2);
            DataSeries SecondRatio            = (TwoFiftyDayPriceChange / Sum.Series(PriceChange, period2));
            //Step 4
            DataSeries Average = (FirstRatio + SecondRatio) / 2;
            DataSeries DVCFE   = PrcRank.Series(Average, rankPeriod);

            base.FirstValidValue = Math.Max(Math.Max(period1, period2), rankPeriod);
            if (FirstValidValue == 1)
            {
                return;
            }

            for (int bar = base.FirstValidValue; bar < bars.Count; bar++)
            {
                base[bar] = DVCFE[bar];
            }
        }
        public void ROC()
        {
            ROC roc = new ROC(12);

            roc.Load(csvPath);
            SingleDoubleSerie serie = roc.Calculate();

            Assert.NotNull(serie);
            Assert.True(serie.Values.Count > 0);
        }
Exemple #7
0
        public void ROC()
        {
            ROC roc = new ROC(12);

            roc.Load(Directory.GetCurrentDirectory() + "\\table.csv");
            SingleDoubleSerie serie = roc.Calculate();

            Assert.IsNotNull(serie);
            Assert.IsTrue(serie.Values.Count > 0);
        }
        public void ROC()
        {
            ROC roc = new ROC(12);

            roc.Load(OhlcList);
            SingleDoubleSerie serie = roc.Calculate();

            Assert.IsNotNull(serie);
            Assert.IsTrue(serie.Values.Count > 0);
        }
        static void Main()
        {
            TechnicalIndicator _indicator = new ROC();

            InititalizeParameters(_indicator.GetType().ToString());

            RabbitCalculateIndicator rabbitROC = new RabbitCalculateIndicator(_exchange, _queueReceiveFrom, _queueSendTo, _indicator);

            rabbitROC.ConsumeData();
        }
Exemple #10
0
        public KST(int p1 = 10, int p2 = 15, int p3 = 20, int p4 = 30, int n = 10) : base(n)
        {
            ROC1 = new ROC(p1);
            ROC2 = new ROC(p2);
            ROC3 = new ROC(p3);
            ROC4 = new ROC(p4);

            SMA1 = new SMA(p1);
            SMA2 = new SMA(p2);
            SMA3 = new SMA(p3);
            SMA4 = new SMA(p4);
        }
Exemple #11
0
        public Alpha(Bars bars, Bars index, int period, string description)
            : base(bars, description)
        {
            base.FirstValidValue = period;
            if (bars.Count < period)
            {
                return;
            }

            Beta beta  = Beta.Series(bars, index, period);
            var  alpha = (Sum.Series(ROC.Series(bars.Close, 1), period) - (beta) * Sum.Series(ROC.Series(index.Close, 1), period)) / period;

            for (int bar = period; bar < bars.Count; bar++)
            {
                base[bar] = alpha[bar];
            }
        }
Exemple #12
0
        public ConsecDaysDown(DataSeries ds, double pct, string description)
            : base(ds, description)
        {
            DataSeries roc = ROC.Series(ds, 1);

            for (int i = ds.Count - 1; i >= 0; i--)
            {
                int cd = 0;
                int b  = i;
                while (b >= 0 && roc[b] < -pct)
                {
                    cd++;
                    b--;
                }
                base[i] = cd;
            }
        }
Exemple #13
0
        public KST(DataSeries ds, int roc1, int roc2, int roc3, int roc4, int sma1, int sma2, int sma3, int sma4, string description)
            : base(ds, description)
        {
            List <int> lstParams = new List <int>(new int[] { roc1, roc2, roc3, roc4, sma1, sma2, sma3, sma4 });

            base.FirstValidValue = lstParams.Max();

            DataSeries RCMA1 = Community.Indicators.FastSMA.Series(ROC.Series(ds, roc1), sma1);
            DataSeries RCMA2 = Community.Indicators.FastSMA.Series(ROC.Series(ds, roc2), sma2);
            DataSeries RCMA3 = Community.Indicators.FastSMA.Series(ROC.Series(ds, roc3), sma3);
            DataSeries RCMA4 = Community.Indicators.FastSMA.Series(ROC.Series(ds, roc4), sma4);
            DataSeries kst   = (RCMA1) + (RCMA2 * 2) + (RCMA3 * 3) + (RCMA4 * 4);

            for (int bar = FirstValidValue; bar < ds.Count; bar++)
            {
                base[bar] = kst[bar];
            }
        }
Exemple #14
0
        //populate
        public override void Populate()
        {
            TimeSeries ds     = Parameters[0].AsTimeSeries;
            Int32      period = Parameters[1].AsInt;

            DateTimes = ds.DateTimes;

            if (period <= 0 || ds.Count == 0)
            {
                return;
            }

            if (ds.Count < period)
            {
                return;
            }

            ROC dailyChange = new ROC(ds, 1);
            var histVola    = new StdDev(dailyChange, period) / 100d; // Excel STDEV is based on sample
            var voltSwitch  = new TimeSeries(DateTimes);

            for (int bar = 0; bar < ds.Count; bar++)
            {
                if (bar >= period)
                {
                    int cnt = 0;

                    for (int i = bar; i >= bar - period; i--)
                    {
                        if (histVola[i] <= histVola[bar])
                        {
                            cnt++;
                        }
                    }

                    Values[bar] = (double)cnt / (double)period;
                }
                else
                {
                    Values[bar] = 0d;
                }
            }
        }
Exemple #15
0
        //===========================================================================================================
        // 5 weeks of Quiet Accumulation
        //-----------------------------------------------------------------------------------------------------------
        protected void show5WeeksOfQuietAccumulation(BarHistory bars)
        {
            ClearAllAnnotations();
            TimeSeries pctChange = ROC.Series(bars.Close, 1);

            for (int bar = 5; bar < bars.Count; bar++)
            {
                if (pctChange[bar] > 0 && pctChange[bar] < 3 &&
                    pctChange[bar - 1] > 0 && pctChange[bar - 1] < 3 &&
                    pctChange[bar - 2] > 0 && pctChange[bar - 2] < 3 &&
                    pctChange[bar - 3] > 0 && pctChange[bar - 3] < 3 &&
                    pctChange[bar - 4] > 0 && pctChange[bar - 4] < 3)
                {
                    for (int i = 0; i <= 3; i++)
                    {
                        DrawBarAnnotation2("|", bar, true);
                    }
                    DrawBarAnnotation2("5wQA", bar, true);
                }
            }
            DrawAllAnnotationsAtEnd(Color.Black, Color.Black, 9);
        }
Exemple #16
0
        public SpecialK_Weekly(Bars bars, string description)
            : base(bars, description)
        {
            base.FirstValidValue = 104 * 3;

            // Weekly constants
            int[] A = { 4, 5, 6, 8, 10, 13, 15, 20, 39, 52, 78, 104 };
            int[] B = { 4, 5, 6, 6, 10, 13, 15, 20, 26, 26, 26, 39 };
            int[] C = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };

            DataSeries sKw = bars.Close - bars.Close;

            for (int k = 0; k < 12; k++)
            {
                sKw += EMA.Series(ROC.Series(bars.Close, A[k]), B[k], EMACalculation.Modern) * C[k];
            }

            for (int bar = FirstValidValue; bar < bars.Count; bar++)
            {
                base[bar] = sKw[bar];
            }
        }
Exemple #17
0
        public SpecialK_Daily(Bars bars, string description)
            : base(bars, description)
        {
            base.FirstValidValue = 530;

            // Daily constants
            int[] a = { 10, 15, 20, 30, 50, 65, 75, 100, 195, 265, 390, 530 };
            int[] b = { 10, 10, 10, 15, 50, 65, 75, 100, 130, 130, 130, 195 };
            int[] c = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };

            DataSeries sKd = bars.Close - bars.Close;

            for (int k = 0; k < 12; k++)
            {
                sKd += Community.Indicators.FastSMA.Series(ROC.Series(bars.Close, a[k]), b[k]) * c[k];
            }

            for (int bar = FirstValidValue; bar < bars.Count; bar++)
            {
                base[bar] = sKd[bar];
            }
        }
Exemple #18
0
        public override void Populate()
        {
            TimeSeries source          = Parameters[0].AsTimeSeries;
            Int32      multiplier      = Parameters[1].AsInt;
            Int32      smoothingPeriod = Parameters[2].AsInt;
            BarHistory bars            = Parameters[3].AsBarHistory;

            TimeSeries sctrTimeSeries = new TimeSeries(source.DateTimes);

            DateTimes = source.DateTimes;

            if (source.Count < 200 + 1)
            {
                return;
            }

            EMA slowEma   = new EMA(source, 200);
            EMA mediumEma = new EMA(source, 50);
            EMA fastEma   = new EMA(source, 9);
            ROC slowRoc   = new ROC(source, 125);
            ROC mediumRoc = new ROC(source, 20);
            RSI fastRsi   = new RSI(source, 14);

            for (int n = 0; n < source.Count; n++)
            {
                double slowEmaValue   = ((bars.Close[n] - slowEma[n]) / slowEma[n]) * 100;
                double mediumEmaValue = (bars.Close[n] - mediumEma[n]) / mediumEma[n];
                double fastEmaValue   = ((bars.Close[n] - fastEma[n]) / fastEma[n]) * 100;

                double slowValue   = 60 * 0.01 * ((slowEmaValue + slowRoc[n]) / 2);
                double mediumValue = 30 * 0.01 * ((mediumEmaValue + mediumRoc[n]) / 2);
                double fastValue   = 10 * 0.01 * ((fastEmaValue + ((fastRsi[n]) - 50)) / 2);

                double sctrValue = 50 + (multiplier * (slowValue + mediumValue + fastValue));
                sctrTimeSeries[n] = sctrValue >= 0.1 && sctrValue <= 99.9 ? sctrValue : sctrValue > 99.9 ? 99.9 : sctrValue < 0.1 ? 0.1 : 0;
                Values[n]         = SMA.Calculate(n, sctrTimeSeries, smoothingPeriod);
            }
        }
Exemple #19
0
        public Beta(Bars bars, Bars index, int period, string description)
            : base(bars, description)
        {
            base.FirstValidValue = period;

            DataSeries Stock         = bars.Close;
            DataSeries Market        = index.Close;
            DataSeries PctStock      = ROC.Series(Stock, 1);
            DataSeries PctMarket     = ROC.Series(Market, 1);
            DataSeries MarketSq      = PctMarket * PctMarket;
            DataSeries ProductSeries = PctStock * PctMarket;

            //  Need to find Linear Regression Slope of Stock return vs. Mkt Return
            double SumMarket   = 0;
            double SumMarketSq = 0;
            double SumStock    = 0;
            double SumProduct  = 0;

            if (bars.Count < period)
            {
                return;
            }

            for (int bar = period; bar < bars.Count; bar++)
            {
                SumMarket = SumMarketSq = SumStock = SumProduct = 0;
                for (int i = bar - period; i <= bar; i++)
                {
                    SumMarket   += PctMarket[i];
                    SumMarketSq += MarketSq[i];
                    SumStock    += PctStock[i];
                    SumProduct  += ProductSeries[i];
                }
                base[bar] = ((period * SumProduct) - (SumStock * SumMarket)) / ((period * SumMarketSq) - (Math.Pow(SumMarket, 2)));
            }
        }
Exemple #20
0
        public InSyncIndex(Bars bars, string description)
            : base(bars, description)
        {
            base.FirstValidValue = 20;

            if (bars.Count < 20)
            {
                return;
            }

            DataSeries BOLInSLB = StdDev.Series(bars.Close, 20, StdDevCalculation.Sample) * 2;

            BOLInSLB = Community.Indicators.FastSMA.Series(bars.Close, 20) - BOLInSLB;

            DataSeries BOLInSUB = StdDev.Series(bars.Close, 20, StdDevCalculation.Sample) * 2;

            BOLInSUB = Community.Indicators.FastSMA.Series(bars.Close, 20) + BOLInSUB;

            DataSeries BOLInS2 = BOLInSUB - BOLInSLB;

            BOLInS2 = (bars.Close - BOLInSLB) / BOLInS2;

            EMV        emv     = EMV.Series(bars, 13);
            DataSeries EMVSer  = Community.Indicators.FastSMA.Series(emv, 10);
            DataSeries EMVInS2 = EMVSer - Community.Indicators.FastSMA.Series(EMVSer, 10);

            DataSeries MACDSer  = Community.Indicators.FastSMA.Series(MACD.Series(bars.Close), 10);
            DataSeries MACDInS2 = MACD.Series(bars.Close) - MACDSer;

            int        Period    = 18;
            DataSeries DPOSeries = bars.Close - (Community.Indicators.FastSMA.Series(bars.Close, Period) >> ((Period / 2) + 1));

            DataSeries PDOSer  = Community.Indicators.FastSMA.Series(DPOSeries, 10);
            DataSeries PDOInS2 = DPOSeries - PDOSer;

            DataSeries ROCSer  = EMA.Series(ROC.Series(bars.Close, 10), 10, EMACalculation.Modern);
            DataSeries ROCInS2 = ROC.Series(bars.Close, 10) - ROCSer;

            DataSeries BOLInSLL = bars.Close * 0;
            DataSeries CCInS    = bars.Close * 0;
            DataSeries EMVInSB  = bars.Close * 0;
            DataSeries EMVInSS  = bars.Close * 0;
            DataSeries MACDInSB = bars.Close * 0;
            DataSeries MACDInSS = bars.Close * 0;
            DataSeries MFIInS   = bars.Close * 0;
            DataSeries PDOInSB  = bars.Close * 0;
            DataSeries PDOInSS  = bars.Close * 0;
            DataSeries ROCInSB  = bars.Close * 0;
            DataSeries ROCInSS  = bars.Close * 0;
            DataSeries RSIInS   = bars.Close * 0;
            DataSeries STODInS  = bars.Close * 0;
            DataSeries STOKInS  = bars.Close * 0;

            for (int bar = base.FirstValidValue; bar < bars.Count; bar++)
            {
                if (BOLInS2[bar] < 0.05)
                {
                    BOLInSLL[bar] = -5;
                }
                else
                if (BOLInS2[bar] > 0.95)
                {
                    BOLInSLL[bar] = +5;
                }

                if (CCI.Series(bars, 14)[bar] > +100)
                {
                    CCInS[bar] = +5;
                }
                else
                if (CCI.Series(bars, 14)[bar] < -100)
                {
                    CCInS[bar] = -5;
                }

                if ((EMVInS2[bar] < 0) & (EMVSer[bar] < 0))
                {
                    EMVInSB[bar] = -5;
                }
                if ((EMVInS2[bar] > 0) & (EMVSer[bar] > 0))
                {
                    EMVInSS[bar] = +5;
                }

                if ((MACDInS2[bar] < 0) & (MACDSer[bar] < 0))
                {
                    MACDInSB[bar] = -5;
                }
                if ((MACDInS2[bar] > 0) & (MACDSer[bar] > 0))
                {
                    MACDInSS[bar] = +5;
                }

                if (MFI.Series(bars, 20)[bar] > 80)
                {
                    MFIInS[bar] = +5;
                }
                else
                if (MFI.Series(bars, 20)[bar] < 20)
                {
                    MFIInS[bar] = -5;
                }

                if ((PDOInS2[bar] < 0) & (PDOSer[bar] < 0))
                {
                    PDOInSB[bar] = -5;
                }
                if ((PDOInS2[bar] > 0) & (PDOSer[bar] > 0))
                {
                    PDOInSS[bar] = +5;
                }

                if ((ROCInS2[bar] < 0) & (ROCSer[bar] < 0))
                {
                    ROCInSB[bar] = -5;
                }
                if ((ROCInS2[bar] > 0) & (ROCSer[bar] > 0))
                {
                    ROCInSS[bar] = +5;
                }

                if (RSI.Series(bars.Close, 14)[bar] > 70)
                {
                    RSIInS[bar] = +5;
                }
                else
                if (RSI.Series(bars.Close, 14)[bar] < 30)
                {
                    RSIInS[bar] = -5;
                }

                if (StochD.Series(bars, 14, 3)[bar] > 80)
                {
                    STODInS[bar] = +5;
                }
                else
                if (StochD.Series(bars, 14, 3)[bar] < 20)
                {
                    STODInS[bar] = -5;
                }

                if (StochK.Series(bars, 14)[bar] > 80)
                {
                    STOKInS[bar] = +5;
                }
                else
                if (StochK.Series(bars, 14)[bar] < 20)
                {
                    STOKInS[bar] = -5;
                }

                base[bar] = 50 +
                            CCInS[bar] + BOLInSLL[bar] + RSIInS[bar]
                            + STODInS[bar] + MFIInS[bar] + EMVInSB[bar]
                            + EMVInSS[bar] + ROCInSS[bar] + ROCInSB[bar]
                            + STOKInS[bar] + MACDInSS[bar] + MACDInSB[bar]
                            + PDOInSS[bar - 10] + PDOInSB[bar - 10];
            }
        }
        protected override void Execute()
        {
            int level;

            signleToSell     = false;
            priceAtCross     = 0;
            firstUp          = false;
            firstDown        = false;
            priceAtCrossDown = 0;
            rocAtCrossDown   = 0;

            topPrice   = 0;
            lastTop    = 0;
            breakPrice = 0;
            rocPrice   = 0;

            //Create and plot  period RSI
            RSI        rsi20       = RSI.Series(Close, 29);
            DataSeries senkouSpanA = SenkouSpanA.Series(Bars);
            DataSeries senkouSpanB = SenkouSpanB.Series(Bars);
            ChartPane  rsiPane     = CreatePane(50, true, true);

            PlotSeries(rsiPane, rsi20, Color.Navy, LineStyle.Solid, 3);
            DrawHorzLine(rsiPane, overbought.Value, Color.Green, LineStyle.Dotted, 2);


            ChartPane paneROC1 = CreatePane(40, true, true);

            PlotSeries(paneROC1, ROC.Series(Close, 20), Color.FromArgb(255, 112, 128, 144), LineStyle.Dotted, 2);
            DrawHorzLine(paneROC1, 0, Color.Red, LineStyle.Solid, 2);

            int period = Math.Max(29, 20);


            if (initPosition)
            {
                // init starting state to force buy into the market
                priceAtCrossDown = Close[period + 1];
                rocAtCrossDown   = ROC.Value(period + 1, Close, 20);
                firstDown        = true;
            }

            //Trading system main loop
            for (int bar = period + 1; bar < Bars.Count; bar++)
            {
                if (Close[bar] > topPrice)
                {
                    topPrice = Close[bar];
                }


                // Ichimoku  start
                if (
                    CrossOver(bar, Close, senkouSpanA))
                //				if (CrossUnder(bar, rsi20, level))
                {
                    if (!firstDown)
                    {
                        // first time to go below
                        // set cross happen and record the targit price
                        firstDown        = true;
                        priceAtCrossDown = Close[bar];
                        rocAtCrossDown   = ROC.Value(bar, Close, 20);
                    }
                }

                // play trayling
                int  addToPosition = 0;
                bool rsiOk         = false;
                bool priceOK       = false;
                if (firstDown)
                {
                    // ROC must improve by delta
                    double delta = Math.Abs(rocAtCrossDown * 0.2);

                    double newrocSMA = ROC.Value(bar, Close, 20);


                    if (bar == Bars.Count - 1)
                    {
                        rocPrice = rocAtCrossDown + delta;
                    }

                    if (newrocSMA <= (rocAtCrossDown + delta))
                    {
                        if (newrocSMA < rocAtCrossDown)
                        {
                            rocAtCrossDown = newrocSMA;
                        }
                    }
                    else
                    {
                        rsiOk = true;
                    }

                    double riseV = 2;
                    delta = priceAtCrossDown * (riseV / 1000.0 + 0.00017);

                    if (bar == Bars.Count - 1)
                    {
                        breakPrice = priceAtCrossDown + delta;
                    }

                    if (Close[bar] < (priceAtCrossDown + delta))
                    {
                        // DrawLabel(PricePane, "ready to buy, price is not rising: " + Close[bar].ToString() + " less than " + (priceAtCrossDown + delta).ToString());

                        if (Close[bar] < priceAtCrossDown)
                        {
                            priceAtCrossDown = Close[bar];
                        }
                    }
                    else
                    {
                        priceOK = true;
                    }

                    if (priceOK && rsiOk)
                    {
                        addToPosition++;
                        firstDown = false;
                    }
                }


                // you can  have only one active position
                foreach (Position pos in Positions)
                {
                    if (pos.Active && pos.PositionType == PositionType.Long)
                    {
                        addToPosition = 0;
                        break;
                    }
                }


                if (addToPosition > 0)
                {
                    // Close all shorts
                    foreach (Position pos in Positions)
                    {
                        if (pos.Active && pos.PositionType == PositionType.Short)
                        {
                            CoverAtMarket(bar + 1, pos);
                        }
                    }

                    //	    DrawLabel(PricePane, "buy at bar = " + bar.ToString());
                    Position p = BuyAtMarket(bar + 1);
                    firstUp = false;
                }


                level = overbought.ValueInt;
                int ClosedTrades = 0;
                signleToSell = false;
                if (CrossOver(bar, rsi20, overbought.ValueInt))
                {
                    if (!firstUp)
                    {
                        // first time to go above
                        // set cross happen and record the targit price
                        firstUp      = true;
                        priceAtCross = Close[bar];
                    }
                }

                if (firstUp)
                {
                    double riseV = 2;

                    double delta = priceAtCross * (riseV / 1000 + 0.00017);

                    priceOK = true;
                    if (Close[bar] >= (priceAtCross - delta))
                    {
                        if (Close[bar] > priceAtCross)
                        {
                            priceAtCross = Close[bar];
                        }
                    }
                    else
                    {
                        priceOK = false;
                    }

                    // keep as long ROC over zero
                    if (ROC.Value(bar, Close, 20) <= 0 && !priceOK)
                    {
                        signleToSell = true;
                        firstUp      = false;
                    }
                }


                // wait until price either move up or stopped out
                if (signleToSell)
                {
                    firstUp = false;
                    //DrawLabel(PricePane, ActivePositions.Count.ToString());
                    foreach (Position pos in Positions)
                    {
                        if (pos.Active && pos.PositionType == PositionType.Long)
                        {
                            SellAtMarket(bar + 1, pos);
                            ClosedTrades++;
                        }
                    }
                    signleToSell = false;
                }
                if (!IsLastPositionActive)
                {
                    if (CrossUnder(bar, Close, senkouSpanB))
                    {
                        // Short only after sell long position
                        if (!trend)
                        {
                            ShortAtMarket(bar + 1);
                        }
                    }
                }

                // sell on % lose
                foreach (Position pos in Positions)
                {
                    if (pos.Active &&
                        pos.PositionType == PositionType.Long &&
                        pos.EntryPrice > (Close[bar] + pos.EntryPrice * (0.008 + 0 * 0.001)) &&
                        bar >= pos.EntryBar
                        )
                    {
                        SellAtMarket(bar + 1, pos, "stop lose");
                        signleToSell = false;
                        firstUp      = false;
                        firstDown    = false;
                        continue;
                    }
                    if (pos.Active &&
                        pos.PositionType == PositionType.Short &&
                        Close[bar] > (pos.EntryPrice + pos.EntryPrice * (0.008 + 0 * 0.001)) &&
                        bar >= pos.EntryBar
                        )
                    {
                        CoverAtMarket(bar + 1, pos, "stop lose Short");
                    }
                }
            }
            double currentPrice = Close[Bars.Count - 1];

            DrawLabel(paneROC1, "Top: " + topPrice.ToString(), Color.Red);
            DrawLabel(paneROC1, "Current Price: " + currentPrice.ToString());

            DrawLabel(paneROC1, "Goal : " + " At +5%  " + (currentPrice * 1.05).ToString(), Color.BlueViolet);
            DrawLabel(paneROC1, "Goal : " + " At +8%  " + (currentPrice * 1.08).ToString(), Color.MediumSpringGreen);
            DrawLabel(paneROC1, "Goal : " + " At +10% " + (currentPrice * 1.10).ToString(), Color.DarkOliveGreen);


            DrawLabel(PricePane, "Drop          : " + " At -5%  " + (topPrice * 0.95).ToString(), Color.DarkGreen);
            DrawLabel(PricePane, "Correction   : " + " At -8%  " + (topPrice * 0.92).ToString(), Color.DarkBlue);
            DrawLabel(PricePane, "Correction   : " + " At -10% " + (topPrice * 0.90).ToString(), Color.Red);
            DrawLabel(PricePane, "Bear market: " + " At -20% " + (topPrice * 0.80).ToString(), Color.DarkRed);

            if (breakPrice > 0)
            {
                DrawLabel(PricePane, "Break above Price: " + breakPrice.ToString(), Color.DarkGoldenrod);
            }
            if (rocPrice > 0)
            {
                DrawLabel(PricePane, "Break above Momuntem: " + rocPrice.ToString(), Color.DarkGoldenrod);
            }


            DrawHorzLine(PricePane, currentPrice * 1.05, Color.BlueViolet, LineStyle.Solid, 6);
            DrawHorzLine(PricePane, currentPrice * 1.08, Color.MediumSpringGreen, LineStyle.Solid, 3);
            DrawHorzLine(PricePane, currentPrice * 1.10, Color.DarkOliveGreen, LineStyle.Solid, 3);


            DrawHorzLine(PricePane, topPrice * 0.95, Color.DarkGreen, LineStyle.Solid, 3);
            DrawHorzLine(PricePane, topPrice * 0.92, Color.DarkBlue, LineStyle.Solid, 3);
            DrawHorzLine(PricePane, topPrice * 0.90, Color.Red, LineStyle.Solid, 3);
            DrawHorzLine(PricePane, topPrice * 0.80, Color.DarkRed, LineStyle.Solid, 3);


            if (breakPrice > 0)
            {
                DrawHorzLine(PricePane, breakPrice, Color.DarkGoldenrod, LineStyle.Solid, 4);
            }

            //Pushed indicator PlotSeries statements
            PlotSeries(PricePane, KijunSen.Series(Bars), Color.FromArgb(255, 128, 0, 128), LineStyle.Solid, 3);
            PlotSeriesFillBand(PricePane, SenkouSpanA.Series(Bars), SenkouSpanB.Series(Bars), Color.FromArgb(255, 128, 0, 255), Color.FromArgb(63, 0, 0, 255), LineStyle.Solid, 3);
            PlotSeriesFillBand(PricePane, SenkouSpanB.Series(Bars), SenkouSpanA.Series(Bars), Color.FromArgb(255, 255, 0, 0), Color.FromArgb(63, 255, 0, 0), LineStyle.Solid, 3);
            PlotSeries(PricePane, TenkanSen.Series(Bars), Color.FromArgb(255, 0, 64, 128), LineStyle.Solid, 3);
        }
Exemple #22
0
        protected override void Execute()
        {
            int level;

            signleToSell     = false;
            priceAtCross     = 0;
            firstUp          = false;
            firstDown        = false;
            priceAtCrossDown = 0;
            rocAtCrossDown   = 0;

            topPrice   = 0;
            lastTop    = 0;
            breakPrice = 0;
            rocPrice   = 0;

            //Create and plot  period RSI
            RSI       rsi20   = RSI.Series(Close, rsiPeriod.ValueInt);
            ChartPane rsiPane = CreatePane(50, true, true);

            PlotSeries(rsiPane, rsi20, Color.Navy, LineStyle.Solid, 3);
            DrawHorzLine(rsiPane, oversold.Value, Color.Red, LineStyle.Dotted, 2);
            DrawHorzLine(rsiPane, overbought.Value, Color.Green, LineStyle.Dotted, 2);


            ChartPane paneROC1 = CreatePane(40, true, true);

            PlotSeries(paneROC1, ROC.Series(Close, rocPeriod.ValueInt), Color.FromArgb(255, 112, 128, 144), LineStyle.Dotted, 2);
            DrawHorzLine(paneROC1, 0, Color.Red, LineStyle.Solid, 2);

            int period = Math.Max(rsiPeriod.ValueInt, rocPeriod.ValueInt);


            if (initPosition)
            {
                // init starting state to force buy into the market
                priceAtCrossDown = Close[period + 1];
                rocAtCrossDown   = ROC.Value(period + 1, Close, rocPeriod.ValueInt);
                firstDown        = true;
            }

            double High60           = 0;
            double Low60            = 0;
            int    n                = Bars.BarInterval;
            int    num              = (60 / n) - 1; // the first hour bar
            int    firstIntradayBar = -1;


            //Trading system main loop
            for (int bar = period + 1; bar < Bars.Count; bar++)
            {
                if (Bars.IntradayBarNumber(bar) == 0)
                {
                    firstIntradayBar = bar;
                }

                if (Bars.IntradayBarNumber(bar) <= num)                    // highlight the first hour
                {
                    SetBarColor(bar, Color.Silver);
                }

                if (Bars.IntradayBarNumber(bar) == num)                    // get the highest high and the lowest low after first hour
                {
                    High60 = Highest.Series(Bars.High, num + 1)[bar];
                    Low60  = Lowest.Series(Bars.Low, num + 1)[bar];

                    if (firstIntradayBar > -1)
                    {
                        DrawLine(PricePane, bar, High60, firstIntradayBar, High60, Color.Blue, LineStyle.Dashed, 1);
                        DrawLine(PricePane, bar, Low60, firstIntradayBar, Low60, Color.Red, LineStyle.Dashed, 1);
                    }
                }

                if (Close[bar] > topPrice)
                {
                    topPrice = Close[bar];
                }

                level = oversold.ValueInt;

                if (CrossUnder(bar, rsi20, level))
                {
                    if (!firstDown)
                    {
                        // first time to go below
                        // set cross happen and record the targit price
                        firstDown        = true;
                        priceAtCrossDown = Close[bar];
                        rocAtCrossDown   = ROC.Value(bar, Close, rocPeriod.ValueInt);
                    }
                }

                // play trayling
                int  addToPosition = 0;
                bool rsiOk         = false;
                bool priceOK       = false;
                if (firstDown)
                {
                    // ROC must improve by delta
                    double delta = Math.Abs(rocAtCrossDown * 0.2);

                    double newrocSMA = ROC.Value(bar, Close, rocPeriod.ValueInt);


                    if (bar == Bars.Count - 1)
                    {
                        rocPrice = rocAtCrossDown + delta;
                    }

                    if (newrocSMA <= (rocAtCrossDown + delta))
                    {
                        if (newrocSMA < rocAtCrossDown)
                        {
                            rocAtCrossDown = newrocSMA;
                        }
                    }
                    else
                    {
                        rsiOk = true;
                    }

                    double riseV = priceRise.ValueInt;
                    delta = priceAtCrossDown * (riseV / 1000.0 + 0.00017);

                    if (bar == Bars.Count - 1)
                    {
                        breakPrice = priceAtCrossDown + delta;
                    }

                    if (Close[bar] < (priceAtCrossDown + delta))
                    {
                        // DrawLabel(PricePane, "ready to buy, price is not rising: " + Close[bar].ToString() + " less than " + (priceAtCrossDown + delta).ToString());

                        if (Close[bar] < priceAtCrossDown)
                        {
                            priceAtCrossDown = Close[bar];
                        }
                    }
                    else
                    {
                        priceOK = true;
                    }

                    if (priceOK && rsiOk)
                    {
                        addToPosition++;
                        firstDown = false;
                    }
                }

                // you can  have only one active position
                foreach (Position pos in Positions)
                {
                    if (pos.Active && pos.PositionType == PositionType.Long)
                    {
                        addToPosition = 0;
                        break;
                    }
                }

                if (addToPosition > 0)
                {
                    // OverSold
                    // Close all shorts
                    foreach (Position pos in Positions)
                    {
                        if (pos.Active && pos.PositionType == PositionType.Short)
                        {
                            CoverAtMarket(bar + 1, pos);
                        }
                    }

                    //	    DrawLabel(PricePane, "buy at bar = " + bar.ToString());
                    Position p = BuyAtMarket(bar + 1);
                    firstUp = false;
                }


                level = overbought.ValueInt;
                int ClosedTrades = 0;
                signleToSell = false;
                if (CrossOver(bar, rsi20, overbought.ValueInt))
                {
                    if (!firstUp)
                    {
                        // first time to go above
                        // set cross happen and record the targit price
                        firstUp      = true;
                        priceAtCross = Close[bar];
                    }
                }

                if (firstUp)
                {
                    double riseV = priceRise.ValueInt;

                    double delta = priceAtCross * (riseV / 1000 + 0.00017);

                    priceOK = true;
                    if (Close[bar] >= (priceAtCross - delta))
                    {
                        if (Close[bar] > priceAtCross)
                        {
                            priceAtCross = Close[bar];
                        }
                    }
                    else
                    {
                        priceOK = false;
                    }

                    // keep as long ROC over zero
                    if (ROC.Value(bar, Close, rocPeriod.ValueInt) <= 0 && !priceOK)
                    {
                        signleToSell = true;
                        firstUp      = false;
                    }
                }

                // sync real account with strategy
                //			if (Bars.Date[bar] == syncDate)
                //			{
                //				signleToSell = true;  // close postions
                //			}

                // Over Bought
                // wait until price either move up or stopped out
                if (signleToSell)
                {
                    firstUp = false;
                    //DrawLabel(PricePane, ActivePositions.Count.ToString());
                    foreach (Position pos in Positions)
                    {
                        if (pos.Active && pos.PositionType == PositionType.Long)
                        {
                            SellAtMarket(bar + 1, pos);
                            ClosedTrades++;
                        }
                    }
                    signleToSell = false;

                    if (ClosedTrades > 0)
                    {
                        // Short only after sell long position
                        if (!trend)
                        {
                            ShortAtMarket(bar + 1);
                        }
                    }
                }
                // sell on % lose
                foreach (Position pos in Positions)
                {
                    if (pos.Active &&
                        pos.PositionType == PositionType.Long &&
                        pos.EntryPrice > (Close[bar] + pos.EntryPrice * (0.01 + stopLose.ValueInt * 0.001)) &&
                        bar >= pos.EntryBar
                        )
                    {
                        SellAtMarket(bar + 1, pos, "stop lose");
                        signleToSell = false;
                        firstUp      = false;
                        firstDown    = false;
                        continue;
                    }
                    if (pos.Active &&
                        pos.PositionType == PositionType.Short &&
                        Close[bar] > (pos.EntryPrice + pos.EntryPrice * (0.01 + stopLose.ValueInt * 0.001)) &&
                        bar >= pos.EntryBar
                        )
                    {
                        CoverAtMarket(bar + 1, pos, "stop lose Short");
                    }
                }
            }
            double currentPrice = Close[Bars.Count - 1];

            DrawLabel(paneROC1, "Top: " + topPrice.ToString(), Color.Red);
            DrawLabel(paneROC1, "Current Price: " + currentPrice.ToString());

            DrawLabel(paneROC1, "Goal : " + " At +5%  " + (currentPrice * 1.05).ToString(), Color.BlueViolet);
            DrawLabel(paneROC1, "Goal : " + " At +8%  " + (currentPrice * 1.08).ToString(), Color.MediumSpringGreen);
            DrawLabel(paneROC1, "Goal : " + " At +10% " + (currentPrice * 1.10).ToString(), Color.DarkOliveGreen);


            DrawLabel(PricePane, "Drop          : " + " At -5%  " + (topPrice * 0.95).ToString(), Color.DarkGreen);
            DrawLabel(PricePane, "Correction   : " + " At -8%  " + (topPrice * 0.92).ToString(), Color.DarkBlue);
            DrawLabel(PricePane, "Correction   : " + " At -10% " + (topPrice * 0.90).ToString(), Color.Red);
            DrawLabel(PricePane, "Bear market: " + " At -20% " + (topPrice * 0.80).ToString(), Color.DarkRed);

            if (breakPrice > 0)
            {
                DrawLabel(PricePane, "Break above Price: " + breakPrice.ToString(), Color.DarkGoldenrod);
            }
            if (rocPrice > 0)
            {
                DrawLabel(PricePane, "Break above Momuntem: " + rocPrice.ToString(), Color.DarkGoldenrod);
            }


            DrawHorzLine(PricePane, currentPrice * 1.05, Color.BlueViolet, LineStyle.Solid, 6);
            DrawHorzLine(PricePane, currentPrice * 1.08, Color.MediumSpringGreen, LineStyle.Solid, 3);
            DrawHorzLine(PricePane, currentPrice * 1.10, Color.DarkOliveGreen, LineStyle.Solid, 3);


            DrawHorzLine(PricePane, topPrice * 0.95, Color.DarkGreen, LineStyle.Solid, 3);
            DrawHorzLine(PricePane, topPrice * 0.92, Color.DarkBlue, LineStyle.Solid, 3);
            DrawHorzLine(PricePane, topPrice * 0.90, Color.Red, LineStyle.Solid, 3);
            DrawHorzLine(PricePane, topPrice * 0.80, Color.DarkRed, LineStyle.Solid, 3);


            if (breakPrice > 0)
            {
                DrawHorzLine(PricePane, breakPrice, Color.DarkGoldenrod, LineStyle.Solid, 4);
            }
        }