Esempio n. 1
0
 public static BracketOrder Sell(
     String symbol,
     Int64 quantity,
     Decimal takeProfitLimitPrice,
     Decimal stopLossStopPrice) =>
 MarketOrder.Sell(symbol, quantity)
 .Bracket(takeProfitLimitPrice, stopLossStopPrice);
Esempio n. 2
0
        public async Task <Trading.OrderResult> PlaceOrder_SellMarket(
            Data.Format format, string symbol, int shares, 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(MarketOrder.Sell(symbol, shares).WithDuration(timeInForce));

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

                return(Trading.OrderResult.Fail);
            }
        }
Esempio n. 3
0
        public static async Task Trader(string[] array)
        {
            String  symbol               = "";
            String  side                 = "";
            int     round_lot            = 0;
            decimal stopLossStopPrice    = 1.0m;
            decimal takeProfitLimitPrice = 1.0m;
            decimal stopLossLimitPrice   = 1.0m;

            for (int i = 0; i < array.Length; i++)
            {
                if (i == 0)
                {
                    symbol = array[i];
                }
                else if (i == 1)
                {
                    side = array[i];
                }
                else if (i == 2)
                {
                    round_lot = int.Parse(array[i]);
                }
                else if (i == 3)
                {
                    stopLossStopPrice = decimal.Parse(array[i]);
                }
                else if (i == 4)
                {
                    takeProfitLimitPrice = decimal.Parse(array[i]);
                }
                else if (i == 5)
                {
                    stopLossLimitPrice = decimal.Parse(array[i]);
                }
            }

            var tradingClient = Alpaca.Markets.Environments.Paper.GetAlpacaTradingClient(
                new SecretKey("PKCPC6RJ84BG84W3PB60", "U1r9Z2QknL9FwAaTztfLl5g1DTxpa5m97qyWCGZ7"));

            if (side == "buy")
            {
                await tradingClient.PostOrderAsync(MarketOrder.Buy(symbol, round_lot).WithDuration(TimeInForce.Gtc).
                                                   Bracket(stopLossStopPrice: stopLossStopPrice, takeProfitLimitPrice: takeProfitLimitPrice,
                                                           stopLossLimitPrice: stopLossLimitPrice));
            }
            else if (side == "sell")
            {
                await tradingClient.PostOrderAsync(MarketOrder.Sell(symbol, round_lot).WithDuration(TimeInForce.Gtc).
                                                   Bracket(stopLossStopPrice: stopLossStopPrice, takeProfitLimitPrice: takeProfitLimitPrice,
                                                           stopLossLimitPrice: stopLossLimitPrice));
            }
        }