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 void GetExpectation__RecognizesCorrectly(float currSMA, float currC, BBTrendType currTrend, BBTrendExpectation expectedTrend) { _statBBMock.SMA[1] = currSMA; _pricesData.C[bbPeriod] = currC; SystemDefs.BBTrendRecognizer.BBTrendRecognizer.GetExpectation(_pricesData, _statBBMock, bbPeriod, currTrend).ShouldBe(expectedTrend); }
[TestCase(104.96f, 114.14f, 123.32f, 106.44f, 113.22f, 119.99f, 108.88f, 105.22f, 113.05f, 114.64f, BBTrendType.Up, BBTrendType.Down)] //test cases from stock data public void RecognizeTrend__RecognizesCorrectly(float prevBBL, float prevSMA, float prevBBH, float currBBL, float currSMA, float currBBH, float prevL, float currL, float prevH, float currH, BBTrendType currTrend, BBTrendType expectedTrend) { _statBBMock.BBL[0] = prevBBL; _statBBMock.BBL[1] = currBBL; _statBBMock.SMA[0] = prevSMA; _statBBMock.SMA[1] = currSMA; _statBBMock.BBH[0] = prevBBH; _statBBMock.BBH[1] = currBBH; _pricesData.L[bbPeriod - 1] = prevL; _pricesData.L[bbPeriod] = currL; _pricesData.H[bbPeriod - 1] = prevH; _pricesData.H[bbPeriod] = currH; SystemDefs.BBTrendRecognizer.BBTrendRecognizer.RecognizeTrend(_pricesData, _statBBMock, bbPeriod, currTrend, out _).ShouldBe(expectedTrend); }
public static void CalculateTrendsAndExpectations(BBTrendFundsData data, DateTime ts, StockDataRange dataRange, ISystemDataLoader dataLoader) { for (int i = 0; i < data.Stocks.Length; i++) { if (!dataLoader.GetWithIndex(data.Stocks[i].FullName, dataRange, ts, data.StatsBB[i].BackBufferLength, out StockPricesData spData, out int dataIndex)) { continue; } BBTrendType lastTrend = data.CurrentTrends[i]; data.CurrentTrends[i] = BBTrendRecognizer.BBTrendRecognizer.RecognizeTrend(spData, data.StatsBB[i], dataIndex, data.CurrentTrends[i], out float trendStartLevel); if (lastTrend != data.CurrentTrends[i]) { data.UpTrendStartValues[i] = trendStartLevel;// spData.H[dataIndex]; data.TrendLength[i] = 0; } data.TrendLength[i]++; BBTrendExpectation lastExpectation = data.CurrentExpectations[i]; data.CurrentExpectations[i] = BBTrendRecognizer.BBTrendRecognizer.GetExpectation(spData, data.StatsBB[i], dataIndex, data.CurrentTrends[i]); data.ExpectationChanged[i] = (lastExpectation != data.CurrentExpectations[i]); } }
public static BBTrendExpectation GetExpectation(StockPricesData data, StatBB statBB, int leadingIndex, BBTrendType currentTrend) { if (currentTrend == BBTrendType.Up) { return(data.C[leadingIndex] > statBB.Data(StatBBData.SMA)[leadingIndex - statBB.BackBufferLength + 1] ? BBTrendExpectation.UpAndRaising : BBTrendExpectation.UpButPossibleChange); } if (currentTrend == BBTrendType.Down) { return(data.C[leadingIndex] <= statBB.Data(StatBBData.SMA)[leadingIndex - statBB.BackBufferLength + 1] ? BBTrendExpectation.DownAndFalling : BBTrendExpectation.DownButPossibleChange); } return(BBTrendExpectation.Unknown); }
public static BBTrendType RecognizeTrend(StockPricesData data, StatBB statBB, int leadingIndex, BBTrendType currentTrend, out float trendStartLevel) { trendStartLevel = 0; if (((currentTrend == BBTrendType.Unknown) || (currentTrend == BBTrendType.Up)) && ((data.L[leadingIndex] < statBB.Data(StatBBData.BBL)[leadingIndex - statBB.BackBufferLength + 1]) || (data.L[leadingIndex] < statBB.Data(StatBBData.BBL)[leadingIndex - statBB.BackBufferLength]))) { trendStartLevel = Math.Max(statBB.Data(StatBBData.BBL)[leadingIndex - statBB.BackBufferLength + 1], statBB.Data(StatBBData.BBL)[leadingIndex - statBB.BackBufferLength]); return(BBTrendType.Down); } if (((currentTrend == BBTrendType.Unknown) || (currentTrend == BBTrendType.Down)) && ((data.H[leadingIndex] > statBB.Data(StatBBData.BBH)[leadingIndex - statBB.BackBufferLength + 1]) || (data.H[leadingIndex] > statBB.Data(StatBBData.BBH)[leadingIndex - statBB.BackBufferLength]))) { trendStartLevel = Math.Min(statBB.Data(StatBBData.BBH)[leadingIndex - statBB.BackBufferLength + 1], statBB.Data(StatBBData.BBH)[leadingIndex - statBB.BackBufferLength]); return(BBTrendType.Up); } return(currentTrend); }