I had the exact same troubles! But first, to clarify, when setting an order, you're giving the quantity of the order (# of stocks to purchase), not the dollar amount for the order. You said "amount" there, and so I wasn't certain which you meant.
As far as selling, you got it: Just give the quantity a negative number, and it will sell that portion of stocks. So if you bought 200 shares of GOOG today...
`MarketOrder("GOOG", 200);`
When you want to sell them in the future, you would simply do:
`MarketOrder("GOOG", 200);`
Here's the docs on MarketOrder. And here's the docs on orders in general.
Now, figuring out specifically what you actually bought and sold and for how much is a little less simple. That's because when you make an order, you need to await its purchase/sale by your brokerage. In other words, some infinite amount of time could ellapse before your order is filled. This is particularly true for Limit Orders where your limit price may take a while to be met - or never be met at all.
Thus the OnOrderEvent. This little fella fires whenever any order event occurs for your broker. Sell orders, buy orders, and everything in-between. All you need to do is filter these events to the ones you care about and do as you will.
public override void OnOrderEvent(OrderEvent orderEvent) {
var symbol = orderEvent.Symbol;
// This will never (read: should never) happen.
if(!Securities.ContainsKey(symbol)) {
Error(string.Format($"Security for symbol '{symbol.Value}' not found in Securities collection when processing OrderId {orderEvent.OrderId}."));
return;
}
// The current details of the security in question.
var security = Securities[symbol];
// The OrderTicket for the order (https://www.quantconnect.com/lean/docs#topic3152.html)
var order = Transactions.GetOrderById(orderEvent.OrderId);
// The actual quantity filled by the broker.
var quantity = orderEvent.FillQuantity;
// The actual price your security went for.
var price = orderEvent.FillPrice;
// Holds OrderDirection.Buy or OrderDirection.Sell
var direction = orderEvent.Direction;
if(orderEvent.Status == OrderStatus.Invalid || orderEvent.Status == OrderStatus.Canceled) {
// Be sure and do something with canceled or invalid orders.
Debug(string.Format("Order {0:000} for {1} {2}.", orderEvent.OrderId, symbol.Value, orderEvent.Status));
return;
}
switch (orderEvent.Direction) {
case OrderDirection.Buy:
if(quantity != 0) {
// Do something with you shiny new stocks.
}
break;
case OrderDirection.Sell:
// For a sale, the quantity is in the negative. Reverse it for logging.
quantity = -quantity;
if(quantity != 0) {
// Do something with your new-found fortunes.
}
break;
}
}
There are a few things that I'd like to point out here:
- Notice that we check to ensure our quantity is not `0`. There are some order events that may fire - other than the actual purchase and sale of the stocks, and this acts as a sort of catch-all to filter out those events. You can break these down more specifically with `orderEvent.Status`.
- The `orderEvent.FillQuantity` for stock sells will be nagative - hence the `quantity = -quantity` in the "sell" block.
- Between the time that you make your order, and the order is fulfilled, your `Portfolio` will not reflect the order. For example, your `Portfolio.Cash` will not be reduced by the cost of this order. Just keep this in mind.
Concerning `Notify` - I don't use it myself, but according to the specs, "These are only sent during live trading."