예제 #1
0
        public List <PositionModel> RunBackTest(string ticker, SortedDictionary <DateTime, ConsolidatedStockModel> stockDictionary, DateTime from, DateTime to, int rsiLow, int rsiHigh, int stopLoss)
        {
            IDictionaryEnumerator myEnumerator =
                stockDictionary.GetEnumerator();


            List <PositionModel> positions = new List <PositionModel>();

            PositionModel openPosition = null;

            while (myEnumerator.MoveNext())
            {
                ConsolidatedStockModel model = (ConsolidatedStockModel)myEnumerator.Value;
                DateTime date = (DateTime)myEnumerator.Key;

                if (date >= from && date <= to)
                {
                    if (model.RsiLow <= rsiLow && openPosition == null && model.Sma200 <= model.Close)
                    {
                        openPosition = new PositionModel()
                        {
                            Open = model.Close, Size = 2000 / model.Close, OpenDate = date
                        };
                        Console.WriteLine($"Buy Date {date} Rsi Low {model.RsiLow} Open {openPosition.Open}");
                    }
                    if (model.RsiHigh >= rsiHigh && openPosition != null)
                    {
                        openPosition.Close     = model.Close;
                        openPosition.CloseDate = date;
                        positions.Add(new PositionModel()
                        {
                            Ticker = ticker, Open = openPosition.Open, Close = openPosition.Close, Size = openPosition.Size, OpenDate = openPosition.OpenDate, CloseDate = openPosition.CloseDate
                        });

                        Console.WriteLine($"Sell Date {date} Rsi Low {model.RsiLow} Profit {(openPosition.CloseValue - openPosition.OpenValue).ToString("N2")} Open {openPosition.Open} Close {openPosition.Close}");

                        openPosition = null;
                    }

                    if (stopLoss != 0 && openPosition != null && model.Close <= (openPosition.Open * (1 - ((double)stopLoss / 100))))
                    {
                        openPosition.Close     = model.Close;
                        openPosition.CloseDate = date;
                        openPosition.Stop      = true;
                        positions.Add(new PositionModel()
                        {
                            Ticker = ticker, Open = openPosition.Open, Close = openPosition.Close, Size = openPosition.Size, OpenDate = openPosition.OpenDate, CloseDate = openPosition.CloseDate, Stop = openPosition.Stop
                        });
                        Console.WriteLine($"Date {date} Rsi Low {model.RsiLow}");
                        Console.WriteLine($"Sell STOP LOSS Profit {(openPosition.CloseValue-openPosition.OpenValue).ToString("N2")}");
                        openPosition = null;
                    }
                }
            }

            return(positions);
        }
        public List <PositionModel> RunBackTest(string ticker, SortedDictionary <DateTime, ConsolidatedStockModel> stockDictionary, DateTime from, DateTime to, int rsiLow, int rsiHigh, int stopLoss)
        {
            IDictionaryEnumerator myEnumerator =
                stockDictionary.GetEnumerator();


            List <PositionModel> positions = new List <PositionModel>();

            PositionModel openPosition = null;

            while (myEnumerator.MoveNext())
            {
                ConsolidatedStockModel model = (ConsolidatedStockModel)myEnumerator.Value;
                DateTime date = (DateTime)myEnumerator.Key;

                if (date >= from && date <= to)
                {
                    if (model.RsiLow <= rsiLow && openPosition == null && model.Sma200 <= model.Close)
                    {
                        openPosition = new PositionModel()
                        {
                            Open = model.Close, Size = 2000 / model.Close, OpenDate = date, Ticker = ticker
                        };
                    }
                    if (model.RsiHigh >= rsiHigh && openPosition != null)
                    {
                        openPosition.PassedTest = true;
                    }

                    if (openPosition != null && openPosition.PassedTest && model.RsiHigh <= rsiHigh)
                    {
                        openPosition.Close     = model.Close;
                        openPosition.CloseDate = date;
                        positions.Add(new PositionModel()
                        {
                            Ticker = ticker, Open = openPosition.Open, Close = openPosition.Close, Size = openPosition.Size, OpenDate = openPosition.OpenDate, CloseDate = openPosition.CloseDate
                        });
                        openPosition = null;
                    }

                    if (stopLoss != 0 && openPosition != null && model.Close <= openPosition.Open * (1 - (double)stopLoss / 100))
                    {
                        openPosition.Close     = model.Close;
                        openPosition.CloseDate = date;
                        openPosition.Stop      = true;
                        positions.Add(new PositionModel()
                        {
                            Ticker = ticker, Open = openPosition.Open, Close = openPosition.Close, Size = openPosition.Size, OpenDate = openPosition.OpenDate, CloseDate = openPosition.CloseDate, Stop = openPosition.Stop
                        });
                        openPosition = null;
                    }
                }
            }


            return(positions);
        }
예제 #3
0
        public List <PositionModel> RunBackTest(string ticker, SortedDictionary <DateTime, ConsolidatedStockModel> stockDictionary, DateTime from, DateTime to, int rsiLow, int rsiHigh, int stopLoss)
        {
            IDictionaryEnumerator myEnumerator =
                stockDictionary.GetEnumerator();


            List <PositionModel> positions = new List <PositionModel>();

            PositionModel openPosition  = null;
            DateTime      lastStockDate = GetLastStockPostionDateInFile(stockDictionary, to);

            while (myEnumerator.MoveNext())
            {
                ConsolidatedStockModel model = (ConsolidatedStockModel)myEnumerator.Value;
                DateTime date = (DateTime)myEnumerator.Key;

                if (date >= from && date <= to)
                {
                    if (date.Date >= from.Date && openPosition == null)
                    {
                        openPosition = new PositionModel()
                        {
                            Open = model.Close, Size = 2000 / model.Close, OpenDate = date
                        };
                        Console.WriteLine($"Buy Date {date} Open {openPosition.Open}");
                    }


                    if (date.Date == lastStockDate && openPosition != null)
                    {
                        openPosition.Close     = model.Close;
                        openPosition.CloseDate = date;
                        positions.Add(new PositionModel()
                        {
                            Ticker = ticker, Open = openPosition.Open, Close = openPosition.Close, Size = openPosition.Size, OpenDate = openPosition.OpenDate, CloseDate = openPosition.CloseDate
                        });

                        Console.WriteLine($"Sell Date {date} Force Close End Of Period. Profit {(openPosition.CloseValue - openPosition.OpenValue).ToString("N2")} Open {openPosition.Open} Close {openPosition.Close}");

                        openPosition = null;
                    }
                }
            }

            return(positions);
        }
예제 #4
0
        private SortedDictionary <DateTime, ConsolidatedStockModel> GetData(string alphaKey, string ticker)
        {
            List <PriceModel> alphaPriceResult   = new PriceQuery().Get(alphaKey, ticker);
            List <RsiModel>   alphaRsiResult     = new RsiQuery().Get(alphaKey, ticker, 4, "low");
            List <RsiModel>   alphaRsiResultHigh = new RsiQuery().Get(alphaKey, ticker, 4, "high");
            List <SmaModel>   alphaSmaResult     = new SmaQuery().Get(alphaKey, ticker, 200);

            SortedDictionary <DateTime, ConsolidatedStockModel> stockDictionary = new SortedDictionary <DateTime, ConsolidatedStockModel>();

            foreach (RsiModel rsiModel in alphaRsiResult)
            {
                ConsolidatedStockModel consolidatedStock;
                if (!stockDictionary.ContainsKey(rsiModel.Time))
                {
                    consolidatedStock        = new ConsolidatedStockModel();
                    consolidatedStock.RsiLow = rsiModel.Rsi;
                    stockDictionary.Add(rsiModel.Time, consolidatedStock);
                }
                else
                {
                    stockDictionary[rsiModel.Time].RsiLow = rsiModel.Rsi;
                }
            }


            foreach (RsiModel rsiModel in alphaRsiResultHigh)
            {
                ConsolidatedStockModel consolidatedStock;
                if (!stockDictionary.ContainsKey(rsiModel.Time))
                {
                    consolidatedStock         = new ConsolidatedStockModel();
                    consolidatedStock.RsiHigh = rsiModel.Rsi;
                    stockDictionary.Add(rsiModel.Time, consolidatedStock);
                }
                else
                {
                    stockDictionary[rsiModel.Time].RsiHigh = rsiModel.Rsi;
                }
            }

            foreach (SmaModel smaModel in alphaSmaResult)
            {
                ConsolidatedStockModel consolidatedStock;
                if (!stockDictionary.ContainsKey(smaModel.Time))
                {
                    consolidatedStock        = new ConsolidatedStockModel();
                    consolidatedStock.Sma200 = smaModel.Sma;
                    stockDictionary.Add(smaModel.Time, consolidatedStock);
                }
                else
                {
                    stockDictionary[smaModel.Time].Sma200 = smaModel.Sma;
                }
            }

            foreach (PriceModel priceModel in alphaPriceResult)
            {
                ConsolidatedStockModel consolidatedStock;
                if (!stockDictionary.ContainsKey(priceModel.Time))
                {
                    consolidatedStock       = new ConsolidatedStockModel();
                    consolidatedStock.Close = priceModel.Close;
                    stockDictionary.Add(priceModel.Time, consolidatedStock);
                }
                else
                {
                    stockDictionary[priceModel.Time].Close = priceModel.Close;
                }
            }

            return(stockDictionary);
        }
        public List <PositionModel> RunBackTest(string ticker, SortedDictionary <DateTime, ConsolidatedStockModel> stockDictionary, DateTime from, DateTime to, int rsiLow, int rsiHigh, int stopLoss)
        {
            IDictionaryEnumerator myEnumerator =
                stockDictionary.GetEnumerator();


            List <PositionModel> positions = new List <PositionModel>();

            PositionModel openPosition = null;
            bool          rsiDropped   = false;

            while (myEnumerator.MoveNext())
            {
                ConsolidatedStockModel currentStock = (ConsolidatedStockModel)myEnumerator.Value;
                DateTime date = (DateTime)myEnumerator.Key;

                if (date >= from && date <= to)
                {
                    if (currentStock.RsiLow <= rsiLow && openPosition == null && currentStock.Sma200 <= currentStock.Close)
                    {
                        rsiDropped = true;
                    }

                    if (rsiDropped && currentStock.RsiLow >= rsiLow && openPosition == null)
                    {
                        rsiDropped = false;
                        // open position
                        openPosition = new PositionModel()
                        {
                            Open = currentStock.Close, Size = 2000 / currentStock.Close, OpenDate = date, Ticker = ticker
                        };
                    }

                    if (openPosition != null && currentStock.RsiHigh >= rsiHigh)
                    {
                        // close position
                        openPosition.Close     = currentStock.Close;
                        openPosition.CloseDate = date;
                        positions.Add(new PositionModel()
                        {
                            Ticker = ticker, Open = openPosition.Open, Close = openPosition.Close, Size = openPosition.Size, OpenDate = openPosition.OpenDate, CloseDate = openPosition.CloseDate
                        });
                        openPosition = null;
                    }

                    if (stopLoss != 0 && openPosition != null && currentStock.Close <= openPosition.Open * (1 - (double)stopLoss / 100))
                    {
                        // close position
                        openPosition.Close     = currentStock.Close;
                        openPosition.CloseDate = date;
                        openPosition.Stop      = true;
                        positions.Add(new PositionModel()
                        {
                            Ticker = ticker, Open = openPosition.Open, Close = openPosition.Close, Size = openPosition.Size, OpenDate = openPosition.OpenDate, CloseDate = openPosition.CloseDate, Stop = openPosition.Stop
                        });
                        openPosition = null;
                    }
                }
            }


            return(positions);
        }