I realize this might be more of a python related question but how do I go about doing this in Quantconnect? Lets say I have the straightforward like the following:
from QuantConnect import *
class NadionResistanceShield(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 1, 1) # Set Start Date
self.SetCash(5000) # Set Strategy Cash
self.tickers = ["SPY", "BAC"]
for symbol in self.tickers:
self.AddEquity(symbol, Resolution.Hour)
ema50 = self.EMA(symbol, 50, Resolution.Daily, Field.Close)
sma200 = self.SMA(symbol, 200, Resolution.Daily, Field.Close)
symbolData = SymbolData(symbol, ema10, sma200)
self.symbolDataBySymbol[symbol] = symbolData
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.BeforeMarketClose("SPY", 10), self.LongTrade)
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.BeforeMarketClose("SPY", 10), self.ShortTrade)
def LongTrade()
.......
def ShortTrade()
....
class SymbolData:
def __init__(self, symbol, ema10, sma200):
self.Symbol = symbol
self.ema10 = ema10
self.sma200 = sma200
but I want to take the LongTrade() and ShortTrade() functions out of this file and put them into another one that would get called by this. How would I go about doing that?
Derek Melchin
Hi Axist,
We can define the functions inside a new file (ex: functions.py). Then in main.py, we add
See the attached backtest for a full example.
Best,
Derek Melchin
Want to invest in QuantConnect as we build the Linux of quant finance? Checkout our Wefunder campaign to join the revolution.
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.
Axist
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!