Hi,
So, using my initial framework, this time I tried a trend based strategy for trading which makes use of a profit target to exit and an rsi indicator for its entry. The strategy performs very well achieving a return of over 1400%. Starting with cash $2000 I end up with just over $30000 over a period of almost 10 years. It achieves a win rate of 89% which a constant amount of risk per trade. To balance the portfolio, I use a few symbols at a time. The strategy performs very well and with better indicators may perform even better.
It would be fun to discuss what other indicators may do and may perform even better.
Deval Keralia
@Daniel Thanx for appreciating the strategy.
My risk control method takes into consideration the volatility of the market. To measure volatility it simply uses standard deviation. So if the market is highly volatile, the strategy would tend to buy accordingly so that it doesn't risk more than it is designed to. So, in this way no matter how fluctuating or unpredictable the market is you would never lose more than you tell it to but if it wins it could win big. This gives you the control on how much you want to risk per trade no matter which strategy you implement or how the market behaves. It is meant to give you more control over your losses. All of these calculations are made in the TradeProfile class.
I haven't faced any issues with the backtest. I think because the backtest is so big and it makes so many trades that it appear on a year by year basis. Just let it run it works well.
If you have more questions, feel free to let me know.
Joaquin
Hi @Deval
I like how your strategy looks. So I am trying to mimic your strategy in LEAN environment. As I don't have an FXCM demo account, and I had to download several years of Forex history, and I did have an Oanda demo account, I used it to download history for EURUSD, GBPUSD, NZDUSD and AUDUSD.
I have not tried yet to backtest your strategy in LEAN through the whole period 2007 - now, but I have backtest from 2015 until now and my results are very different from yours, and I don't know why...
These statistics are from a backtest from 20150101 until now.
Maybe the history is different from FXCM to Oanda and I must adjust it before backtesting?
Any ideas?
20160704 13:52:41 Trace:: Debug: Algorithm Id:(TrendVolatilityForex) completed in 973,64 seconds at 2k data points per second. Processing total of 2.408.249 data points. 20160704 13:52:44 Trace:: STATISTICS:: Total Trades 47024 20160704 13:52:44 Trace:: STATISTICS:: Average Win 0,02% 20160704 13:52:44 Trace:: STATISTICS:: Average Loss -0,16% 20160704 13:52:44 Trace:: STATISTICS:: Compounding Annual Return -0,710% 20160704 13:52:44 Trace:: STATISTICS:: Drawdown 12,800% 20160704 13:52:44 Trace:: STATISTICS:: Expectancy 0.000 20160704 13:52:44 Trace:: STATISTICS:: Net Profit -1,068% 20160704 13:52:44 Trace:: STATISTICS:: Sharpe Ratio -0.004 20160704 13:52:44 Trace:: STATISTICS:: Loss Rate 12% 20160704 13:52:44 Trace:: STATISTICS:: Win Rate 88% 20160704 13:52:44 Trace:: STATISTICS:: Profit-Loss Ratio 0.14 20160704 13:52:44 Trace:: STATISTICS:: Alpha -0.001 20160704 13:52:44 Trace:: STATISTICS:: Beta 0.054 20160704 13:52:44 Trace:: STATISTICS:: Annual Standard Deviation 0.095 20160704 13:52:44 Trace:: STATISTICS:: Annual Variance 0.009 20160704 13:52:44 Trace:: STATISTICS:: Information Ratio -0.121 20160704 13:52:44 Trace:: STATISTICS:: Tracking Error 0.111 20160704 13:52:45 Trace:: STATISTICS:: Treynor Ratio -0.007 20160704 13:52:45 Trace:: STATISTICS:: Total Fees $1880,96
Joaquin
Sorry, my fault. I've realized I had mistyped something, because I've rewritten again the whole CS program from scratch and now my backtest in LEAN looks equal to the one done in Algorithm Lab.
Just one comment @Deval , do you think this is a feasible strategy in real? I've seen all the orders are opened and closed in the same minute... Wouldn't we be under the terrible effect of the spread of these forex pairs in real conditions?
Joaquin
Sorry, again, my fault.. Orders were not opened and closed in the same minute, I was mistaken with the STOP MARKET orders, which are triggered just after launching MARKET orders.
I'm testing this strategy live in a demo account. I'll let you know thoughts and achievements.
Deval Keralia
Thanx Joaquin for taking time and reading my strategy. Yes maybe the FXCM and Oanda data is different. Also the fee structure for the two brokerages maybe different. You can tinker around with the parameters to adjust for it. The main focus here is he risk control model and the volatility framework and how it saves you from huge losses. The strategy that I am currently using is a very simple one. Maybe with a better strategy it may perform better.
I would love to know how your strategy does live on the market. :)
Deval Keralia
@daniel I have for a short time. It was pretty decent given how simple my signal was but my losses were minimal.
Brian Pfistner
Brian Pfistner
Deval Keralia
Do you mean value of the 'minimum trade amount' or the 'maximum trade amount'?
Brian Pfistner
Sorry, Maximum Trade Amount
Brian Pfistner
Are you having any issues with the orignal settings you posted? Today I'm getting a lot of Runtime Errors 'Insufficient buying power to complete order'
Jared Broad
The insufficient buying power errors are because you've increased the maximum trade size to $1M and there isn't enough capital to buy the portfolio of assets (5 assets x $1M would require 250x leverage). You should use a small maximum trade to keep the risk per trade small.
There is a genuine error though because one of the orders is less than 1000 EUR (minimum quantity for the brokerage). The fix is to set the quantity to 1000 if the quantity is too small in the tradeProfile.Quantity. This was caused by a recent fix to LEAN (previously trade was placed and ignored, now the trade is simply not placed so cannot be found).
public int Quantity { get { if (_volatility == 0) return 0; long quantity = (long)(_risk / _volatility); if (quantity > _maximumTradeQuantity) return _maximumTradeQuantity; if (quantity < 1000) { // The FIX quantity = 1000; } return (int)quantity; } }
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.
Brian Pfistner
running that now has issues.
Deval Keralia
A quick update on my initial strategy. This time with some minor bug fixes.
1. Added a check for quantity and maximum trade quantity when they are below 1000 to avoid it being 0.
// Calclate the quantity based on the target risk in dollars. public int Quantity { get { if (_volatility == 0) return 0; long quantity = (long)(_risk / _volatility); //Check if the value for the maximum trade quantity is less than zero to avoid placing 0 value trade if (quantity > _maximumTradeQuantity) { return _maximumTradeQuantity < 1000 ? 1000 : _maximumTradeQuantity; } return (int) quantity < 1000 ? 1000 : (int) quantity; } }
2. Added a check in OnData to see if all the tradeBars contains data while running. Previous implementation caused the program to crash on few occasions when the resolution was set to seconds since all the symbols may not be ready. The simple fix below fixes it. So you just wait until all the symbols have data.
public void OnData(TradeBars data) { //Checks if the tradebars for all the symbols are ready and present. if (data.Count < Symbols.Length) return;
If any more bug are to be found in the future, I would be more than happy to fix them. Thanx to community for pointing them out.
Joaquin
Hi @Jared, Hi @Deval,
regarding the "Insufficient buying power": I am testing the strategy in LEAN, with a demo account. Its balance is 99,000 €, and leverage is 50. I have RiskPerTrade set to 400, and MaximumTradeSize set to 100K. And I get lots of "Insuficient buying power". Sometimes I get 2 positions opened, even sometimes 3, but when these error emerges, even the stop orders are not always opened because the strategy "thinks" that it lacks buying power... Although only about 5% of leverage is being used.
I think there must be a bug in the code, but I haven't had the time to debug it... @Deval maybe you have some hint of what could be happening?
Roberto Terpilowski
I'm noticing the exact same issue using the paper trader. Orders can't be opened due to insufficient buying power, including stop loss orders. There have also been some wild swings in the portfolio equity from $100k to up to $200k and back down to $100k again. I'm seeing in the logs that there have been many orders with a size of 0 being submitted as well.
Jared Broad
@Roberto, stops show as 0 order quantity until they are filled.
The portfolio value swings was a bug fixed on Monday/Tuesday. If you stop and start your algorithm you'll get the new version. You may want to do a fresh clone so the charts don't show the swings (so the chart axis auto scales zoom into your area easier).
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.
Deval Keralia
@Roberto @Joaquin What brokerage are you using? What symbols are you using? Could you please post your algorithms here so I can debug them better? Looking at the code would help.
Thank You.
Stefano Raggi
Hi @Deval, sadly there is a bug in the algorithm that makes it "too good to be true" :)
The following line:
Securities[symbol].FeeModel = new ConstantFeeModel(0.04m);
should be changed to:
 Securities[symbol].FeeModel = new FxcmFeeModel();
FXCM commissions are $0.04/$0.06 per 1K lot:
https://github.com/QuantConnect/Lean/blob/master/Common/Orders/Fees/FxcmFeeModel.cs#L44
so the original algorithm is only charging 1/10th of the real fees.
Deval Keralia
@Stefano I set a constant fee because my quantity sizes were anyways about 3k-4k. With the current fxcm fee model it was charging me fees even for submitting my stop orders which is not the case in reality. Fees are only charged when the order gets filled. since it was amplifying my fees for just submitting the stop order I kept the fee value constant.
Deval Keralia
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!