predict (int type, vars Data, int TimePeriod, var Threshold) : int

Predicts an event, such as a crossover of two curves or a peak of a curve, several bars before it happens. A polynomial regression function is used for the prediction. This function can be used to generate very early trade signals.

Most trade systems analyze the recent price curve with functions or indicators that introduce more or less lag. This means that trade signals are always late, which greatly reduces the profit of a system. One way to minimize lag is using low-lag functions (for instance, lowpass filters instead of moving averages). Another way is predicting the signals before they actually occur. This is the purpose of the predict function that works by extrapolating signal curves into the future.

Parameters:

type Event to be predicted:
CROSSOVER - crossing of Data over the zero line
PEAK - Data peak
VALLEY - Data valley
+PARABOLIC - use parabolic instead of linear regression.
Data Data series to be predicted, with a minimum length of TimePeriod.
TimePeriod Number of bars used for the prediction.
Threshold Prediction threshold, or 0 for no threshold.

Returns

Bar offset (negative) of the predicted event, in the -TimePeriod..0 range. 0 predicts that the event will happen during the next bar. -TimePeriod is returned when no event is predicted within the time period.

Modifies

rMomentum - Data movement per bar at the time of the event.

Remarks:

Examples:

function run()
{
  vars Price = series(price());
  var LP50 = LowPass(Price,50);
  var LP150 = LowPass(Price,150);
  
  var CO = predict(CROSSOVER,series(LP50-LP150),10,0.5*PIP); // predict crossover
  var CU = predict(CROSSOVER,series(LP150-LP50),10,0.5*PIP); // predict crossunder
    
  plot("LP50",LP50,0,RED);
  plot("LP150",LP150,0,BLUE);
  plot("CrossOver",CO,NEW,BLUE);
  plot("CrossUnder",CU,0,RED);
}
// Trading with crossover vs. trading with prediction
#define USE_PREDICT
function run() 
{
  BarPeriod = 1440;
  asset("SPX500");
  vars Osc = series(StochEhlers(series(price()),20,10,10));
#ifndef USE_PREDICT  // use normal crossover
  if(crossOver(Osc,0.8)) 
    reverseShort(1);
  if(crossUnder(Osc,0.2))
    reverseLong(1);
#else                // use predicted crossover
  if(predict(CROSSOVER,series(Osc[0]-0.8),10,0.01) > -5) 
    reverseShort(1);
  if(predict(CROSSOVER,series(0.2-Osc[0]),10,0.01) > -5) 
    reverseLong(1);
#endif
}
Examples of signal prediction can also be found in the Predict and Ehlers scripts.

See also:

detect, advise, polyfit, crossOver, peak/valley

► latest version online