Hello All,
I am new to quantconnect and trying to learn my way around. I have browsed through some of the documentation and QCU algorithms and have decided to dive in. The attached algorithm is an attempt to simultate the basic moving average crossover model from Andreas Clenow's Following the Trend. The system is always in the market, going long and short when the 10 day SMA crosses the 100 day SMA. The algorithm should also set position size based on the ATR measured volatility. I was hoping I could get some assitance with that functionality.
So far the system trades a portfolio of six currency pairs, all using equal position sizes. I have found six to be the max number of pairs that can be used without causing memory errors during backtests. I also found that using the SetHoldings method to enter positions resulted in unpredictable order sizes. MarketOrders are used instead.
One other quirk I found was that the MarketOrder method causes errors when testing with daily resolution data. It appears that they are automatically converted to market on open orders which are not supported in forex testing. This was remedied by using minute data.
The last piece I need to code is the position sizing. However, I am finding the documentation somewhat cumbersome and was hoping I could get some help. The basic formula I need to reproduce is "Risk% x Equity / ATR * Pointvalue". I am having trouble finding some basic functions like how to query the account equity and how to determine the point value of a currency pair. Is anyone interested in collaborating, or pointing me in the right direction?
Alexandre Catarino
Hi Shawn,
For Portfolio Equity, use:
var equity = Portfolio.TotalPortfolioValue;
And for Point Value:
// E.g.: symbol = "EURGBP"; var lotSize = Portfolio.Securities[symbol] .SymbolProperties.LotSize; // lotSize = 1000 var conversionRate = Portfolio.Securities[symbol] .QuoteCurrency.ConversionRate; // conversionRate = 1.31819 (GBPUSD) var pointValue = lotSize * conversionRate; // pointValue = 1318.19 USD
I hope that helps!
Shawn Emhe II
Alexandre, thank you. That's exactly what I needed. When I get home I'll work that into the algo and post an updated version in case anyone is interested.
Alexandre Catarino
Shawn, you are welcome. Please post an updated version.
I need to note that I found out that people use standard lot size (100,000) intead of what our engine give use (1,000: this is FXCM minimum lot size) to calculate point value. You could try both and tell us what is the proper value.
Shawn Emhe II
Alexandre, I prefer the 1k lot size because it allows more granular control over the position size and risk. I have attached an updated version that uses the code you provided to dynamically size positions. The positions are sized so that the average daily movement (1 ATR) represents 20 basis points (0.2%) of the account.
I also worked a few other changes in to allow the system to trade all 10 currency pairs without memory errors. The trade logic has been moved into a scheduled function so that it only runs once per day, and the data is added to the strategy using hourly instead of minute resolution.
The next task I'm going to undertake is coding the second basic system Andreas provides in his book. So far the results confirm one of the central themes of his book, which is that even a simple system like this can produce positive results with proper diversification and risk management.
Chris J.T. Auld
Anyone looked at porting this to latest APIs? Guessing that TradeBars changed to QuoteBars for Forex?
Shawn Emhe II
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!