| Coding Forum Collaborate, receive help, or discuss coding related issues. |
![]() | | Tweet | |
| | #1 | ||
![]() | MACDBB Enhancements If somebody could take a look at this code it would be greatly apprciated. Thanks Code: [LegacyColorValue = false];
{ ************************************************** *******************
MACD-BB MACD Bolinger Banded
Last Updated 8/30/2007
Added Up/Down Color to MACD
************************************************** *********************}
Input:
Use.XAvg(False),
PlotCross(True),
FastMA( 5), {12 default}
SlowMA( 13), { 26 default}
price( myprice),
PlotBBands(true),
SDev( 1.5), { 1.5 default}
PlotBBAvg(false),
BBavg( 10), { 10 default}
Plot0Line(true),
UpColor(cyan),
UpAbove(darkgreen),
DnColor(Magenta),
DnBelow(darkred),
Shadow(Yellow),
Expanding(Green);
Vars:
BWI(0);
value1 = MACD(price,FastMA,SlowMA);
value2 = BollingerBand(MACD(price,FastMA,SlowMA),BBavg,SDev); //Upper Band
value3 = BollingerBand(MACD(price,FastMA,SlowMA),BBavg,-SDev) ; //Lower Band
// Band Width Indicator
if Use.XAvg then
value5 = XAverage(value1,BBavg)
else
Value5 = averagefc(value1,BBavg) ;
{value5 = Average(value1,BBavg) ; }
if value1 > value1[1] and value1 > value2 then
begin
// Plot1[1](Plot1[1],"MACD",UpAbove);
Plot1(value1,"MACD",UpAbove);
end else
begin
if value1 > value1[1] then begin
// Plot1[1](Plot1[1],"MACD",UpColor);
Plot1(value1,"MACD",UpColor);
end else
begin
if value1 < value1[1] and value1 < value3 then begin
// Plot1[1](Plot1[1],"MACD",DnBelow);
Plot1(value1,"MACD",DnBelow);
end else
begin
if value1 < value1[1] then begin
// Plot1[1](Plot1[1],"MACD",DnColor);
Plot1(value1,"MACD",DnColor);
end; end; end; end;
if Plot0Line = true then
begin
if value1 < 0 then
SetPlotColor(2,Red)
else
SetPlotColor(2,Blue);
Plot2(0,"ZeroLine");
end;
if PlotBBands = true then
begin
////////
If Value5>Value5[1] then
SetPlotColor[1](3,Expanding);
If Value5>Value5[1] then
SetPlotColor[1](4,Expanding);
///////
plot3(value2,"bband+");
plot4(value3,"bband-");
end;
if PlotBBAvg = true then begin
Plot5(value5,"avg");
end;
Plot6(value1,"MDShadow");
If PlotCross
then
begin
If Value1[1]<0 and Value1>0
then
Plot7(Value1,"CrossUp") else
NoPlot(7);
If Value1[1]>0 and Value1<0
then
Plot8(Value1,"CrossDn") else
NoPlot(8);
end; | ||
| |
|
| | #2 | ||
![]() | Re: MACDBB Enhancements what does "but they do not look quite right" mean? let me guess... the pink should be lime green?
__________________ Only an idiot would reply to a stupid post | ||
| |
|
| | #3 | ||
![]() | Re: MACDBB Enhancements The BB's are not always expanding when the plot changes color. I have attached a screenshot of what I am looking at. | ||
| |
|
| | #4 | ||
![]() | Re: MACDBB Enhancements Quote:
you have to be MORE specific... a lot more specific (this is to reflect the clarity of your thinking) what color you are looking at ? (what is the plot # ?) what color it should be? and WHY ? WHY and WHY ? what causes it to change? how many ways does it supposed to change? (# of options/permutations) which section of the code that is supposed to paint the color? can you articulate the logic in English/pseudo-code ? (in one-line-per-logic format?) if you can write them out one thought at a time... you will easily see where it went astray.
__________________ Only an idiot would reply to a stupid post | ||
| |
|
| | #5 | ||
![]() | Re: MACDBB Enhancements do not use generic variable names. ie. Value1, Value2, etc., create a custom variable name that makes sense... so that you know what kind of data you are working with. when your code gets lengthy... you will loose track of which is what. when you come back to the code 3 months from now... you will have to STUDY the code to figure out which is what. bugs are to be avoided at all cost... this is the first step.
__________________ Only an idiot would reply to a stupid post | ||
| |
|
| | #6 | ||
![]() | Re: MACDBB Enhancements I guess this is what happens when you take someone elses code, in this case two different authors code and then try to marry them together and then to make changes. The only outstanding issue with my code is that I am trying to have the Bollinger Bands change [in this case from yellow to a green] colour when the bands are expanding. If they are not expanding then just leave them a yellow color. I agree that using Values 1 through 5 is ambiguous and I should look at changing the code. Value 5 at this point is // Band Width Indicator if Use.XAvg then value5 = XAverage(value1,BBavg) else Value5 = averagefc(value1,BBavg) ; Then later on in the code I am using this to change the color: If Value5>Value5[1] then SetPlotColor[1](3,Expanding); If Value5>Value5[1] then SetPlotColor[1](4,Expanding); /////// plot3(value2,"bband+"); plot4(value3,"bband-"); end; I do need to get an understanding of the code that I took from the site first. I then need to reconstruct this whole thing so that the naming conventions are meaningful to me as you suggest. Cheers | ||
| |
|
| | #7 | ||
![]() | Re: MACDBB Enhancements You see, you have lots of conditional logic in your code. Many of the logics are nested... ie, there are multiple layers of IF condition1=true THEN do-this ELSE do-that... When you have a lot of code, the logic is easy to get lost in the "spaghetti". Formatting (indentation) of the code helps you to visualize your logic flow. Formatting has no effect on the computing process, this is strictly for human consumption. Here's the formated code: Code: [LegacyColorValue = false];
{ ************************************************** *******************
MACD-BB MACD Bolinger Banded
Last Updated 8/30/2007
Added Up/Down Color to MACD
************************************************** *********************}
Input:
Use.XAvg(False),
PlotCross(True),
FastMA( 5), {12 default}
SlowMA( 13), { 26 default}
price( myprice),
PlotBBands(true),
SDev( 1.5), { 1.5 default}
PlotBBAvg(false),
BBavg( 10), { 10 default}
Plot0Line(true),
UpColor(cyan),
UpAbove(darkgreen),
DnColor(Magenta),
DnBelow(darkred),
Shadow(Yellow),
Expanding(Green);
Vars:
BWI(0);
value1 = MACD(price,FastMA,SlowMA);
value2 = BollingerBand(MACD(price,FastMA,SlowMA),BBavg,SDev); //Upper Band
value3 = BollingerBand(MACD(price,FastMA,SlowMA),BBavg,-SDev) ; //Lower Band
// Band Width Indicator
if Use.XAvg then
value5 = XAverage(value1,BBavg)
else
Value5 = averagefc(value1,BBavg) ;
{value5 = Average(value1,BBavg) ; }
if value1 > value1[1] and value1 > value2 then
begin
// Plot1[1](Plot1[1],"MACD",UpAbove);
Plot1(value1,"MACD",UpAbove);
end
else
begin
if value1 > value1[1] then
begin
// Plot1[1](Plot1[1],"MACD",UpColor);
Plot1(value1,"MACD",UpColor);
end
else
begin
if value1 < value1[1] and value1 < value3 then
begin
// Plot1[1](Plot1[1],"MACD",DnBelow);
Plot1(value1,"MACD",DnBelow);
end
else
begin
if value1 < value1[1] then
begin
// Plot1[1](Plot1[1],"MACD",DnColor);
Plot1(value1,"MACD",DnColor);
end;
end;
end;
end;
if Plot0Line = true then
begin
if value1 < 0 then
SetPlotColor(2,Red)
else
SetPlotColor(2,Blue);
Plot2(0,"ZeroLine");
end;
if PlotBBands = true then
begin
////////
If Value5>Value5[1] then
SetPlotColor[1](3,Expanding);
If Value5>Value5[1] then
SetPlotColor[1](4,Expanding);
///////
plot3(value2,"bband+");
plot4(value3,"bband-");
end;
if PlotBBAvg = true then
begin
Plot5(value5,"avg");
end;
Plot6(value1,"MDShadow");
If PlotCross then
begin
If Value1[1]<0 and Value1>0 then
Plot7(Value1,"CrossUp")
else
NoPlot(7);
If Value1[1]>0 and Value1<0 then
Plot8(Value1,"CrossDn")
else
NoPlot(8);
end; here's an illustration of how the formatting helps you see the grouping of logics. ![]() Different people may format their codes different ways; there is no right or wrong way to format the code, the only purpose is to help you visualize the logic. .
__________________ Only an idiot would reply to a stupid post Last edited by Tams; 12-12-2009 at 12:20 AM. | ||
| |
|
| | #8 | ||
![]() | Re: MACDBB Enhancements I have changed the terminology of the code to something more meaningful and re-examined what the two different pieces of code were ttrying to achieve, married the two together and achieved a result that looks acceptable to me. Yes there are lots of colours but I find these meaningful. To explain the attached screenshot a rising MACD between the BB's is cyan, above the BB's is blue. A falling MACD between the bands is Magenta and below is red. The BB's change from red to green only when the BB's are expanding. The zero line crossing can be toggled on or off. The zero line changes colour depending on the MACD being above or below. | ||
| |
|
![]() |
| Thread Tools | |
| Display Modes | Help Others By Rating This Thread |
| |