PureBytes Links
Trading Reference Links
|
Hello Martin,
I've got to admit that the code is hard to understand,
particularly because all the formatting was lost when I
posted it to the forum. I hope I have got the formatting
and font figured out now; this has been as almost as much
work as writing the code.
The buy/sell qualifiers are as defined by DeMark, pp.48-58.
Specifically, a buy qualifies as a "good" breakout if any one
of the following is true: 1. Yesterday's close was lower than
the previous day's close. 2. The value found by adding the
difference between yesterday's close and yesterday's low (or
the previous day's close, if it is lower) to yesterday's close
is below the supply line. 3. Today's open is above the supply
line. The sell qualifiers are just the inverse of the above.
The qualifiers are implemented in the looping code via the
parameter selection, and are applied (or not) automatically
when the parameter slider is adjusted.
The buy and sell arrays are calculated in the looping structure,
and it is set up so that signals are only possible on or after
the day after the pivot is defined. The plotting code does draw
the lines starting on the day of the pivot, but buys/sells are
not plotted until the second day. The line-plotting part of the
code plots buy/sell arrows above or below the arrows from the
buy/sell signals generated in the loops. As you move the cursor
line, you will see that these arrows move around with changes
in cursor position, but the complete set of buy/sell arrows
(signals) are fixed and represent the buys/sells with all the
redundant signals removed.
The case of simultaneous buy/sell signals can be treated as you
wish (favoring buys or sells) in the looping code; presently it
changes state; i.e. if the system is presently on a buy as the
cursor line is moved forward to a point where simultaneous
signals are produced, then the sell signal is taken. This
behavior can be observed by watching the orange and aqua arrows
versus the red and green ones.
I have added plots for buy and hold and the trading result,
assuming the trades are made at the price of the demand or
supply line. So far, the best results come using your three-day
pivot definition and DeMark's B/S qualifiers. Using the Param
slider makes this easy to see. The results do not seem too good
on the few stocks I have checked. I guess the next step is to
modify the definitions of the pivot points to try for better
results. This Amibroker probably is a much better tool than
DeMark used for his research and development!
Below is my latest version of the code.
Bill
--------------------------------------------------------------
/* DeMark's Demand and Supply Line Trading System -
Original code by marmal417, Johsun, Dimitris Tsokakis
with variations by Bill Barnard, 2/19/04.
The numerous loops make for a slow calculation
when the stock of interest has more than two
or three years of data. Suggest making short-lived
duplicates of stocks to be studied.
Colors set for use with black background */
/* Following line allows changing the number of days which
must preceed a pivot. "equal" allows a pivot which is
equal in value to the preceeding day. */
rules = Param("1-1day, 2-Equal, 3-3day", 3,1,3,1);
/* Following line allows the use of DeMark's three
qualifiers for a legitimate breakout. */
Q_On = Param("Use Qualifier", 1,0,1,1);
extension = 20; // length of demand/supply line extension
Plot(C,"",colorWhite,styleCandle | styleNoLabel);
prevLow = IIf(Ref(L,-1)<Ref(C,-2), Ref(L,-1), Ref(C,-2));
prevHigh = IIf(Ref(H,-1)>Ref(C,-2), Ref(H,-1), Ref(C,-2));
pivothigh = IIf(rules==1, prevHigh<H AND H>Ref(H,1),
IIf(rules==2, prevHigh<=H AND H>Ref(H,1),
IIf(rules==3, Ref(C,-3)<H AND Ref(H,-2)<H AND
Ref(H,-1)<H AND H>=Ref(H,1), 0)));
pivotlow = IIf(rules==1, prevLow>L AND L<Ref(L,1),
IIf(rules==2, prevLow>=L AND L<Ref(L,1),
IIf(rules==3, Ref(C,-3)>L AND Ref(L,-2)>L AND
Ref(L,-1)>L AND L<=Ref(L,1), 0)));
/* Following section loops through the data to develop
all the Buy/Sell points for back testing purposes. */
pLo = IIf(pivotLow==1, L, 1000000);
pHi = IIf(pivotHigh==1, H, 0);
By = 0; Sel = 0; CountHi = 0; CountLo = 0;
for (i=3; i<BarCount; i++)
{
if (plo[i]<1000000)
{
CountLo = CountLo + 1;
j = i;
while (plo[j-0]>=plo[i] AND j>2)
{
pplo[j-1] = plo[j-1];
j = j - 1;
}
dLine = IIf(CountLo<2 OR pplo[j]>plo[i],Null,
LineArray(j,pplo[j],i,plo[i],1));
exit = 0;
for (k = i+2; exit==0 AND k<BarCount; k++)
{
SellQualifier = IIf(Q_On, C[k-1]>C[k-2] OR
(2*C[k-1] - IIf(H[k-1]<C[k-2],
C[k-2], H[k-1])) > dLine[k-1]
OR O[k] < dLine[k], 1);
if (L[k]<dLine[k] AND SellQualifier)
{
Sel[k] = 1;
SellPrice[k]=ShortPrice[k]=dLine[k];
exit = 1;
for (m=k+1; m<BarCount; m++)
{
Sel[m] = 0;
}
}
}
}
if (phi[i]>0)
{
CountHi = CountHi + 1;
j = i;
while (phi[j-0]<=phi[i] AND j>2)
{
pphi[j-1] = phi[j-1];
j = j - 1;
}
sLine = IIf(CountHi<2 OR pphi[j]<phi[i], Null,
LineArray(j,pphi[j],i,phi[i],1));
exit = 0;
for (k = i+2; exit==0 AND k<BarCount; k++)
{
BuyQualifier = IIf(Q_On, C[k-1]<C[k-2] OR
(2*C[k-1] - IIf(L[k-1]>C[k-2],
C[k-2], L[k-1])) < sLine[k-1] OR
O[k] > sLine[k], 1);
if (H[k]>sLine[k] AND BuyQualifier)
{
By[k] = 1;
BuyPrice[k] = CoverPrice[k]= sLine[k];
exit = 1;
for (m=k+1; m<BarCount; m++)
{
By[m] = 0;
}
}
}
}
}
Buy = Cover = ExRem(By, Sel);
Sell = Short = ExRem(Sel, By);
SetTradeDelays(0, 0, 0, 0);
eq = Equity( 0, -2 );
/* Following section constructs the plots of the demand and
supply lines at the area of selection with the pole. */
x1=SelectedValue(ValueWhen(pivotlow,BarIndex(),1));
y1=SelectedValue(ValueWhen(pivotlow,L,1));
x0=SelectedValue(ValueWhen(pivotlow AND L<y1,BarIndex(),1));
y0=SelectedValue(ValueWhen(pivotlow AND L<y1,L,1));
x3=SelectedValue(ValueWhen(pivothigh,BarIndex(),1));
y3=SelectedValue(ValueWhen(pivothigh,H,1));
x2=SelectedValue(ValueWhen(pivothigh AND H>y3,BarIndex(),1));
y2=SelectedValue(ValueWhen(pivothigh AND H>y3,H,1));
demandLine =IIf(x0==0,Null,LineArray(x0,y0,x1,y1,1));
supplyLine =IIf(x2==0,Null,LineArray(x2,y2,x3,y3,1));
Plot(IIf(Cum(1)<x3+extension, supplyLine, Null),"",colorWhite,
styleLine|styleThick|styleNoRescale|styleNoLabel);
Plot(IIf(Cum(1)<x1+extension, demandLine, Null),"",colorWhite,
styleLine|styleThick|styleNoRescale|styleNoLabel);
BuyQualifier = IIf(Q_On, Ref(C, -1)<Ref(C,-2) OR (2*Ref(C,-1) -
IIf(Ref(L,-1)>Ref(C,-2), Ref(C,-2), Ref(L,-1))) <
Ref(supplyline, -1) OR O > supplyline, 1);
SellQualifier = IIf(Q_On, Ref(C, -1)>Ref(C,-2) OR (2*Ref(C,-1) -
IIf(Ref(H,-1)<Ref(C,-2), Ref(C,-2), Ref(H,-1))) >
Ref(demandline, -1) OR O < demandline, 1);
By = Cross(Cum(H>supplyline AND BarIndex()-1>x3 AND
BuyQualifier),0);
Sel = Cross(Cum(L<demandLine AND BarIndex()-1>x1 AND
SellQualifier),0);
PlotShapes((BarIndex()==x0 OR BarIndex()==x1)*
shapeSmallCircle,colorWhite,0,L, -8);
PlotShapes(Sell*shapeDownArrow,colorRed,0,H);
PlotShapes(Sel*shapeDownArrow,colorLightOrange,0,H,-25);
PlotShapes((BarIndex()==x2 OR BarIndex()==x3)*
shapeSmallCircle,colorWhite,0,H,8);
PlotShapes(Buy*shapeUpArrow,colorBrightGreen,0,L);
PlotShapes(By*shapeUpArrow,colorAqua,0,L, -25);
// buy and hold simulation for plotting in IB
Short = Cover =0;
Buy = Status("firstbarintest");
Sell = Status("lastbarintest");
SetTradeDelays(0,0,0,0); PositionSize = -100;
ApplyStop(0,0,0,0);
ApplyStop(1,0,0,0);
ApplyStop(2,0,0,0);
bh = Equity( 0, -2 );
/* Following section sets limits for StyleOwnScale plots.
A typical plot statement would be:
Plot(xxxx, "xxxx", 0, styleOwnScale,
LL2(xxxx, yyyy, 5), UL1(xxxx, yyyy, 20));
where the 5 and the 20 represent the percent of freespace
below and above the plots of xxxx and yyyy */
function LL2 (Variable1, Variable2, FreespacePercent) // lower
{
barpresent = Status("barvisible");
Hii = IIf(LastValue(Highest(Variable1*barpresent)) >
LastValue(Highest(Variable2*barpresent)),
LastValue(Highest(Variable1*barpresent)),
LastValue(Highest(Variable2*barpresent)));
Loo = IIf(LastValue(Lowest((Variable1-Hii)*barpresent)+Hii)<
LastValue(Lowest((Variable2-Hii)*barpresent)+Hii),
LastValue(Lowest((Variable1-Hii)*barpresent)+Hii),
LastValue(Lowest((Variable2-Hii)*barpresent)+Hii));
range = Hii -Loo;
result = Loo - 0.01*FreespacePercent*range;
return result;
}
function UL2 (Variable1, Variable2, FreespacePercent) // upper
{
barpresent = Status("barvisible");
Hii = IIf(LastValue(Highest(Variable1*barpresent)) >
LastValue(Highest(Variable2*barpresent)),
LastValue(Highest(Variable1*barpresent)),
LastValue(Highest(Variable2*barpresent)));
Loo = IIf(LastValue(Lowest((Variable1-Hii)*barpresent)+Hii)<
LastValue(Lowest((Variable2-Hii)*barpresent)+Hii),
LastValue(Lowest((Variable1-Hii)*barpresent)+Hii),
LastValue(Lowest((Variable2-Hii)*barpresent)+Hii));
range = Hii -Loo;
result = Loo + range / (1 - 0.01*FreespacePercent);
return result;
}
Plot( bh,"Buy&Hold", colorWhite,
styleOwnScale, LL2(bh, eq, 5), UL2(bh, eq, 70));
Plot(eq, "Trading Result", colorRed,
styleOwnScale, LL2(bh, eq, 5), UL2(bh, eq, 70));
Title= Name()+" "+Date()+EncodeColor(colorWhite)+
" Open "+WriteVal(Open, 1.3)+" Hi "+WriteVal(High,1.3)+
" Lo "+WriteVal(Low,1.3)+" Close "+WriteVal(Close,1.3)+
EncodeColor(colorRed)+" Sell =,"+WriteVal((y1-y0)/(x1-x0)+
SelectedValue(DEMAndline),1.3)+EncodeColor(colorBrightGreen)+
" Buy =,"+WriteVal((y3-y2)/(x3-x2)+
SelectedValue(supplyline),1.3 )+
"\n\\c03Use Param to select Qualifiers and Days \n "+
EncodeColor(colorWhite)+" Buy and Hold = "+WriteVal(bh, 1.3)+
EncodeColor(colorRed)+"\n Trading Result = "+WriteVal(eq, 1.3);
GraphXSpace=24;
Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> To unsubscribe from this group, send an email to:
amibroker-unsubscribe@xxxxxxxxxxxxxxx
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|