Hello guys,
i would like to extend the functionality of PortfolioTarget Class, which currently takes only Symbol and Quantity as arguments, with something like this:
class BasicPortfolioConstructionModel(PortfolioConstructionModel):
def CreateTargets(self, algorithm: QCAlgorithm, insights: typing.List[Insight]):
targets = []
for insight in insights:
target = Target(insight.Symbol, targetQty, insight.SourceModel, stop)
targets.append(target)
return targets
class Target(PortfolioTarget):
def __init__(self, symbol, quantity, alpha, stop) -> None:
super().__init__(symbol, quantity)
self.alpha = alpha
self.stop = stop
If I do that i get following error:
20210831 16:37:00.213 ERROR:: Extensions.SetRuntimeError(): Extensions.SetRuntimeError(): RuntimeError at 8/31/2021 4:37:00 PM UTC. Context: OnData Python.Runtime.PythonException: TypeError : No constructor matches given arguments: (<class 'QuantConnect.Symbol'>, <class 'int'>, <class 'str'>, <class 'int'>)
File "/Lean/Algorithm.Python/BasicPortfolioConstructionModel.py", line 46, in CreateTargets
target = Target(insight.Symbol, targetQty, insight.SourceModel, stop)
===
at Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw)
at Python.Runtime.PyObject.InvokeMethod(String name, PyTuple args, PyDict kw)
at Python.Runtime.PyObject.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result)
at CallSite.Target(Closure , CallSite , Object , QCAlgorithm , Insight[] )
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at QuantConnect.Algorithm.Framework.Portfolio.PortfolioConstructionModelPythonWrapper.CreateTargets(QCAlgorithm algorithm, Insight[] insights)+MoveNext() in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Algorithm/Portfolio/PortfolioConstructionModelPythonWrapper.cs:line 107
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
at QuantConnect.Algorithm.QCAlgorithm.ProcessInsights(Insight[] insights) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Algorithm/QCAlgorithm.Framework.cs:line 178
at QuantConnect.Algorithm.QCAlgorithm.OnFrameworkData(Slice slice) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Algorithm/QCAlgorithm.Framework.cs:line 163
at QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnFrameworkData(Slice slice) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs:line 600
at QuantConnect.Lean.Engine.AlgorithmManager.Run(AlgorithmNodePacket job, IAlgorithm algorithm, ISynchronizer synchronizer, ITransactionHandler transactions, IResultHandler results, IRealTimeHandler realtime, ILeanManager leanManager, IAlphaHandler alphas, CancellationToken token) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Engine/AlgorithmManager.cs:line 117
My understanding is, that it can not extend it, due to the PortfolioTarget and IPortfolioTarget.
So, is it possible without rewriting the core.
Best Regards!
Varad Kabade
Hi Sergej Gorev,
Unfortunately, it is not possible to do so.
Best,
Varad Kabade
Sergej Gorev
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!