I have created a new chart in Initialize and added new series to that chart. I can successfully plot the daily open and ema from OnData, but can't find a way to plot the candles.
I can plot OHCL data separately but can't figure out how to plot them together.
from datetime import timedelta
from AlgorithmImports import *
STOCK = "EURUSD"
TP = 0.5
SL = 0.25
class DayOpenEma(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 1, 1) # Set Start Date
self.SetEndDate(2022, 1, 10) # Set End Date
self.SetCash(10000) # Set Strategy Cash
self.AddForex(STOCK, Resolution.Minute)
#constructing 3h candles
self.stock = self.Consolidate(STOCK,timedelta(hours=3),self.OnThreeHourCandle)
self.ema = self.EMA(STOCK,3,Resolution.Hour)
# warming up the ema
self.SetWarmup(3, Resolution.Hour)
overlayPlot = Chart("Overlay Plot")
overlayPlot.AddSeries(Series(STOCK, SeriesType.Candle, 0))
#overlayPlot.AddSeries(Series("Buy", SeriesType.Scatter, 1))
#overlayPlot.AddSeries(Series("Sell", SeriesType.Scatter, 0))
overlayPlot.AddSeries(Series("EMA", SeriesType.Line, 0))
overlayPlot.AddSeries(Series("Day Open", SeriesType.Line, 0))
self.AddChart(overlayPlot)
self.PlotIndicator("Overlay Plot",STOCK,self.stock)
# used to create daily open
self.previous_day = 0
self.day_open = 0
# used to create bracket orders
self.entry_ticket = None
# used to cancel the limit order after 24h
self.entry_ticket_time = None
self.order_timer = False
# handles the orders
def OnOrderEvent(self, orderEvent):
if orderEvent.Status != OrderStatus.Filled:
return
if self.entry_ticket is not None:
# if main order is filled
if orderEvent.OrderId == self.entry_ticket.OrderId:
# cancelling the 24h order expiration
self.order_timer = False
self.entry_ticket_time = None
# getting price and quantity of the fill
price = orderEvent.FillPrice
quantity = orderEvent.FillQuantity
# if order was long
if quantity>0:
# placing TP and SL
self.LimitOrder(STOCK, -quantity, round(price*(1+TP/100),5))
self.StopMarketOrder(STOCK, -quantity, round(price*(1-SL/100),5))
# if order was short
if quantity<0:
# placing TP and SL
self.LimitOrder(STOCK, -quantity, round(price*(1-TP/100),5))
self.StopMarketOrder(STOCK, -quantity, round(price*(1+SL/100),5))
# if TP or SL order is filled
else:
self.Transactions.CancelOpenOrders(STOCK)
self.entry_ticket = None
# every 3h candle
def OnThreeHourCandle(self,bar):
# warming up the ema indicator
if self.IsWarmingUp:
return
# getting the data
candleOpen = self.Securities[STOCK].Open
candleClose = self.Securities[STOCK].Close
candleTime = self.Time
ema = self.ema.Current.Value
dayOpen = self.day_open
# creating day open
if candleTime.day != self.previous_day:
self.previous_day = candleTime.day
self.day_open = candleOpen
dayOpen = candleOpen
#self.Plot("Overlay Plot", STOCK, self.stock)
self.Plot("Overlay Plot", "EMA", self.ema.Current.Value)
self.Plot("Overlay Plot", "Day Open", self.day_open)
# handles the order expiration
if self.order_timer:
# if todays day is greater than the day of the order
# if today hour is equal or bigger than the hour of the order
# means that 24 hours have passed from the order submission to now
if candleTime.day > self.entry_ticket_time.day:
if candleTime.hour >= self.entry_ticket_time.hour:
# the order is cancelled
self.Transactions.CancelOpenOrders(STOCK)
# order is removed
self.entry_ticket = None
# order timer is cancelled
self.order_timer = False
self.entry_ticket_time = None
# if we are not in the market
if not self.Portfolio.Invested and self.entry_ticket is None:
#self.Log("Candle Open:" + str(candleOpen))
#self.Log("Candle Close:" + str(candleClose))
#self.Log("EMA:" + str(ema))
#self.Log("Day Open:" + str(self.dayOpen))
# buy signal
if candleClose > dayOpen and candleClose > ema:
candleRange = Math.Abs(candleClose - candleOpen)
limitPrice = round(candleClose - 0.25 * candleRange,5)
quantity = self.CalculateOrderQuantity(STOCK, 1)
self.entry_ticket = self.LimitOrder(STOCK, quantity, limitPrice)
self.entry_ticket_time = candleTime
self.order_timer = True
# sell signal
if candleClose < dayOpen and candleClose < ema:
candleRange = Math.Abs(candleOpen - candleClose)
limitPrice = round(candleClose + 0.25 * candleRange,5)
quantity = - self.CalculateOrderQuantity(STOCK, 1)
self.entry_ticket = self.LimitOrder(STOCK, quantity, limitPrice)
self.entry_ticket_time = candleTime
self.order_timer = True
Nico Xenox
Hey Rupert Johnes,
QC isn’t made for charting but I can link you to different posts where this issue has already been discussed:
Link 1
Link 2 - potential solution (involves exporting candles to jupyter notebook)
Â
Rupert Johnes
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!