I desparately need help replacing my Quantopian language with QuantConnect language. Can anyone advise how I can replace items 1 through 5 below? See the log below the code to see the result.
#The following code has been adapted to ask the above question in forum.
for stock in context.portfolio.positions:
avg_price = context.portfolio.positions[stock].cost_basis
shares_qty = context.portfolio.positions[stock].amount
total_cost = avg_price * shares_qty
context.do[stock] = 1
print "1-" #Identifier to aid with reading log.
print context.portfolio.positions #Outputs dictionary, with info on stock and related info
print "2-"
print context.portfolio.positions[stock].cost_basis # Average cost per share for stock.
print "3-"
print context.portfolio.positions[stock].amount # Number of shares owned of this stock
print "4-"
print context.portfolio.positions[stock].last_sale_date #When did this position open? (my algo only opens and closes, no rebalancing, so it will be the date when position opened)
print "5-"
print context.portfolio.portfolio_value #Whats the total account value? = Cash + Current value of positions.
2017-07-24 08:45 PRINT 1-
2017-07-24 08:45 PRINT {Equity(3496 [HD]): Position({'last_sale_date': Timestamp('2017-07-21 14:01:00+0000', tz='UTC'), 'amount': 24, 'last_sale_price': 146.6, 'asset': Equity(3496 [HD]), 'cost_basis': 146.42667096119587})}
2017-07-24 08:45 PRINT 2-
2017-07-24 08:45 PRINT 146.426670961
2017-07-24 08:45 PRINT 3-
2017-07-24 08:45 PRINT 24
2017-07-24 08:45 PRINT 4-
2017-07-24 08:45 PRINT 2017-07-21 14:01:00+00:00
2017-07-24 08:45 PRINT 5-
2017-07-24 08:45 PRINT 7504.15989693
AMDQuant
(bump) Can anyone offer advice on this? If you are reading this, but don't have time to answer right now, can you advise a simple yes, no, or sort-of, on whether the replacement (Python) code exists?
Petter Hansson
I can't help with Python specifically, but have a look at the class SecurityHolding accessible from Portfolio property of algorithm. In C# I can e.g. do
Log("Average cost of SPY: " + Portfolio["SPY"].AverageCost);
Search for SecurityHolding and SecurityPortfolioManager here, I couldn't get correct links to them for some reason:
Jared Broad
the SecurityHolding class is the core model under the surface. You can see all the supported properties on there: this is a link to the class -
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.
AMDQuant
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!