Esempio n. 1
0
        public void Update(Prices prices)
        {
            var closes = prices[Price.Close];

            if (closes.Count >= Period)
            {
                int start      = closes.Count - Period;
                var lastCloses = closes.GetRange(start, Period);
                var avg        = lastCloses.Average();
                var meanChange = closes.Last() * MEAN_CHANGE;
                UpperBand.Add(avg + meanChange);
                LowerBand.Add(avg - meanChange);
            }
        }
Esempio n. 2
0
        public void Update(Prices prices)
        {
            var closes = prices[Price.Close];

            if (closes.Count >= Period)
            {
                int start      = closes.Count - Period;
                var lastCloses = closes.GetRange(start, Period);
                var avg        = lastCloses.Average();
                var variance   = lastCloses.Sum(p => p * p) / Period - avg * avg;
                var stdev      = Math.Sqrt(variance);
                UpperBand.Add(avg + Width * stdev);
                LowerBand.Add(avg - Width * stdev);
            }
        }
Esempio n. 3
0
        public void Update(Prices prices)
        {
            var closes = prices[Price.Close];

            if (closes.Count >= Period + 1)
            {
                int start      = closes.Count - Period - 1;
                var lastLows   = prices[Price.Low].GetRange(start, Period + 1);
                var lastHighs  = prices[Price.High].GetRange(start, Period + 1);
                var lastCloses = closes.GetRange(start, Period + 1);
                var avg        = lastCloses.Skip(1).Average();
                var ranges     = new List <double>();
                for (int i = 0; i < lastCloses.Count; i++)
                {
                    var lowRange  = Math.Abs(lastCloses[i] - lastLows[i]);
                    var highRange = Math.Abs(lastCloses[i] - lastHighs[i]);
                    var range     = Math.Max(lowRange, highRange);
                    ranges.Add(range);
                }
                var atr = ranges.Average();
                UpperBand.Add(avg + Width * atr);
                LowerBand.Add(avg - Width * atr);
            }
        }
Esempio n. 4
0
        public SimulationResult Simulate(DateTime start, DateTime end, int interval, StrategyParameters parameters)
        {
            //Read the OHLC from database
            var connectionName   = ConfigurationManager.AppSettings["ConnectionName"];
            var connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;

            List <OHLC> ohlcList = new List <OHLC>();

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand("", connection))
                    {
                        command.CommandType = System.Data.CommandType.Text;
                        command.CommandText = "SELECT o.Id, o.[Open], o.High, o.Low, o.[Close], Start, Volume FROM dbo.OHLC o " +
                                              "WHERE o.[Start] >= @start AND o.[End] < @end ORDER BY o.Id ASC";
                        command.Parameters.Add(new SqlParameter("start", start));
                        command.Parameters.Add(new SqlParameter("end", end));

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                var ohlc = new OHLC();

                                ohlc.Open  = reader.GetDecimal(1);
                                ohlc.High  = reader.GetDecimal(2);
                                ohlc.Low   = reader.GetDecimal(3);
                                ohlc.Close = reader.GetDecimal(4);
                                ohlc.Start = reader.GetDateTime(5);
                                var volume = reader.GetDecimal(6);
                                ohlc.Volume = (volume * 1000);

                                ohlcList.Add(ohlc);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            //Set the trading state
            State   = TradingState.Initial;
            Orders  = new List <Order>();
            Account = new Account()
            {
                Euro = 10000
            };

            //l h o c

            //Find the first trade
            //Find all trades within the next 5 minutes
            //If there are any trade
            //Calculate low, high, open, close
            if (ohlcList.Any())
            {
                start = ohlcList.First().Start > start?ohlcList.First().Start : start;

                for (DateTime i = start; i < end; i = i.AddMinutes(interval))
                {
                    var windowStart = i;
                    var windowEnd   = windowStart.AddMinutes(interval);

                    var ohlcInTheSameWindow = ohlcList.Where(o => o.Start >= windowStart && o.Start < windowEnd).ToList();

                    if (ohlcInTheSameWindow.Any())
                    {
                        var low    = ohlcInTheSameWindow.Select(t => t.Low).Min();
                        var high   = ohlcInTheSameWindow.Select(t => t.High).Max();
                        var open   = ohlcInTheSameWindow.First().Open;
                        var close  = ohlcInTheSameWindow.Last().Close;
                        var volume = ohlcInTheSameWindow.Sum(t => t.Volume);

                        var ohlc = new OHLC {
                            Open = open, High = high, Low = low, Close = close, Volume = volume, Start = windowStart, End = windowEnd
                        };

                        DataPoints.Insert(0, ohlc);

                        //Check for indicators and make trading decisions
                        DonchienBreakoutStrategy(ohlc, windowEnd, parameters);

                        //Add Donchian Channels
                        var dca = CalculateDonchianChannel(55);

                        UpperBand.Add((double)dca.UpperBand);
                        LowerBand.Add((double)dca.LowerBand);
                    }
                }
            }

            //Stats after simulation
            var stats = new Stats(Orders, Account, DataPoints)
            {
                Market = DataPoints.First().Close - DataPoints.Last().Close,
                MarketRiskAdjustedReturn = (double)((DataPoints.First().Close - DataPoints.Last().Close) / DataPoints.Last().Close) * 100,
                Target = (end - start).Days * 80, //80 EUR profit per day
            };

            return(new SimulationResult()
            {
                Dates = DataPoints.OrderBy(dp => dp.Start).Select(dp => dp.End).ToList(),
                Values = DataPoints.OrderBy(dp => dp.Start).Select(dp => dp.Close).ToList(),
                Volumes = DataPoints.OrderBy(dp => dp.Start).Select(dp => dp.Volume).ToList(),
                Upper = UpperBand,
                Lower = LowerBand,
                Orders = Orders,
                Events = Events,
                Stats = stats
            });
        }