Portfolio variables update


I am new to Quantconnect.

I am trying to create a track of my portfolio value, by creating a list of it.

Running the following code, i can see that the variable self.Portfolio.TotalPortfolioValue doesn't update, and the list is not extended.

Maybe this is because of some python / C# incompability ?


from clr import AddReference

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Packets import *
from datetime import timedelta
import numpy as np

class BasicTemplateForexAlgorithm(QCAlgorithm):

    def Initialize(self):
        # Set the cash we'd like to use for our backtest

        # Start and end dates for the backtest.
        self.SetStartDate(2013, 10, 7)
        self.SetEndDate(2013, 10, 11)

        # Add FOREX contract you want to trade
        # find available contracts here
        self.AddForex("EURUSD", Resolution.Minute)
        self.AddForex("GBPUSD", Resolution.Minute)
        self.AddForex("EURGBP", Resolution.Minute)

        self.History(5, Resolution.Daily)
        self.History(5, Resolution.Hour)
        self.History(5, Resolution.Minute)

        history = self.History(TimeSpan.FromSeconds(5), Resolution.Second)

        for data in sorted(history, key=lambda x: x.Time):
            for key in data.Keys:
                self.Log(str(key.Value) + ": " + str(data.Time) + " > " + str(data[key].Value))

    def OnData(self, slice):

        self.Portfolio_value = list()

        for key in slice.Keys:
            #print(str(key.Value) + ": " + str(slice.Time) + " > " + str(slice[key].Value))
            self.Log("Key + ID " + str(key.ID))
            self.Log("Time " + str(slice.Time))
            self.Log("Open " + str(slice[key].Open))
            self.Log("High " + str(slice[key].High))
            self.Log("Low " + str(slice[key].Low))
            self.Log("Close " + str(slice[key].Close))
        self.Log("Portfolio value : " + str(self.Portfolio_value))
        self.Log("Portfolio value : " + str(len(self.Portfolio_value)))

Update Backtest

Meh, I have resolved the problem.

Had to declare the variable Portfolio_Value in Initialize() method.


Update Backtest


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.


This discussion is closed