Buying at high of previous Bar sometimes buys at Low if there is a Gap up

This is the strategy for trading the Ehlers correlation indicator mentioned in the June 2020 issue of S&C.
My logic is simple:
if correlation indicator starts rising (current value > previous value), then Buy next bar at high of current bar.
Today, I test if indicator was rising previous day. If yes, I buy at or above the high of previous bar.
The trade executes correctly, when there is no gap up. But if there is a gap, the system buys at the low of the day, when it should buy at the open. Why?
Also, my exploration is not showing any results. Sorry for these elementary questions.

Here is the code:

_SECTION_BEGIN("SC-june20");


// june 2020 ehlers corelation
// matches with the code in the magazine

function ParamOptimize( description, defaultVal, minv, maxv, step )
    { 
    return Optimize(description, Param(description,defaultVal, minv, maxv, step ), minv, maxv, step ); 
    }

SetPositionSize( 1, spsShares ); // 1 shares by default


Period = ParamOptimize( "Period", 54, 5, 175, 1 );

Length = Period;

Sx = Sy = Sxx = Sxy = Syy = 0;
PIx2 = 6.28318530718;

for( count = 1; count <= Length; count++ )
{
X = Ref( Close, -(count-1));
	// the next two line are supposed to be a replacement for
	// Y = Cosine( 360 * ( Count - 1 ) / Period ) ;
	temp = PIx2 * (count - 1) / period;
	y = cos(temp);
	Sx = Sx + X;
	Sy = Sy + Y;
	Sxx = Sxx + X * X;
	Sxy = Sxy + X * Y;
	Syy = Syy + Y * Y;	
}

Num = Length * Sxy - Sx * Sy;

Denom = sqrt( ( Length * Sxx - Sx * Sx ) * ( Length * Syy - Sy * Sy ) );

Real = IIf( Denom != 0, Num / Denom, Null );

Plot( Real, "SCJune20", colorRed, styleThick );


GraphXSpace = 10;
SetTradeDelays(0,0,0,0);

BuySignal = Ref(Real,-1) > Ref(Real,-2);
SellSignal = Ref(Real,-1) <= Ref(Real,-2);

BuyStopPrice = Ref(H,-1); // yesterday high
SellStopPrice = Ref(LLV(L,2),-1); // lowest low of last two bars

// now we check if stop was hit
Buy = BuySignal AND H > BuyStopPrice;
Sell = Sellsignal AND L < SellStopprice;

// if Open price is above the Buystop, then we use Open for entry
BuyPrice = Max( Open, BuyStopPrice );  // if there is a gap up, buy at open.


// if Open price is below the Sellstop, then we use Open for entry
SellPrice = Min( Open, SellStopPrice );


Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow, IIf( Buy, colorGreen, colorRed ));

_SECTION_END();

// surprise! exploration gives no results on current instrument, with from to dates even on one bar

Filter = 1;

AddColumn(Date,"Date"));
AddColumn(BuyStopPrice,"BPr");
AddColumn(SellStopPrice,"SPr");

Well, solved the exploration issue. I should be writing:
Filter = true.
Now, I am getting the exploration results.

I am getting the correct results. Maybe I was using some other code (unlikely!)
Anyway, the system is buying at the open if there is a gap up. This is the correct action. Here is the final code:

_SECTION_BEGIN("SC-june20");


// june 2020 ehlers corelation
// matches with the code in the magazine

function ParamOptimize( description, defaultVal, minv, maxv, step )
    { 
    return Optimize(description, Param(description,defaultVal, minv, maxv, step ), minv, maxv, step ); 
    }

SetPositionSize( 1, spsShares ); // 1 shares by default


Period = ParamOptimize( "Period", 54, 5, 175, 1 );

Length = Period;

Sx = Sy = Sxx = Sxy = Syy = 0;
PIx2 = 6.28318530718;

for( count = 1; count <= Length; count++ )
{
X = Ref( Close, -(count-1));
	// the next two line are supposed to be a replacement for
	// Y = Cosine( 360 * ( Count - 1 ) / Period ) ;
	temp = PIx2 * (count - 1) / period;
	y = cos(temp);
	Sx = Sx + X;
	Sy = Sy + Y;
	Sxx = Sxx + X * X;
	Sxy = Sxy + X * Y;
	Syy = Syy + Y * Y;	
}

Num = Length * Sxy - Sx * Sy;

Denom = sqrt( ( Length * Sxx - Sx * Sx ) * ( Length * Syy - Sy * Sy ) );

Real = IIf( Denom != 0, Num / Denom, Null );

Plot( Real, "SCJune20", colorRed, styleThick );


GraphXSpace = 10;
SetTradeDelays(0,0,0,0);

BuySignal = Ref(Real,-1) > Ref(Real,-2);
SellSignal = Ref(Real,-1) <= Ref(Real,-2);

BuyStopPrice = Ref(H,-1); // yesterday high
SellStopPrice = Ref(LLV(L,2),-1); // lowest low of last two bars

// now we check if stop was hit
Buy = BuySignal AND H > BuyStopPrice;
Sell = Sellsignal AND L < SellStopprice;

// if Open price is above the Buystop, then we use Open for entry
BuyPrice = Max( Open, BuyStopPrice );  // if there is a gap up, buy at open.


// if Open price is below the Sellstop, then we use Open for entry
SellPrice = Min( Open, SellStopPrice );


Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow, IIf( Buy, colorGreen, colorRed ));

_SECTION_END();

// Exploration

Filter = True;

//AddColumn(Date,"Date");
AddColumn(BuyStopPrice,"BPr");
AddColumn(BuyPrice,"buyPr");
1 Like

Filter = 1 and Filter = True are the same thing

Date is string function and it was without brackets and incorrect Add..column function.

AddColumn(Date,"Date"));// incorrect
AddTextColumn(Date(),"Date");// correct

BTW, the posted code is copied from other existing codes.

@charttrader , Filter = 1; was fine and is equivalent to Filter = true;.

The previous version of code did not display exploration results since there was an error on the line:

AddColumn(Date,"Date");

that you commented out on the revised version.

1 Like

@fxshrat and @Beppe, thank you for your replies.

@Fxshrat, almost all code I write in Amibroker is copied from other existing codes found in the forum. I still plan the code in Multicharts language, then start searching for equivalent in Amibroker.
I like Amibroker so much, I continue to spend time on it, hoping to get a working knowledge soon.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.