Short Selling not working

I am using the CSharp API and am trying to submit a SELL order and I keep getting the error: “***insufficient qty for order (requested 1, available 0 )***”. When I do a BUY order - using similar code - it WORKS. So I am unclear what I am doing wrong at this point…Below is the code…Any help would be appreciated…

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing; 
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Alpaca.Markets;

namespace AlpacaCSharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            var KEY_ID = "PKAG1FI9XXXXXXXXXXXXXXXXXXXXX";
            var SECRET_KEY = "49V9VHbVhker5SJMi2XGZYXXXXXXXXXXXXXXXXXXXX";
            var API_URL = "https://paper-api.alpaca.markets";

            var client = new Alpaca.Markets.RestClient(KEY_ID, SECRET_KEY, API_URL);

            var clock = client.GetClockAsync().Result;

            if (clock != null)
            {
                Console.WriteLine(
                    "Timestamp: {0}, NextOpen: {1}, NextClose: {2}",
                    clock.Timestamp, clock.NextOpen, clock.NextClose);
            }

            //await PlaceOrder();
            try
            {
                var t = Task.Run(() => PlaceOrder());
                t.Wait();

            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }

            var position = Task.Run(() => GetPositions());

        }

        public static async Task GetPositions()
        {
            var KEY_ID = "PKAG1XXXXXXXXXXXXXXXXXXXX";
            var SECRET_KEY = "49V9VHbVhker5SJMi2XGZY/XXXXXXXXXXXXXXX";
            var API_URL = "https://paper-api.alpaca.markets";

            var client = new Alpaca.Markets.RestClient(KEY_ID, SECRET_KEY, API_URL);
            var position = await client.GetPositionAsync("AAPL");

            var x = position.Symbol;
            var acct = position.AccountId;
        }

        public static async Task PlaceOrder()
        {
            var KEY_ID = "PKAGXXXXXXXXXXXXXXXXXXXXXX";
            var SECRET_KEY = "49V9VHbVhkeXXXXXXXXXXXXXXXXXXXXXXXx";
            var API_URL = "https://paper-api.alpaca.markets";

            var client = new Alpaca.Markets.RestClient(KEY_ID, SECRET_KEY, API_URL);

            var symbol = "SPY";

            var barSet = await client.GetBarSetAsync(new[] { symbol }, TimeFrame.FifteenMinutes, 1);
            var bars = barSet[symbol].ToList();
            var price = bars[0].Close;

            // Submit a market order to buy 1 share of Apple at market price
            //var order = await client.PostOrderAsync("AAPL", 1, OrderSide.Buy, OrderType.Limit,     TimeInForce.Day, Convert.ToDecimal(200.12));

            //decimal price = 200.0;
            var order2 = await client.PostOrderAsync(symbol, 1, OrderSide.Sell, OrderType.Limit, TimeInForce.Day, price);

            // Submit a limit order to attempt to sell 1 share of AMD at a
            // particular price ($20.50) when the market opens
            //order = await client.PostOrderAsync("AMZN", 1, OrderSide.Buy, OrderType.Market, TimeInForce.Day, Convert.ToDecimal(20.5));

            //Console.Read();
            System.Threading.Thread.Sleep(1000);


        }




    }
}

That output I would expect if I didn’t currently own the stock I’m trying to sell

1 Like

Yes typically you see that error message if you have 1 share position with 1 open sell order pending. Make sure you do NOT have open orders and open positions.