Hi, I am building an algorithm that buys a specific crypto asset when the stochastic value drops below a certain number and to sell when it reaches a percentage of profitability.
I built the framework with interchangability in mind, meaning I can alternate between indicators like the stochastic, RSI, MACD, etc... I also wanted to allow myself to be able to interchange the percentage of profitability and purchase price points. This algorithm is meant to be used as a day trading tool.
The issues I have encountered so far are as follows (as displayed in backtesting):
1. The trading algorithm is not buying or selling according to the actual crypto asset chart (I.E. some days it only makes 1 if any trades some it makes many).
2. The algorithm is giving me a frequent error message stating that there is enough assets to proceed with a trade even though it proceeds to buy and sell (just not as much as it should be).
3. It delivers the "Price is at a X% gain" message (from the debug method) everytime a limit sell is placed not when it executes.
4. I have not been able to successfuly add a stop loss or stop market order into the algorithm because when I do, the assets are locked in the limit sell trade and cannot be used to place a market order sell.
Please let me know if and how to solve any of these issues and any imporvements I should make.
Thank you in advanced!
class BasicTemplateAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 2, 15)
self.SetEndDate(2021, 2, 19)
self.SetCash(100)
self.AddCrypto("ETHUSD", Resolution.Minute, Market.GDAX)
self.SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash)
self.sto = self.STO("ETHUSD", 14)
def OnData(self, data):
if not self.sto.IsReady:
return
if self.sto.Current.Value < 10 and self.Portfolio["ETHUSD"].Invested <= 0:
self.Debug("STO is low")
#self.MarketOrder("OXTUSD", 4000)
self.SetHoldings("ETHUSD", 1)
self.Debug("Market order was placed")
close = self.Securities["ETHUSD"].Close
quantity = self.CalculateOrderQuantity("ETHUSD", 0)
self.LimitOrder("ETHUSD", quantity, (1 + 0.01) * close)
self.Debug("Price is at a 1% gain")
Derek Melchin
Hi Sandra,
#1 Please clarify. The algorithm enters into ETH when
self.sto.Current.Value < 10 and not self.Portfolio["ETHUSD"].Invested
#2 We were unable to reproduce this error using the algorithm above.
#3/4 To resolve these issues, we can sell ETH and log the message when the price reaches the profit target level.
See the attached backtest for reference. Note that the SetHoldings and method have been replaced. When trading crypto, we should manually calculate the order quantity. This technique is demonstrated in the BasicTemplateCryptoAlgorithm.
Best,
Derek Melchin
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.
Sandra Navarro
Hi Derek,
Thank you so much! This is exactly what I was looking for!
The line below is where the algorithm purchases the ETH.
self.SetHoldings("ETHUSD", 1)
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!