Gamestudio Links
Zorro Links
Newest Posts
Training with the R bridge does not work
by frutza. 11/20/25 22:32
Zorro 2.70
by Martin_HH. 11/20/25 17:37
Zorro 2.66
by Martin_HH. 11/20/25 17:28
ZorroGPT
by TipmyPip. 11/19/25 10:10
MRC.c and WFO
by 11honza11. 11/18/25 15:22
webGL
by Ice2642. 11/17/25 21:27
Camera always moves upwards?
by NeoDumont. 11/17/25 09:56
Future of ZorroHFT
by TipmyPip. 11/16/25 13:52
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
3 registered members (JeyKey II, frutza, AndrewAMD), 28,893 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
SkinnyApe, tritom, sheliepaley, Blueguy, blobplayintennis
19179 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Training with the R bridge does not work #488962
11/05/25 00:46
11/05/25 00:46
Joined: Jun 2023
Posts: 7
F
frutza Online OP
Newbie
frutza  Online OP
Newbie
F

Joined: Jun 2023
Posts: 7
Hi,

I am experimenting with the Zorro / R bridge. I have the following Lite-C script, which makes calls to R.


Code
#include <r.h>

function run()
{
  set(PLOTNOW+PARAMETERS+LOGFILE);
  BarPeriod = 1440;
  LookBack = 100;
  MaxLong = MaxShort = 1;
  
  if(Init) {
    if(!Rstart())
      return quit("Error - R won't start!");
    Rx("rm(list = ls());"); // clear the workspace
    Rx("library(tseries)"); // load time series library
  }
  
  if(is(LOOKBACK)) return;

  int size = optimize(50,10,100);

  Rset("Data",rev(seriesC(),size),size); // send Close series to R
  Rx("ADF = adf.test(Data)"); // Augmented Dickey-Fuller test
  var adf = Rd("ADF$p.value");
  
  if(adf > 0.6) enterLong();
  if(adf < 0.6) exitLong();
  
  plot("ADF p-value", adf ,NEW,RED); //display p-value
}


When I hit the train button, it takes the default value (50) and saves it in the .par file, but no actual training takes place.
Once the value 50 is saved, I can hit test, and everything runs fine. However, there is no optimization happening.

What am I doing wrong here?

Thanks!

Last edited by frutza; 11/05/25 00:56.
Re: Training with the R bridge does not work [Re: frutza] #488976
11/15/25 09:31
11/15/25 09:31
Joined: Apr 2008
Posts: 598
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 598
Austria
I believe it cannot train because your system starts with no optimize call. The number of optimize calls must not change from bar to bar, since the parameter count is derived from it.

Re: Training with the R bridge does not work [Re: Petra] #488983
11/19/25 22:32
11/19/25 22:32
Joined: Jun 2023
Posts: 7
F
frutza Online OP
Newbie
frutza  Online OP
Newbie
F

Joined: Jun 2023
Posts: 7
Hi Petra,

Thank you for your answer. I found (by trial and error) that my optimize call was misplaced within the code. I guess it has to be placed right at the beginning, after the flags and Zorro variables initialization. The following version of the code works as expected.

Code
#include <r.h>

function run()
{
  set(PLOTNOW+PARAMETERS+LOGFILE);
  BarPeriod = 1440;
  LookBack = 100;
  MaxLong = MaxShort = 1;

  int size = optimize(50,10,100);
  
  if(Init) {
    if(!Rstart())
      return quit("Error - R won't start!");
    Rx("rm(list = ls());"); // clear the workspace
    Rx("library(tseries)"); // load time series library
  }
  
  if(is(LOOKBACK)) return;

  Rset("Data",rev(seriesC(),size),size); // send Close series to R
  Rx("ADF = adf.test(Data)"); // Augmented Dickey-Fuller test
  var adf = Rd("ADF$p.value");
  
  if(adf > 0.6) enterLong();
  if(adf < 0.6) exitLong();
  
  plot("ADF p-value", adf ,NEW,RED); //display p-value
}


The optimize call is now right before the start of the R session.

Last edited by frutza; 11/19/25 22:33.
Re: Training with the R bridge does not work [Re: frutza] #488985
Yesterday at 14:03
Yesterday at 14:03
Joined: Apr 2008
Posts: 598
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 598
Austria
That looks better, but still a bit misplaced. Normally you place the optimize call just where you use that parameter. But of course before any return statements, whichever comes first.

Re: Training with the R bridge does not work [Re: frutza] #488989
Yesterday at 22:07
Yesterday at 22:07
Joined: Jun 2023
Posts: 7
F
frutza Online OP
Newbie
frutza  Online OP
Newbie
F

Joined: Jun 2023
Posts: 7
Well, I guess that's exactly what I tried to do in the first version of the code I posted here (see my very first post above):


Code
int size = optimize(50,10,100);

Rset("Data",rev(seriesC(),size),size); // send Close series to R


It makes complete sense to do it like that, but... it does not work. No training is happening.
What am I missing here?

Re: Training with the R bridge does not work [Re: frutza] #488990
Yesterday at 22:13
Yesterday at 22:13
Joined: Feb 2017
Posts: 1,810
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,810
Chicago
It is after if(is(LOOKBACK)) return; in the first version. optimize() absolutely needs to be called before that. Remember that run() is called repeatedly and that optimize() needs to be called the same number of times at every run() call for it to work correctly.

Re: Training with the R bridge does not work [Re: AndrewAMD] #488991
Yesterday at 22:32
Yesterday at 22:32
Joined: Jun 2023
Posts: 7
F
frutza Online OP
Newbie
frutza  Online OP
Newbie
F

Joined: Jun 2023
Posts: 7
Hi AndrewAMD,

Thank you for your reply. You are correct.

Here are three versions of the script, just in case someone else runs into the same issue.
The optimize() call is placed at three different positions. One can clearly see what works and what doesn't.
Thanks to Petra and AndrewAMD, I think I get it now

Attached Files before_lookback.PNGoptimize after vars.PNGoptimize before usage.PNG

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1