class CalmLightBrownKitten(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1) # Set Start Date
self.SetEndDate(2021 , 1 , 1)
self.SetCash(1000) # Set Strategy Cash
self.btcusd = self.AddCrypto("USDTUSD", Resolution.Minute , Market.FTX).Symbol
self.SetBrokerageModel(BrokerageName.FTX, AccountType.Margin)
self.entryPrice = 0
self.nextEntryTime = self.Time
self.roc = self.ROC(self.btcusd, 6, Resolution.Hour)
# History warm up for shortcut helper SMA indicator
closing_prices = self.History(self.btcusd, 6, Resolution.Hour)["close"]
for time, price in closing_prices.loc[self.btcusd].item():
self.roc.Update(time, price)
self.RegisterIndicator(self.btcusd, self.roc, Resolution.Daily)
def OnData(self, data):
#usdt_price = data[self.btcusd].Price
self.Securities["USDTUSD"].Price
#self.Log(f"Time: {self.Time}; Price: {usdt_price};")
history = self.History(self.btcusd, 6, Resolution.Hour)
if not self.Portfolio.Invested:
if self.roc.Current.Value < -0.01:
if self.nextEntryTime <= self.Time:
self.Invested = self.SetHoldings(self.btcusd, 1)
self.Log("BUY USDTUSD @" + str(usdt_price))
self.entryPrice = usdt_price
if self.entryPrice * 0.01 < usdt_price:
self.Liquidate()
self.Log("SELL USDTUSD @" + str(usdt_price))
self.nextEntryTime = self.Time + self.period
Blackpanther
'''hi i am getting a continous error 'During the algorithm initialization, the following exception has occurred: Trying to retrieve an element from a collection using a key that does not exist in that collection throws a KeyError exception. To prevent the exception, ensure that the close key exist in the collection and/or that collection is not empty.
 at pandas._libs.hashtable.PyObjectHashTable.get_item
 File "hashtable_class_helper.pxi" in hashtable_class_helper.pxi: line 1613
 KeyError : 'close'' . Please help me to solve'''
Â
Nico Xenox
Hey blackpantherÂ
There are a few things here that might have to be changed.
That solves the error but there are a lot of other things for example:
Â
These are just a few things u might wanna go over your code again since there are other problems that might come up
Â
Â
Vladimir
Blackpanther
It looks like QC not ready for  USDTUSD
Try Formal Blue Shark for BTCUSD.
Fred Painchaud
Hi Blackpanther,
Here is your code debugged.
1- QC started to get data for FTX/USDTUSD later than 2020-1-1.
2- You misplaced .loc[].
3- You misspelled .item() → .items().
4- usdt_price was not defined anymore (commented out).
Now you're missing a variable - self.period. I'm letting you define it. timedelta(minutes=1) I guess?
Fred
Blackpanther
Hi , Fred i used the code which u had customized , but the problem is that the program does not match the expectation .What i wanted was when the price goes down  by -1%  then ‘buy’ and when it increases by +1% than what i had bought then ‘sell ’ .So by the  movement the i should have got 60% return on capital per month but in whole year it just gives me 0$ net profit . Guide me on this problem , and thanks for the help given at first it was really helpful Â
Code is the same which u have used
Â
    Â
Blackpanther
hi , some answers would be helpful
Blackpanther
hi , some answers would be helpful
Vladimir
Blackpanther
This is another attempt to recreate your strategy and visualize Price and ROC movement.
With your setup:
when the price goes down by -1% then ‘buy’ and when it increases by +1% than what i had bought then ‘sell’Â
‘buy’ threshold was hit only once 2020-03-13 07:40:00.
‘sell’ threshold was never hit.
You can try to play with the parameters of the strategy to make it more profitable.
Blackpanther
so , the code works correctly but the market is not that volatile , did i get it right?will the program work as  i expected in more volatile markets?
thanks for ur help vladimir.
Vladimir
Blackpanther
--> the code works correctly but the market is not that volatile, did i get it right?
Yes.
--> will the program work as i expected in more volatile markets?
Try what I have already recommended to you.
If you are satisfied with my answer, please accept it and don't forget to like it.
Blackpanther
Vladimir  hi , as u have suggested I tried the btc bot it works great . I will surely use it , can i ask u one more question. How can i trade btc in 5 min trade bar when only 30 seconds are remaining and the price is low , same with when the price is high and only 30 seconds are remaining for bar to close?
Vladimir
Blackpanther
Try this to get btc in 5 min trade bar.
Blackpanther
Â
Vladimir  what is that ?Â
Varad Kabade
Hi Blackpanther,
We use the helper method to create the ROC indicator and register the indicator for updates in the given algorithm. This would result in the indicator getting updated every hour due to:
Â
and be updated daily due to:
Â
This would result in wrong calculation; therefore, the algorithm will not work as expected.
Best,
Varad Kabade
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!