Skip to content

Instantly share code, notes, and snippets.

@crowcoder
Created November 5, 2017 20:58
Show Gist options
  • Save crowcoder/21582bf48e7fd858dee1a25b6b4e7767 to your computer and use it in GitHub Desktop.
Save crowcoder/21582bf48e7fd858dee1a25b6b4e7767 to your computer and use it in GitHub Desktop.
using Service;
using System;
namespace Business
{
public class StocksLogic
{
private readonly IStockService _stockService;
private readonly ILogService _logService;
public StocksLogic(IStockService stockService, ILogService logService)
{
_stockService = stockService;
_logService = logService;
}
public Trade GetRich(string ticker)
{
try
{
Trade lastTrade = _stockService.GetLastTrade(ticker);
decimal currentPrice = _stockService.GetCurrentPrice(ticker);
if (lastTrade.Side == "sell")
{
//Buy 200 shares if current price is at least 15% lower than last sell.
if (currentPrice <= (lastTrade.TradePrice - (lastTrade.TradePrice * .15m)))
{
return _stockService.Buy(ticker, 200);
}
}
else //was a buy
{
//Sell 200 shares if current price is 15% higher than last buy.
if (currentPrice >= (lastTrade.TradePrice + (lastTrade.TradePrice * .15m)))
{
return _stockService.Sell(ticker, 200);
}
}
//no trade criteria met, return no trade.
return new Trade { Ticker = ticker, Side = "none" };
}
catch (Exception ex)
{
_logService.Log(ex.Message);
return new Trade { Ticker = ticker, Side = "none" };
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment