Back

Optimising our Trading Bot - Trading Bot in Python #7/#8

Hey everyone,

I've attached the backtest and algo for the two latest episodes of the python trading bot series. You can find #7 here and #8 here.

In these two episodes we develop an optimiser class using the CVXPY optimisation package, implement it into our trading bot and then evaluate the backtest with some extra charts.

Hope you enjoy it!

Ollie

Update Backtest








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.


Well done! Thanks.

0

Have you tried using skylight? Do you know how to synch pycharm with quantconnect cloud? How?

0

Hi Dave,

Syncing local QC project files with our cloud servers using Skylight is straightforward. Watch our latest YouTube video for a quick demonstration. For more information on Skylight, checkout our recent blog post about it: A New Era For Desktop Algorithmic Trading.

Best,
Derek Melchin

1

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.


Thanks for this wonderful series, Ollie. I am trying to understand certain things

In the SelectFine method, the secrities list from the main algo gets set to the selected universe and the method also returns the universe. does the returned universe have any function?

def SelectFine(self, fine):
universe = self.FilterFactor(self.FilterFinancials(fine))
# self.algorithm.Log(f"Universe consists of {len(universe)} securities")
self.algorithm.securities = universe
return [f.Symbol for f in universe]

 

0

Hey Mohamed,

The return universe is the set of securities whose data we are subscribed to. The data for those securities can be accessed in the AlphaModel within the Update method.

class MyAlpha(AlphaModel):

def Update(self, algorithm, data):
insights = []
for symbol in data.Keys:
# create insights
return insights

The algorithm Ollie created doesn't use this feature, however, a set of symbols, a empty array or Universe.Unchanged must be returned during universe selection, otherwise the algorithm will throw an error.Learn more in the documentation on Universe Selection and Alpha Creation.

Best
Rahul

1

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.


Thanks Rahul! Appreciate it!

0

Hey Ollie, 

I just cloned this and ran a backtest and for some reason in Nov 2008 I am getting it drop down to 0 dollars....its taking a big short in something I think. Anyway not sure if I am doing something wrong, I dont think I changed anything when I cloned your code above. Does this happen to you now?

0

Hi Jason,

I was able to backtest the strategy over November 2008 without experiencing the same issue. See the attached backtest for reference. I'd recommend cloning the original code files again to fix the issue.

Best,
Derek Melchin

1

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.


Is QC using the actual bid ask spread now or is that still on the TODO-list?

Since I thought I should contribute the way you did: Be wary about such small wins and losses. I got Sharpe 31 out of sample (June) but what I immediately pay attention to is an average loss of 0.02%, and I would expect the spread to be wider than that except maybe in the largest cap stocks at best of times.

Interesting code sample nonetheless.

1

Hi Petter,

Bid-Ask spread data (aka L1 data) is available for backtests, but it is not yet ready for Live Trading.

Best,
Shile Wen

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.


Hi Ollie,

Thanks for the great series.

I noticed that you defined a new class vs extended existing class when it comes to the Algorithm framework. For example: 

class ValueAlphaModel():

vs.

class NullAlphaModel(AlphaModel):

Are there any specific reasons why you chose the first but not the second? I am also surprised that the Algorithm object can pick up your newly created model. Could you shed some light here? Thanks!

Leo

0

Hi Dung Le,

Ollie's implementation does not implement the Algorithm Framework. We can see that because it doesn't use SetAlpha, SetPortfolioConstruction, etc... it only borrows the terminology.

The algorithm object is able to pick up the models used in the backtest even though they are not subclassing the built-in framework models because they are all called on in the RebalancePortfolio method in main.py.

def RebalancePortfolio(self):
alpha_df = self.CustomAlphaModel.GenerateAlphaScores(self, self.securities)
portfolio = self.CustomPortfolioConstructionModel.GenerateOptimalPortfolio(self, alpha_df)
self.CustomExecution.ExecutePortfolio(self, portfolio)

Best,
Derek Melchin

2

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.


Thanks for the detailed explanation, Derek. That makes perfect sense.

Leo

0

Update Backtest





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.


Loading...

This discussion is closed