using System; using System.Threading; using System.Threading.Tasks; using FinlyticCore.Dtos.Bot; using FinlyticCore.Dtos.TechnicalAnalysis; namespace FinlyticBot.Services.Alpaca; /// /// Outcome of a confirmed Alpaca position liquidation (). /// Only ever constructed after Alpaca's REST API has accepted the liquidation order — see the method's /// XML doc for why callers may treat its mere existence as proof the broker confirmed the close. /// /// The Alpaca order ID of the liquidation (market) order. /// The Alpaca order status returned immediately after submission (e.g. "Accepted", "Filled"). /// /// The average fill price if Alpaca already reports one at submission time; when the /// liquidation order has been accepted but not yet filled (e.g. outside market hours). Callers must fall back /// to the position's last known synced price in that case rather than treating as zero. /// public record AlpacaPositionCloseResult(string OrderId, string OrderStatus, decimal? AverageFillPrice); public interface IAlpacaTradingService { bool IsConfigured { get; } Task PlaceBracketOrderAsync( string symbol, SignalDirection direction, decimal quantity, decimal entryPrice, decimal stopLossPrice, decimal takeProfitPrice, CancellationToken cancellationToken = default); Task UpdateStopLossAsync( string alpacaOrderId, decimal newStopLossPrice, CancellationToken cancellationToken = default); Task CancelOrderAsync( string alpacaOrderId, CancellationToken cancellationToken = default); /// /// Liquidates the entire open position for at market price via Alpaca's native /// position-close endpoint. Returns only once Alpaca has ACCEPTED the liquidation order — a caller (e.g. /// the panic-close handler) must only mark the corresponding local position as closed AFTER this call /// returns without throwing, never optimistically before calling it. /// /// Alpaca is not configured/reachable. Task ClosePositionAsync( string symbol, CancellationToken cancellationToken = default); Task GetPortfolioSummaryAsync(CancellationToken cancellationToken = default); }