Hello, I hope you all are having a great day. I started to work on a VWAP algorithm that trades with the intersection of VWAP and price but I realized that it misses trades regularly and the trades it makes are inconsistent with the intersections. Can anyone help?
# region imports
from AlgorithmImports import *
# endregion
class DeterminedAsparagusManatee(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 6, 22) # Set Start Date
self.SetEndDate(2020, 6, 23)
self.SetCash(100000) # Set Strategy Cash
self.ethusd = self.AddCrypto("ETHUSD", Resolution.Minute).Symbol
self.window = RollingWindow[Decimal](2)
self.vwap = self.VWAP(self.ethusd)
self.p1 = 0.01
self.p2 = 0.02
stockplot = Chart("TradePlot")
stockplot.AddSeries(Series("Price", SeriesType.Line, 0))
stockplot.AddSeries(Series("VWAP", SeriesType.Line, 0))
self.AddChart(stockplot)
def OnData(self, data: Slice):
self.window.Add(data[self.ethusd].Close)
if not self.window.IsReady:
return
pnl = self.Securities[self.ethusd].Holdings.UnrealizedProfitPercent
self.Plot("TradePlot", "Price", self.Securities[self.ethusd].Close)
self.Plot("TradePlot", "VWAP", self.vwap.Current.Value)
currentclose = self.window[0]
pastclose = self.window[1]
vwap = self.vwap.Current.Value
if not self.Portfolio.Invested:
if pastclose < vwap and currentclose > vwap:
self.Buy(self.ethusd, 1)
elif pastclose > vwap and currentclose < vwap:
self.Sell(self.ethusd, 1)
elif self.Portfolio.Invested:
if pnl > self.p2 or pnl < -self.p1:
self.Liquidate(self.ethusd)
Vladimir
Try this way.
If you are satisfied with my answer, please accept it and don't forget to like it.
Yuri Lopukhov
Hi, Vladimir last elif will never be checked. In fact it also contains a typo in condition.
Yuri Lopukhov
Also, results are quite different if you add brokerage model with this line:
so it is something to watch out for if you are planning to actually use this algorithm…
Vladimir
Yuri Lopukhov
Thank you. This helped me find a typo on line 35.
Here's the corrected version.
Ertuğ ümsür
Thanks for all the help. Did you understand why my code gave the wrong trade orders? Also, do you know the reason why when it is changed to the Binance market whole strategy collapses? It seems like the fees but it feels unrealistic.
Vladimir
If the algorithm I have attached to this thread solved the problem of your code:
it misses trades regularly and the trades are inconsistent with the intersections
please accept it and don't forget to like it.
Ertuğ ümsür
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!