Don't have an account? Join QuantConnect Today
QuantConnect Community Discussions
QUANTCONNECT COMMUNITY
LEAN is the open-source algorithmic trading engine powering QuantConnect. Founded in 2012 LEAN has been built by a global community of 180+ engineers and powers more than 300+ hedge funds today.
Join QuantConnect's Discord server for real-time support, where a vibrant community of traders and developers awaits to help you with any of your QuantConnect needs.
The Open-Quant League is a quarterly competition between universities and investment clubs for the best-performing strategy. The previous quarter's code is open-sourced, and competitors must adapt to survive.
Discussion on trading and orders documentation in algorithm reference.
Continue ReadingRefer to our Research Guidelines for high quality research posts.
Create an account on QuantConnect for the latest community delivered to your inbox.
Sign Up Today
|
|
|||||||
|
|
||||||||
|
Documentation discussion algorithm-reference/trading-and-orders
QuantConnect Robot | September 2018
QuantConnectâ„¢ 2025. All Rights Reserved
Roland Le Franc
Is there any Trailing Stop Loss order type? If not, why?
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.
Alexandre Catarino
Hi Roland Le Franc, we have an open GitHub issue about it:
Add support for Trailing Stop Orders #2580
There is no particular reason other than it is more complex than the other types and there was no demand.
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.
Andrey Solovyev
I have a question - is there any way of forcing the price to be taken by an order while backtesting? Ie I would like to enter long position with ask price and close long position with bid price. Or, to open a position with Open and close that with the next after a trigger Bar Open. How can I implement that in code?
Thanks a lot for answering.
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.
Jack Simonson
Hi Andrey,
LEAN supports custom fill models which can be used to force order fulfillment based on customized criteria. The documentation section can help illustrate how these might be implemented.
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.
Andrew Angeles
Building an algo using OandaBrokerageModel, not sure how best handle:
"Backtest Handled Error: OrderID: 1 Warning - Code: NotSupported - The OandaBrokerageModel does not support MarketOnOpen order type"
From what I've read, a signal a trade signal is generated, I call SetHoldings(_fx.Symbol,1); or Liquidate(_fx.Symbol); and a market order is attempted. However, because the order cannot be filled in the current session it is converted into a MarketOnOpen, which is not supported by FXCM or Oanda. My question is, how do I get arround this?
The last piece of information I gathered from Alexandre Catarino's post here:
https://www.quantconnect.com/forum/discussion/1761/just-downloaded-lean-have-a-few-questions/p1The 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.
Jack Simonson
Hi Andrew,
The best way to get around this would be to use the Order Status and Order Ticket features, whose documentation can be found here, to cancel any open orders at market close and then re-submit them upon market open. This is likely the best way to accomplish this, assuming you want to place the order on market open no matter what happens overnight.
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.
Laszlo Pasztor Questive Ltd INVESTOR
Is there a way to use OCA Group orders with IB?
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.
Link Liang
Hi Laszlo,
There are certainly ways to implement OCA orders. In fact, there are many discussions about One-Cancel-Others orders in the community, so I recommend to search in our forum. You might want to read this and this.
Meanwhile, you may subscribe to this Github Issue for updates of IB API support. Hope it helps.
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.
Laszlo Pasztor Questive Ltd INVESTOR
Thanks for your comment. IB API order Class has OcaGroup
property. One can set it when submitting the order. So there is no way I can Set that in quantconnect? I would prefer IB monitor and managa the OCA Froups rather me in my code.
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.
Link Liang
Hi Laszlo,
Thanks for the information you provided. We will work on implementing this IB API. Stay tuned!
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.
Meghdoot dhameliya
I'm working on custom brokerage, i have following queries
1) how can I check the trade placed for intraday order? by using security holding i am only getting overnight orders
2) brokers api return overnight order in holding after t + 1 day, how can i cancel that order on same day
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.
Jared Broad
Hi meghdoot dhameliya I think this would be best discussed on GitHub with an open pull request. Here it's hard to really understand your needs as the problems you're describing are abstract.
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.
Ryland Mathews
def OnData(self, slice):
if slice.Bars.Count == 0: return
if self.changes is None: return
# start fresh
self.Liquidate()
percentage = 1 / slice.Bars.Count
for tradeBar in slice.Bars.Values:
self.Log("trade bar {0}".format(tradeBar))
self.SetHoldings(tradeBar.Symbol, percentage)
self.Log("percentage {0}".format(percentage))
# reset changes
self.changes = None
when using setholdings() and liquidate() like above what type of order is being used? and when is it being executed. I get a backtest but I cant tell if its liquidating on open? on close? (assuming daily data) thanks!
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.
Alethea Lin
Hi Ryland,
Both SetHoldings() and Liquidate() places market orders. Since they are called within the OnData() block, they will execute on open of the next day assuming daily data. Please refer to "Understanding Time" for more information! Below is a quote from the documentation page:
"Once a datapoint's EndTime has passed it will be transmitted to your algorithm OnData() method. For bar data, this is the beginning of the next bar. "
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.
Laurent Crouzet
Jared Broad : thanks a lot for the update of this page, which is really helpful!
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.
Alexandre Catarino
Hi Carlos Rendon ,
QuantConnect/lean does have such feature: TradeBuilder. Please check out the following thread:
Are there any examples of using TradeBuilder/StatisticsBuilder classes in Python? #3251
for details.
We will add information about the IAlgorithm.TradeBuilder as soon as possible.
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.
Carlos Rendon
Thanks for the quick response Alexandre!!
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.
Shailesh Raval
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.
Tyler Bandy
Thanks for writing this Quantopian transition guide. In Quantopian's Morningstar fundamentals, you can get a company's sector with morningstar.asset_classification.morningstar_sector_code. I don't see an equivalent version in Quant Connect's Morningstar documentation. Am I missing it? Is there some other way to filter by sector?
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.
Yan Xiaowei
Hi Tyler Bandy
Check out the doc for Morningstar fundamental data here
I also made a simple demonstration algo based on Alexandre Catarino 's example above.
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.
Jared Broad
Vladimir Prelovac - We don't limit account size; but we'd prefer institutional investors contact us and setup a institutional / dedicated account.
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.
Tyler Bandy
Hi Yan Xiaowei,
Thanks for the response. I was already familiar with that Morningstar fundamental doc. It has the other fundamental factors I want, but the Morningstar Sector Code seems to be missing. The IndustryTemplateCode that you used in your demonstration only differentiates between normal, mining, utility, transportation, bank, and insurance. Unfortunately, I need a little more granularity. The Quantopian Morningstar Sector Code is a 3 digit code that represents different sectors. Since Quant Connect seems to have most of the same Morningstar data, I thought it would be available.
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.
Eric Novinson
Thanks for the tutorials, cloning them and testing them out now.
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.
Jonathan Gomez
Having some trouble with getting data from history. Since its bars I assume that the OHLC data can be called via ['open'] ['close']. Shouldnt this work if I wanted to get a list of close data where close[-1] gives the last bar close.
data = self.History(self.qqq, period, Resolution.Daily) close = data['close'] #So that close[-1] > close[-2] = 1 bar ago close greater than 2 bars ago.
Also in history is volume included in this bar object?
Thanks
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.
Jonathan Gomez
For example to get something like this working.
import numpy as np import pandas as pd class TestAlgo(QCAlgorithm): def Initialize(self): # Backetst Info self.SetCash(25000) self.SetStartDate(2009,1,1) self.SetEndDate(2011,1,1) # Initialize Assets self.AddEquity('QQQ') self.qqq = 'QQQ' #Schedule self.Schedule.On(self.DateRules.EveryDay("QQQ"),self.TimeRules.AfterMarketOpen("QQQ"),Action(self.trade)) def trade(self): period = 10 data = self.History(self.qqq, period, Resolution.Daily) close = data['close'] if (close[-1] > close[-2]): self.SetHoldings(self.qqq,1.0) else: self.SetHoldings(self.qqq,0)
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.
Eric Cheshier
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.
Alexandre Catarino
Jonathan Gomez, if you want a pandas.DataFrame of the Historical Request, you need to use a list of simbols for the first parameter:
data = self.History([self.qqq], period, Resolution.Daily) if data.empty: return
I found out that History Request for Daily QQQ is returning an empty list. We will look into it.
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.
Alexandre Catarino
We create an example to show how to use a simple indicator with universe selection.
A dictionary is created to save object that keeps the state of a exponencial moving average cross:
class SymbolData(object): def __init__(self, symbol): self.symbol = symbol self.tolerance = d.Decimal(1.01) self.fast = ExponentialMovingAverage(100) self.slow = ExponentialMovingAverage(300) self.is_uptrend = False self.scale = 0 def update(self, time, value): datapoint = IndicatorDataPoint(time, value) if self.fast.Update(datapoint) and self.slow.Update(datapoint): fast = self.fast.Current.Value slow = self.slow.Current.Value self.is_uptrend = fast > slow * self.tolerance if self.is_uptrend: self.scale = (fast - slow) / ((fast + slow) / 2)
We select the securities that are up trending and get those with the highest "scale":
# In the selector method: CoarseSelectionFunction # Filter the values of the dict: we only want up-trending securities values = filter(lambda x: x.is_uptrend, self.averages.values()) # Sorts the values of the dict: we want those with greater difference between the moving averages values.sort(key=lambda x: x.scale, reverse=False)
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.
Jared Broad
Resolution of Universe Data:
A common misunderstanding is this line of the universe selection:
self.UniverseSettings.Resolution = Resolution.Daily
This means the assets being added to your universe will be added at Daily resolution. It defaults to Minute resolution (i.e. universe added securities are in Minute resolution).
It is great for doing really fast backtests -- but it also means all market orders (or SetHoldings) will be converted to MarketOnOpen orders as when the daily bar arrives -- the market has already closed.
As the orders are placed when market is closed it can also make portfolio modelling tricky as we don't know what price the market order will fill for -- so some orders may be rejected due to insufficient margin. Those orders will show up as Invalid in the console.
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.
Alexandre Catarino
Leverage
On Quantopian, there is no limit to the amount of money that can be invested by an algorithm, regardless of its starting capital, so controlling cash and leverage is necessary.
On QuantConnect, each asset has its own pre-defined leverage (2 for Equity, 50 for Forex and CFD, etc). This value are used in margin models to check whether there is margin for an order to be filled. We can change the default values in two different ways: either using the method to subscribe to the security or the SetLeverage mthod that belong to the Security object:
AddEquity("SPY", Resolution.Minute, Market.USA, True, 1)
# or
spy = AddEquity("SPY")
spy.SetLeverage(1)
# or
self.Securities["SPY"].SetLeverage(1)
If we want to use all the leverage in SetHoldings helper method, we can verify the leverage the secutiry has and multiply it by the target percentage:
import decimal as d
spy_leverage = self.Securities["SPY"].Leverage
qqq_leverage = self.Securities["QQQ"].Leverage
self.SetHoldings("SPY", d.Decimal(0.5) * spy_leverage)
self.SetHoldings("QQQ", d.Decimal(0.5) * qqq_leverage)
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.
Jonathan Gomez
import numpy as np import pandas as pd import talib as tb class TestAlgo(QCAlgorithm): def Initialize(self): # Backetst Info self.SetCash(25000) self.SetStartDate(2009,1,1) self.SetEndDate(2011,1,1) # Initialize Assets self.AddEquity('SPY') self.spy = ['SPY',0] self.stocks = [self.spy] #Schedule self.Schedule.On(self.DateRules.EveryDay("SPY"),self.TimeRules.AfterMarketOpen("SPY"),Action(self.trade)) def trade(self): period = 30 spy = self.spy for i in self.stocks: data = self.History([i[0]], period, Resolution.Daily) i[1] = data['close'] rsi = tb.RSI(spy[1],timeperiod=14) for i in self.stocks: if (i[1][-1] > i[1][-2]) and (rsi[-1] > rsi[-2]): self.SetHoldings(i[0],1.0) else: self.SetHoldings(i[0],0)
Just a note, I write things in loops because later on there would be more than just 1 security. If I can get it working for 1 I can get it working for all.
So in Quantopian when you used talib you would be able to source the data as the close (aka spy[1]) and then it would spit out a list of values I could reference using [-1] for last bar, [-2] for 2 bars ago ect. When I added rsi[-1] > rsi[-2] it breaks. I assume just as in price its not outputting a list?
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.
Jared Broad
Specifying Assets - Symbols
In Quantopian you specify a security id sid(#). The number is unique from the QP database.
In QuantConnect you should use the string name of the ticker, as of today/when you launch the algorithm (or use universe selection). We use that string to look up the Symbol object.
Under the surface we use Symbol objects to uniquely identify securities. These are class objects which encode all the information required to uniquely identify a symbol. This information includes: original ticker string, list date of the security, security type, contract expiry, contract strike price.
We encode it into a symbol string: e.g. YHOO R735QTJ8XC9X is actually the original list string, YHOO, with the rest of the information encoded into the hash string after it. Over the last few years that company was renamed to AABA. For more information on decoding this see the symbol class. If you specify "AABA" we automatically look up the right Symbol object for you.
Common Issue
Because of delistings, renames etc the encoded symbol isn't neccessarily the same as the ticker, People often do things like this:
Symbol _spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
Which is OK for stocks which don't really change symbol but sometimes with complex symbols like GOOG this can cause trouble. The SPY string is the ticker string *at the listing date*. Depending on the listing date of the ticker you might select the wrong one by accident. The listetd GOOG is today called GOOGL.
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.
Takis Mercouris
Thanks for writing these short helpers. Regarding history requests for a particular symbol, is it possible to get the history in the form of a panda data frame or Series timeseries object? I can't seem to find a way to get the date part of the TradeBar enumerations returned by self.History(symbol, n, Resolution.Daily).
Thanks for your help,
Takis
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.
AMDQuant
Hi Takis,
I used np.array to convert a (pandas?) series into a (numpy?) array. I imagine you may find a similar code on google. I also wanted to post how I was able to 'resample' the minutely pricing into 30-Minute time bars for anyone interested. I'm certain there is a better way, but it works for now.
for stock in self.stock_list: _30mprices = [] _30mrsi = [] pricesm = self.History(stock, 8000, Resolution.Minute).close price_array = np.array(pricesm) #until code can be resampled, iterating through numbers to pull close of last minute of 30 minute time period. for i in range (1, 250): _30mprices.append(price_array[(29-i*30)]) #list[-1], list[-31], list[-61], etc. _30mprices.reverse() #To correct the order _30ray = np.array(_30mprices) _30mrsi = talib.RSI(_30ray, timeperiod=14)
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.
AMDQuant
Also Takis, Alexandre probably provided an answer earlier in this thread.
""
Alexandre Catarino
Staff Pro , 19 hours ago, ,
Jonathan Gomez, if you want a pandas.DataFrame of the Historical Request, you need to use a list of simbols for the first parameter:
I found out that History Request for Daily QQQ is returning an empty list. We will look into it.
""
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.
Jared Broad
QuantConnect Debug Message Flooding:
Its common for people to put a debug message into the primary flow of data. As we're working with high resolution data that can result in millions of debug packets being streamed to the browser; slowing everything down and often crashing your browser tab. To prevent these self-DDOS attacks we require debug messages to be slightly different each time. An exact duplicate message will be filtered out. You can simply get around this by putting the time onto the message:
self.Debug(str(self.Time) + " Test")
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.
Takis Mercouris
Addison/Jonathan, Thanks for the answer -- the pandas dataframe returned when you use a list of symbols in the history request is what I was looking for.
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.
LukeI
Three questions, sorry for the length, I thought this was going to be shorter when I started typing:
1. When do I need to use the warmup functionality? If I use "data = self.History([self.qqq], period, Resolution.Daily)" do I also need to use warmup? If my course and fine universe selection selects stocks based on a 30 day moving average crossover do I need to warmup?
2. Is there a 'best practice' to calculate stuff prior to market open? The equivalent quantopian code would be:
def before_trading_start(context, data): #Run before trading
Which runs about 45min before trading begins for the day.
3. What order do all the functions run, or is there a specific time that the algo beings executing? If I have a function scheduled to run at 10:00am and a function that is running every minute with OnData(self, data) which one will fire first? What time of day does the algorithm universe selection run?
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!