Hi!
I'm trying to import data from a .CSV which I can easily modify in Excel to use it as a IDE to perform a simple strategy.
The strategy consists in having a list of instruments that I want to trade daily and I want to have simple rules for each one of them:
every instrument will have :
1)quantity
2)buy time
3)sell time
4) stop loss (for example 0.99 means 1% loss)
5) take profit (for example 1.02 means 2% gain).
I've developed and backtested the strategy and it works as intended, but I can't scale in my code to make it work for an arbitrary number of instruments:
Emiliano fraticelli
In this screenshot you can see the structure of my CSV, unfortunately the code works just for the latter instrument (IBM).
It doesn't work for both, let alone for n instruments...I don't know how to do it
Adam W
The issue here is that the trading logic references self.security throughout, but in this for loop:
for i in range(len(self.df)) : self.security=str(self.df.iloc[i,0]).replace(" ", "") self.quantity=self.df.iloc[i,1] self.AddEquity(self.security,Resolution.Minute).SetDataNormalizationMode(DataNormalizationMode.Raw) self.Debug(i)
you are overwriting self.security with every i. Since IBM is the last instrument in the loop, self.security == 'IBM'.
What you may want to do instead is:
def Initialize(self): # Create a list to hold stuff self.securities = [] self.quantities = [] for i in range(len(self.df)) : security=str(self.df.iloc[i,0]).replace(" ", "") quantity=self.df.iloc[i,1] self.AddEquity(security,Resolution.Minute).SetDataNormalizationMode(DataNormalizationMode.Raw) self.Debug(i) # Add to the list self.securities.append(security) self.quantities.append(quantity)
Then within the scheduled functions and OnData, loop over self.securities and do stuff.
Emiliano fraticelli
thank you very much Adam!
Emiliano fraticelli
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!