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; Yesterday at 22:33.