You need to enable JavaScript in order to visit our site!
 
Logo PTMC
NAVIGATION
CANCEL
 
or via PTMC account
Restore password
or via PTMC account
Logo PTMC
NAVIGATION
or via PTMC account
Restore password
or via PTMC account
or via PTMC account
Restore password
or via PTMC account

Increase the power of PTMC with R language

An era of the magic ball in trading comes to an end, with more and more trading opportunities available in forthcoming days. However to distinguish a real deal among of plenty varieties of platforms out there in the global net is not an easy task. Here is where PTMC client shines at best amid its adjustability with any tool trader could possibly imagine.

This time R language came in the scope for boosting advanced math and statistical options of PTMC.

1. Install the R-client.

Download and install R-client https://cran.r-project.org/bin/windows/base/R-3.3....

2. Bound the R-environment to our project.

The following solutions were found:

  • adding the NuGet packet to the project for working with the R-environment;
  • usage of the intermediate data format (*.csv)
  • working through the console with reading results from this console
  • COM-connection

The quickest and the most suitable solution is the first one. However, there is one little issue, which took some time. Development and supporting of the R.NET project has stopped and now continued its existence in the new project - R.NET.Community. Previous packet usage is impossible, even in case we re-pack it using the source code or replace the version by an older one.

Other solutions require more time for realization, and some of them are working slowly.

After connecting NuGet packet to the project you need to create an instance of the REngine class to work with the R-environment and initialize it:

/// <summary> starts R engine     
void StartEngine()  
{           
     REngine.SetEnvironmentVariables();    
     engine = REngine.GetInstance();  
     engine.Initialize();       
}

3. Get the initial data.

A day tick history is the best to serve as an example of operating a large amount of data. Its upload looks as follows:

// Load trade history
TD = PlatformEngine.GetHistoricalData(new HistoricalDataRequest(curInstrument, Period.Tick, DataType.Trade)) as TradeData;
TD.Load(DateTime.UtcNow.AddDays(-1));

4. Realize calculations in the R-environment.

As an example let’s consider the simple calculation of the price pivots. According to data input let’s create R-script “example.r”:

df <- data.frame(price = price, time = time, side = side)
res <- aggregate(cbind(count = time) ~ price + side, data = df, FUN = function(x) { NROW(x) })
table <- na.omit(res[order( -res$count),] [1:rows,])

In the project add line with the path to created R-script which loads external script and starts our calculations:

// load external script
engine.Evaluate(@"source('"+scriptName+"')");

5. Transmit data to the R-side.

Let’s create a data package for transfer:

// perform input data
            int count = TD.Count;
            double[] price = new double[count];
            string[] time = new string[count];
            string[] side = new string[count];
            for (int i = 0; i < count - 1; i++)
            {
                price[i] = TD.Price(i);
                time[i] = TD.Time(i).ToString();
                side[i] = TD.Aggressor(i).ToString();
            }

Data transfer - is the variables creation in the R-environment with values of transmitted data (the arrays which were created earlier):

// R-input
            engine.SetSymbol("rows", engine.CreateInteger((int)biggestValue.Value));
            engine.SetSymbol("price", engine.CreateNumericVector(price));
            engine.SetSymbol("time", engine.CreateCharacterVector(time));
            engine.SetSymbol("side", engine.CreateCharacterVector(side));

6. Output the result.

Let’s take the results of calculations from the R-environment as an output having a view of the DataFrame object:

// R-output
var table = engine.GetSymbol("table").AsDataFrame();

The further data display can be organized via any convenient method.

That is all. I’m sure that any programmer can find its own benefit of PTMC & R mutual usage, so if you have any questions or proposals, please comment on this article.

Going further,

Described approach allows us to use R-scripts in C# code - pass data into R-Environment and get back results into C#. It would be much better if we could have another opportunity to get PTMC’s quotes, historical data and even make trading actions directly from R. As for traders which professionally using R it would be an excellent choice to get away from C# coding.

So, What do You think about placing R API as feature request?

Discussion
Join PTMC community to post your comments
PTMC is a professional trading platform that combines all the best charting and analytical tools to trade on different financial markets. It is developed by PFSOFT UK LTD, a global provider of trading technology for banks & brokers
© 2017. PTMC is based on Protrader technology
Head office
PFSOFT UK LTD
20 Centenary Avenue
South Shields, Tyne and Wear,
NE34 6QH
United Kingdom
Ukraine office
TOV PTMC
26, Naberezhna str., Dnipro,
Dnipropetrovs'k region,
Ukraine
Contact us
Phone: +380 56 740 31 01
E-mail: info@protrader.org
Skype: PTMC_support
Social groups