Jan

Optimization moving average crossing strategy with “machine learning”

Category: Strategies By: Jan Created: March 10, 2020, 9:56 AM
March 10, 2020, 9:56 AM
Strategies
95 Comments
Optimization moving average crossing strategy with “machine learning”

This is a “machine learning” strategy for finding the best crossing strategy, comparing 70 different averages with each other, given a certain time frame and a certain market.

(The source of the very most of the averages is https://www.prorealcode.com/prorealtime-indicators/average-filter-regression/ posted by Laurenzo in 2017)

The strategy is simple:
If the first average crosses above the second average, go buy long
and vice versa if the first average crosses under the second average, sell short

It can be used for currencies and indices, presumably also for stocks (non-daytrading)

This strategy determines which of the 70 different averages is to be crossed with which of the same 70 different averages to give the best result in a certain market in a certain time frame, what in my opinion can be seen as a kind of  “machine learning”

Further added to this strategy:   (up to the user to adjust those variables if wanted)

  1. to protect the position, a stop loss (TSL) is added  (coded TSL = round(100/10000*close))
  2. exit a position every day at 16:00 hr (coded Defparam Flatafter = 160000) (Daytrading)
  3. limit to only one trade per day  (coded OTD = Barindex – TradeIndex(1) > IntradayBarIndex, If using “TradeIndex(3)”, it would allow 2 opening trades per day, TradeIndex(5) would allow 3 trades per day, and so on.
  4. Opening trades only between 8:00 and 11:30, for American trades other times can be set.

There are 70 different types of averages, (listed below), which are used to cross each other (the first is named MAType, to be crossing the second MATypeV2)

To limit zigzagging of the averages due to market noise, an exponential average of 30 is added before the crossing condition is measured (Coded as wAFR = exponentialaverage[30](AFR) and wAFRv2 = exponentialaverage[30](AFRv2))

To limit the possibilities, the period is set for all average-types to 15 (Coded as Period = 15, Period2 = 15), periods may be adjusted.

Different stop distances can be used or a trailing stop can be used.

Be aware that if you use this “machine learning” on a small set of data, the outcome will be over- fitted and a different optimized set of averages for a different small set of data will appear (this happens with all strategies, robustness of the strategy has to be tested).

Be aware when running the given strategy, already 70 x 70 combinations has to be optimized, which is already 4.900 combinations, which makes the optimizing process (very) slow. It might be a good idea to split the combination into 5 sets, lets say from 0 to 13 averages comparing to the 69 averages, and next 14 to 26 comparing to 69 averages, and so on, then taking the best from the 5 sets.

I run the code first without In Sample/Out of Sample, to find the  top 10 best average combinations, and around those top 10  run several In Sample/Out of Sample optimizations.

For In Sample/Out of Sample optimizations use 66% IS and 34% OOS, non repeated (not five runs)

The code is extremely long (> 3.600 lines), due to defining 70 and 70 different averages. Maybe it can be coded shorter ?

The crossing strategy itself can be found at the last rows !

I was not aware that PRT allows more then 3.600 lines of code as a strategy!

  1. Ahrens Moving Average
    1. Adjustable Lag FIR
    2. Arnaud Legoux Moving Average
    3. 2-Pole Butterworth Smoothing Filter
    4. 3-Pole Butterworth Smoothing Filter
    5. Corrected Moving Average by A.Uhl
    6. d9 Unscented Kalman Filter (Responsiveness Version)
    7. d9 Unscented Kalman Filter
    8. Double Exponential Moving Average
    9. Exponential Least Square Moving Average
    10. Exponential Moving Average
    11. Elastic Volume Weighted Moving Average
    12. Fast Adaptive Trend Line
    13. Fractional-Bar Quick Moving Average
    14. Fractal Adaptive Moving Average
    15. Generalized DEMA
    16. 1-Pole Gaussian Filter
    17. 2-Pole Gaussian Filter
    18. 3-Pole Gaussian Filter
    19. 4-Pole Gaussian Filter
    20. Hull Moving Average
    21. IE/2 Combination of LSMA and ILRS
    22. Integral of Linear Regression Slope
    23. iTrend by John Ehlers
    24. Jurik Moving Average (Responsiveness Version)
    25. Jurik Moving Average
    26. Kaufman Adaptive Moving Average
    27. Kalman Filter by John Ehlers
    28. Kalman Filter (Responsiveness Version)
    29. Kalman Filter
    30. Leader Exponential Moving Average
    31. Laguerre Filter by John Ehlers
    32. Least Square Moving Average
    33. McGinley Dynamic
    34. Middle High Low Range Moving Average
    35. McNicholl Moving Average
    36. Non Lag Moving Average (Responsiveness Version)
    37. Non Lag Moving Average
    38. One More Average
    39. Pentuple Exponential Moving Average
    40. Parabolic Weighted Moving Average
    41. Quadruple Exponential Moving Average
    42. Regularized EMA by Chris Satchwell
    43. Reference Fast Trend Line
    44. Reference Slow Trend Line
    45. Slow Adaptive Trend Line
    46. Simple Moving Average
    47. 2-Pole Super Smoothing Filter
    48. 3-Pole Super Smoothing Filter
    49. Smoothed Simple Moving Average
    50. Sine Weighted Moving Average
    51. T3 Moving Average
    52. Triple Exponential Moving Average
    53. Triangular Moving Average
    54. Time Series Average
    55. Variable Index Dynamic Average
    56. Variable Moving Average
    57. Volume Weighted Average Price
    58. Wilder Moving Average
    59. Weighted Least Square Moving Average
    60. Weighted Moving Average
    61. Zero Lag BMT
    62. Zero Lag Double Exponential Moving Average
    63. Zero Lag FIR Filter
    64. Zero Lag IIR Filter
    65. Zero Lag John Ehlers
    66. Zero Lag Weighted BMT
    67. Range based AV high + STD – low – STD
    68. Average highest [period] and lowest[period]
    69 DEMA adjusted by itself

Please download the attached itf (200308-Averages-crossing.itf) to play around with this strategy.

Download
Filename: 200308-Averages-crossing.itf
Downloads: 3537
Download
Filename: Euro-USD-5-min-result-nov18-mrt20.jpg
Downloads: 1892
Jan
Jan Veteran
Currently debugging life, so my bio is on hold. Check back after the next commit for an update.
Author’s Profile

Comments

Maik2404
8 months ago
#

Hallo zusammen, gibt es schon eine neue Version des codes? Alesandro?

Alessandro Furlani
2 years ago
#

Hi Ian, hope you still use PRT and so you can read this post. I have tested a lot your work and i think it is a very good idea. So, i have tried to improve it modifying a little bit your work. First of all i have Inserted some variables to make more easy the backtesting to find the best options for each financial instrument. In detail, i have added the following: 1. MinLotti, MaxLotti,MultiplierLot, LotMngt - To automate a Lot calculation where if the last trade was in loss the next trade will have a Lot increased by the MultiplierLot. If the last trade was in gain, nothing happen. 2. The Time to start and close was substituted by 2 variable so, it is possible to find the better time frame for any kind of symbol, future, etc... and it is adaptabe to the various markets who open at different time. Obviously for the point 1. i have added some code to make this strategy working fine. I'm also working to make dinamic Period and Period2 but as you know PRT has limit on the number of possible combination so i'm trying to find a smart way and i want to raise a question to you. Normally the cross between two or more MA is made using different period and if the period of the first MA is 10 (for example) we need at least a double period for the second MA. So i'm thinking to a mechanism that choose automatically Period2 based on Period so we could limitate the combinations. What do you think ? Do you have another idea ? In the meantime i would to share with you and the other friends my version of the script you create but in this interface it is not possible attach .itf files so, who want the code can contact me directly and i'll send it immediately or if you wants, you can ask me the file and add it to your original thread. My email is: alex.furlani@gmail.com Let me know

JADINVEST
3 years ago
#

Hello Jan, hello everyone, Thanks Jan for this strategy! Since 2020, have any of you found a strategy optimization/improvement? THANKS

Ciccarelli Franco
4 years ago
#

Per lasciare che la strategia venga eseguita (dopo aver importato il file): Basta eliminare MAType e MATypeV2 ai parametri di ottimizzazione (direttamente sopra la prima riga del codice) Quindi digitare il codice Once MAType = ciò che si desidera selezionare Once MATypeV2 = ciò che si desidera selezionare Spero che ti aiuti. Ho letto questo post e avendo lo stesso problema, chiedo dove si trova MAType e MATypeV2, io non riesco a trovarlo. Grazie

frenqle
4 years ago
#

Hi There.. It seems the TSL stop loss is not working.. it does not respond is that correct?

Jiankyr82
5 years ago
#

It is a very nice idea, I have taken so many ideas

ChasingProfits
5 years ago
#

I have now played around with this strategy for a while and my gut feeling is that it suffers from over-optimization. If you evaluate all the possible combinations you will find many that give fantastic results, but none of them provided any meaningful robustness upon closer inspection. So, did any of you find any combination on any index/time frame that provided you with any robustness?

Filoutube
6 years ago
#

Found the needed correction, just send the 2 Defparam lines at the very begining of the code, then no more error. https://ibb.co/SmhxqBp

tibo002
5 years ago
#

Bonjour Filoutube, je suis également bloqué comme vous mais votre dernier lien de photo avec la solution ne fonctionne pas. Pourriez vous m'aider ? Merci bcp !

Filoutube
6 years ago
#

https://postimg.cc/1fcBLjJj

Filoutube
6 years ago
#

Dear Jan, Thanks for your great work. I just tested the code and manage to follow the whole process except that I am blocked at this last step (as many of us it seems). I use PRT V11.1. Could you please let me know what has to be corrected in line 22 ? See attached picture for details Many thanks for your support. BR Philippe [url=https://postimg.cc/1fcBLjJj][img]https://i.postimg.cc/SNHPz08k/temp.png[/img][/url]

macdopa
6 years ago
#

Jan, hello and congratulations! This algo-strategy works wonderfully for time-frames longer than 5 minutes. Help: How do I use it in "real trading mode" with ProRealTime version 11.1 for auto-trading?

Berno
6 years ago
#

Hello Jan, thanks for your amazing work! :) I have one question: How much data have you found to be most efficient for optimizing the model? 1 - 2 months or longer?

Brisvegas
6 years ago
#

I would be surprised if this is anything but datamining TBH but i will have a play and see what we find .

superprof
6 years ago
#

bonsoir comment faire les tests In Sample et Out of Sample.? je ne comprend pas le sens

Jezza34
6 years ago
#

Thank you for that amazing program ! :-)

IvanFinanza
6 years ago
#

To activate it, I read that I have to remove the first 2 rows of code MATV1 and MATv2 ... I can't figure out where I am and if I need to delete the whole row or replace with other parameters Can you paste the code exactly by mentioning the line numbers? Thanks

IvanFinanza
6 years ago
#

Sorry, I found where I need to set MatV1 and 2 But to have it exactly identical to the BackTest with the 69 averages, how should I set it?

supermyguel
6 years ago
#

Hi Jan! In your opinion is it possible to set an instruction that every day, at a certain time f.e. 07am, automatically set the best value for the day?

Jan
Jan
6 years ago
#

Some late response of my site: same kind of question also asked in the beginning by MaoRai54, Your idea is to quasi optimise the "best" crossing every day. Therefore you should also determine the period to be back-tested every day. Please bear in mind that a short period with only a few trades is not very reliable, a different period with more trade may give completely different best crossings. The main assumption is that the / a short last period gives the best guidance for short term future movements, which I tend to believe is true. Program code concept could be as follows (not worked out yet), maybe the community gives excellent tips. When time = 70000 then Do for x bars backwards until now (period choosen for optimise crossing) For Avg1 to Avg 67 do For Avg21 to Avg2 67 do determine trade result for the period, given the crossing Next Next Select Avg and Avg2 which have given the best trade result Run the av crossing trading algo with the daily selected best Avg and Avg2 Be aware that running this kinds of code could be (terrible) slow ! This is true if you test this, I have not yet tested it in a strategy. Lots of discussion can be found at topic https://www.prorealcode.com/topic/machine-learning-in-proorder/

fleglo
6 years ago
#

Hello Jan, thank you very much for this tremendous and huge work done. It is really a fantastic time saving. After filtering here and there and adjusted to my creterias, it works properly. Thanks again for the time saving. Bravo! Fred

lisamitch50
6 years ago
#

Hi Jan, wondering i you can help please, can you let me know how i now implement the strategy the system has found please. Great code by the way and would LOVE to talk to you more about it. Please let me know if we can chat about it more. But i am struggling to "Use" the strategy that i feel fits my portfolio and way.. Many thanks.

oliverkeim
6 years ago
#

Hello, Please can you help. I use 10.3 and are not able to set the strategy live. "Folgende Veränderung müssen vor der Übermittlung des Codes an Proorder vorgenommen werden: Ersetzen Sie die Variable durch spezifische Werte. ( MA Type is set 10 & 30) Thanks Olli

Jan
Jan
6 years ago
#

It might be a good idea to reduce the average to 15 instead of 30 to reduce market noise effects, especially at higher timeframes. "To limit zigzagging of the averages due to market noise, an exponential average of 30 is added before the crossing condition is measured (Coded as wAFR = exponentialaverage[30](AFR) and wAFRv2 = exponentialaverage[30](AFRv2))"

Jan
Jan
6 years ago
#

Also the variables on top ( var1 0-69 and var2 0-69), those variables are needed to determine the best crossing and are not in the code, has to be deleted for Auto Trading

ProRealCode ProRealCode
Loading...