public async Task Trade() { if (!initialized) { throw new InvalidOperationException("Trader must be Initialized before it can Trade"); } Current = await broker.CheckPriceAsync(); await reporter.ReportNewPrice(broker, Current); LastSale = LastSale ?? Current; High = High == null || High.Value < Current.Value ? Current : High; Low = Low == null || Low.Value > Current.Value ? Current : Low; if (Math.Abs(Current.Value - LastSale.Value) < Current.Value * (decimal)config.NoiseThreshold) { return; // The current activity is too small for us to care } decimal timeSensitiveThreshold = CalcThresholdWithDecay(Current, LastSale); if (Bullish) { var thresholdValue = High.Value - (timeSensitiveThreshold * (High.Value - Low.Value)); if (Current.Value < thresholdValue) { await reporter.ReportAttemptedSell(broker, Current); await broker.Sell(Current); await reporter.ReportSell(broker, Current); Bullish = false; Low = null; LastSale = Current; } } else // if bearish { var thresholdValue = Low.Value + (timeSensitiveThreshold * (High.Value - Low.Value)); if (Current.Value > thresholdValue) { await reporter.ReportAttemptedBuy(broker, Current); await broker.Buy(Current); await reporter.ReportBuy(broker, Current); Bullish = true; High = null; LastSale = Current; } } }