Is there any example of a Forex day trading strategy implementation using the Algorithm Framework? I like the idea of separating alpha generation from universe selection, portfolio construction and other aspects - and I like the highly flexible Insight abstraction.
I just have problems fitting common day trading strategies into this framework.
More specifically:
- What is the recommended way of integrating position sizing, which can have a huge impact on a Forex strategy performance? As part of the AlphaModel, using Insight.weight? As a part of portfolio construction or risk management?
- How do profit targets and stop losses fit?
Rahul Chowdhury
Hey Filib,
1) You can use insight weights along with InsightWeightingPortfolioConstructionModel (IWPCM) to tune your position sizing. In IWPCM, the insight weight is used to determine what proportion of your available margin should be allocated to that position.
You can also directly control order sizing in the ExecutionModel since order sizing is an execution concern. In this case, you would have to make a custom execution model which determines position sizes from portfolio targets. Check out StandardDeviationExecutionModel as an example.
The approach you want to take will depend on your needs. You should thoroughly study your options before picking an approach.
2) There are two ways of approaching exiting positions using the framework.
The first approach is to tune the insight duration to make more accurate predictions about price movements. In this approach, the position exits when the insight expires or when the risk management model exits prematurely.
The second approach is to use flat insights to exit positions. For every Up/Down insight you create, you can create a flat insight with the same expiration to exit that position. In this approach, you can create an InsightCollection for each symbol. InsightCollections are a helper data structure which has methods like GetActiveInsights, which returns all active insights in that collection. Then using Insight.CloseTimeUtc, you can create a flat insight with the same expiration of each non-flat active insight you want to "cancel" out.
class MyAlpha(AlphaModel): def __init__(self): self.insightDict = {} def Update(self, algorithm, data): insights = [] .... return insights def OnSecuritiesChanged(self, algorithm, changes): for security in changes.AddedSecurities: symbol = security.Symbol if symbol not in self.insightDict: self.insightDict[symbol] = InsightCollection()
Filib Uster
Thanks, Rahul Chowdhury.
Attached, you find a multi time frame Forex Strategy I have been working on. The code is still a bit rough, but it works. I would like to port it to the Algorithm Framework in an early stage, so that I can hone the different aspects like alpha, order management and position sizing separately.
Could you help me separating the concerns using the Algorithm Framework? This could be a nice example for other users, as well....
Filib Uster
The attached backtest contains the basic outline of the target framework algorithm.
The Initialize method currently looks like this:
class MultiTimeFrameForexScalping(QCAlgorithm): def Initialize(self): self.SetStartDate(2019, 9, 10) # Set Start Date self.SetCash(100000) # Set Strategy Cash self.AddUniverseSelection(G10CurrencySelectionModel()) self.AddAlpha(MultiTimeFrameEmaAlphaModel()) self.SetPortfolioConstruction(ForexPortfolioConstructionModel()) self.SetExecution(StopLossAndProfitTargetExecutionModel())
Let's start with the AlphaModel.
from clr import AddReference AddReference("QuantConnect.Common") AddReference("QuantConnect.Algorithm") AddReference("QuantConnect.Algorithm.Framework") AddReference("QuantConnect.Indicators") from QuantConnect import * from QuantConnect.Indicators import * from QuantConnect.Algorithm import * from QuantConnect.Algorithm.Framework import * from QuantConnect.Algorithm.Framework.Alphas import * class MultiTimeFrameEmaAlphaModel(AlphaModel): '''Alpha model that uses an EMA cross to create insights''' def __init__(self, barHistoryWindow = 5, longLookBackPeriod = 21, mediumLookBackPeriod = 13, shortLookBackPeriod = 8): pass def Update(self, algorithm, data): insights = [] return insights class SymbolData: '''Contains data specific to a symbol required by this model''' def __init__(self, symbol, longLookBackPeriod, mediumLookBackPeriod, shortLookBackPeriod): self.Symbol = symbol
Rahul Chowdhury
Hey Filib,
I set up some of the alpha model using the elements from your algorithm. There are definitely still some issues as too many insights are being emitted. But the basic structure of the alpha model is there. Also, the portfolio construction can possibly inherit from EqualWeightingPCM or InsightWeightingPCM. This is a cool project to undertake and I'll definitely keep following it. Hopefully other members of the community will participate as well.
Best
Rahul
Filib Uster
Thanks! I really like how you redesigned the alpha model.
Filib Uster
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!