Hi , guys when i was working with rolling frame , i got this error can u tell me how to solve it?
I was comparing current bar to past bar value . If the past bar value is than -10% than the current value SetHoldings(self.crypto, 1.00)
but there was one obstacle , the runner gave this error :- Runtime Error: Trying to perform a summation, subtraction, multiplication or division between 'TradeBar' and 'float' objects throws a TypeError exception. To prevent the exception, ensure that both values share the same type.
at OnData
if currBar <= pastBar*-0.0001:
===
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 28 (Open Stacktrace)
Can anyone look into my problem?
from QuantConnect.Data.Market import TradeBar
CRYPTO = "BTCUSD"; BAR = 5 ;
class CryptoConsolidator(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 4, 1)
self.SetEndDate(2021, 5, 1)
self.SetCash(1000)
self.crypto = self.AddCrypto(CRYPTO, Resolution.Minute,Market.FTX).Symbol
self.window = RollingWindow[TradeBar](2)
def BarHandler(self, consolidated):
self.Plot("BTCUSD",self.Securities[self.crypto].Price)
def OnData(self, data):
self.window.Add(data[CRYPTO])
price = self.Securities[CRYPTO].Price #2
if not self.window.IsReady : return
currBar = self.window[0] # Current bar had index zero.
pastBar = self.window[1] # Past bar has index one.
self.Log("Price: {0} -> {1} ... {2} -> {3}".format(pastBar.Time, pastBar.Close, currBar.Time, currBar.Close))
if not self.Portfolio[self.crypto].Invested:
if currBar <= pastBar*-0.001:
self.SetHoldings(self.crypto, 1.00 , "buy")
elif self.Portfolio[self.crypto].Invested:
if currBar >= pastBar*0.001:
self.Liquidate(self.crypto,"sell")
Vladimir
Blackpanther
Try the algorithm with my changes.
It doesn't issue any of the TypeError exception you mentioned.
If you are satisfied with my answer, please accept it and don't forget to like it.
Blackpanther
Vladimir hi , vladimir your code worked for me , how can i add the data of my previous trade on which i had bought , simply how can i compare price of coin bought to current price ?
Vladimir
Blackpanther
If you are satisfied with my answer, please accept it. I'll be happy to answer other questions in another thread.
Blackpanther
Vladimir ok then
Vladimir
Blackpanther
Key words in my last post: “please accept it”.
Blackpanther
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!