Need help with Polynomial System Study

I was going through the Nth order polynomial velocity system by Meyers. He has calculated the velocity and acceleration using following equation ( Screen shot attached).

I am not understanding the way he calculated Bj using o(k). Anyone studied this topic before ?
nth-order-polynimial

Can some one help me out here.
Thanks in advance for the support and helps

looks like the velocity is the value of the polynomial P at time T+1 minus the value of the polynomial P at time T.

The acceleration is then the same procedure done with the velocity (that you just calculated).

So what he does is he uses the past N closing prices to calculate the polynomial through these N data points. Then he calculates the extended Polynomial 1 bar into the future. From this he derives the velocity and the acceleration.

This is an iterative process. So if you use for instance 20 Closing prices. You calculate the polynomial for these 20 prices, then calculate the extension 1 bar into the future, calculate the velocity and acceleration and you store the velocity and acceleration. Then you shift 1 bar and do the calculation again until you are out of data.

I just glanced over Meyers' methods. Unfortunately he doe not explain thiings with graphs.

The way I understand it at every price point he calculates a Polynomial with a length of N bars closing prices.

So in chart1 you see the closing prices in yellow. Superposed is the 3rd order 10 bar polynomial. The last bar of this polynomial is at the selected value in the chart (vertical line). The "red dot" is the 1 bar extension of this polynomial in the future.

meyers1

So if you do this calculation for every bar and store all the "red dots" in an array you get the polynomials 1 bar ahead prediction for the price. This is shown in chart2, blue line. You can see the red dot sits on top of this blue line. So it calculates this 10 bar polynomial for every bar and stores the "1 bar ahead polynomial price" inside the blue array.

meyers2

This function is then used to calculate the velocity simply by calculating: BlueArray( t+1 ) - BlueArray( t )

This is most easily coded using the Python plugin because these functions are all in the Python libraries. Below the code. Since Amibrokers' arrays have a fixed length of Barcount I have not yet included the calculation of the "red dot" at point Barcount. It is ofcourse possible but I will first wait if I get any feedback. With this setup you can at least test if the method could work. But I am not sure if you are familiar with the Python plugin yet.

AFL code:

// https://forum.amibroker.com/t/need-help-with-polynomial-system-study/28070
// https://meyersanalytics.com/publications2/es1fixmv1-3.pdf

SetBarsRequired( sbrAll, sbrAll );
PyLoadFromFile( "polyfit using Python", "C:\\Users\\win 10\\AppData\\Local\\Programs\\Python\\Python38\\mypython\\fitting\\PolyfitMeyersLegendre.py" );

nbars = Param( "Last N Bars Closing Prices", 30, 10, 1000, 1 );
lookback = Param( "Lockback (starting point)", 100, 20, 500, 1 );
Nforecast = 1;//Param( "Forecast (bars)", 1, 0, 20, 1 );
order = Param( "Polynomial Order", 3, 1, 10, 1 );
triggerCalculation = ParamTrigger( "Start Calculation", "Press Here" );
triggerResetStaticVariables = ParamTrigger( "Reset Static Variables", "Press Here" );

bi = BarIndex();

// test function
// C = Cum(1);

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorYellow, styleDots, Null, Null, 0, 0, 1 );

lastIndex = LastValue( SelectedValue( bi ) );
firstIndex = lastIndex - nbars;

xarr = IIf( bi > firstIndex, bi, 0 );
xarr = IIf( bi > lastIndex, 0, xarr );
yarr = IIf( bi > firstIndex, C, 0 );
yarr = IIf( bi > lastIndex, 0, yarr );
xnew = IIf( bi > firstIndex, bi, 0 );
xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );

ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
ynew = IIf( bi > firstIndex, ynew, Null );
ynew = IIf( bi > lastIndex + Nforecast, Null, ynew );
Plot( IIf( bi <= lastIndex, ynew, Null ), "", ColorRGB( 3, 157, 252 ), styleDots | styleNoRescale, Null, Null, 0, 1, 2 );
Plot( IIf( bi > lastIndex AND bi <= lastIndex + Nforecast, ynew, Null ), "", ColorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, 0, 0, 6 );

forecast = Null;

if( triggerCalculation )
{
    Say( "start calculation" );

    for( i = BarCount - lookback; i < BarCount - 1; i++ )
    {
        lastIndex = i;
        firstIndex = lastIndex - nbars;
        xarr = IIf( bi > firstIndex, bi, 0 );
        xarr = IIf( bi > lastIndex, 0, xarr );
        yarr = IIf( bi > firstIndex, C, 0 );
        yarr = IIf( bi > lastIndex, 0, yarr );
        xnew = IIf( bi > firstIndex, bi, 0 );
        xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );

        ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
        forecast[i + 1] = ynew[i + 1];
    }

    StaticVarSet( "Forecast", forecast );
    Say( "finished" );
}

forecast =  Nz( StaticVarGet( "Forecast" ) );
Plot( IIf( forecast, forecast, Null ), "", ColorBlue, styleDots | styleNoRescale, Null, Null, 0, -1, 1 );

velocity = forecast - Ref( forecast, -1 );
velocity[BarCount - lookback + 1] = 0;
acceleration = velocity - Ref( velocity, -1 );
acceleration[BarCount - lookback + 1] = 0;

//Plot( IIf( velocity, velocity, Null ), "Velo", ColorGreen, styleLine | styleNoRescale | styleOwnScale, Null, Null, 0, -1, 3 );
//Plot( IIf( acceleration, acceleration, Null ), "Accel", colorViolet, styleLine | styleNoRescale | styleOwnScale, Null, Null, 0, -1, 3 );

if( triggerResetStaticVariables )
{
    Say( "remove static variables" );
    StaticVarRemove( "Forecast" );
}

Title = "" +
"Order: " + Order +
" | N bars per calculation: " + nbars +
" | Look Back: " + lookback;

Python code:

import AmiPy
import numpy as np
import pandas as pd

def polyfitInput( x, y, order, xnew ):

    df = pd.DataFrame()
    df['x'] = x.astype(int)
    df['y'] = y
    df['xnew'] = xnew.astype(int)
    order = int(order)
    # AmiPy.Print(str(type(xnew)) +'\n')
    x = df['x'].loc[df['x'] != 0]
    y = df['y'].loc[df['y'] != 0]
    xnew = df['xnew'].to_numpy()
    avg = int(np.mean(x))
    coefs = np.polynomial.legendre.legfit(x-avg, y, order)
    ynew = np.polynomial.legendre.legval(xnew-avg, coefs)
    return ynew
4 Likes

Hi Edward
Thanks you very much for spending more time on this problem and well detailed explanation.
I could load your python script and Ami Broker formula on my set up and did some testing.
Initial observation was there could be certain signal we can make with this study.

Please do check the screenshot.
nthorder polynomail study

I will test it further and let you know the feedback.
Once again thank you very much for spending time for this.

Regards
Ajesh Mathew

hi Ajesh,

that is great that you were able to load the code. I will expand the code a bit so that it can also predict the last bar in the chart. The velocity chart (green chart) can actually be moved backwards 1 point. Since the blue-line is the forecast. So the velocity at time t can use the prediction at time t+1.

So:

velocity( t ) = Blue-line( t + 1 ) - Blue-line( t )

I will get back to you later with some expansions of the code. Not sure if it will be today though.

i updated the code. So when the the lastIndex is equal to (Barcount - 1), which is the last bar in the chart you need to do some trickery because the Forecasted value will then be at index Barcount which is outside of the array. So you need to shift the index, see code.

The result is that the forecast array is already shifted 1 bar to the "left". This is what we want to calculate the velocity, so this makes that calculation easy.

In the chart you see the vertical line at the lastIndex and you see that the forcast (red dot) 1 bar to the right of that. Also the forecast array is correct however I use shift=1 to plot it correctly.

meyers3

You can toggle between Price mode and Velocity mode. So I am not sure this is what Meyers is doing but must be something like this. The velocity chart looks similar. Here is de AFL code. Python code stays the same

// https://forum.amibroker.com/t/need-help-with-polynomial-system-study/28070
// https://meyersanalytics.com/publications2/es1fixmv1-3.pdf

SetBarsRequired( sbrAll, sbrAll );
PyLoadFromFile( "polyfit using Python", "C:\\Users\\win 10\\AppData\\Local\\Programs\\Python\\Python38\\mypython\\fitting\\PolyfitMeyersLegendre.py" );

nbars = Param( "Last N Bars Closing Prices", 30, 10, 1000, 1 );
lookback = Param( "Lockback (starting point)", 100, 20, 500, 1 );
Nforecast = 1;//Param( "Nforecast (bars)", 1, 0, 20, 1 );
order = Param( "Polynomial Order", 3, 1, 10, 1 );
triggerCalculation = ParamTrigger( "Start Calculation", "Press Here" );
triggerResetStaticVariables = ParamTrigger( "Reset Static Variables", "Press Here" );
toggleDisplay = ParamToggle( "Toggle Display", "Velocity|Price", 1 );

bi = BarIndex();

// test function
// C = Cum(1);

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );

// this part is just for display purpose
lastIndex1 = LastValue( SelectedValue( bi ) );
firstIndex1 = lastIndex1 - nbars;

xarr = IIf( bi > firstIndex1, bi, 0 );
xarr = IIf( bi > lastIndex1, 0, xarr );
yarr = IIf( bi > firstIndex1, C, 0 );
yarr = IIf( bi > lastIndex1, 0, yarr );

shift1 = 0;

// special case when lastIndex (or the SelectedValue with the mouse) coincides with the last bar
if( ( lastIndex1 + Nforecast ) - ( BarCount - 1 ) == 1 )
{
    shift1 = ( lastIndex1 + Nforecast ) - ( BarCount - 1 );
}

xnew = IIf( bi > firstIndex1 - shift1, bi + shift1, 0 );
xnew = IIf( bi > lastIndex1 + Nforecast, 0, xnew );
ynew1 = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
ynew1 = IIf( bi > firstIndex1, ynew1, Null );
ynew1 = IIf( bi > lastIndex1 + Nforecast, Null, ynew1 );

// real calculations start here
forecast = Null;
velocity = Null;
acceleration = Null;

if( triggerCalculation )
{
    Say( "start calculation" );

    for( i = BarCount - lookback; i < BarCount - 1; i++ )
    {
        lastIndex = i;
        firstIndex = lastIndex - nbars;
        xarr = IIf( bi > firstIndex, bi, 0 );
        xarr = IIf( bi > lastIndex, 0, xarr );
        yarr = IIf( bi > firstIndex, C, 0 );
        yarr = IIf( bi > lastIndex, 0, yarr );
        xnew = IIf( bi > firstIndex, bi, 0 );
        xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );

        ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
        forecast[i + 1] = ynew[i + 1];
    }

    StaticVarSet( "Forecast", forecast );
    Say( "finished" );
}

// special case: the last bar
shift = 1;
lastIndex = BarCount - 1;
firstIndex = lastIndex - nbars;
xarr = IIf( bi > firstIndex, bi, 0 );
xarr = IIf( bi > lastIndex, 0, xarr );
yarr = IIf( bi > firstIndex, C, 0 );
yarr = IIf( bi > lastIndex, 0, yarr );
xnew = IIf( bi > firstIndex - shift, bi + shift, 0 );
xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );
ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
ynew = IIf( bi > firstIndex, ynew, Null );
ynew = IIf( bi > lastIndex + Nforecast, Null, ynew );
StaticVarSet( "lastForecast", ynew[lastIndex] );

forecast =  Nz( StaticVarGet( "Forecast" ) );
forecast = Ref( forecast, shift );
forecast[ lastIndex ] = Nz( StaticVarGet( "lastForecast" ) );

velocity = forecast - Ref( forecast, -1 );
velocity[BarCount - lookback] = 0;
acceleration = velocity - Ref( velocity, -1 );
acceleration[BarCount - lookback] = 0;

if( toggleDisplay )
{
    Plot( C, "C", colorYellow, styleDots, Null, Null, 0, 0, 1 );

    if( shift1 == 0 )
    {
        Plot( IIf( bi <= lastIndex1, ynew1, Null ), "", ColorRGB( 3, 157, 252 ), styleDots | styleNoRescale, Null, Null, 0, 1, 2 );
        Plot( IIf( bi > lastIndex1 AND bi <= lastIndex1 + Nforecast, ynew1, Null ), "", ColorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, 0, 0, 6 );
    }
    else
        if( shift1 == 1 )
        {
            Plot( IIf( bi < lastIndex1, ynew1, Null ), "", ColorRGB( 3, 157, 252 ), styleDots | styleNoRescale, Null, Null, shift1, 1, 2 );
            Plot( IIf( bi == lastIndex1, ynew1, Null ), "", ColorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, shift1, 0, 6 );
        }

    Plot( IIf( forecast, forecast, Null ), "", ColorBlue, styleDots | styleNoRescale, Null, Null, shift, -1, 1 );
    //Plot( IIf( forecast, forecast, Null ), "", colorAqua, styleDashed | styleNoRescale, Null, Null, 0, -1, 1 );
}
else
{
    Plot( IIf( velocity, velocity, Null ), "", ColorGreen, styleLine );
    //Plot( IIf( acceleration, acceleration, Null ), "", colorViolet, styleLine | styleNoRescale | styleOwnScale, Null, Null, 0, -1, 3 );
}

if( triggerResetStaticVariables )
{
    Say( "remove static variables" );
    StaticVarRemove( "Forecast" );
    StaticVarRemove( "lastForecast" );
}
5 Likes

Hi Edward
Thanks for sharing the updated code.
I did test further on couple of symbols. It seems, this concept was producing good trade opportunities. I am attaching few screen shot with this thread below. Please do refer the same.

Parameter tested: Polynomial order 4 and period 20symbol1
symbol2

Threshold will varies and it depended on symbols

On the other hand, the calculation process was tedious, means the chart was stuck during the calculation process. So, to apply this concept on 1 minute chart might not be possible as he mentioned in the document.
Do you have any idea why this take to much time?, is it because of python API?
Can we do some thing to make the calculation faster. ?

Regards
Ajesh

hi,

yes the range of the velocity will depend on the price of the symbol. Maybe an idea would be to normalize the velocity. Then it will be independent of the price.

Indeed for the 1-minute it take a long time. So what you need to do is calculate the velocity for multiple bars for only for system development. Once you have a strategy you will only have to calculate the last bar.

It is slow because the calculation is quite complicated. So for instance if you have the parameter "nbars" set to 20 then it needs to calculate the coefficients for this and it will need to look at all these 20 bars. So even if you would code it inside AFL is also will be slow.

Therefore you could for instance save the calculations in persistent variables when developing a strategy. Once you have a strategy you only have to calculate the last bar when trading. So what you can do during trading is calculate the forecast once. Then when new data come in only calculate the last bar. And add this bar to the forecast array and then maybe save it in a persistent variable.

maybe also try to use

SetBarsRequired( lookback, nbars );

see code

// https://forum.amibroker.com/t/need-help-with-polynomial-system-study/28070
// https://meyersanalytics.com/publications2/es1fixmv1-3.pdf

//SetBarsRequired( sbrAll, sbrAll );
PyLoadFromFile( "polyfit using Python", "C:\\Users\\win 10\\AppData\\Local\\Programs\\Python\\Python38\\mypython\\fitting\\PolyfitMeyersLegendre.py" );

nbars = Param( "Last N Bars Closing Prices", 30, 10, 1000, 1 );
lookback = Param( "Lockback (starting point)", 100, 20, 2000, 1 );
Nforecast = 1;//Param( "Nforecast (bars)", 1, 0, 20, 1 );
order = Param( "Polynomial Order", 3, 1, 10, 1 );
triggerCalculation = ParamTrigger( "Start Calculation", "Press Here" );
triggerResetStaticVariables = ParamTrigger( "Reset Static Variables", "Press Here" );
toggleDisplay = ParamToggle( "Toggle Display", "Velocity|Price", 1 );

SetBarsRequired( lookback, nbars );
bi = BarIndex();

// test function
// C = Cum(1);

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );

// this part is just for display purpose
lastIndex1 = LastValue( SelectedValue( bi ) );
firstIndex1 = lastIndex1 - nbars;

xarr = IIf( bi > firstIndex1, bi, 0 );
xarr = IIf( bi > lastIndex1, 0, xarr );
yarr = IIf( bi > firstIndex1, C, 0 );
yarr = IIf( bi > lastIndex1, 0, yarr );

shift1 = 0;

// special case when lastIndex (or the SelectedValue with the mouse) coincides with the last bar
if( ( lastIndex1 + Nforecast ) - ( BarCount - 1 ) == 1 )
{
    shift1 = ( lastIndex1 + Nforecast ) - ( BarCount - 1 );
}

xnew = IIf( bi > firstIndex1 - shift1, bi + shift1, 0 );
xnew = IIf( bi > lastIndex1 + Nforecast, 0, xnew );
ynew1 = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
ynew1 = IIf( bi > firstIndex1, ynew1, Null );
ynew1 = IIf( bi > lastIndex1 + Nforecast, Null, ynew1 );

// real calculations start here
forecast = Null;
velocity = Null;
acceleration = Null;

if( triggerCalculation )
{
    Say( "start calculation" );

    for( i = BarCount - lookback; i < BarCount - 1; i++ )
    {
        lastIndex = i;
        firstIndex = lastIndex - nbars;
        xarr = IIf( bi > firstIndex, bi, 0 );
        xarr = IIf( bi > lastIndex, 0, xarr );
        yarr = IIf( bi > firstIndex, C, 0 );
        yarr = IIf( bi > lastIndex, 0, yarr );
        xnew = IIf( bi > firstIndex, bi, 0 );
        xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );

        ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
        forecast[i + 1] = ynew[i + 1];
    }

    StaticVarSet( "Forecast", forecast );
    Say( "finished" );
}

// special case: the last bar
shift = 1;
lastIndex = BarCount - 1;
firstIndex = lastIndex - nbars;
xarr = IIf( bi > firstIndex, bi, 0 );
xarr = IIf( bi > lastIndex, 0, xarr );
yarr = IIf( bi > firstIndex, C, 0 );
yarr = IIf( bi > lastIndex, 0, yarr );
xnew = IIf( bi > firstIndex - shift, bi + shift, 0 );
xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );
ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
ynew = IIf( bi > firstIndex, ynew, Null );
ynew = IIf( bi > lastIndex + Nforecast, Null, ynew );
StaticVarSet( "lastForecast", ynew[lastIndex] );

bullarray =  Nz( StaticVarGet( "bullarray" ) );
beararray =  Nz( StaticVarGet( "beararray" ) );
forecast =  Nz( StaticVarGet( "Forecast" ) );
forecast = Ref( forecast, shift );
forecast[ lastIndex ] = Nz( StaticVarGet( "lastForecast" ) );

velocity = forecast - Ref( forecast, -1 );
velocity[BarCount - lookback] = 0;
acceleration = velocity - Ref( velocity, -1 );
acceleration[BarCount - lookback] = 0;

if( toggleDisplay )
{
    Plot( C, "C", colorYellow, styleDots, Null, Null, 0, 0, 1 );

    if( shift1 == 0 )
    {
        Plot( IIf( bi <= lastIndex1, ynew1, Null ), "", ColorRGB( 3, 157, 252 ), styleDots | styleNoRescale, Null, Null, 0, 1, 2 );
        Plot( IIf( bi > lastIndex1 AND bi <= lastIndex1 + Nforecast, ynew1, Null ), "", ColorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, 0, 0, 6 );
    }
    else
        if( shift1 == 1 )
        {
            Plot( IIf( bi < lastIndex1, ynew1, Null ), "", ColorRGB( 3, 157, 252 ), styleDots | styleNoRescale, Null, Null, shift1, 1, 2 );
            Plot( IIf( bi == lastIndex1, ynew1, Null ), "", ColorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, shift1, 0, 6 );
        }

    Plot( IIf( forecast, forecast, Null ), "", colorBlue, styleDots | styleNoRescale, Null, Null, shift, -1, 1 );
    //Plot( IIf( forecast, forecast, Null ), "", colorAqua, styleDashed | styleNoRescale, Null, Null, 0, -1, 1 );
}
else
{
    Plot( IIf( velocity, velocity, Null ), "", ColorGreen, styleLine, Null, Null, 0, 0, 2 );
    //Plot( IIf( acceleration, acceleration, Null ), "", colorViolet, styleLine | styleNoRescale, Null, Null, 0, 0, 2 );
    Plot( 0, "", colorWhite, styleLine, Null, Null, 0, 0, 2 );
}

if( triggerResetStaticVariables )
{
    Say( "remove static variables" );
    StaticVarRemove( "Forecast" );
    StaticVarRemove( "lastForecast" );
}

Title = "" +
        "Order: " + Order +
        " | N bars per calculation: " + nbars +
        " | Look Back: " + lookback +
        " | Forecast: " + forecast +
        " | Velocity: " + velocity +
        " | Acceleration: " + acceleration;

You don't need Python to do Polyfit. The codes for Polyfit in pure AFL have been published many times, including even Users' Guide:

https://www.amibroker.com/guide/afl/mxsolve.html

Look at the Example 3. code.

Python should be avoided if you have native solution.

2 Likes

with respect to the speed of the calculation the setting

SetBarsRequired( lookback, nbars );

works good. Then if you calculate 2000 bars in the daily or 1min timeframe will take just as long. You can of course program it all inside Amibroker but the speed difference will not be significantly better. This just uses a lot of computing power. Also the Python code uses Legendre Polynomial because this is what Meyers was using. To be honest I did not dig into the difference between the Polyfit function and the Legendre function but I think it is worth the effort to use Python because so many functions are available and it is so easy to use them now inside Amibroker.

But try the code below it is much faster now for 1min data.

Also the velocity depends on the price. You could add a StDev function in the velocity plot as shown in the code.

// https://forum.amibroker.com/t/need-help-with-polynomial-system-study/28070
// https://meyersanalytics.com/publications2/es1fixmv1-3.pdf

PyLoadFromFile( "polyfit using Python", "C:\\Users\\win 10\\AppData\\Local\\Programs\\Python\\Python38\\mypython\\fitting\\PolyfitMeyersLegendre.py" );

nbars = Param( "Last N Bars Closing Prices", 30, 10, 1000, 1 );
lookback = Param( "Lockback (starting point)", 100, 20, 2000, 1 );
Nforecast = 1;//Param( "Nforecast (bars)", 1, 0, 20, 1 );
order = Param( "Polynomial Order", 3, 1, 10, 1 );
triggerCalculation = ParamTrigger( "Start Calculation", "Press Here" );
triggerResetStaticVariables = ParamTrigger( "Reset Static Variables", "Press Here" );
toggleDisplay = ParamToggle( "Toggle Display", "Velocity|Price", 1 );

SetBarsRequired( lookback, nbars );
bi = BarIndex();

// test function
// C = Cum(1);

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );

// this part is just for display purpose
lastIndex1 = LastValue( SelectedValue( bi ) );
firstIndex1 = lastIndex1 - nbars;

xarr = IIf( bi > firstIndex1, bi, 0 );
xarr = IIf( bi > lastIndex1, 0, xarr );
yarr = IIf( bi > firstIndex1, C, 0 );
yarr = IIf( bi > lastIndex1, 0, yarr );

shift1 = 0;

// special case when lastIndex (or the SelectedValue with the mouse) coincides with the last bar
if( ( lastIndex1 + Nforecast ) - ( BarCount - 1 ) == 1 )
{
    shift1 = ( lastIndex1 + Nforecast ) - ( BarCount - 1 );
}

xnew = IIf( bi > firstIndex1 - shift1, bi + shift1, 0 );
xnew = IIf( bi > lastIndex1 + Nforecast, 0, xnew );
ynew1 = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
ynew1 = IIf( bi > firstIndex1, ynew1, Null );
ynew1 = IIf( bi > lastIndex1 + Nforecast, Null, ynew1 );

// real calculations start here
forecast = Null;
velocity = Null;
acceleration = Null;

if( triggerCalculation )
{
    Say( "start calculation" );

    for( i = BarCount - lookback; i < BarCount - 1; i++ )
    {
        lastIndex = i;
        firstIndex = lastIndex - nbars;
        xarr = IIf( bi > firstIndex, bi, 0 );
        xarr = IIf( bi > lastIndex, 0, xarr );
        yarr = IIf( bi > firstIndex, C, 0 );
        yarr = IIf( bi > lastIndex, 0, yarr );
        xnew = IIf( bi > firstIndex, bi, 0 );
        xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );

        ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
        forecast[i + 1] = ynew[i + 1];
    }

    StaticVarSet( "Forecast", forecast );
    Say( "finished" );
}

// special case: the last bar
shift = 1;
lastIndex = BarCount - 1;
firstIndex = lastIndex - nbars;
xarr = IIf( bi > firstIndex, bi, 0 );
xarr = IIf( bi > lastIndex, 0, xarr );
yarr = IIf( bi > firstIndex, C, 0 );
yarr = IIf( bi > lastIndex, 0, yarr );
xnew = IIf( bi > firstIndex - shift, bi + shift, 0 );
xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );
ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
ynew = IIf( bi > firstIndex, ynew, Null );
ynew = IIf( bi > lastIndex + Nforecast, Null, ynew );
StaticVarSet( "lastForecast", ynew[lastIndex] );

forecast =  Nz( StaticVarGet( "Forecast" ) );
forecast = Ref( forecast, shift );
forecast[ lastIndex ] = Nz( StaticVarGet( "lastForecast" ) );

velocity = forecast - Ref( forecast, -1 );
velocity[BarCount - lookback] = 0;
acceleration = velocity - Ref( velocity, -1 );
acceleration[BarCount - lookback] = 0;

if( toggleDisplay )
{
    Plot( C, "C", colorYellow, styleDots, Null, Null, 0, 0, 1 );

    if( shift1 == 0 )
    {
        Plot( IIf( bi <= lastIndex1, ynew1, Null ), "", ColorRGB( 3, 157, 252 ), styleDots | styleNoRescale, Null, Null, 0, 1, 2 );
        Plot( IIf( bi > lastIndex1 AND bi <= lastIndex1 + Nforecast, ynew1, Null ), "", ColorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, 0, 0, 6 );
    }
    else
        if( shift1 == 1 )
        {
            Plot( IIf( bi < lastIndex1, ynew1, Null ), "", ColorRGB( 3, 157, 252 ), styleDots | styleNoRescale, Null, Null, shift1, 1, 2 );
            Plot( IIf( bi == lastIndex1, ynew1, Null ), "", ColorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, shift1, 0, 6 );
        }

   Plot( IIf( forecast, forecast, Null ), "", colorBlue, styleDots | styleNoRescale, Null, Null, shift, -1, 1 );
    //Plot( IIf( forecast, forecast, Null ), "", colorAqua, styleDashed | styleNoRescale, Null, Null, 0, -1, 1 );
}
else
{
    Plot( IIf( velocity, velocity, Null ), "", ColorGreen, styleLine, Null, Null, 0, 0, 2 );
    Plot( IIf( velocity, StDev( velocity, nbars ), Null ),"", ColorYellow, styleLine, Null, Null, 0, 0, 1 );
    Plot( IIf( velocity, -StDev( velocity, nbars ), Null ),"", ColorYellow, styleLine, Null, Null, 0, 0, 1 );
    //Plot( IIf( acceleration, acceleration, Null ), "", colorViolet, styleLine | styleNoRescale, Null, Null, 0, 0, 2 );
    Plot( 0, "", colorWhite, styleDashed, Null, Null, 0, 0, 1 );
}

if( triggerResetStaticVariables )
{
    Say( "remove static variables" );
    StaticVarRemove( "Forecast" );
    StaticVarRemove( "lastForecast" );
}

Title = "" +
        "Order: " + Order +
        " | N bars per calculation: " + nbars +
        " | Look Back: " + lookback +
        " | Forecast: " + forecast +
        " | Velocity: " + velocity +
        " | Acceleration: " + acceleration;

below I added a simple trading system which can be backtested in the Amibroker backtester.

// https://forum.amibroker.com/t/need-help-with-polynomial-system-study/28070
// https://meyersanalytics.com/publications2/es1fixmv1-3.pdf

PyLoadFromFile( "polyfit using Python", "C:\\Users\\win 10\\AppData\\Local\\Programs\\Python\\Python38\\mypython\\fitting\\PolyfitMeyersLegendre.py" );

nbars = Param( "Last N Bars Closing Prices", 30, 10, 1000, 1 );
lookback = Param( "Lockback (starting point)", 100, 20, 2000, 1 );
Nforecast = 1;//Param( "Nforecast (bars)", 1, 0, 20, 1 );
order = Param( "Polynomial Order", 3, 1, 10, 1 );
triggerCalculation = ParamTrigger( "Start Calculation", "Press Here" );
triggerResetStaticVariables = ParamTrigger( "Reset Static Variables", "Press Here" );
toggleDisplay = ParamToggle( "Toggle Display", "Velocity|Price", 1 );

SetBarsRequired( lookback, nbars );
bi = BarIndex();

// test function
// C = Cum(1);

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );

// this part is just for display purpose
lastIndex1 = LastValue( SelectedValue( bi ) );
firstIndex1 = lastIndex1 - nbars;

xarr = IIf( bi > firstIndex1, bi, 0 );
xarr = IIf( bi > lastIndex1, 0, xarr );
yarr = IIf( bi > firstIndex1, C, 0 );
yarr = IIf( bi > lastIndex1, 0, yarr );

shift1 = 0;

// special case when lastIndex (or the SelectedValue with the mouse) coincides with the last bar
if( ( lastIndex1 + Nforecast ) - ( BarCount - 1 ) == 1 )
{
    shift1 = ( lastIndex1 + Nforecast ) - ( BarCount - 1 );
}

xnew = IIf( bi > firstIndex1 - shift1, bi + shift1, 0 );
xnew = IIf( bi > lastIndex1 + Nforecast, 0, xnew );
ynew1 = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
ynew1 = IIf( bi > firstIndex1, ynew1, Null );
ynew1 = IIf( bi > lastIndex1 + Nforecast, Null, ynew1 );

// real calculations start here
forecast = Null;
velocity = Null;
acceleration = Null;

if( triggerCalculation )
{
    Say( "start calculation" );

    for( i = BarCount - lookback; i < BarCount - 1; i++ )
    {
        lastIndex = i;
        firstIndex = lastIndex - nbars;
        xarr = IIf( bi > firstIndex, bi, 0 );
        xarr = IIf( bi > lastIndex, 0, xarr );
        yarr = IIf( bi > firstIndex, C, 0 );
        yarr = IIf( bi > lastIndex, 0, yarr );
        xnew = IIf( bi > firstIndex, bi, 0 );
        xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );

        ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
        forecast[i + 1] = ynew[i + 1];
    }

    StaticVarSet( "Forecast", forecast );
    Say( "finished" );
}

// special case: the last bar
shift = 1;
lastIndex = BarCount - 1;
firstIndex = lastIndex - nbars;
xarr = IIf( bi > firstIndex, bi, 0 );
xarr = IIf( bi > lastIndex, 0, xarr );
yarr = IIf( bi > firstIndex, C, 0 );
yarr = IIf( bi > lastIndex, 0, yarr );
xnew = IIf( bi > firstIndex - shift, bi + shift, 0 );
xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );
ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
ynew = IIf( bi > firstIndex, ynew, Null );
ynew = IIf( bi > lastIndex + Nforecast, Null, ynew );
StaticVarSet( "lastForecast", ynew[lastIndex] );

forecast =  Nz( StaticVarGet( "Forecast" ) );
forecast = Ref( forecast, shift );
forecast[ lastIndex ] = Nz( StaticVarGet( "lastForecast" ) );

velocity = forecast - Ref( forecast, -1 );
velocity[BarCount - lookback] = 0;
vup = IIf( velocity, StDev( velocity, nbars ), Null );
vdn = IIf( velocity, -StDev( velocity, nbars ), Null );
acceleration = velocity - Ref( velocity, -1 );
acceleration[BarCount - lookback] = 0;

SetTradeDelays( 0, 0, 0, 0 );
SetOption( "FuturesMode", True );
SetOption( "PriceBoundChecking", False );
SetOption( "AllowSameBarExit", True );
SetOption( "CommissionAmount", 2.1 );
SetOption( "MaxOpenPositions", 6 );
PositionSize = MarginDeposit;

Buy = Cross( velocity, vup );
Sell = Cross( vdn, velocity );
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = Sell;
Cover = Buy;
BuyPrice = SellPrice = ShortPrice = CoverPrice = C;

if( toggleDisplay )
{
    Plot( C, "C", colorYellow, styleDots, Null, Null, 0, 0, 1 );

    if( shift1 == 0 )
    {
        Plot( IIf( bi <= lastIndex1, ynew1, Null ), "", ColorRGB( 3, 157, 252 ), styleDots | styleNoRescale, Null, Null, 0, 1, 2 );
        Plot( IIf( bi > lastIndex1 AND bi <= lastIndex1 + Nforecast, ynew1, Null ), "", ColorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, 0, 0, 6 );
    }
    else
        if( shift1 == 1 )
        {
            Plot( IIf( bi < lastIndex1, ynew1, Null ), "", ColorRGB( 3, 157, 252 ), styleDots | styleNoRescale, Null, Null, shift1, 1, 2 );
            Plot( IIf( bi == lastIndex1, ynew1, Null ), "", ColorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, shift1, 0, 6 );
        }

    Plot( IIf( forecast, forecast, Null ), "", colorBlue, styleDots | styleNoRescale, Null, Null, shift, -1, 1 );
    //Plot( IIf( forecast, forecast, Null ), "", colorAqua, styleDashed | styleNoRescale, Null, Null, 0, -1, 1 );
    PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorDarkGreen, 0, L, -15 );
    PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
    PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, -15 );
    PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );
}
else
{
    Plot( IIf( velocity, velocity, Null ), "", ColorGreen, styleLine, Null, Null, 0, 0, 2 );
    Plot( vup, "", ColorYellow, styleLine, Null, Null, 0, 0, 1 );
    Plot( vdn, "", ColorYellow, styleLine, Null, Null, 0, 0, 1 );
    //Plot( IIf( acceleration, acceleration, Null ), "", colorViolet, styleLine | styleNoRescale, Null, Null, 0, 0, 2 );
    Plot( 0, "", colorWhite, styleDashed, Null, Null, 0, 0, 1 );
}

if( triggerResetStaticVariables )
{
    Say( "remove static variables" );
    StaticVarRemove( "Forecast" );
    StaticVarRemove( "lastForecast" );
}

Title = "" +
        "Order: " + Order +
        " | N bars per calculation: " + nbars +
        " | Look Back: " + lookback +
        " | Forecast: " + forecast +
        " | Velocity: " + velocity +
        " | Acceleration: " + acceleration;



1 Like

Hi Edward / Thomas

Thanks for sharing the feed back and code snippet. I will load this system and test.
Mean time , I have one general query ,
What will be best normalization technique , which can be applied on unsigned array and it should be normalized between -1 to 1.
Any suggestion ?

Regards
Ajesh

hi, in my last code I used.

vup = IIf( velocity, StDev( velocity, nbars ), Null );
vdn = IIf( velocity, -StDev( velocity, nbars ), Null );

You need to use something that is independent of price and timeframe. Or you have to do what Meyers seems to do is use some number for a certain symbol and a certain timeframe. I would prefer what I used the StDev.

With respect to normalization then also you need to use some formula that does not repaint. So that could be something like:

function normalization( vel, nn )
{
    yv1 = vel - LLV( vel, nn );
    yv2 = HHV( vel, nn ) - LLV( vel, nn );
    yv = SafeDivide( yv1, yv2 );
    return yv;
}

and the vup and vdn can be calculated using:

vup = IIf( velocity, normalization( velocity, nbars ), Null );
vdn = IIf( velocity, -normalization( velocity, nbars ), Null );
1 Like

Hi Edward
Thank you very much for the response and the valuable thought.
I was studying different normalization technique we can apply on the velocity data set;further, i understood , Your thought process to use standard deviation seems to be absolutely correct as other min - max normalization technique, which you mentioned above(or other technique which i read) can create wrong data set because it took few element of the velocity array (Number of Bar) for calculation. Eventually we cannot make any decision on those data.

Regards
Ajesh

hi Ajesh,

yes my remark about normalization was just a thought to get a buy/sell level independent of the absolute value. I just do not know how he determines those levels (vdn/vup). Probably he just tries some levels. But if new data comes in, then what does he use? Does he adjust the level? But if so, then the adjusted level does not apply to the results he shows.

So might as well use something like what I used StDev.

Below I expanded the code a bit. It calculates the results of the trading system "on the fly". So you can then change the "Level factor" in the Param window and see what it does for the results.

But this is setup for trading of 1 future. For stocks you will have to adjust it a bit.

So I am not sure if he has also some better working ideas. If so let me know. He (Meyers) seems a bit over the top mathematical in his explanations

// https://forum.amibroker.com/t/need-help-with-polynomial-system-study/28070
// https://meyersanalytics.com/publications2/es1fixmv1-3.pdf

PyLoadFromFile( "polyfit using Python", "C:\\Users\\win 10\\AppData\\Local\\Programs\\Python\\Python38\\mypython\\fitting\\PolyfitMeyersLegendre.py" );

nbars = Param( "Last N Bars Closing Prices", 20, 10, 1000, 1 );
lookback = Param( "Lockback (starting point)", 100, 20, 2000, 1 );
Nforecast = 1;//Param( "Nforecast (bars)", 1, 0, 20, 1 );
order = Param( "Polynomial Order", 3, 1, 10, 1 );
levfactor = Param( "Level Factor", 1, 0.1, 5, 0.1 );
triggerCalculation = ParamTrigger( "Start Calculation", "Press Here" );
triggerResetStaticVariables = ParamTrigger( "Reset Static Variables", "Press Here" );
toggleDisplay = ParamToggle( "Toggle Display", "Velocity|Price", 1 );

SetBarsRequired( lookback, nbars );
bi = BarIndex();

// test function
// C = Cum(1);

SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );

// this part is just for display purpose
lastIndex1 = LastValue( SelectedValue( bi ) );
firstIndex1 = lastIndex1 - nbars;

xarr = IIf( bi > firstIndex1, bi, 0 );
xarr = IIf( bi > lastIndex1, 0, xarr );
yarr = IIf( bi > firstIndex1, C, 0 );
yarr = IIf( bi > lastIndex1, 0, yarr );

shift1 = 0;

// special case when lastIndex (or the SelectedValue with the mouse) coincides with the last bar
if( ( lastIndex1 + Nforecast ) - ( BarCount - 1 ) == 1 )
{
    shift1 = ( lastIndex1 + Nforecast ) - ( BarCount - 1 );
}

xnew = IIf( bi > firstIndex1 - shift1, bi + shift1, 0 );
xnew = IIf( bi > lastIndex1 + Nforecast, 0, xnew );
ynew1 = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
ynew1 = IIf( bi > firstIndex1, ynew1, Null );
ynew1 = IIf( bi > lastIndex1 + Nforecast, Null, ynew1 );

// real calculations start here
forecast = Null;
velocity = Null;
acceleration = Null;

if( triggerCalculation )
{
    Say( "start calculation" );

    for( i = BarCount - lookback; i < BarCount - 1; i++ )
    {
        lastIndex = i;
        firstIndex = lastIndex - nbars;
        xarr = IIf( bi > firstIndex, bi, 0 );
        xarr = IIf( bi > lastIndex, 0, xarr );
        yarr = IIf( bi > firstIndex, C, 0 );
        yarr = IIf( bi > lastIndex, 0, yarr );
        xnew = IIf( bi > firstIndex, bi, 0 );
        xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );

        ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
        forecast[i + 1] = ynew[i + 1];
    }

    StaticVarSet( "Forecast", forecast );
    Say( "finished" );
}

// special case: the last bar
shift = 1;
lastIndex = BarCount - 1;
firstIndex = lastIndex - nbars;
xarr = IIf( bi > firstIndex, bi, 0 );
xarr = IIf( bi > lastIndex, 0, xarr );
yarr = IIf( bi > firstIndex, C, 0 );
yarr = IIf( bi > lastIndex, 0, yarr );
xnew = IIf( bi > firstIndex - shift, bi + shift, 0 );
xnew = IIf( bi > lastIndex + Nforecast, 0, xnew );
ynew = PyEvalFunction( "polyfit using Python", "polyfitInput", xarr, yarr, order, xnew );
ynew = IIf( bi > firstIndex, ynew, Null );
ynew = IIf( bi > lastIndex + Nforecast, Null, ynew );
StaticVarSet( "lastForecast", ynew[lastIndex] );

forecast =  Nz( StaticVarGet( "Forecast" ) );
forecast = Ref( forecast, shift );
forecast[ lastIndex ] = Nz( StaticVarGet( "lastForecast" ) );

velocity = forecast - Ref( forecast, -1 );
velocity = IIf( bi > BarCount - lookback, velocity, Null );
vup = IIf( velocity, levfactor * StDev( velocity, nbars ), Null );
vdn = IIf( velocity, -levfactor * StDev( velocity, nbars ), Null );

Buy = Cross( velocity, vup );
Sell = Cross( vdn, velocity );
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
Short = Sell;
Cover = Buy;
BuyPrice = SellPrice = ShortPrice = CoverPrice = C;

BuyPrice = ValueWhen( Buy, BuyPrice );
ShortPrice = ValueWhen( Short, ShortPrice );
CoverPrice = ValueWhen( Cover, CoverPrice );
SellPrice = ValueWhen( Sell, SellPrice );

longResult = Nz( Prec( IIf( Sell, SellPrice - ValueWhen( Buy, BuyPrice ), 0 ), 2 ) );
shortResult = Nz( Prec( IIf( Cover, ValueWhen( Short, ShortPrice ) - CoverPrice, 0 ), 2 ) );

if( toggleDisplay )
{
    Plot( C, "C", colorYellow, styleCandle, Null, Null, 0, 0, 1 );

    if( shift1 == 0 )
    {
        Plot( IIf( bi <= lastIndex1, ynew1, Null ), "", ColorRGB( 3, 157, 252 ), styleDots | styleNoRescale, Null, Null, 0, 1, 2 );
        Plot( IIf( bi > lastIndex1 AND bi <= lastIndex1 + Nforecast, ynew1, Null ), "", ColorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, 0, 0, 6 );
    }
    else
        if( shift1 == 1 )
        {
            Plot( IIf( bi < lastIndex1, ynew1, Null ), "", ColorRGB( 3, 157, 252 ), styleDots | styleNoRescale, Null, Null, shift1, 1, 2 );
            Plot( IIf( bi == lastIndex1, ynew1, Null ), "", ColorRed, styleDots | styleNoLine | styleNoRescale, Null, Null, shift1, 0, 6 );
        }

    Plot( IIf( forecast, forecast, Null ), "", colorBlue, styleDots | styleNoRescale, Null, Null, shift, -1, 1 );
    //Plot( IIf( forecast, forecast, Null ), "", colorAqua, styleDashed | styleNoRescale, Null, Null, 0, -1, 1 );
    PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorAqua, 0, L, -15 );
    PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );
    PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorOrange, 0, H, -15 );
    PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorWhite, 0, SellPrice, 0 );

    totalresult = ( Cum( longResult ) * PointValue + Cum( shortResult ) * PointValue );
    Plot( IIf( totalresult, totalresult, Null ), "", colorPink, styleLine | styleOwnScale, Null, Null, 0, -1, 1 );
}
else
{
    Plot( IIf( velocity, velocity, Null ), "", ColorGreen, styleLine, Null, Null, 0, 0, 2 );
    Plot( vup, "", ColorYellow, styleLine, Null, Null, 0, 0, 1 );
    Plot( vdn, "", ColorYellow, styleLine, Null, Null, 0, 0, 1 );
    Plot( 0, "", colorWhite, styleDashed, Null, Null, 0, 0, 1 );
    
    PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorAqua, 0, vup, 25 );
    PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorOrange, 0, vdn, 25 );
}

if( triggerResetStaticVariables )
{
    Say( "remove static variables" );
    StaticVarRemove( "Forecast" );
    StaticVarRemove( "lastForecast" );
}

Title = "" +
        "Order: " + Order +
        " | N bars per calculation: " + nbars +
        " | Look Back: " + lookback +
        " | Forecast: " + forecast +
        " | Velocity: " + velocity +
        " | Acceleration: " + acceleration + "\n" +
        "Long Trade: " + longResult * PointValue + "\n" +
        "Short Trade: " + shortResult * PointValue + "\n" +
        "Long Result: " + Cum( longResult ) * PointValue + "\n" +
        "Short Result: " + Cum( shortResult ) * PointValue + "\n" +
        "Total Result: " + ( Cum( longResult ) * PointValue + Cum( shortResult ) * PointValue );
         

Hi Edward
I felt like, He has developed the system for a specific symbol(JPN Yen Future). So, the vpn and vdn values are hard-coded based on optimization result. On the other hand, the threshold(vpn, vdn) value calculation was critical. In case, if we can not calculate it correct, the system can produce lot of false signal even in trending market condition.

As of now i do not have any idea about the calculation of threshold, I am still trying to identify some solution for this.

Regards
Ajesh

hi Ajesh, i looked a bit on his site but not sure which of his systems might be the most "promising". If you figure it out please let me know. Then I will try to code it. My last code I posted has some additions and also I made sure vup and vdn start delayed with nbars.

Moderator comment: totally off-topic video removed

Hi Edward
I have loaded the new code. I will test it on multiple symbol and share my observation by tomorrow.
I was bit busy in last couple of days.

Yea, I am studying his multiple topic on trading system topic. i can share you the feedback, if i came across something .

Regards
Ajesh

Hi Edward
I was going through the latest code, From my observation, The standard deviation concept will not fit here, the spike in the velocity can not identified using the standard deviation channel breakout . Please refer the attached screen shot for reference.

stderror

So , Which implies Meyers was using static threshold calculation mechanism
On the other hand, I have different view on threshold calculation process.
As we know the velocity means the rate of change of price action. Which indirectly tells how strongly the stock moves in a direction. Which indicates previous day price action and current day price action can be same. My suggestion was to calculate threshold(vpn,vdn) from previous day data. So, the following method can be adopted to calculate the static threshold(vpn, vdn) dynamically.

Pseudo code:
HHVVelocity = Calculate the Highest high value of velocity array in the previous day ( Say 10 HHV positive values)
AvgHighThreshold = MA/ Median ( HHVVelocity ) - It can be MA or median
LLVOfVelocity = Calculate the Lowest Low value of velocity in the previous day ( Say 10 LLV negative values)
AvgLowThreshold = MA/Median(LLVOfVelocity ) - It can be MA or median

if AvgHighThreshold > AvgLowThreshold { // This check will help to identify the trending day)
vdn = AvgHighThreshold ;
vpn = -AvgHighThreshold ;
}else{
vdn = AvgLowThreshold ;
vpn = -AvgLowThreshold ;
}

Can you help me to code it on afl as i will take more time to implement?
Eventually we can calculate the average positive and negative threshold like Meyers and the system can give more accurate signal.

As we are calculating the threshold from previous day data, the process will be independent of scripts and we can apply on any symbols.

From the document , He has mentioned 65 % success rate on this system. And the system developed on 1 minute chart, I feel, this system can produce good risk to reward.

What is your view on this?

Regards
Ajesh