Does anybody want to take a crack at translating the following Wealthlabscript code?
Announcement
Collapse
No announcement yet.
Wealthscript code translation
Collapse
X
-
Perhaps I shouldn't test everyone's mind reading skills and post the code:
var BAR, F, X, DAYS, CYCLELENGTH, LASTLOWBAR, COUNT, DAYS1,DAYS2,DAYS3,DAYS4,DAYS5, DIVIDEBY, mo1,mo2,mo3,mo4,mo5,thePrice,theVolume,AD: integer;
var AVG, DUHigh, DULow, DUBand, curvol, VOL, N, DU, PV, BASE1,BASE2,BASE3,BASE4,BASE5,PEAK1,PEAK2,PEAK3,PE AK4,PEAK5,GAIN1,GAIN2,GAIN3,GAIN4,GAIN5,AVERAGEGAI N,DU1,DU2,DU3,DU4,DU5,AVERAGEDU : float;
var RETRACE, theMACD: integer;
var HersheyStochD : integer = StochDSeries(14, 1);
var HersheyStochK : integer = SMASeries(HersheyStochD, 3);
function MACDExSeries( Series: Integer; Period1: Integer; Period2: Integer ): integer;
begin
var Bar: integer;
var sName: string;
var Value: float;
sName := 'MACDEx(' + GetDescription( Series ) + ',' + IntToStr( Period1 ) + ',' + IntToStr( Period2 ) + ')';
Result := FindNamedSeries( sName );
if Result >= 0 then
Exit;
Result := CreateNamedSeries( sName );
for Bar := Round( Max( Period1, Period2 ) ) to BarCount - 1 do
begin
{ Calculate your indicator value here }
Value := EMA( Bar, Series, Period1 ) - EMA( Bar, Series, Period2 );
SetSeriesValue( Bar, Result, Value );
end;
end;
function MACDEx( Bar: integer; Series: Integer; Period1: Integer; Period2: Integer ): float;
begin
Result := GetSeriesValue( Bar, MACDExSeries( Series, Period1, Period2 ) );
end;
procedure paintMacdPane(period1 : integer; period2 : integer; smoothPeriod : integer;
price : integer; MacdPane : integer; tick : float);
begin
var Bar : integer;
var MCD : integer = MACDExSeries(price, period1, period2);
var Trig : integer = EMASeries(MCD,smoothPeriod);
var Histo : integer = SubtractSeriesvalue(MCD, 0);
var MACDNeutral : boolean;
DrawHorzLine(0.0, MacdPane, #black, #solid);
PlotSeriesLabel(MCD, MacdPane, #blue, #thin, 'MACD(5,13,6)');
PlotSeries(Histo, MacdPane, #black, #thickhist);
DrawHorzLine(2*tick, MacdPane, #blue, #thin);
DrawHorzLine(4*tick, MacdPane, #blue, #thin);
DrawHorzLine(-2*tick, MacdPane, #red, #thin);
DrawHorzLine(-4*tick, MacdPane, #red, #thick);
for Bar := 0 to BarCount - 1 do
begin
if ((@Histo[Bar] < 2*tick) and (@Histo[Bar] > -2*tick)) then
MACDNeutral := true
else
MACDNeutral := false;
if ((@MCD[Bar] > @Trig[Bar]) and (@Histo[Bar] > 0) and (not MACDNeutral)) then
begin
SetSeriesBarColor( Bar, Histo, #Green ) ;
end
else if ((@MCD[Bar] > @Trig[Bar]) and (@Histo[Bar] < 0) and (not MACDNeutral)) then
begin
SetSeriesBarColor( Bar, Histo, #Red) ;
end
else if ((@MCD[Bar] < @Trig[Bar]) and (@Histo[Bar] < 0) and (not MACDNeutral)) then
begin
SetSeriesBarColor( Bar, Histo, #Red) ;
end
else if ((@MCD[Bar] < @Trig[Bar]) and (@Histo[Bar] > 0) and (not MACDNeutral)) then
begin
SetSeriesBarColor( Bar, Histo, #Green );
end;
end;
end;
function getPVADScore() : integer;
begin
var Score : integer = 0 ;
if (@#Close[BarCount - 1] > @#Close[BarCount -2]) then
Score := Score + 4;
if (@#volume[BarCount - 1] > @#volume[BarCount - 2]) then
Score := Score + 2;
if (PriceClose( BarCount - 1 ) > PriceOpen( BarCount - 1 )) then
Score := Score + 1;
Result := Score;
end;
PlotSeries( SMASeries( #VOLUME, 65) , 1, #Red, #Thin );
mo1:=BARCOUNT() -1 ;
mo2:=BARCOUNT() -21 ;
mo3:=BARCOUNT() -41 ;
mo4:=BARCOUNT() -61 ;
mo5:=BARCOUNT() -81 ;
DrawText( +' VOLUME AVG(65)= '+FormatFloat( '0,0' , SMA(mo1, #VOLUME, 65) ) , 1, 5, 15, #Blue, 8 );
RETRACE := 1;
F:=40;COUNT:=0;LASTLOWBAR :=1000000;
FOR BAR:= BARCOUNT() -1 DOWNTO BARCOUNT()-127 do
BEGIN
IF LASTLOWBAR < BAR THEN BAR:= LASTLOWBAR ;
//CHECK CYCLE AT SIX DAYS
X:= PEAKBAR(BAR, #HIGH, RETRACE) ;
N:= PEAK(BAR, #HIGH, RETRACE) - LOWEST(X-1, #LOW, 5) ;
IF N/LOWEST(X-1, #LOW, 5) >= 0.20 THEN CYCLELENGTH:= 5 ELSE
BEGIN
//RAISE CYCLE TO SEVEN
X:= PEAKBAR(BAR, #HIGH, RETRACE) ;
N:= PEAK(BAR, #HIGH, RETRACE) - LOWEST(X-1, #LOW, 6) ;
IF N/LOWEST(X-1, #LOW, 6) >= 0.20 THEN
CYCLELENGTH:= 6 ELSE
BEGIN
//RAISE CYCLE TO EIGHT
X:= PEAKBAR(BAR, #HIGH, RETRACE) ;
N:= PEAK(BAR, #HIGH, RETRACE) - LOWEST(X-1, #LOW, 7) ;
IF N/LOWEST(X-1, #LOW, 7) >= 0.20 THEN
CYCLELENGTH:= 7;
END;
END;
IF N/LOWEST(X-1, #LOW, CYCLELENGTH) >= 0.20 THEN
BEGIN
DAYS:= PEAKBAR(BAR, #HIGH, RETRACE) - LOWESTBAR(X-1, #LOW, CYCLELENGTH)+1 ;
IF PRICECLOSE(PEAKBAR(BAR, #HIGH, RETRACE) ) < PRICECLOSE(PEAKBAR(BAR, #HIGH, RETRACE)-1 ) THEN
PV:= VOLUME(PEAKBAR(BAR, #HIGH, RETRACE)-1 ) ELSE PV:= VOLUME(PEAKBAR(BAR, #HIGH, RETRACE) ) ;
DrawCircle( 6, 0, PEAKBAR(BAR, #HIGH, RETRACE), PEAK(BAR, #HIGH, RETRACE) , #green, #thick );
DrawCircle( 6, 0, LOWESTBAR(X-1, #LOW, CYCLELENGTH), LOWEST(X-1, #LOW, CYCLELENGTH) , #blue, #thick );
AnnotateBar('High', PEAKBAR(BAR, #HIGH, RETRACE), true, #blue, 8);
AnnotateBar('Low', LOWESTBAR(X-1, #LOW, CYCLELENGTH), false, #red, 8);
DrawLine(LOWESTBAR(X-1, #LOW, CYCLELENGTH), LOWEST(X-1, #LOW, CYCLELENGTH), PEAKBAR(BAR, #HIGH, RETRACE), PEAK(BAR, #HIGH, RETRACE), 0, #blue, #thin);
F:=F+10;
LASTLOWBAR:=LOWESTBAR(X-1, #LOW, CYCLELENGTH)-1 ;
COUNT:= COUNT+1;
IF COUNT=1 THEN BEGIN
GAIN1:=N/LOWEST(X-1, #LOW, CYCLELENGTH) ;DAYS1:=DAYS;DU1:=LOWEST(mo1, #VOLUME, 20);
END;
IF COUNT=2 THEN BEGIN
GAIN2:=N/LOWEST(X-1, #LOW, CYCLELENGTH) ;DAYS2:=DAYS;DU2:=LOWEST(mo2, #VOLUME, 20);
END;
IF COUNT=3 THEN BEGIN
GAIN3:=N/LOWEST(X-1, #LOW, CYCLELENGTH) ;DAYS3:=DAYS;DU3:=LOWEST(mo3, #VOLUME, 20);
END;IF COUNT=4 THEN BEGIN
GAIN4:=N/LOWEST(X-1, #LOW, CYCLELENGTH) ;DAYS4:=DAYS;DU4:=LOWEST(mo4, #VOLUME, 20);
END;
IF COUNT=5 THEN BEGIN
GAIN5:=N/LOWEST(X-1, #LOW, CYCLELENGTH) ;DAYS5:=DAYS;DU5:=LOWEST(mo5, #VOLUME, 20);
AVERAGEGAIN:= (GAIN1+GAIN2+GAIN3+GAIN4+GAIN5)/5 ;
IF DU1>0 THEN DIVIDEBY:=1;
IF DU2>0 THEN DIVIDEBY:=2;
IF DU3>0 THEN DIVIDEBY:=3;
IF DU4>0 THEN DIVIDEBY:=4;
IF DU5>0 THEN DIVIDEBY:=5;
AVERAGEDU:= (DU1+DU2+DU3+DU4+DU5)/DIVIDEBY;
curVol := VOLUME(Barcount-1);
DUBand := AVERAGEDU*(4986/10000);
DUHigh := AverageDU + DUBand;
DULow := AverageDU - DUBand;
AddCommentary('<h3>Symbol: ' + GetSymbol + '</h3>' );
Addcommentary(' Upper Band = '+FormatFloat('0,0' , DUHigh ) + ' <br> ' );
Addcommentary(' Average DU = '+FormatFloat('0,0' , AVERAGEDU ) + ' <br> ' );
Addcommentary(' Lower Band = '+FormatFloat('0,0' , DULow ) + ' <br> ' );
Addcommentary(' FRV = '+FormatFloat('0,0' , 3*AVERAGEDU ) + ' <br> ' );
Addcommentary(' Peak = '+FormatFloat('0,0' , 2*(3*AVERAGEDU) ) + ' <br> ' );
END;
IF COUNT = 5 THEN BREAK;
END;
END;
IF DUHigh > curvol THEN SetColorScheme(#Black, #red, #blue, #GreenBkg, 888, #Silver);
for Bar := 0 to BarCount - 1 do
begin
if (@#volume[Bar] > DUHigh) then
SetSeriesBarCOlor(Bar, #volume, #blue)
else if (@#volume[Bar] < DUHigh) then
SetSeriesBarColor(Bar, #volume, #red);
end;
var SMA1 : integer = SMASeries( #Close,50 );
PlotSeries( SMA1, 0, #Blue, #Thick);
var macdPane : integer = CreatePane(60, false, true);
paintMacdPane(5, 13, 6, #close, macdPane, GetTick);
var stochPane : integer = CreatePane(60, false, true);
DrawHorzLine(75, stochPane, #black, #dotted);
DrawHorzLine(20, stochPane, #black, #dotted);
PlotSeries(HersheyStochD, stochPane, #blue, #thick);
PlotSeries(HersheyStochK, stochPane, #red, #thick);
Drawtext('Stoch(14,1,3)', stochPane, 4, 4, #blue, 009 );
var PVADScore : integer = getPVADScore;
if AVERAGEGAIN > 0 then
DrawText('Rank: '+FormatFloat( '0.00', 100*((AVERAGEGAIN)/((DAYS1+DAYS2+DAYS3+DAYS4+DAYS5)/5)) ), 0, 6, F, #Blue, 10 )
else
DrawText('Rank: None', 0, 6, F, #Blue, 10 );
Drawtext('Score: '+FormatFloat( '0', PVADScore ), 0, 6, F+15, #blue, 10 );
Comment