コード例 #1
0
        public async Task <Trading.OrderResult> PlaceOrder_SellLimit(
            Data.Format format, string symbol, int shares, decimal limitPrice, TimeInForce timeInForce = TimeInForce.Gtc)
        {
            IAlpacaTradingClient trading = null;

            try {
                if (format == Data.Format.Live)
                {
                    if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Secret))
                    {
                        return(Trading.OrderResult.Fail);
                    }

                    trading = Environments.Live.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Live_Key, Settings.API_Alpaca_Live_Secret));
                }
                else if (format == Data.Format.Paper)
                {
                    if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Secret))
                    {
                        return(Trading.OrderResult.Fail);
                    }

                    trading = Environments.Paper.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Paper_Key, Settings.API_Alpaca_Paper_Secret));
                }

                // Prevents exceptions or unwanted behavior with Alpaca API
                var account = await trading.GetAccountAsync();

                if (trading == null || account == null ||
                    account.IsAccountBlocked || account.IsTradingBlocked ||
                    account.TradeSuspendedByUser)
                {
                    return(Trading.OrderResult.Fail);
                }

                // Prevents unintentionally short selling (selling into negative digits, the API interprets that as intent to short-sell)
                var positions = await trading.ListPositionsAsync();

                if (!positions.Any(p => p.Symbol == symbol))                // If there is no position for this symbol
                {
                    return(Trading.OrderResult.Fail);
                }

                var position = await trading.GetPositionAsync(symbol);      // If there were no position, this would throw an Exception!

                if (position == null || position.Quantity < shares)         // If the current position doesn't have enough shares
                {
                    return(Trading.OrderResult.Fail);
                }

                var order = await trading.PostOrderAsync(LimitOrder.Sell(symbol, shares, limitPrice).WithDuration(timeInForce));

                return(Trading.OrderResult.Success);
            } catch (Exception ex) {
                await Log.Error($"{MethodBase.GetCurrentMethod().DeclaringType}: {MethodBase.GetCurrentMethod().Name}", ex);

                return(Trading.OrderResult.Fail);
            }
        }
コード例 #2
0
ファイル: Commands.cs プロジェクト: sondirn/LunarTrader
        private static async Task SubmitLimitOrder(string symbol, OrderSide orderSide, decimal price, int quantity)
        {
            LimitOrder           order;
            IAlpacaTradingClient client;

            client = Core.ServiceProvider.GetService <IAlpacaTradingClient>();

            if (orderSide == OrderSide.Buy)
            {
                order = LimitOrder.Buy(symbol, quantity, price);
                try
                {
                    var result = await client.PostOrderAsync(order);

                    Core.Logger.LogInfo($"Order for {result.Symbol} submitted at {result.CreatedAtUtc}");
                }
                catch (Exception e)
                {
                    Core.Logger.LogError($"{symbol} could not be limit ordered: {e.Message}");
                }
            }
            if (orderSide == OrderSide.Sell)
            {
                order = LimitOrder.Sell(symbol, quantity, price);
                try
                {
                    var result = await client.PostOrderAsync(order);

                    Core.Logger.LogInfo($"Order for {result.Symbol} submitted at {result.CreatedAtUtc}");
                }
                catch (Exception e)
                {
                    Core.Logger.LogError($"{symbol} could not be limit ordered: {e.Message}");
                }
            }
        }