Ejemplo n.º 1
0
        private string DownloadFileDaily(StockDefinition stockDefinition)
        {
            string res = _dataFileDownloader.InitializeDownload(stockDefinition, DataPumpDownloadRange.Daily);

            _dataFileDownloader.WaitForDownload(res);
            return(res);
        }
Ejemplo n.º 2
0
        public DateTime GetNearestTickGETicksBefore(StockDefinition stockDef, StockDataRange dataRange, int intradayInterval, DateTime ts, int ticksBefore)
        {
            DateTime res = DateTime.MinValue;

            string qry =
                "select min(ts), count(*) " +
                "from " +
                "( " +
                "select tsWindow.ts " +
                "from " +
                "( " +
                "select min(ts) as \"mints\" " +
                $"from {_tblSelector.GetTableName(stockDef.Type, dataRange, intradayInterval)} " +
                $"where fk_id_spolki = {stockDef.ID} " +
                $"and ts >= {ts.ToTimestampQueryValue()} " +
                $") tsNearest, {_tblSelector.GetTableName(stockDef.Type, dataRange, intradayInterval)} tsWindow " +
                $"where tsWindow.fk_id_spolki = {stockDef.ID} and tsWindow.ts < tsNearest.mints " +
                "order by tsWindow.ts desc " +
                $"limit {ticksBefore} " +
                ") t";

            ProcessSelectQuery(qry, (reader) =>
            {
                reader.Read();
                if ((!reader.IsDBNull(0)) && (reader.GetFieldValue <int>(1) == ticksBefore))
                {
                    res = reader.GetFieldValue <DateTime>(0);
                }
            });
            return(res);
        }
Ejemplo n.º 3
0
        private void ProcessFileDaily(string fileName, StockDefinition stockDefinition)
        {
            DataPumpStockData data = new DataPumpStockData();

            _stockDataToDBWriter.StartSession();
            try
            {
                _dataFileIterator.Open(fileName);
                try
                {
                    if (!_dataFileIterator.SetOnLineAfterTS(_dataPumpProvider.GetMaxTS(stockDefinition, StockDataRange.Daily, 0)))
                    {
                        return;
                    }
                    while (!_dataFileIterator.Eof())
                    {
                        string currLine = _dataFileIterator.ReadLine();
                        _lineToStockData.Map(currLine, _dataFileIterator.PreviousLine(), data);
                        _stockDataToDBWriter.WriteDaily(data, stockDefinition);
                    }
                }
                finally
                {
                    _dataFileIterator.Close();
                }
            } finally
            {
                _stockDataToDBWriter.EndSession();
            }
        }
Ejemplo n.º 4
0
        public void InitializeDownload_Ticks__ThrowsNotImplemented()
        {
            StockDefinition stock = new StockDefinition()
            {
                ID = 793, Type = StockType.NBPCurrency, FullName = "USD"
            };

            Should.Throw <NotImplementedException>(() => TestObj.InitializeDownload(stock, DataPumpDownloadRange.Ticks));
        }
 public static void ToStockDefinition(NpgsqlDataReader reader, StockDefinition data)
 {
     data.ID            = reader.GetFieldValue <int>(reader.GetOrdinal("id"));
     data.Type          = (StockType)reader.GetFieldValue <int>(reader.GetOrdinal("stock_type"));
     data.Enabled       = reader.GetFieldValue <bool>(reader.GetOrdinal("enabled"));
     data.FullName      = reader.GetFieldValue <string>(reader.GetOrdinal("stock_fullname"));
     data.Name          = reader.GetFieldValue <string>(reader.GetOrdinal("stock_name"));
     data.FileNameDaily = reader.GetFieldValue <string>(reader.GetOrdinal("filename_daily"));
 }
        public void Prepare_Daily__PreparesPath()
        {
            StockDefinition stock = new StockDefinition()
            {
                ID = 1, Type = StockType.Stock, FullName = "teststock"
            };

            TestObj.Prepare(stock, DataPumpDownloadRange.Daily).ShouldBe(_rootPath + "\\" + "mstall.zip");
        }
        public void Prepare_Ticks__ThrowsNotImplemented()
        {
            StockDefinition stock = new StockDefinition()
            {
                ID = 1, Type = StockType.Stock, FullName = "teststock"
            };

            Should.Throw <NotImplementedException>(() => TestObj.Prepare(stock, DataPumpDownloadRange.Ticks));
        }
Ejemplo n.º 8
0
        public void Prepare_Daily__PreparesPath()
        {
            StockDefinition stock = new StockDefinition()
            {
                ID = 1, Type = StockType.Stock, FullName = "teststock"
            };

            CheckPreparedPathDaily(TestObj.Prepare(stock, DataPumpDownloadRange.Daily), stock);
        }
Ejemplo n.º 9
0
        public async Task <ActionResult <decimal> > GetStockValue(string ticker)
        {
            StockDefinition stock = await _context.StockDefinitions.FindAsync(ticker);

            if (stock == null)
            {
                return(NotFound($"Could not find ticker {ticker}"));
            }

            return(stock.Current_Value);
        }
 private void CheckOpenedPosition(Position pos, StockDefinition stock, PositionDir dir, float open, float vol, float commission, DateTime ts, StockDataRange range, int interval)
 {
     pos.Stock.ShouldBe(stock);
     pos.Direction.ShouldBe(dir);
     pos.TSOpen.ShouldBe(ts);
     pos.Open.ShouldBe(open);
     pos.OpenCommission.ShouldBe(commission);
     pos.Volume.ShouldBe(vol);
     pos.DataRange.ShouldBe(range);
     pos.IntradayInterval.ShouldBe(interval);
     pos.EntrySignal.ShouldBe(EntrySignal);
 }
        public string Prepare(StockDefinition stockDefinition, DataPumpDownloadRange downloadRange)
        {
            switch (downloadRange)
            {
            case DataPumpDownloadRange.Daily:
                return(GetPathDaily(stockDefinition));

            case DataPumpDownloadRange.Ticks:
                return(GetPathTicks(stockDefinition));
            }
            return("");
        }
Ejemplo n.º 12
0
        private void GenerateData(StockDefinition stockDefinition, StockDataRange generateRange, string dataRange)
        {
            DateTime tsFrom    = _provider.GetMaxTS(stockDefinition, generateRange, 0);
            string   destTable = _provider.GetTableName(stockDefinition.Type, generateRange, 0);
            string   srcTable  = _provider.GetTableName(stockDefinition.Type, StockDataRange.Daily, 0);

            if (tsFrom != DateTime.MinValue)
            {
                DeleteLastTSRow(stockDefinition.ID, destTable, tsFrom);
            }
            InsertAllRows(stockDefinition.ID, destTable, srcTable, dataRange, tsFrom);
        }
Ejemplo n.º 13
0
        public string InitializeDownload(StockDefinition stockDefinition, DataPumpDownloadRange downloadRange)
        {
            string downloadFilePath = GetDownloadFilePath(stockDefinition, downloadRange);
            string unzipPath        = GetUnzipPath(downloadFilePath);
            string downloadUrl      = GetDownloadUrl(stockDefinition, downloadRange);

            if (!AlreadyDownloaded(downloadUrl))
            {
                DownloadAndProcess(downloadUrl, downloadFilePath, unzipPath);
            }

            return(downloadFilePath);
        }
Ejemplo n.º 14
0
 private bool GetStockDefinition(string stockName, out StockDefinition stockDef)
 {
     stockDef = null;
     try
     {
         stockDef = _dataProvider.GetStockDefinition(stockName);
         return(true);
     } catch (Exception e)
     {
         _msgDisplay.Error(e.Message);
         return(false);
     }
 }
        public void GetStockDefinition_ExistingName__GetsData()
        {
            StockDefinition data = TestObj.GetStockDefinition("TRITON");

            data.ID.ShouldBe(1);
            data.FullName.ShouldBe("TRITON");
            data.Type.ShouldBe(StockType.Stock);

            data = TestObj.GetStockDefinition(STOCKNAME_WIG);
            data.ID.ShouldBe(STOCKID_WIG);
            data.FullName.ShouldBe(STOCKNAME_WIG);
            data.Type.ShouldBe(StockType.Index);
        }
Ejemplo n.º 16
0
        public void InitializeDownload_Daily__DownloadsAndUnzipsFile()
        {
            StockDefinition stock = new StockDefinition()
            {
                ID = 793, Type = StockType.NBPCurrency, FullName = "USD"
            };
            string downloadFilePath = TestObj.InitializeDownload(stock, DataPumpDownloadRange.Daily);

            downloadFilePath.ShouldBe(Path.Combine(_rootPath, ZipMstnbp));
            File.Exists(downloadFilePath).ShouldBeTrue();
            Directory.Exists(Path.Combine(_rootPath, "mstnbp")).ShouldBeTrue();
            File.Exists(Path.Combine(_rootPath, "mstnbp", stock.FullName + ".mst")).ShouldBeTrue();
        }
Ejemplo n.º 17
0
 private void ProcessSingleStock(StockDefinition stockDefinition, Action <StockDefinition> generateOp)
 {
     _processingInfo.Stock = stockDefinition;
     _processingInfo.CurrentPosition++;
     _onStockStartProcessing?.Invoke(_processingInfo);
     try
     {
         generateOp(stockDefinition);
     }
     catch (Exception e)
     {
         ExecuteStockProcessingException(e);
     }
 }
Ejemplo n.º 18
0
 private void TestDailyFor20191104(StockDefinition stock, DateTime initialDateTime, List <string> expectedFirst5Inserts)
 {
     expectedFirst5Inserts.Count.ShouldBe(5);
     _executedQueries.Clear();
     _dataPumpProvider.GetMaxTS(Arg.Compat.Any <StockDefinition>(), StockDataRange.Daily, 0).Returns(initialDateTime);
     TestObj.PumpDaily(stock);
     Console.WriteLine($"{stock.FullName}:");
     Console.WriteLine(string.Join(Environment.NewLine, _executedQueries.ToArray()));
     _executedQueries.Count.ShouldBeGreaterThanOrEqualTo(5);
     for (int i = 0; i < 5; i++)
     {
         _executedQueries[i].ShouldBe(expectedFirst5Inserts[i]);
     }
 }
        public SignalsPriceCrossingSMA(string stockName, StockDataRange dataRange, int smaPeriod, ISystemDataLoader dataLoader, IStockDataProvider dataProvider, IMMSignalVolume signalVolumeCalculator)
        {
            _dataRange              = dataRange;
            _smaPeriod              = smaPeriod;
            _dataLoader             = dataLoader;
            _dataProvider           = dataProvider;
            _signalVolumeCalculator = signalVolumeCalculator;

            _stock   = _dataProvider.GetStockDefinition(stockName);
            _statSMA = new StatSMA("")
                       .SetParam(StatSMAParams.Period, new MOParamInt()
            {
                Value = _smaPeriod
            });
        }
 public void SetUp()
 {
     _stock = new StockDefinition()
     {
         Type = StockType.Stock
     };
     _stock2  = new StockDefinition();
     _testObj = new SystemState()
     {
         Cash = CashValue
     };
     _stockPrices      = new StockPricesData(1);
     _dataLoader       = SystemDataLoaderUtils.CreateSubstitute(_stockPrices);
     _commission       = CommissionUtils.CreateSubstitute(Commission);
     _slippage         = SlippageUtils.CreateSusbstitute();
     EntrySignal.Stock = _stock;
 }
Ejemplo n.º 21
0
        private void TestDailyKGHMFor20191104(DateTime initialDateTime)
        {
            StockDefinition kghm = new StockDefinition()
            {
                ID = 125, FullName = "KGHM", Type = StockType.Stock, FileNameDaily = "KGHM"
            };
            List <string> expectedInserts = new List <string>()
            {
                "insert into at_dzienne0(fk_id_spolki, ts, open, high, low, close, refcourse, volume) values (125, to_date('2019-11-04', 'YYYY-MM-DD'), 85.3200, 89.0000, 85.1200, 88.9200, 83.6400, 974976)",
                "insert into at_dzienne0(fk_id_spolki, ts, open, high, low, close, refcourse, volume) values (125, to_date('2019-11-05', 'YYYY-MM-DD'), 89.9000, 92.6200, 89.3800, 91.6400, 88.9200, 962176)",
                "insert into at_dzienne0(fk_id_spolki, ts, open, high, low, close, refcourse, volume) values (125, to_date('2019-11-06', 'YYYY-MM-DD'), 90.6400, 91.5400, 90.3000, 91.0000, 91.6400, 396956)",
                "insert into at_dzienne0(fk_id_spolki, ts, open, high, low, close, refcourse, volume) values (125, to_date('2019-11-07', 'YYYY-MM-DD'), 91.6000, 96.7000, 91.6000, 96.7000, 91.0000, 1425770)",
                "insert into at_dzienne0(fk_id_spolki, ts, open, high, low, close, refcourse, volume) values (125, to_date('2019-11-08', 'YYYY-MM-DD'), 95.5000, 96.9400, 94.6200, 96.4000, 96.7000, 897528)"
            };

            TestDailyFor20191104(kghm, initialDateTime, expectedInserts);
        }
Ejemplo n.º 22
0
        public StockDefinition GetStockDefinition(int stockID)
        {
            StockDefinition res = new StockDefinition();

            string qry = $"select * from at_spolki2 where id={stockID}";

            ProcessSelectQuery(qry, (reader) =>
            {
                if (!reader.HasRows)
                {
                    throw new Exception($"No data for stock id={stockID}");
                }
                reader.Read();
                PgDataToStockDefinitionConverter.ToStockDefinition(reader, res);
            });
            return(res);
        }
Ejemplo n.º 23
0
        public DateTime GetNearestTickGE(StockDefinition stockDef, StockDataRange dataRange, int intradayInterval, DateTime ts)
        {
            DateTime res = DateTime.MinValue;

            string qry = $"select min(ts) from {_tblSelector.GetTableName(stockDef.Type, dataRange, intradayInterval)} where fk_id_spolki={stockDef.ID} and ts >= {ts.ToTimestampQueryValue()}";

            ProcessSelectQuery(qry, (reader) =>
            {
                reader.Read();
                if (reader.IsDBNull(0))
                {
                    throw new Exception($"No nearest tick data for stock name = {stockDef.FullName}");
                }
                res = reader.GetFieldValue <DateTime>(0);
            });
            return(res);
        }
Ejemplo n.º 24
0
        private void TestDailyWIGFor20191104(DateTime initialDateTime)
        {
            StockDefinition wig = new StockDefinition()
            {
                ID = 288, FullName = "WIG", Type = StockType.Index, FileNameDaily = "WIG"
            };
            List <string> expectedInserts = new List <string>()
            {
                "insert into at_dzienne1(fk_id_spolki, ts, open, high, low, close, refcourse, volume) values (288, to_date('2019-11-04', 'YYYY-MM-DD'), 58294.4300, 59326.5200, 58250.4600, 59326.1300, 57783.0200, 1037276.603)",
                "insert into at_dzienne1(fk_id_spolki, ts, open, high, low, close, refcourse, volume) values (288, to_date('2019-11-05', 'YYYY-MM-DD'), 59352.2000, 59651.5200, 59234.5300, 59517.7000, 59326.1300, 816794.889)",
                "insert into at_dzienne1(fk_id_spolki, ts, open, high, low, close, refcourse, volume) values (288, to_date('2019-11-06', 'YYYY-MM-DD'), 59378.5200, 59523.8000, 59174.2900, 59174.2900, 59517.7000, 637393.894)",
                "insert into at_dzienne1(fk_id_spolki, ts, open, high, low, close, refcourse, volume) values (288, to_date('2019-11-07', 'YYYY-MM-DD'), 59447.2500, 59612.6900, 59376.4500, 59492.2200, 59174.2900, 845697.212)",
                "insert into at_dzienne1(fk_id_spolki, ts, open, high, low, close, refcourse, volume) values (288, to_date('2019-11-08', 'YYYY-MM-DD'), 59373.7000, 59407.0900, 59127.9800, 59191.7100, 59492.2200, 901038.557)"
            };

            TestDailyFor20191104(wig, initialDateTime, expectedInserts);
        }
Ejemplo n.º 25
0
        public StockDefinition GetStockDefinition(string stockName)
        {
            StockDefinition res = new StockDefinition();

            string qry = $"select * from at_spolki2 where stock_fullname='{stockName}' or stock_name='{stockName.ToUpper()}'";

            ProcessSelectQuery(qry, (reader) =>
            {
                if (!reader.HasRows)
                {
                    throw new Exception($"No data for stock name = {stockName}");
                }
                reader.Read();
                PgDataToStockDefinitionConverter.ToStockDefinition(reader, res);
            });
            return(res);
        }
Ejemplo n.º 26
0
        public SignalsBBTrend(string stockName, StockDataRange dataRange, int bbPeriod, float bbSigmaWidth, ISystemDataLoader dataLoader, IStockDataProvider dataProvider, IMMSignalVolume signalVolumeCalculator)
        {
            _dataRange              = dataRange;
            _bbPeriod               = bbPeriod;
            _bbSigmaWidth           = bbSigmaWidth;
            _dataLoader             = dataLoader;
            _dataProvider           = dataProvider;
            _signalVolumeCalculator = signalVolumeCalculator;

            _stock  = _dataProvider.GetStockDefinition(stockName);
            _statBB = new StatBB("")
                      .SetParam(StatBBParams.Period, new MOParamInt()
            {
                Value = _bbPeriod
            })
                      .SetParam(StatBBParams.SigmaWidth, new MOParamFloat()
            {
                Value = bbSigmaWidth
            });
        }
Ejemplo n.º 27
0
        public DateTime GetMaxTS(StockDefinition stockDefinition, StockDataRange dataRange, int intradayInterval)
        {
            DateTime res = DateTime.MinValue;

            string qry = $"select max(ts) from {GetTableName(stockDefinition.Type, dataRange, intradayInterval)} where fk_id_spolki={stockDefinition.ID}";

            ProcessSelectQuery(qry, (reader) =>
            {
                if (!reader.HasRows)
                {
                    return;
                }
                reader.Read();
                if (!reader.IsDBNull(0))
                {
                    res = reader.GetFieldValue <DateTime>(0);
                }
            });
            return(res);
        }
Ejemplo n.º 28
0
        public static void Open(this SystemState systemState, StockDefinition stock, PositionDir dir, DateTime ts, float price, float volume, float commission, StockDataRange dataRange, int intradayInterval, Signal entrySignal)
        {
            Position pos = new Position
            {
                Stock            = stock,
                DataRange        = dataRange,
                IntradayInterval = intradayInterval,
                Direction        = dir,
                Open             = price,
                OpenCommission   = commission,
                TSOpen           = ts,
                Volume           = volume,
                EntrySignal      = entrySignal,
                TicksActive      = 1
            };

            systemState.PositionsActive.Add(pos);
            systemState.Cash -= pos.DirectionMultiplier() * pos.OpenValue();
            systemState.Cash -= pos.OpenCommission;
        }
Ejemplo n.º 29
0
        public List <StockDefinition> GetAllStockDefinitions()
        {
            List <StockDefinition> res = new List <StockDefinition>();

            string qry = $"select * from at_spolki2";

            ProcessSelectQuery(qry, (reader) =>
            {
                if (!reader.HasRows)
                {
                    return;
                }
                while (reader.Read())
                {
                    StockDefinition def = new StockDefinition();
                    PgDataToStockDefinitionConverter.ToStockDefinition(reader, def);
                    res.Add(def);
                }
            });
            return(res);
        }
Ejemplo n.º 30
0
        public StockPricesData GetPricesData(StockDefinition stockDef, StockDataRange dataRange, int intradayInterval, DateTime tsFrom, DateTime tsTo)
        {
            PricesTemporalData tmp = new PricesTemporalData();

            string qry = $"select open, high, low, close, volume, ts from {_tblSelector.GetTableName(stockDef.Type, dataRange, intradayInterval)} where fk_id_spolki={stockDef.ID} and ts >= {tsFrom.ToTimestampQueryValue()} and ts <= {tsTo.ToTimestampQueryValue()} order by ts";

            ProcessSelectQuery(qry, (reader) =>
            {
                if (!reader.HasRows)
                {
                    return;
                }
                tmp.AddAllRecords(reader);
            });

            StockPricesData res = tmp.ToStockPricesData();

            res.Range             = dataRange;
            res.IntrradayInterval = intradayInterval;
            return(res);
        }