LEAN is the open source
algorithmic trading engine powering QuantConnect. Founded in 2013 LEAN has been built by a
global community of 80+ engineers and powers more than a dozen hedge funds today.
Alpha League Competition: $1,000 Weekly Prize Pool
Qualifying Alpha Streams Reentered Weekly Learn
more
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?
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.
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.
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).
0
Edited by Alexandre Catarino
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
550
,
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?
0
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) )
0
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
550
,
Ok thanks!
0
Dan Whitnable
1.5k
,
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.
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.
Loading...
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!
You do not have enough QC Credit to send this award, get a QC Credit Pack
here.
Award Sent Successfully
Thank you for giving back to the community.
Processing...
Choose a Credit Pack
Payment Confirmation
QuantConnect Credit (QCC) can be applied to your cloud-invoices or
gifted to others in the community with Community Awards in recognition of their contributions.
Community Awards highlight the awesome work your fellow community members are doing and inspires
high quality algorithm contributions to the community. Select an option below to add
QuantConnect Credit to your account:
$5
500 Credit Points
 
$10
1,000 Credit Points
 
$18
2,000 Credit Points
10% bonus
$45
5,000 Credit Points
10% bonus
03/23XXXX XXXX XXXX 0592
We will charge your default organization credit card on file, click
here to update.