You might think it is because there are no trades in A and B. But the error also happens when I inject (random) trades.
Code:
function junk(var p1) {
  vars P = series(random());
  vars Osc = series(WMA(P, p1));
  if (valley(Osc)) {
    exitShort();
    enterLong();
  }
  if (peak(Osc)) {
    exitLong();
    enterShort();
  }
}

function run() {
  set(TESTNOW+PLOTNOW);
  set(PARAMETERS);
  NumYears = 1;
  LookBack = 334;
  asset("EUR/AUD");

  if (random() > 0) {
    algo("A");
    var p1 = optimize(150, 1, 333, -33);
    junk(p1);
  } else {
    algo("B");
    var p2 = optimize(150, 1, 333, -33);
    junk(p2);
  }
}