protected override void Signal(HistoricalPriceVolumeEntry entry) { AroonIndicator aroon = (AroonIndicator)Indicators["Aroon"]; DateTime yesterday = entry.Date.AddDays(-1); TechnicalIndicatorItem currentUp = aroon.AroonUp.Data[entry.Date]; TechnicalIndicatorItem currentDown = aroon.AroonDown.Data[entry.Date]; TechnicalIndicatorItem previousUp = null; TechnicalIndicatorItem previousDown = null; if (aroon.AroonUp.Data.ContainsKey(yesterday)) { previousUp = aroon.AroonUp.Data[yesterday]; previousDown = aroon.AroonDown.Data[yesterday]; } if (currentUp.Value > currentDown.Value && !_bought) { int shares = decimal.ToInt32(Math.Floor(BuyingPower / entry.Close)); Trade(entry.Date, TradeType.Buy, entry.Close, shares); _bought = true; } else if (currentDown.Value > currentUp.Value && _bought) { // int max = GetMaxBuy(); // int owned = Math.Abs(SharesOwned); // Trade(entry.Date, TradeType.Cover, entry.Close, max > owned ? owned : max); Trade(entry.Date, TradeType.Sell, entry.Close, Math.Abs(SharesOwned)); _bought = false; } }
protected override void Signal(HistoricalPriceVolumeEntry entry) { AroonIndicator aroon = (AroonIndicator)Indicators["Aroon"]; DateTime yesterday = entry.Date.AddDays(-1); TechnicalIndicatorItem currentUp = aroon.AroonUp.Data[entry.Date]; TechnicalIndicatorItem currentDown = aroon.AroonDown.Data[entry.Date]; TechnicalIndicatorItem previousUp = null; TechnicalIndicatorItem previousDown = null; if (aroon.AroonUp.Data.ContainsKey(yesterday)) { previousUp = aroon.AroonUp.Data[yesterday]; previousDown = aroon.AroonDown.Data[yesterday]; } if ((previousDown?.Value >= 50 || previousUp?.Value < 100) && currentDown.Value < 50 && currentUp.Value == 100 && !_bought) { int shares = decimal.ToInt32(Math.Floor(BuyingPower / entry.Close)); _bought = Trade(entry.Date, TradeType.Buy, entry.Close, shares); } else if ((previousUp?.Value >= 50 || previousDown?.Value < 100) && currentUp.Value < 50 && currentDown.Value == 100 && _bought) { _bought = Trade(entry.Date, TradeType.Sell, entry.Close, SharesOwned); } }
protected override void CalculateFor(int current) { HistoricalPriceVolumeEntry entry = HistoricalData.Items[current]; // int current = i + 1; DateTime date = entry.Date; // int back = current - Period; // back = back < 0 ? 0 : back; // List<HistoricalPriceVolumeEntry> range = HistoricalData.GetPrevAsList(current, BeginCalculationOffset); List <HistoricalPriceVolumeEntry> range = GetPrevRange(); int dayHigh = IndicatorUtils.GetHighestId(range.Select(e => e.High)); // int daysSinceHigh = range.Count - dayHigh; decimal aroonUp = 100 * (BeginCalculationOffset - dayHigh) / (decimal)BeginCalculationOffset; int dayLow = IndicatorUtils.GetLowestId(range.Select(e => e.Low)); // int daysSinceLow = range.Count - dayLow; decimal aroonDown = 100 * (BeginCalculationOffset - dayLow) / (decimal)BeginCalculationOffset; // Debug.WriteLine(""); // Debug.WriteLine($"Date: {date.ToString("d")}"); // Debug.WriteLine($"Up: {aroonUp}"); // Debug.WriteLine($"Down: {aroonDown}"); _upItems.Add(new TechnicalIndicatorItem(date, aroonUp)); _downItems.Add(new TechnicalIndicatorItem(date, aroonDown)); }
public void Calculate(HistoricalPriceVolume data) { List <HistoricalPriceVolumeEntry> graph = data.Items; for (int i = Period; i < graph.Count; i++) { HistoricalPriceVolumeEntry entry = graph[i]; int current = i + 1; DateTime date = entry.Date; int back = current - Period; back = back < 0 ? 0 : back; List <HistoricalPriceVolumeEntry> range = graph.GetRange(i - Period, Period + 1); int dayHigh = IndicatorUtils.GetHighestId(range.Select(e => e.High)); int daysSinceHigh = range.Count - dayHigh; decimal aroonUp = 100 * (Period - daysSinceHigh) / (decimal)Period; int dayLow = IndicatorUtils.GetLowestId(range.Select(e => e.Low)); int daysSinceLow = range.Count - dayLow; decimal aroonDown = 100 * (Period - daysSinceLow) / (decimal)Period; Debug.WriteLine(""); Debug.WriteLine($"Date: {date.ToString("d")}"); Debug.WriteLine($"Up: {aroonUp}"); Debug.WriteLine($"Down: {aroonDown}"); AroonGraph.Add(date, new[] { aroonUp, aroonDown }); } }
protected abstract void Signal(HistoricalPriceVolumeEntry entry);
public BacktestResult(decimal capital, decimal result, StockInfo stock, List <TradeInfo> trades, HistoricalPriceVolumeEntry last) { Stock = stock; Trades = trades; Capital = capital; Result = result; Profit = result - capital; Gain = Profit / Capital * 100; decimal profit = 0; int shares = 0; foreach (TradeInfo trade in trades) { if (trade.Type == TradeType.Buy || trade.Type == TradeType.Cover) { profit -= trade.Price * trade.Amount; shares += trade.Amount; } else { profit += trade.Price * trade.Amount; shares -= trade.Amount; } } profit += shares * last.Close; Profit = profit; // for (int i = 0; i < trades.Count; i++) // { // if (i % 2 == 0 && i + 1 < trades.Count) // { // if (tr) // } // } // foreach (TradeInfo trade in trades) // { // switch (trade.Type) // { // case TradeType.Buy: // Buys++; // break; // case TradeType.Sell: // Sells++; // break; // case TradeType.Short: // Shorts++; // break; // case TradeType.Cover: // Covers++; // break; // } // } }