Back

SetCash() Method

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

Update Backtest








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

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.


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

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.


Ok thanks!

0

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.

0

Update Backtest





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.


Loading...

This discussion is closed