示例#1
0
        public List <Signal> GenerateOnClose(DateTime ts, int leadingIndex, SystemState systemState)
        {
            List <Signal> res = new List <Signal>();

            if (leadingIndex <= _statBB.BackBufferLength)
            {
                return(res);
            }

            StockPricesData data = _dataLoader.Get(_stock.FullName, _dataRange, 0, ts, ts);

            _currentTrend = BBTrendRecognizer.BBTrendRecognizer.RecognizeTrend(data, (StatBB)_statBB, leadingIndex, _currentTrend, out _);
            BBTrendExpectation expectation = BBTrendRecognizer.BBTrendRecognizer.GetExpectation(data, (StatBB)_statBB, leadingIndex, _currentTrend);

            if (systemState.PositionsActive.Count > 0)
            {
                if (systemState.PositionsActive.Count > 1)
                {
                    throw new Exception("More than 1 active position");
                }
                if ((expectation == BBTrendExpectation.DownAndFalling) || (expectation == BBTrendExpectation.DownButPossibleChange))
                {
                    systemState.PositionsActive[0].CloseMode = PositionCloseMode.OnOpen;
                }
            }
            else
            {
                if ((expectation == BBTrendExpectation.UpAndRaising) || (expectation == BBTrendExpectation.UpButPossibleChange))
                {
                    res.Add(CreateSignal(PositionDir.Long, systemState, data.C[leadingIndex]));
                }
            }

            return(res);
        }
        public override void Calculate(StockPricesData data)
        {
            HLChannelData res = HLChannel.Calculate(data.H, data.L, _statParams.Get(StatHLChannelParams.Period).As <int>());

            _data[StatHLChannelData.H] = res.H;
            _data[StatHLChannelData.L] = res.L;
        }
        public List <Signal> GenerateOnClose(DateTime ts, int leadingIndex, SystemState systemState)
        {
            List <Signal> res = new List <Signal>();

            if (leadingIndex <= _statSMA.BackBufferLength)
            {
                return(res);
            }

            StockPricesData data = _dataLoader.Get(_stock.FullName, _dataRange, 0, ts, ts);

            if ((data.C[leadingIndex - 1] <= _statSMA.Data(StatSMAData.SMA)[leadingIndex - 1 - _statSMA.BackBufferLength]) &&
                (data.C[leadingIndex] > _statSMA.Data(StatSMAData.SMA)[leadingIndex - _statSMA.BackBufferLength]))
            {
                res.Add(CreateSignal(PositionDir.Long, systemState, data.C[leadingIndex]));
            }

            if ((data.C[leadingIndex - 1] >= _statSMA.Data(StatSMAData.SMA)[leadingIndex - 1 - _statSMA.BackBufferLength]) &&
                (data.C[leadingIndex] < _statSMA.Data(StatSMAData.SMA)[leadingIndex - _statSMA.BackBufferLength]))
            {
                res.Add(CreateSignal(PositionDir.Short, systemState, data.C[leadingIndex]));
            }

            return(res);
        }
 private void ThrowIfOverlappingData(StockPricesData dataLeft, StockPricesData dataRight)
 {
     if (dataRight.TS[0] <= dataLeft.TS[dataLeft.Length - 1])
     {
         throw new Exception("Overlapping data");
     }
 }
 private void CheckMergedObject(StockPricesData merged, StockPricesData expected)
 {
     merged.Length.ShouldBe(expected.Length);
     merged.O.Length.ShouldBe(expected.O.Length);
     merged.H.Length.ShouldBe(expected.H.Length);
     merged.L.Length.ShouldBe(expected.L.Length);
     merged.C.Length.ShouldBe(expected.C.Length);
     merged.V.Length.ShouldBe(expected.V.Length);
     merged.TS.Length.ShouldBe(expected.TS.Length);
     for (int i = 0; i < merged.O.Length; i++)
     {
         merged.O[i].ShouldBe(expected.O[i]);
     }
     for (int i = 0; i < merged.H.Length; i++)
     {
         merged.H[i].ShouldBe(expected.H[i]);
     }
     for (int i = 0; i < merged.L.Length; i++)
     {
         merged.L[i].ShouldBe(expected.L[i]);
     }
     for (int i = 0; i < merged.C.Length; i++)
     {
         merged.C[i].ShouldBe(expected.C[i]);
     }
     for (int i = 0; i < merged.V.Length; i++)
     {
         merged.V[i].ShouldBe(expected.V[i]);
     }
     for (int i = 0; i < merged.TS.Length; i++)
     {
         merged.TS[i].ShouldBe(expected.TS[i]);
     }
 }
 /// <summary>
 /// Gets stockpricesdata and index of specified ts. Returns true if index found.
 /// </summary>
 /// <param name="dataLoader"></param>
 /// <param name="stockName"></param>
 /// <param name="dataRange"></param>
 /// <param name="ts"></param>
 /// <param name="spData"></param>
 /// <param name="dataIndex"></param>
 /// <returns></returns>
 public static bool GetWithIndex(this ISystemDataLoader dataLoader, string stockName, StockDataRange dataRange, DateTime ts,
                                 out StockPricesData spData, out int dataIndex)
 {
     spData    = dataLoader.Get(stockName, dataRange, 0, ts, ts);
     dataIndex = spData.FindByTS(ts);
     return(dataIndex >= 0);
 }
        public void GetPricesData_TSOutOfRange__GetsEmptyObject()
        {
            StockPricesData data = TestObj.GetPricesData(TestObj.GetStockDefinition(STOCKID_WIG), StockDataRange.Daily, 0, new DateTime(1980, 1, 1), new DateTime(1981, 1, 1));

            data.Range.ShouldBe(StockDataRange.Daily);
            data.IntrradayInterval.ShouldBe(0);
            data.Length.ShouldBe(0);
        }
示例#8
0
        public void FindByTSLE_ValueOnList__ReturnsItemIndex()
        {
            StockPricesData d = CreateTestObj();

            d.FindByTSLE(TestStartTS).ShouldBe(TESTDATALEN - 1, "last");
            d.FindByTSLE(TestStartTS.AddDays(-1)).ShouldBe(TESTDATALEN - 2, "mid");
            d.FindByTSLE(TestStartTS.AddDays(-2)).ShouldBe(TESTDATALEN - 3, "first");
        }
示例#9
0
        public override void Calculate(StockPricesData data)
        {
            BBData res = (new BB()).Calculate(data.C, _statParams.Get(StatBBParams.Period).As <int>(), _statParams.Get(StatBBParams.SigmaWidth).As <float>());

            _data[StatBBData.BBH] = res.BBH;
            _data[StatBBData.SMA] = res.SMA;
            _data[StatBBData.BBL] = res.BBL;
        }
 private void AddRightToLeft(StockPricesData dataResult, StockPricesData dataLeft, StockPricesData dataRight)
 {
     CopyBothToResult <float>(ref dataResult.O, ref dataLeft.O, ref dataRight.O);
     CopyBothToResult <float>(ref dataResult.H, ref dataLeft.H, ref dataRight.H);
     CopyBothToResult <float>(ref dataResult.L, ref dataLeft.L, ref dataRight.L);
     CopyBothToResult <float>(ref dataResult.C, ref dataLeft.C, ref dataRight.C);
     CopyBothToResult <Int64>(ref dataResult.V, ref dataLeft.V, ref dataRight.V);
     CopyBothToResult <DateTime>(ref dataResult.TS, ref dataLeft.TS, ref dataRight.TS);
 }
        public void MergeRightBeforeLeft()
        {
            StockPricesData d1       = CreateTestPricesObj();
            StockPricesData d2       = CreateTestPricesObj(-100);
            StockPricesData expected = new StockPricesData(2 * TESTDATALEN);

            CopyDataToExpected(d2, d1, expected);
            CheckMergedObject(TestObj.Merge(d1, d2), expected);
        }
 /// <summary>
 /// Gets stockpricesdata and index of specified ts. Returns true if index found, and has required length of back buffer.
 /// </summary>
 /// <param name="dataLoader"></param>
 /// <param name="stockName"></param>
 /// <param name="dataRange"></param>
 /// <param name="ts"></param>
 /// <param name="requiredBackBufferLength"></param>
 /// <param name="spData"></param>
 /// <param name="dataIndex"></param>
 /// <returns></returns>
 public static bool GetWithIndex(this ISystemDataLoader dataLoader, string stockName, StockDataRange dataRange, DateTime ts, int requiredBackBufferLength,
                                 out StockPricesData spData, out int dataIndex)
 {
     if (GetWithIndex(dataLoader, stockName, dataRange, ts, out spData, out dataIndex))
     {
         return(dataIndex >= requiredBackBufferLength);
     }
     return(false);
 }
示例#13
0
 public static void PrependStockStatData(this PriceVolumeChart chart, StockPricesData data, StockStat stat)
 {
     for (int i = 0; i < stat.DataCount; i++)
     {
         Series s = chart.GetSeries(stat.ChartSeriesName(i));
         s.Points.Clear();
     }
     AppendStockStatData(chart, data, stat);
 }
 private float CalcActive(SystemState system, DateTime ts, ISystemDataLoader dataLoader)
 {
     return(system.PositionsActive.Sum(p =>
     {
         StockPricesData prices = dataLoader.Get(p.Stock.FullName, p.DataRange, p.IntradayInterval, ts, ts);
         int ix = prices.FindByTS(ts);
         return p.DirectionMultiplier() * prices.C[ix] * p.Volume;
     }));
 }
示例#15
0
        private void ProcessConfiguredSystem(SystemConfiguration systemConfiguration, SystemState systemState)
        {
            StockPricesData leadingPricesData = GetLeadingData(systemConfiguration);

            var(from, to) = PricesDataRangeFinder.Find(leadingPricesData, systemConfiguration.tsFrom, systemConfiguration.tsTo);
            for (int i = from; i <= to; i++)
            {
                ProcessSingleTick(leadingPricesData, i, systemState);
            }
        }
示例#16
0
        public static StockPricesData CreatePricesData(float o, float h, float l, float c)
        {
            StockPricesData res = new StockPricesData(1);

            res.O[0] = o;
            res.H[0] = h;
            res.L[0] = l;
            res.C[0] = c;
            return(res);
        }
示例#17
0
        private StockPricesData GetLeadingData(SystemConfiguration systemConfiguration)
        {
            StockPricesData res = _dataLoader.Get(systemConfiguration.dataDefinition.stocks[0].stock.FullName, systemConfiguration.dataDefinition.stocks[0].dataRange, 0, systemConfiguration.tsFrom, systemConfiguration.tsTo);

            if (res.Length == 0)
            {
                throw new Exception("Leading prices data is empty.");
            }
            return(res);
        }
示例#18
0
 public void SetUp()
 {
     _statBBMock = new StatBBMock("");
     _statBBMock.SetParam(StatBBParams.Period, new MOParamInt()
     {
         Value = bbPeriod
     });
     _pricesData = new StockPricesData(bbPeriod + 1);
     _statBBMock.Calculate(_pricesData);
 }
 private void PrependData(StockPricesData newData)
 {
     using (new SuspendDrawingUpdate(chartPV))
     {
         chartPV.PrependStockData(newData);
         foreach (var stat in _currentData.Stats)
         {
             chartPV.PrependStockStatData(_currentData.Prices, stat);
         }
     }
 }
        public static (int from, int to) Find(StockPricesData leadingPricesData, DateTime tsFrom, DateTime tsTo)
        {
            int ixFrom = leadingPricesData.FindByTSGE(tsFrom);
            int ixTo   = leadingPricesData.FindByTSLE(tsTo);

            if ((ixFrom < 0) || (ixTo < 0))
            {
                throw new Exception("Configured dates out of range.");
            }
            return(ixFrom, ixTo);
        }
        private void SwapIfLeftAfterRight(ref StockPricesData dataLeft, ref StockPricesData dataRight)
        {
            if (dataRight.TS[0] > dataLeft.TS[dataLeft.Length - 1])
            {
                return;
            }
            StockPricesData t = dataLeft;

            dataLeft  = dataRight;
            dataRight = t;
        }
示例#22
0
 public static float OnStopHit(Position position, StockPricesData pricesData, int priceIndex)
 {
     if (((position.Direction == PositionDir.Long) && (pricesData.O[priceIndex] <= position.CloseModePrice)) ||
         ((position.Direction == PositionDir.Short) && (pricesData.O[priceIndex] >= position.CloseModePrice)))
     {
         return(pricesData.O[priceIndex]);
     }
     else
     {
         return(position.CloseModePrice);
     }
 }
        public StockPricesData ToStockPricesData()
        {
            StockPricesData data = new StockPricesData(_o.Count);

            Array.Copy(_o.ToArray(), data.O, data.Length);
            Array.Copy(_h.ToArray(), data.H, data.Length);
            Array.Copy(_l.ToArray(), data.L, data.Length);
            Array.Copy(_c.ToArray(), data.C, data.Length);
            Array.Copy(_v.ToArray(), data.V, data.Length);
            Array.Copy(_ts.ToArray(), data.TS, data.Length);
            return(data);
        }
示例#24
0
 public void SetUp()
 {
     _pricesData = new StockPricesData(DataRange);
     for (int i = 0; i < _pricesData.Length; i++)
     {
         _pricesData.O[i]  = DataRange;
         _pricesData.H[i]  = DataRange;
         _pricesData.L[i]  = DataRange;
         _pricesData.C[i]  = DataRange;
         _pricesData.TS[i] = StartDate.AddDays(i);
     }
 }
        public void GetPricesData_TSInRange__GetsData()
        {
            StockPricesData data = TestObj.GetPricesData(TestObj.GetStockDefinition(STOCKID_WIG), StockDataRange.Daily, 0, new DateTime(2017, 1, 1), new DateTime(2018, 1, 1));

            data.Range.ShouldBe(StockDataRange.Daily);
            data.IntrradayInterval.ShouldBe(0);
            data.Length.ShouldBeGreaterThan(0);
            for (int i = 1; i < data.Length; i++)
            {
                data.TS[i].ShouldBeGreaterThan(data.TS[i - 1]);
            }
        }
示例#26
0
 private void CheckCreatedObject(StockPricesData obj, int expectedLength)
 {
     obj.Range.ShouldBe(StockDataRange.Undefined);
     obj.IntrradayInterval.ShouldBe(0);
     obj.O.Length.ShouldBe(expectedLength);
     obj.H.Length.ShouldBe(expectedLength);
     obj.L.Length.ShouldBe(expectedLength);
     obj.C.Length.ShouldBe(expectedLength);
     obj.V.Length.ShouldBe(expectedLength);
     obj.TS.Length.ShouldBe(expectedLength);
     obj.Length.ShouldBe(expectedLength);
 }
 public static float OnPrice(Signal signal, StockPricesData pricesData, int priceIndex)
 {
     if (((signal.Direction == PositionDir.Long) && (pricesData.O[priceIndex] >= signal.Price)) ||
         ((signal.Direction == PositionDir.Short) && (pricesData.O[priceIndex] <= signal.Price)))
     {
         return(pricesData.O[priceIndex]);
     }
     else
     {
         return(signal.Price);
     }
 }
示例#28
0
        public static void PrependStockData(this PriceVolumeChart chart, StockPricesData data)
        {
            for (int i = 0; i < data.Length; i++)
            {
                chart.PricesCandles.Points.InsertXY(i, data.TS[i], data.H[i]);
                chart.PricesCandles.Points[i].YValues[1] = data.L[i];
                chart.PricesCandles.Points[i].YValues[2] = data.O[i];
                chart.PricesCandles.Points[i].YValues[3] = data.C[i];

                chart.PricesLine.Points.InsertXY(i, data.TS[i], data.C[i]);
                chart.Volume.Points.InsertXY(i, data.TS[i], data.V[i]);
            }
        }
        private bool LastDataTSNotBeforeSimulationEnds(string fundName, DateTime ts)
        {
            if (!_dataLoader.GetWithIndex(fundName, _dataRange, ts, out StockPricesData spData, out int dataIndex))
            {
                return(false);
            }
            if (dataIndex < spData.Length - 2)
            {
                return(true);
            }
            StockPricesData spFirstData = _dataLoader.Get(_fundsData.Stocks[0].FullName, _dataRange, 0, ts, ts);

            return(spData.TS[spData.Length - 1] == spFirstData.TS[spFirstData.Length - 1]);
        }
        private void btnPrependData_Click(object sender, EventArgs e)
        {
            if (OnPrependData == null)
            {
                return;
            }
            StockPricesData newData = OnPrependData.Invoke(_currentData);

            _currentData.Prices = _currentData.Prices.Merge(newData);
            RecalculateStats();
            PrependData(newData);
            chartPV.ResetZoom();
            DisplayCurrentStockInfo();
        }