#region imports
from AlgorithmImports import *
#endregion
class CreativeFluorescentYellowCaterpillar(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2011, 7, 6) # Set Start Date
self.SetEndDate(2013, 2, 1) #Set End Date
self.SetCash(100000) # Set Strategy Cash
self.stocks = ["AAPL","TSLA"]
self.Assets = {}
for stock in self.stocks:
ticker = self.AddEquity(stock, Resolution.Daily)
#symbol = self.AddEquity("TSLA", Resolution.Daily)
#symbol = self.AddCfd("WTICOUSD", Resolution.Daily, Market.Oanda)
#symbol = self.AddCrypto("BTCUSD", Resolution.Daily, Market.Bitfinex)
#symbol = self.AddCrypto("ETHUSD", Resolution.Daily, Market.Bitfinex)
self.Assets[symbol] = ticker.Symbol
self.Assets[rsi] = self.RSI(ticker.Symbol, 14, MovingAverageType.Simple, Resolution.Daily)
self.Assets[current] = None
self.Assets[entry_price] = None
self.Assets[highestPrice] = 0
def OnData(self, data):
for asset in self.Assets:
self.asset[current] = self.Securities[self.asset[symbol]].Close
if self.asset[current] == None: continue
# trading conditions, only when we are not invested
if self.asset[rsi].Current.Value > 60:
self.Debug("RSI is more than 60")
if not self.Securities[self.asset[symbol]].Invested:
self.SetHoldings(self.asset[symbol], 0.1) #invested 10% of our capital
self.asset[entry_price] = self.asset[current]
self.Debug("Entry at " + str(self.asset[entry_price]))
#exit conditions, only if we are invest
if self.Securities[self.asset[symbol]].Invested:
if self.asset[current] <= self.asset[entry_price] - (self.asset[entry_price]*0.05): #cut loss at 5%
self.Liquidate(self.asset[symbol], "stop-loss")
self.Debug("stopped out at " + str(self.asset[current]))
elif self.asset[current] >= self.asset[entry_price] + (self.asset[entry_price]*0.1): #tp > 10%
self.Liquidate(self.asset[symbol], "take-profit")
self.Debug("TP at " + str(self.asset[current]))
Hi all,
I am rather new to python and Quantconnect. I wanted to construct a portfolio with a mixture of equities, CFDs and cryptos. I do not wish to write multiple times AddEquities, AddCfd and AddCrypto. Any code examples i can refer to?
Any help would be greatly appreciated. Thanks in advance!
Adam W
Not sure I fully understand the question, but why not just loop over it like you are already doing:
Piggyinu
Wow… that's a great help… but how do i assess to the ticker.Symbol, ticker,rsi for the def OnData below?
Adam W
You can just store all of those things in another dictionary, like:
Piggyinu
self.Universe seems like a reserved word… Im having errors declaring self.Universe = { 'Equity' : ['AAPL','TSLA'], 'Crypto' : ['BTCUSD'] }
Adam W
Ah probably - maybe better to change it to `self.MyUniverse` or something.
Fred Painchaud
Hi,
It is already a method in QCAlgorithm.
https://lean-api-docs.netlify.app/classQuantConnect_1_1Algorithm_1_1QCAlgorithm.html#aefbc329edad228ad3ae3b1e87fcf6df8
So you are trying to make Universe point to something else. It would need to be a callable. Plus, LEAN will break. Use another name.
You'll also get a key error on
self.symbolData['ticker']['RSI'] = self.RSI(self.Symbol(ticker), #args)
You need to first define something for
self.symbolData['ticker'] = …
Usually, a SymbolData instance. And you would rather have:
self.symbolData[ticker] = …
Fred
Adam W
Good points - typed that up very quickly so hopefully not too confusing @piggyinu.
You can create some class to store the data as Fred suggested, manually populate the elements of `symbolData` as a dict of dicts, or use defaultdict:
Make sure you change the for-loop part to `self.symbolData[ticker]['RSI']` not `self.symbolData['ticker']['RSI']` as Fred pointed out.
Vladimir
Piggyinu
--> but how do i assess to the ticker.Symbol, ticker, rsi for the def OnData?
Try “RSI for equities and cryptos”.
If you are satisfied with my answer, please accept it and don't forget to like it
Piggyinu
Woo! Thank you so much! 😊seems like its getting some where… but there should be a logic error somewhere coz im having too much unrealised profits
Piggyinu
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!