139 lines
3.3 KiB
C#
139 lines
3.3 KiB
C#
using Backend.ServiceHander;
|
|
using Backend.MarketDataRequest;
|
|
using Backend.Interface;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Globalization;
|
|
|
|
namespace Backend.Controller
|
|
{
|
|
[ApiController]
|
|
[Route("api/market")]
|
|
public class MarketController : ControllerBase
|
|
{
|
|
private readonly MarketDataService _marketDataService;
|
|
private readonly IHistoricalMarketDataCache _cache;
|
|
|
|
public MarketController(MarketDataService marketDataService, IHistoricalMarketDataCache cache)
|
|
{
|
|
_marketDataService = marketDataService;
|
|
_cache = cache;
|
|
}
|
|
|
|
[HttpGet("last-trading-day")]
|
|
public async Task<IActionResult> GetLastTradingDay(
|
|
[FromQuery] string symbol = "SPY",
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
var lastDay = await _cache.GetLastTradingDayAsync(
|
|
symbol.Trim().ToUpperInvariant(), cancellationToken);
|
|
|
|
if (lastDay is null)
|
|
return Ok(new { found = false, lastTradingDay = (string?)null });
|
|
|
|
return Ok(new
|
|
{
|
|
found = true,
|
|
lastTradingDay = lastDay.Value.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new { error = ex.ToString() });
|
|
}
|
|
}
|
|
|
|
[HttpGet("symbol-check/{symbol}")]
|
|
public IActionResult CheckSymbol(string symbol)
|
|
{
|
|
try
|
|
{
|
|
var (found, message) = _marketDataService.CheckSymbol(symbol.ToUpperInvariant());
|
|
return Ok(new { found, message });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new { error = ex.ToString() });
|
|
}
|
|
}
|
|
|
|
[HttpGet("live/{symbol}")]
|
|
public IActionResult GetLive(string symbol)
|
|
{
|
|
try
|
|
{
|
|
var data = _marketDataService.GetLiveQuote(symbol.ToUpperInvariant());
|
|
return Ok(new
|
|
{
|
|
symbol = data.Symbol,
|
|
lastPrice = data.LastPrice,
|
|
volume = data.Volume,
|
|
previousClose = data.PreviousClose,
|
|
openPrice = data.Open,
|
|
marketOpen = data.MarketOpen
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new { error = ex.ToString() });
|
|
}
|
|
}
|
|
|
|
[HttpGet("historical")]
|
|
public async Task<IActionResult> GetHistorical(
|
|
[FromQuery] string symbol,
|
|
[FromQuery] string endDateTime,
|
|
[FromQuery] string duration,
|
|
[FromQuery] HistoricalBarSize barSize,
|
|
[FromQuery] HistoricalWhatToShow whatToShow = HistoricalWhatToShow.Trades,
|
|
[FromQuery] bool useRth = true)
|
|
{
|
|
try
|
|
{
|
|
var data = await _marketDataService.GetHistoricalAsync(
|
|
symbol.ToUpperInvariant(),
|
|
endDateTime,
|
|
duration,
|
|
barSize,
|
|
whatToShow,
|
|
useRth
|
|
);
|
|
|
|
return Ok(data);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new { error = ex.ToString() });
|
|
}
|
|
}
|
|
|
|
[HttpGet("historical-line")]
|
|
public async Task<IActionResult> GetHistoricalLine(
|
|
[FromQuery] string symbol,
|
|
[FromQuery] string endDateTime,
|
|
[FromQuery] string duration,
|
|
[FromQuery] HistoricalBarSize barSize,
|
|
[FromQuery] HistoricalWhatToShow whatToShow = HistoricalWhatToShow.Trades,
|
|
[FromQuery] bool useRth = true)
|
|
{
|
|
try
|
|
{
|
|
var data = await _marketDataService.GetHistoricalLineAsync(
|
|
symbol.ToUpperInvariant(),
|
|
endDateTime,
|
|
duration,
|
|
barSize,
|
|
whatToShow,
|
|
useRth
|
|
);
|
|
|
|
return Ok(data);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return StatusCode(500, new { error = ex.ToString() });
|
|
}
|
|
}
|
|
}
|
|
} |