R/Empty4 indicator

Place your new trading idea here to see if someone can automate it.
6y588

R/Empty4 indicator

Post by 6y588 »

Hi All

For those who are interested in R and Forex, there are some good courses in http://www.coursera.org
a) Stanford Online Machine Learning (I am currently enrolled)
b) John Hopkins University Computing for Data Analysis
c) John Hopkins University Data Analysis
d) University of Washington Introduction to Computational Finance and Financial Econometrics

Coding in R is a steep learning path, but it should be easier once you have done the courses above (I have completed 3 of 4 courses above)

Given the wide and diversified interest in the R community, it is impossible to cover this reasonably in a forum. Hence, I will narrow my focus to these topics below:
a) Writing an indicator/expert advisor that integrates with R using 7bit's dll (https://sites.google.com/site/prof7bit/ ... tatrader-4).
b) Creating tools to help in compiling and debugging R/Empty4 code - as this process is a hazard because there are two syntax/languages that you have to decipher.
c) Creating R/Empty4 code that encapsulates time series analysis/prediction functions and packages in R. For example, to find the gradient of a slope, it is just two lines of code in R - "model = lm(x ~ y)" and "slope = model$coeff[2]".

To kick off discussion, I have created an R/Empty4 indicator that performs a gradient calculation of the last n bars. Basically, in a rising market, the gradient (or slope) of the line, which is a regression line calculated using lm() function in R and using the coeff[2], is positive. Conversely, in a falling market, the gradient is negative. Also, a steeper line will result in a higher slope.

If anyone is interested, I have attached the GradientVolatility.mq4 indicator file here.

Regards
You do not have the required permissions to view the files attached to this post.
Salbine
Posts: 1
Joined: Thu Nov 17, 2011 1:25 pm
Location: Perth, Ontario, Canada

Re: R/Empty4 indicator

Post by Salbine »

Hello:
Yes, it looks interesting. I downloaded and installed your indi , but it will not run on my platform.

The error message tells me that I do have " mt4R.mqh " included.
Could you please tell where to search for this file? Thanks,

Best wishes,

Salbine
6y588

Re: R/Empty4 indicator

Post by 6y588 »

There is a link in the above message that will take you to 7bit's home page.

However, for convenience I am including it in the steps below:

1) Install R project (I used version 2.15.3): http://tinyurl.com/cdpxela
- if you use a different version, make sure 32-bit exe is used and Rpath is correct.

2) Install 7bit's mt4R library (latest version): http://tinyurl.com/d9p7czb
- copy "mt4r.mqh" and "mt4r.dll" into "include" and "libraries" folders of your Empty4 installation directory.

3) Open R console application, and type in the prompt exactly below

Code: Select all

install.packages("zoo") 
- this will ask you for a mirror site (just select the site nearest to you).

4) Compile the "GradientVolatility.mq4" indicator in your Empty4 client/editor and attach it to a chart.
eigenvector
Trader
Posts: 20
Joined: Sat Jul 14, 2012 3:14 pm

Re: R/Empty4 indicator

Post by eigenvector »

Thanks for the tip on the courses! I've taken one of the listed courses and am enrolled in one currently.

Regarding point b) Creating tools to help in compiling and debugging R/Empty4 code... I've found that the easiest way to get things working properly with the least amount of aggravation is to first export data to an R workspace. Then after I've worked out the code directly in R that I wish to use in Empty4 or some other language such as C#, VBA, etc., I simply save the code as a script in R (if applicable) and source the script to load a particular function, or if single lines are necessary, just transcribe the code over from R to the target platform.

I find by doing the above, I don't have much need for debugging outside of transcription errors and typos.

Time series analysis is an interesting and deep topic. Thanks for posting the indicator and in particular, for presenting the idea in code. After studying the code, it presents a simple yet also deep idea that is quite interesting. I found the rollapply function to be quite interesting (wasn't aware of it) in applying a function based windowing algorithm. I have been experimenting with the concept presented in the code, in R and seeing some rather interesting results.

Even if nobody else stops by, I'm interested in keeping the discussion going.
6y588

Re: R/Empty4 indicator

Post by 6y588 »

I would definitely like to keep this thread alive, even if it is only a few people in discussion.

Regarding point b) this is one method to ensure that the R source code is probably debugged and tested before porting it to Empty4. Most of the errors are basic errors - such as not loading the library OR not initializing the variables. Fortunately, this can be mitigated using the source() function on a pre-tested R script.

Another method that I use is to generate MQ4 code (on-the-fly) in R. In theory, MQ4 has only one required function, i.e. start(). All other functions are optionally added. I use System() function to call the MQ4 compiler. It may sound convoluted, but the advantages are: (1) R/Empty4 syntax errors are reduced significantly, (2) MQL editor has been replaced by RStudio, (3) can focus more on the strategies rather than coding syntax.

The R library "nueralnet" has some useful neural net functions, which I had thought about generating to Empty4. However, as I am currently enrolled in Stanford Online Machine Learning I decided to wait until after learning Neural Net, where the students are expected to write their own Neural Net program using Octave. This would probably be a starting point for me to write my own neural net library in R, and perhaps using my own library instead.

As mentioned in my first post, I am keen on R/Empty4 code that encapsulates time series analysis/prediction functions. These are various links on time series functions in R:

a) Alternative to Monte Carlo Testing. URL: http://rbresearch.wordpress.com/2012/07 ... o-testing/
b) Neural Networks with R Simple Example. URL: http://gekkoquant.com/2012/05/26/neural ... e-example/
c) Drawdown Determined Position Size. URL: http://timelyportfolio.blogspot.sg/2012 ... -size.html
d) ARMA Models for Trading. URL: http://www.quintuitive.com/2012/08/22/a ... r-trading/

In particular, I find the ARMA/GARCH models fascinating as these are supposedly used by top hedge fund managers, such as Goldman Sachs, JP Morgan.
eigenvector
Trader
Posts: 20
Joined: Sat Jul 14, 2012 3:14 pm

Re: R/Empty4 indicator

Post by eigenvector »

Great links! I haven't seen too many articles that go into depth with applications of ARMA/GARCH models to trading so I may have to spend some time with that one. The infinite variability of future price paths makes projections a hit and miss operation. But simple price projections do have some merit at certain times, so perhaps the more sophisticated methods can show improved application. I'll add one more topic since linear regression has already been mentioned:

Cointegration Using R to test Pairs of Securities for Cointegration
adamfx
Posts: 4
Joined: Thu May 23, 2013 11:59 pm

Re: R/Empty4 indicator

Post by adamfx »

Dear Sir,

I was trying to compile the GradientVolatility.mq4 indicator.
However I was getting an error message on the line of code:

R = Rinit(Rterm,2);

"Rinit" function is not defined

I would be grateful for some help on how I can fix this to get it to compile.
6y588

Re: R/Empty4 indicator

Post by 6y588 »

adamfx wrote:Dear Sir,

I was trying to compile the GradientVolatility.mq4 indicator.
However I was getting an error message on the line of code:

R = Rinit(Rterm,2);

"Rinit" function is not defined

I would be grateful for some help on how I can fix this to get it to compile.
Follow steps in post #3 above. If that has been done, and it still does not work.

Then, change the "Rinit" to "RInit".
adamfx
Posts: 4
Joined: Thu May 23, 2013 11:59 pm

Re: R/Empty4 indicator

Post by adamfx »

Thanks, it now works great. I tried to code the auto.arima function from the forecast package but I .can't seem to get it to work. It would be great to see a few more examples which would help me figure this out.

adamfx
6y588

Re: R/Empty4 indicator

Post by 6y588 »

e) Down and Dirty Forecasting: Part 1 URL: http://0utlier.blogspot.sg/2013/05/down ... art-1.html

f) Down and Dirty Forecasting: Part 2 URL: http://0utlier.blogspot.sg/2013/05/down ... art-2.html
Post Reply

Return to “Ideas for Possible Automation”