Hi QuantConnect Community,
I am building a simple equities trading algorithm that buys and sells SPY (or any specified equity) when Stochastic goes above and below certain values.
I am fairly new and have previously made successful crypto algorithms using a similar algorithm, which is where I got the framework from.
The backtest is returning no trades but no errors either.
Please let me know what I am doing wrong and how to solve it?
Thank you in advanced!
class TradierEquitiesStochAlpha(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 1, 1)
self.SetEndDate(2021, 2, 1)
self.SetCash(1000)
self.AddEquity("SPY", Resolution.Daily, Market.USA)
self.SetBrokerageModel(BrokerageName.TradierBrokerage)
self.sto = self.STO("SPY", 14)
self.sell_price = None
def OnData(self, data):
if not self.sto.IsReady:
return
price = self.Securities["SPY"].Close
if self.sto.Current.Value < 30 and self.Portfolio == 0:
self.Debug("Daily SPY STO is < 30")
self.MarketOrder("SPY", 1)
self.Debug(f"Market order was placed for 95% of protfolio in SPY")
self.sell_price = (1 + 0.05) * price
if self.sell_price is not None and price >= self.sell_price:
self.Debug("SPY sold at a 5% gain or more")
self.MarketOrder("SPY", -self.Portfolio.CashBook["SPY"].Amount)
self.sell_price = None
Varad Kabade
Hi Sandra Navarro,
When using daily or hourly data, if the algorithm places an order after the market is closed, the Market order gets converted into MarketOnOpenOrder, which is not supported by Tradier brokerage. Changing the brokerage fixes this issue. Alternatively, the algorithm will need to use high resolution data and place orders when the market is open.
Best,
Varad Kabade
Sandra Navarro
Hi Varad Kabade,
Thank you for the help!
How would excluding the self.SetBrokerageModel() method affect my alpha?
If I want to keep the self.SetBrokerageModel() method how would you go about restricting trades to specific times of the day?
Thank you again!
Vladimir
Sandra Navarro,
To trade this algorithm on TradierBrokerage you need
in self.AddEquity() set resolution to Resolution.Hour or Resolution.Minute.
self.AddEquity("SPY", Resolution.Hour, Market.USA)
This resolution setup determines how often OnData() will be called.
I would also recommend to use resolution setup in self.STO()
self.sto = self.STO("SPY", 14, Resolution.Hour)
This resolution setup determines data resolution for STO calculation.
It may be equal or lower frequency than in self.AddEquity().
Enjoy
Varad Kabade
Hi Sandra Navarro,
Information about tradier brokerage is not currently present in the Documentation; we will add it as soon as possible.
Refer to this doc for information about brokerage models and what property changes along with different models.
Thank you, Vladimir, for showing us the example.
Best,
Varad Kabade
Sandra Navarro
Thank you so much Varad kabade and Vladimir!
You have both been very helpful. I will implement the recommended changes!
Sandra Navarro
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!