Crypto Trading with C# api

Is there a way to place crypto orders for BTCUSD in fractions of 1? The link below says it supports minimum qty 0.0001 for BTC but the api seems to only accept Long integers and not decimal amounts?? here is my example code for the order but i want to be able to purchase 0.0001 or similar instead. Any help is appreciated.

var order = await alpacaTradingClient.PostOrderAsync(
side.Market(symbol, 1));

You are right @Michael_D - some extension methods don’t properly support the OrderQuantity type as an argument for order placement object creation. I’ll fix in the upcoming release (you can track progress in the newly created GitHub issue).

As a temporary workaround, you can use static methods of the MarketOrder type:

var order = await alpacaTradingClient.PostOrderAsync(
MarketOrder.Buy(symbol, OrderQuantity.Fractional(0.01)));

or

var order = await alpacaTradingClient.PostOrderAsync(
MarketOrder.Sell(symbol, OrderQuantity.Notional(0.01)));
1 Like

This still does not work doing this, still shows order quantity should be positive value?? See attached screenshot.

The 5.2.13 version of SDK contains a fix for the original issue and for this validation error.

excellent, i’ll test it out. appreciate the quick response!

I’m still getting the same error, both on trying fractional orders and the advanced orders. I put both below that I’m trying.

var order = await alpacaTradingClient.PostOrderAsync(MarketOrder.Buy(Symbol, OrderQuantity.Fractional(0.01M)));

Error: Alpaca.Markets.RequestValidationException: ‘Order quantity should be positive value.’

await alpacaTradingClient.PostOrderAsync(MarketOrder.Buy(Symbol, 100).WithDuration(TimeInForce.Gtc)
.Bracket(
stopLossStopPrice: price * 0.95M,
takeProfitLimitPrice: price * 1.005M,
stopLossLimitPrice: price * 0.94M));

Error: Alpaca.Markets.RestClientErrorException: ‘crypto orders not allowed for advanced order_class: otoco’

The first problem (validation exception) is my fault - my last merge into the 5.x branch was incorrect and miss one important line. The updated 5.2.14 version is already available on NuGet.
The second problem (REST error) is a server-side limitation - AFAIK the crypto API supports only a limited set of order types and brackets not in this list yet.

Perfect the fractional piece works now, thanks for the quick response! Do you know when crypto orders will support the bracket orders or if its in the road map?

@Michael_D Unfortunately, I’m unable to answer your question - I don’t have enough information. I’m not working in Alpaca - just do maintenance/support work for their .NET SDK on GitHub.

Got it, thanks for your help!

Thanks

This has been working with Market orders but will it be able to work with LimitOrders? I can’t seem to get it to work.

var order = await alpacaTradingClient.PostOrderAsync(LimitOrder.Buy(Symbol, OrderQuantity.Fractional(0.01M), price));

Severity Code Description Project File Line Suppression State
Error CS1503 Argument 2: cannot convert from ‘Alpaca.Markets.OrderQuantity’ to ‘long’

The fractional trading supported only for day market orders:

Alpaca currently supports fractional trading for market and day order types (support for other order types are in our roadmap).

I am not able to sell my fractional shares when I do a market sell. i purchased .401 shares and now trying to sell them.

var order = await alpacaTradingClient.PostOrderAsync(MarketOrder.Sell(Symbol, (OrderQuantity)Positions.Quantity));

var order = await alpacaTradingClient.PostOrderAsync(MarketOrder.Sell(Symbol, (OrderQuantity.Fractional)Positions.Quantity));

This one should work:

var position = await alpacaTradingClient.GetPositionAsync(Symbol);
var order = await alpacaTradingClient.PostOrderAsync(
    MarketOrder.Sell(Symbol, OrderQuantity.Fractional(position.Quantity)));

as well as this one:

await alpacaTradingClient.DeletePositionAsync(new DeletePositionRequest(Symbol));

Hi,

Thank you for contributing! We have a dedicated discord focused solely on Crypto related queries with an active community and more engagement. Feel free to join via the invite link below;

Best,