Hello,
I need to calculate my position based on money loss at the stop level. To be more specific, my position sizing strategy consists of risking a fixed % of the account balance at the stop loss, given that there is enough margin available. If there is not enough margin the position will use only a fraction of margin available.
In the platform, I can only see position sizing based on target, which is not what I am looking for. What would be the fastest way to implement this?
thanks in advance for your support.
Diego
Diego Fanesi
given an OrderTicket, I will also need to close that specific position. I found that I can close all positions in a specific market or make another order to sell positions in the same market, but my case is different. I have multiple positions in a single market and I need to close a specific position, leaving all the others open.
There is also no way to link a stop order to a specific position in the market. is there any way to implement this?
Gurumeher Sawhney
Position sizing based on target is done by self.SetHoldings() and self.CalculateOrderQuantity(), with the difference being CalculateOrderQuantity() is used with MarketOrder() while SetHoldings() just executes the trades. However, it is possible to determine the value of the Portfolio and based upon that calculate the size of the necessary order. Methods that could be useful in this case are Portfolio.Cash, Portfolio.MarginRemaining, Portfolio.TotalHoldingsValue, Portfolio.TotalPortfolioValue and Portfolio.GetBuyingPower. Please refer to "Securities and Portfolio" to see how how these can be used.
After calling these methods and some arithmetic the position size necessary be determined. With this position size an appropriate order can be made. Please refer to the "Trading and Orders" to see the various order types that can be used. Once creating the orders they can be accessed via Transactions methods such as Transactions.GetOpenOrders and Transactions.GetOrderById(int orderID. This way all orders can be tracked and individually handled, regardless of the overall positions. An idea would be to check a transaction record of open positions/filled orders so that it will be easier to determine which position to close later. The last link shows a sample algorithm where an order is created using Portfolio.MarginRemaining and basic arithmetic to determine the order size.
Diego Fanesi
Thank you very much. this has been very helpful.
Diego Fanesi
last question: I am looking at the source code of Transactions and I see that there are methods like CancelOrder. This sounds like a method to cancel an outstanding order, not a position that is currently open, but I am not sure. There is also RemoveOrder which I don't understand the difference between the two and documentation is really poor on it.
would you mind to share a little snippet of code where, given an orderId, it cancels the open position? python is fine if it works best for you.
Thanks,
Diego
Diego Fanesi
Looking at the comment on line 104 on this file: link
it looks that the cancel request will be successful only if the order is not filled yet. Looking at the source code of Transactions it looks all methods are using the same CancelRequest.
ignore link below. I can't delete it.
Diego Fanesi
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!