If I have a live account which has, say $10,000 in it and I call this method
public override void Initialize()
{
// cash allocation
SetCash(5000);
}
and in my algorithm I call:
SetHoldings("SYMBOL", .33);
What would the order do? Would it order 0.33*$5,000 or 0.33*$10,000 since that's the total cash in the account. Also, say it sold and the cash increased from $5,000 to $7,000 (with a total cash balance of $12,000) would again be 0.33*$7,000 or 0.33*$12,000?
Thanks and best regards
Alexandre Catarino
Here is the passage from the docs where we discuss "Setting Cash":
In backtests you can set your starting capital using the SetCash(decimal cash) method. In live trading this is ignored and your brokerage cash is used instead. In paper trading we set the cash to a fictional $100,000 USD (you can override the paper trading equity by clicking the "Live Algorithm Equity" button in your project tab).
Iman Mohtashemi
Thanks! So there is no way to modify how much cash it uses live? For example, say I had 30k in my account and I wanted to only use 5k. Then 5k grew by 40% so now I have 7k but now I only want to reinvest the 7k it grew to ignoring the other 25k that is in the account. Can I do that programmatically?
Jared Broad
Hi Iman you can test it for yourself with a quick test - 0.33 would be 0.33 of the unlevered portfolio value. Assuming $10k cash it would attempt to buy 3.3k.
If you wanted to only use 5k then use MarketOrder("SPY", (int) (5,000 / price) )
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.
Iman Mohtashemi
Ok thanks!
Dan Whitnable
I'd suggest using the 'LimitOrder' (or more reluctantly the 'MarketOrder' or other explicit order type). The 'SetHoldings' method is a convenient shortcut when getting an algorithm flushed out, but doesn't offer the control one would typically require in actual trading.
If you wanted to only invest a portion of one's cash, then simply check for available cash, take a portion of that, then divide that by the current price of the security one wishes to purchase. That will be the number of shares to purchase. Something like this (in python).
# Get the current cash.
portfolio_cash = context.Portfolio.Cash
# Get the price of the security you wish to trade
# Price is a funky decimal type so change to float first
# Assumes 'symbol' has been previously defined
price = float(context.Securities[symbol].Price)
# Now figure out how many shares to order
# Assume one wants to only use 80% of their cash
# The // operator is the python 'floor' function
percent_of_cash_to_use = .80
net_cash = percent_of_cash_to_use * portfolio_cash
shares = net_cash // price
# Now simply order that many shares
# Set the limit price to the current price (or something else if one wishes)
# I always use a limit order to ensure I'm not surprised by the actual price
limit_price = price
context.LimitOrder(symbol, shares, limit_price)
Of course this may be more complicated depending upon what rules you want to use in determining orders, but it gives the general approach. You may also want to calculate the commission and check that it doesn't put you over the amount of cash available. This is basically what I do for my live trading.
Def returns
Was the ability to override the brokerage's equity mentioned by Alex earlier removed? I'm having trouble finding the “Live Algorithm Equity” button he mentioned in the project tab.
Varad Kabade
Hi Tomes,
Yes, it was changed with the recent updates.
To change the Cash amount in paper trading, we must click on the "Algorithm Cash State" tab and make the required changes.
Refer to the following doc for more information.
Best,
Varad Kabade
Iman Mohtashemi
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!