I am trading my entire account and keep getting an error that I am trying to trade more money than I have in my account. What command will trade all my account minus a minimal amount for trading fee. It would be better to have a percentage than a dollar amount to accommodate account size for trading fee.
My last trade during the day is a market order around 3:30 so there is no concern regarding not having a trade implemented before close. I.E. I do not need a safety margin other than the trading fee.
I have tried the following unsuccessfully
METHOD 1
INITIALIZE
self.Settings.FreePortfolioValuePercentage = 0.025
ONDATA
Quantity = self.CalculateOrderQuantity('SPY', 1)
self.MarketOrder('SPY',Quantity)
METHOD 2
Works but keeps too much money in cash, I want to minimize. Also, when money grows I eventually get the error again at around 60k.
ONDATA
Quantity = self.CalculateOrderQuantity('SPY', 1)
OrderQuantity = Quantity - round (1500 / price)
self.MarketOrder('SPY',OrderQuantity)
METHOD 3
ONDATA
Quantity = self.CalculateOrderQuantity('SPY', .97)
self.MarketOrder('SPY',Quantity)
Nico Xenox
Hey Kenneth Lamers
You could use set holdings which allocates a percentage that you want to choose for a certain symbol.
Kenneth Lamers
This automatically sets aside 2.5%?
Is there a command that does not?
Nico Xenox
Hey Kenneth Lamers
As I mentioned SetHoldings allocates the percentage of your portfolio to a symbol which means that you can change the percentage number. Using 90% or 97.5% of your portfolio would mean that the number is 0.975
This would set the desired percentages aside.
Nico Xenox
Also I forgot to mention, if I’m not wrong you can set it to one because it automatically takes the fees into account.
Kenneth Lamers
I changed to
self.SetHoldings("SPY", 1)
Thanks for the help but it still gave me an invalid trade error due to a shortage of funds. It shows $68,318.08 available and cash needed is $71,495.47. That's about a 4.5% difference! I am not understanding how this command works. I have no other activity in this account and all funding is available. Broker is InteractiveBrokers.
I will try setting to the .97 but it will not be trading my full account which is what my goal is. If my trading fee is $10 dollars, why would I not be able to trade all but $10??? QC could have a command that purchases the max number of shares with the funds available minus the fees which it already estimates.
Louis Szeto
Hi Kenneth
Sometimes we set FreePortfolioValuePercentage to have buffer cash in this purpose, the default is already 0.25%:
It is unlikely for a stock to have a 4.5% difference purely based on trading fees. In this case, we'll have to consider if the stock is illiquid, so the market friction is large due to bid-ask spread. Consider execute the trade better, for example, using a SpreadExecutionModel.
Best
Louis
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.
Kenneth Lamers
I have figured out the issue. For MARKET bids, the algorithm calculates the number of shared to be ordered based on the bid price of the stock. Interactive Brokers looks at the number of shares requested and multiplies by the ASK price which is higher, thus does not accept the ticket and gives the error that you do not have enough funds. I have seen this difference over 4.5%.
SOLUTION
I do not want to use limit bid because I want the order to go through. Instead, I place two orders. The first is for a quantity at 6% under the maximum shares I could purchase. The second quantity is set to the remaining 6% minus 6%. This ends up trading most of the account. If there is a lot of money in the account, create a third order with the same method. I included a wait time of 10 seconds between the two orders. The code has not run yet so I am waiting to confirm there are no errors…
Quantity = self.CalculateOrderQuantity('SPY', 1)
FirstOrderQuantity = round (Quantity * .94)
SecondOrderQuantity = round ((Quantity - FirstOrderQuantity)*.94)
# This next line tells the program to wait 10 seconds after submitting a market order to go to the next line of code. Without this line, the default is to wait 5 seconds
self.Transactions.MarketOrderFillTimeout = timedelta(seconds=10)
self.MarketOrder('SPY',FirstOrderQuantity)
self.MarketOrder('SPY',SecondOrderQuantity)
Louis Szeto
Hi Kenneth
We don't recommend an ice-berg order for this purpose, as it would increase the trading cost and slippage risk. Instead, you may set the free cash percentage in your portfolio to reserve the cash buffer for buying power:
The CalculateOrderQuantity method has already incorporated the real-time margin requirement, so the bid/ask price was not the reason, but most likely slippage.
Best
Louis
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.
Kenneth Lamers
This solution regulates me back to not using all the cash in my account which was my goal. I do not want thousands of dollars not working for me.
I believe the trade fees will come out about the same. Interactive Brokers charges on a per share basis with a 1$ minimum. So, whether I have one purchase of 1000 shares or a purchase of 940 and a second purchase of 60 shares, the total fees should come out pretty close.
Kenneth Lamers
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!