Example of R script that uses "MetaTrader5" (Python) package to load quotes. The "reticulate" library must be installed in R. This library provides interaction with Python. Read carefully here and here. The first time you run the reticulate library you will be offered to install miniconda. I recommend that you do not refuse. This will create the conda environment r-reticulate with python(3.6.xx) and an initial set of packages installed. If you have already installed Pyhon (or several versions) at the beginning of the script you should activate required environment. All commands of MetaTrader5 package are here.
We’re excited to announce that 1.14 is now available on CRAN! You can install it with: With this release, we are introducing a major new feature: can now automatically configure a Python environment for the user, in coordination with any loaded R packages that depend on . This means that: Ultimately, the goal is for R packages using to be able...
I think that the number of deals does not need to be counted here. And just subtract the spread with the commission from each deal. That's the way it is:
c "rle" you can count the number of signal changes, those number of deals
sig <- c(1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,1)
> sig
[1] 1111100001110000111
> rle(sig)
Run Length Encoding
lengths: int [1:5] 54343
values : num [1:5] 10101
You can see by "values" that the signal changed 5 times, it means that there were 5 deals.
Vladimir Perervenko:
I do not understand the difference.
I think my implementation takes into account commission as if "of the first trade, the opening of which we don't have, there is only the closing"
sig <- c(1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,1)
[1] 1111100001110000111
Like for this deal, my variant takes a commission
and your version starts charging this one, your version is more correct, but it's easy to correct, just subtract "1"
sig <- c(1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,1)
> sig
[1] 1111100001110000111
With "rle" you can calculate the number of signal changes, i.e. number of deals
It is clear from "values" that the signal changed 5 times, so there were 5 trades.
I think my implementation also takes into account the commission and as "the first trade, the opening of which we do not have, there is only the closing"
like for this deal, my variant takes a commission
and your version starts to take this one, your version is more correct, but it's easy to correct, just do, just subtract "1"
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Do you happen to have a function that calculates both the net worth and the commission?
And you do not have by any chance a function which counts and equities and commission?
Language, exchange, forex? I don't know what you mean.
Language, Stock Exchange, Forex? I don't know what that means.
R-ka, forex.
Well a function that would calculate the balance chart for trades + commission\spread (costs)
I wrote this, I don't know if it's correct
calc.balance <- function(sig,x, comision=0){ sig <- ifelse(sig>0, 1, -1) trades.count <- length(rle(c(sig))$values) # сколько было сделок comis <- comision*trades.count sig <- na.omit( dplyr::lag(sig) ) dC <- c(NA, diff(x)) bal <- cumsum(sig * tail(dC, length(sig) ) ) bal <- bal-comis return(bal)}Example of R script that uses "MetaTrader5" (Python) package to load quotes. The "reticulate" library must be installed in R. This library provides interaction with Python. Read carefully here and here. The first time you run the reticulate library you will be offered to install miniconda. I recommend that you do not refuse. This will create the conda environment r-reticulate with python(3.6.xx) and an initial set of packages installed. If you have already installed Pyhon (or several versions) at the beginning of the script you should activate required environment. All commands of MetaTrader5 package are here.
Initialization script
To download quotes we will write the function
#---Function------------------------------- GetCotirPy <- function(sym){ selected = mt5$symbol_select(sym,TRUE) if (selected) py_run_string(" import pandas as pd import MetaTrader5 as mt5 rates = pd.DataFrame(mt5.copy_rates_from_pos(r.sym, r.tf, 1, r.bar))" ) return(py$rates) }Let's see the structure of obtained data.
Next, the necessary calculations.
R-card, forex.
Well, a function that would calculate the balance chart for trades + commission/spread (costs)
wrote this, I do not know if this is right
I do not understand here
R-card, forex.
Well, a function that would calculate the balance chart for trades + commission/spread (costs)
wrote this, I do not know if it is correct
I think that the number of deals should not be counted here. And just subtract spread with commission from each deal. That's how it is:
calc.balance <- function(sig,x, comision=0){ sig <- ifelse(sig>0, 1, -1) sig <- na.omit( dplyr::lag(sig) ) dC <- c(NA, diff(x)) bal <- cumsum(sig * tail(dC, length(sig) ) ) bal <- bal-comision return(bal)}I think that the number of deals does not need to be counted here. And just subtract the spread with the commission from each deal. That's the way it is:
Not like that. For example
The commission will not be charged for each signal, but only when the signal is reversed. I use the function
cnt<-function(x){ n <- 1:(length(x)-1) cnt <- 0 for(i in n) {if(x[i]!=x[i+1]) {cnt<-cnt+1}} return(cnt) }For the signal vector above
The difference is not clear.
And for calculating the balance the function
test_bal <- function(sig, CO){ import::here(poorman, Lag = lag) import::here(.from = fTrading, maxDrawDown) sig %>% Lag() %>% na.omit()->sig CO %>% tail(length(sig))-> CO bal <- cumsum(CO * sig) K <- (last(bal)/length(bal) * 10^Dig)%>% round() Kmax <- max(bal)/which.max(bal) * 10^Dig %>% round() dd <- maxDrawDown(bal)[[1]] %>% round() op <-cnt(sig) tst<-list(bal = bal, k = K, k.max = Kmax, op = op, dd = dd) return(tst) }Good luck
I don't get it here.
c "rle" you can count the number of signal changes, those number of deals
You can see by "values" that the signal changed 5 times, it means that there were 5 deals.
I do not understand the difference.
I think my implementation takes into account commission as if "of the first trade, the opening of which we don't have, there is only the closing"
Like for this deal, my variant takes a commission
and your version starts charging this one, your version is more correct, but it's easy to correct, just subtract "1"
Example script in R using ...
Thanks, I will look into it
With "rle" you can calculate the number of signal changes, i.e. number of deals
It is clear from "values" that the signal changed 5 times, so there were 5 trades.
I think my implementation also takes into account the commission and as "the first trade, the opening of which we do not have, there is only the closing"
like for this deal, my variant takes a commission
and your version starts to take this one, your version is more correct, but it's easy to correct, just do, just subtract "1"
Yes, your version is more correct.