Hi I was wondering how could i make my algorithm go:
-long when the ema indicator value > vwap indicator value
-short when ema < vwap
I'm quite sure that I have mistaken some points..
Could you help me?
Thankyou in advance for your time!
import numpy as np
from clr import AddReference
AddReference("System")
AddReference("NodaTime")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")
from datetime import datetime, timedelta
from System import *
from NodaTime import DateTimeZone
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Brokerages import *
from QuantConnect.Data.Market import *
def Initialize(self):
if self.Portfolio.TotalUnrealizedProfit > 1999.99: #take profit
self.Liquidate()
class DataConsolidationAlgorithm(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetCash(100000)
self.StopRisk = 1
self.SetStartDate(2018,1,1) #Set Start Date
self.SetEndDate(datetime.now()) #Set End Date
# Find more symbols here: http://quantconnect.com/data
self.AddEquity("BOXL", Resolution.Minute)
self.ema = self.EMA("BOXL", 30, MovingAverageType.Simple, Resolution.Minute)
self.vwap = self.VWAP("BOXL", 30, Resoltion.Minute)
# define our 5 minute trade bar consolidator. we can
# access the 5 minute bar from the DataConsolidated events
fiveMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=5))
# attach our event handler. The event handler is a function that will
# be called each time we produce a new consolidated piece of data.
fiveMinuteConsolidator.DataConsolidated += self.fiveMinuteBarHandler
# this call adds our 5-minute consolidator to
# the manager to receive updates from the engine
self.SubscriptionManager.AddConsolidator("BOXL", fiveMinuteConsolidator)
def fiveMinuteBarHandler(self, sender, bar):
'''This is our event handler for our 5-minute trade bar defined above in Initialize(). So each time the consolidator produces a new 5-minute bar, this function will be called automatically. The sender parameter will be the instance of the IDataConsolidator that invoked the event '''
self.Debug(str(self.Time) + " " + str(bar))
def OnData(self, data):
pass
if self.ema > self.vwap
return True
if self.ema < self.vwap
return True
Aaron Janeiro Stone
def OnData(self, data):
if self.ema > self.vwap:
self.MarketOrder("BOXL",1)
if self.ema < self.vwap:
self.MarketOrder("BOXL",-1)
For further info:
https://www.quantconnect.com/docs/algorithm-reference/trading-and-orders#Trading-and-Orders-Market-Orders
Pietro ciarmatori
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!