Page 1 of 1

Can someone please fix and compile this MACD6color indicator

Posted: Wed Mar 03, 2010 3:18 am
by pips4life
Hi,

I copied the MACDnew.dpr file and made edits to change the behavior but I have no compiler and not enough knowledge about the exact syntax.

The logic, however, should be easy for someone to read and change as necessary so it will compile.


The new behavior is simply this: Whereas the previous "MACDnew" version uses a single color histogram to show the difference value, this new version uses 4 colors for the histogram. It also keeps the 2 colored lines for the usual MACD values, hence, 6 colors total.

Upon a bullish crossing of the zero line, the histogram is the color for DiffBull. If the next bar is still above the zero line, it is compared to the previous DiffBull value, and if greater, it is also the DiffBull color. But if it is smaller (i.e. closer to the zero line), then it is the DiffWeakerBull color. There are 2 similar colors for the Bear side of the zero line.

Because there are 6 total colors and I figured to name it "MACD_6color" but I don't much care what name it is called.

I would like the usual MACD values and all the colors to be easily changeable by the user when adding it to a chart. I guess it will need a .opt file with the defaults listed.

Here's the file. A picture is attached for what it should look like.

Thanks very much in advance for your assistance to fix and compile this indicator!

pips4life



Code: Select all

//---------------------------------------------------------------------------
// MACD indicator new
//---------------------------------------------------------------------------
library MACDnew;

uses
  SysUtils, classes, graphics, windows, IndicatorInterfaceUnit,
  TechnicalFunctions;

var
  // External variables
  FastEMAPeriod: integer;
  SlowEMAPeriod: integer;
  SMAPeriod: integer;
  ApplyToPrice: integer;

  // Buffers
  FastEMA, SlowEMA, _MACD, SMA, DiffBull, DiffWeakerBull, DiffBear, DiffWeakerBear: TIndexBuffer;


//---------------------------------------------------------------------------
// Initialize indicator
//---------------------------------------------------------------------------
procedure Init; stdcall;
begin
  // define properties
  IndicatorShortName('MACD new');
  SetOutputWindow(ow_SeparateWindow);
  AddLevel(0, psDot, 1, cl_GridColor);

  // register options
  AddSeparator('Common');

  RegOption('Fast EMA period', ot_Integer, FastEMAPeriod);
  SetOptionRange('Fast EMA period', 1, MaxInt);
  FastEMAPeriod := 5;

  RegOption('Slow EMA period', ot_Integer, SlowEMAPeriod);
  SetOptionRange('Slow EMA period', 1, MaxInt);
  SlowEMAPeriod := 13;

  RegOption('SMA period', ot_Integer, SMAPeriod);
  SetOptionRange('SMA period', 1, MaxInt);
  SMAPeriod := 3;

  RegApplyToPriceOption(ApplyToPrice);

  // create buffers
  FastEMA := CreateIndexBuffer;
  SlowEMA := CreateIndexBuffer;
  _MACD := CreateIndexBuffer;
  SMA := CreateIndexBuffer;
  DiffBull := CreateIndexBuffer;
  DiffWeakerBull := CreateIndexBuffer;
  DiffBear := CreateIndexBuffer;
  DiffWeakerBear := CreateIndexBuffer;

  IndicatorBuffers(6);
  SetIndexBuffer(0, _MACD);
  SetIndexStyle(0, ds_Line, psSolid, 1, clBlue);
  SetIndexLabel(0, 'MACD');

  SetIndexBuffer(1, SMA);
  SetIndexStyle(1, ds_Line, psDot, 1, clRed);
  SetIndexLabel(1, 'Signal Line');

  SetIndexBuffer(2, DiffBull);
  SetIndexStyle(2, ds_Histogram, psSolid, 1, clLime);
  SetIndexLabel(2, 'MACD - SignalBull+');

  SetIndexBuffer(3, DiffWeakerBull);
  SetIndexStyle(3, ds_Histogram, psSolid, 1, clDarkGreen);
  SetIndexLabel(3, 'MACD - SignalBull-');

  SetIndexBuffer(4, DiffBear);
  SetIndexStyle(4, ds_Histogram, psSolid, 1, clRed);
  SetIndexLabel(4, 'MACD - SignalBear+');

  SetIndexBuffer(5, DiffWeakerBear);
  SetIndexStyle(5, ds_Histogram, psSolid, 1, clMaroon);
  SetIndexLabel(5, 'MACD - SignalBear-');

end;

//---------------------------------------------------------------------------
// Deinitialize indicator
//---------------------------------------------------------------------------
procedure Done; stdcall;
begin

end;

//---------------------------------------------------------------------------
// Calculate requested bar
//---------------------------------------------------------------------------
procedure Calculate(index: integer); stdcall;
var
  i: integer;
  sum: double;
  diff: double;
begin
  FastEMA[index] := GetMA(index, 0, FastEMAPeriod, ma_EMA, TPriceType(ApplyToPrice), FastEMA[index + 1]);
  SlowEMA[index] := GetMA(index, 0, SlowEMAPeriod, ma_EMA, TPriceType(ApplyToPrice), SlowEMA[index + 1]);
  _MACD[index] := FastEMA[index] - SlowEMA[index];

  sum := 0;
  for i:=index to index + SMAPeriod - 1 do
    sum := sum + _MACD[i];
  SMA[index] := sum/SMAPeriod;
 
  // If necessary, set all 4 Diff buffers to 0
  DiffBull[index] := 0;
  DiffWeakerBull[index] := 0;
  DiffBear[index] := 0;
  DiffWeakerBear[index] := 0;

  diff = _MACD[Index] - SMA[index];
 
  // The following logic should accomplish the desired 4 color histogram:
  //   The first histogram bar to cross the zero line is always either DiffBull or DiffBear
  //   While staying on one side of the zero line or the other, so long as the
  //   current bar is greater than the last bar, it is also DiffBull or DiffBear.
  //   If the trend is weakening and the current bar is smaller than the previous bar (i.e. it
  //   is closer to the zero line than the previous bar), then the DiffWeakerBull
  //   or DiffWeakerBear buffer is used.
  // Below, I guessed at the syntax to do a nested if (A-AND-B) OR (C-AND-D)/then BLOCK-of-commands.  Fix as needed.
  if diff >= 0.0  then
    begin
      // My assumption is that "index + 1" is the previous array element. Fix as needed, maybe "index - 1" ??
      if ( DiffBull[index + 1] > 0 AND diff < DiffBull[index + 1] )
    OR ( DiffWeakerBull[index + 1] > 0 AND diff < DiffWeakerBull[index + 1] )
      DiffWeakerBull[index] := diff;
      else
      DiffBull[index] := diff;
    end;
  else    //diff is < 0
    begin
      if ( DiffBear[index + 1] < 0 AND diff > DiffBear[index + 1] )
    OR ( DiffWeakerBear[index + 1] < 0 AND diff > DiffWeakerBear[index + 1] )
      DiffWeakerBear[index] := diff;
      else
      DiffBear[index] := diff;
    end;
end;

exports

Init, Done, Calculate;

end.