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); }
private float CalculateBalanceForExpectation(BBTrendExpectation expectation) { switch (expectation) { case BBTrendExpectation.UpAndRaising: return(0.8f); case BBTrendExpectation.UpButPossibleChange: return(0.2f); case BBTrendExpectation.DownButPossibleChange: return(0.2f); case BBTrendExpectation.DownAndFalling: return(0f); case BBTrendExpectation.Unknown: return(0f); default: return(0f); } }
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]); } }