Example #1
0
        public static List <IndicatorBasket> CalcIndicators()
        {
            //   Console.WriteLine("Calculating Indicators...");
            var Indicators = new List <IndicatorBasket>();

            //Base Level Calcs
            var ohlc      = Trades.Select(z => z.OHLC);
            var OHLC_LIST = ohlc.ToList();
            var volume    = Trades.ToDictionary(x => x.TimeStamp, x => x.TradeVolume);
            var trigger   = Trades.ToDictionary(x => x.TimeStamp, x => x.TradeTrigger);
            var EM        = AlsiUtils.Factory_Indicator.createAdaptiveMA_MAMA(P.Mamapar1, P.Mamapar2, OHLC_LIST).ToDictionary(x => x.TimeStamp, x => x.Mama);
            var adx_DMI   = AlsiUtils.Factory_Indicator.createADX(P.ADX_period, OHLC_LIST);
            var RSI       = AlsiUtils.Factory_Indicator.createRSI(P.RSI_period, OHLC_LIST);


            //Second Level Clacs
            //EMA OF DMI+ and DMI-
            List <VariableIndicator> varDmiUP   = new List <VariableIndicator>();
            List <VariableIndicator> varDmiDOWN = new List <VariableIndicator>();

            foreach (var f in adx_DMI)
            {
                var vdown = new VariableIndicator()
                {
                    TimeStamp = f.TimeStamp,
                    Value     = f.DI_Down,
                };
                var vup = new VariableIndicator()
                {
                    TimeStamp = f.TimeStamp,
                    Value     = f.DI_Up,
                };

                varDmiUP.Add(vup);
                varDmiDOWN.Add(vdown);
            }

            var Dmi_Up_EMA   = Factory_Indicator.createEMA(P.Di_UP_avg_period, varDmiUP);
            var Dmi_Down_EMA = Factory_Indicator.createEMA(P.Di_DOWN_avg_period, varDmiDOWN);

            //EMA OF RSI
            List <VariableIndicator> varRsi = new List <VariableIndicator>();

            foreach (var r in RSI)
            {
                var rsi = new VariableIndicator()
                {
                    TimeStamp = r.TimeStamp,
                    Value     = r.RSI,
                };
                varRsi.Add(rsi);
            }

            var RSI_EMA = Factory_Indicator.createEMA(P.RSI_EMA, varRsi);

            //slope OF RSI
            var rsi_slope = Factory_Indicator.createRegression(P.RSI_Slope_period, varRsi);

            //CREATE DICTIONARY
            var DMI_UP_DIC   = Dmi_Up_EMA.ToDictionary(x => x.TimeStamp, x => x.Ema);
            var DMI_Down_DIC = Dmi_Down_EMA.ToDictionary(x => x.TimeStamp, x => x.Ema);
            var RSI_DIC      = RSI_EMA.ToDictionary(x => x.TimeStamp, x => x.Ema);
            var RSI_SLOPE    = rsi_slope.ToDictionary(x => x.TimeStamp, x => x.Slope);



            //  var sw = new StreamWriter(@"d:\Indicators.csv");
            // sw.WriteLine("Trade" + "," + "Stamp" + "," + "PriceClose" + "," + "MAMA" + "," + "DI-UP" + "," + "DI-DOWN" + "," + "VOL" + "," + "RSI" + "," + "RSI SLOPE" + "," + "extra 3" + "," + "extra 4");
            foreach (var r in adx_DMI)
            {
                double em = 0;
                EM.TryGetValue(r.TimeStamp, out em);

                double rs = 0;
                RSI_DIC.TryGetValue(r.TimeStamp, out rs);

                double rsslope = 0;
                RSI_SLOPE.TryGetValue(r.TimeStamp, out rsslope);

                int vol = 0;
                volume.TryGetValue(r.TimeStamp, out vol);

                double DI_up = 0;
                DMI_UP_DIC.TryGetValue(r.TimeStamp, out DI_up);

                double DI_Down = 0;
                DMI_Down_DIC.TryGetValue(r.TimeStamp, out DI_Down);

                Trade.Trigger Trig;
                trigger.TryGetValue(r.TimeStamp, out Trig);



                var topband = DI_up;
                var midband = em;
                var lowband = DI_Down;

                var b1     = rs;
                var adjVol = (vol == null) ? 0 : vol;
                var b2     = rsslope;;
                var b3     = 0;
                var b4     = 0;
                var trade  = Trig;

                var i = new IndicatorBasket()
                {
                    Action    = trade,
                    Timestamp = r.TimeStamp,
                    DI_DOWN   = DI_Down,
                    DI_UP     = DI_up,
                    Mama      = em,
                    RSI       = rs,
                    RSI_SLOPE = rsslope,
                    Price     = r.Price_Close,
                    Volume    = adjVol,
                };
                Indicators.Add(i);
                //   sw.WriteLine(trade + "," + r.TimeStamp + "," + r.Price_Close + "," + midband + "," + topband + "," + lowband + "," + adjVol + "," + b1 + "," + b2 + "," + b3 + "," + b4);
            }
            //   sw.Close();

            return(Indicators);
        }
        public static List<IndicatorBasket> CalcIndicators()
        {
         //   Console.WriteLine("Calculating Indicators...");
            var Indicators = new List<IndicatorBasket>();

            //Base Level Calcs
            var ohlc = Trades.Select(z => z.OHLC);
            var OHLC_LIST = ohlc.ToList();
            var volume = Trades.ToDictionary(x => x.TimeStamp, x => x.TradeVolume);
            var trigger = Trades.ToDictionary(x => x.TimeStamp, x => x.TradeTrigger);
            var EM = AlsiUtils.Factory_Indicator.createAdaptiveMA_MAMA(P.Mamapar1, P.Mamapar2, OHLC_LIST).ToDictionary(x => x.TimeStamp, x => x.Mama);
            var adx_DMI = AlsiUtils.Factory_Indicator.createADX(P.ADX_period, OHLC_LIST);
            var RSI = AlsiUtils.Factory_Indicator.createRSI(P.RSI_period, OHLC_LIST);


            //Second Level Clacs
            //EMA OF DMI+ and DMI-
            List<VariableIndicator> varDmiUP = new List<VariableIndicator>();
            List<VariableIndicator> varDmiDOWN = new List<VariableIndicator>();
            foreach (var f in adx_DMI)
            {
                var vdown = new VariableIndicator()
                    {
                        TimeStamp = f.TimeStamp,
                        Value = f.DI_Down,
                    };
                var vup = new VariableIndicator()
                {
                    TimeStamp = f.TimeStamp,
                    Value = f.DI_Up,
                };

                varDmiUP.Add(vup);
                varDmiDOWN.Add(vdown);
            }

            var Dmi_Up_EMA = Factory_Indicator.createEMA(P.Di_UP_avg_period, varDmiUP);
            var Dmi_Down_EMA = Factory_Indicator.createEMA(P.Di_DOWN_avg_period, varDmiDOWN);

            //EMA OF RSI
            List<VariableIndicator> varRsi = new List<VariableIndicator>();
            foreach (var r in RSI)
            {
                var rsi = new VariableIndicator()
                {
                    TimeStamp = r.TimeStamp,
                    Value = r.RSI,
                };
                varRsi.Add(rsi);
            }

            var RSI_EMA = Factory_Indicator.createEMA(P.RSI_EMA, varRsi);

            //slope OF RSI
            var rsi_slope = Factory_Indicator.createRegression(P.RSI_Slope_period, varRsi);

            //CREATE DICTIONARY 
            var DMI_UP_DIC = Dmi_Up_EMA.ToDictionary(x => x.TimeStamp, x => x.Ema);
            var DMI_Down_DIC = Dmi_Down_EMA.ToDictionary(x => x.TimeStamp, x => x.Ema);
            var RSI_DIC = RSI_EMA.ToDictionary(x => x.TimeStamp, x => x.Ema);
            var RSI_SLOPE = rsi_slope.ToDictionary(x => x.TimeStamp, x => x.Slope);



          //  var sw = new StreamWriter(@"d:\Indicators.csv");
           // sw.WriteLine("Trade" + "," + "Stamp" + "," + "PriceClose" + "," + "MAMA" + "," + "DI-UP" + "," + "DI-DOWN" + "," + "VOL" + "," + "RSI" + "," + "RSI SLOPE" + "," + "extra 3" + "," + "extra 4");
            foreach (var r in adx_DMI)
            {
                double em = 0;
                EM.TryGetValue(r.TimeStamp, out em);

                double rs = 0;
                RSI_DIC.TryGetValue(r.TimeStamp, out rs);

                double rsslope = 0;
                RSI_SLOPE.TryGetValue(r.TimeStamp, out rsslope);

                int vol = 0;
                volume.TryGetValue(r.TimeStamp, out vol);

                double DI_up = 0;
                DMI_UP_DIC.TryGetValue(r.TimeStamp, out DI_up);

                double DI_Down = 0;
                DMI_Down_DIC.TryGetValue(r.TimeStamp, out DI_Down);

                Trade.Trigger Trig;
                trigger.TryGetValue(r.TimeStamp, out Trig);



                var topband = DI_up;
                var midband = em;
                var lowband = DI_Down;

                var b1 = rs;
                var adjVol = (vol == null) ? 0 : vol;
                var b2 = rsslope; ;
                var b3 = 0;
                var b4 = 0;
                var trade = Trig;

                var i = new IndicatorBasket()
                {
                    Action = trade,
                    Timestamp = r.TimeStamp,
                    DI_DOWN = DI_Down,
                    DI_UP = DI_up,
                    Mama = em,
                    RSI = rs,
                    RSI_SLOPE = rsslope,
                    Price = r.Price_Close,
                    Volume = adjVol,

                };
                Indicators.Add(i);
             //   sw.WriteLine(trade + "," + r.TimeStamp + "," + r.Price_Close + "," + midband + "," + topband + "," + lowband + "," + adjVol + "," + b1 + "," + b2 + "," + b3 + "," + b4);
            }
         //   sw.Close();
           
            return Indicators;
        }