Exemple #1
0
 public static BracketOrder Buy(
     String symbol,
     Int64 quantity,
     Decimal takeProfitLimitPrice,
     Decimal stopLossStopPrice) =>
 MarketOrder.Buy(symbol, quantity)
 .Bracket(takeProfitLimitPrice, stopLossStopPrice);
Exemple #2
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));
            }
        }
Exemple #3
0
        public async Task <Trading.OrderResult> PlaceOrder_BuyMarket(
            Data.Format format, string symbol, int shares, TimeInForce timeInForce = TimeInForce.Gtc, bool useMargin = false)
        {
            IAlpacaTradingClient trading = null;
            IAlpacaDataClient    data    = 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));
                    data    = Environments.Live.GetAlpacaDataClient(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));
                    data    = Environments.Paper.GetAlpacaDataClient(new SecretKey(Settings.API_Alpaca_Paper_Key, Settings.API_Alpaca_Paper_Secret));
                }

                var account = await trading.GetAccountAsync();

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

                var order = await trading.PostOrderAsync(MarketOrder.Buy(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);
            }
        }