Mike, what's wrong with this?

How to create strategies and indicators
Message
Author
User avatar
Tantalus
Posts: 302
Joined: Fri Mar 23, 2007 3:51 pm
Contact:

Mike, what's wrong with this?

#1 Postby Tantalus » Fri Jan 11, 2008 7:26 pm

Why does this indicator get bunched up at the top of the window? It should vary from -1 to 0 to 1 in discrete steps - sort of like a square wave... it looks like to output is OK but the scaling of the window is weird, but I can't see it well enough to be sure...

Is there a way to set the scale in code??

Thanks.

Code: Select all

library HMADSD;

uses
  graphics,
  IndicatorInterfaceUnit,
  TechnicalFunctions;

var
  // External variables
  period: integer = 32;

  // Buffers
  WMA1, WMA2, WMADiff, HMA, DSD: TIndexBuffer;


//---------------------------------------------------------------------------
// Initialize indicator
//---------------------------------------------------------------------------
procedure Init; stdcall;
begin
  // define properties
  IndicatorShortName('HMADSD');
  SetOutputWindow(ow_SeparateWindow);
  {AddLevel(0, psSolid, 1, cl_GridColor);
  SetEmptyValue(0);}

  // register options
  AddSeparator('Common');
  RegOption('Period', ot_Integer, period);
  SetOptionRange('Period', 1, MaxInt);

  // create buffers
  DSD := CreateIndexBuffer;
  WMA1 := CreateIndexBuffer;
  WMA2 := CreateIndexBuffer;
  WMADiff := CreateIndexBuffer;
  HMA := CreateIndexBuffer;

  IndicatorBuffers(1);
  SetIndexBuffer(0, DSD);
  SetIndexStyle(0, ds_Line, psSolid, 1, clYellow);
  SetIndexLabel(0, 'DSD');
end;

//---------------------------------------------------------------------------
// Calculate requested bar
//---------------------------------------------------------------------------
procedure Calculate(index: integer); stdcall;
var
  halfperiod: integer;
  sqrtperiod: integer;
  sum: double;
  weight: integer;
  i: integer;

begin
  if index + period >= Bars then
    exit;

  halfperiod := Trunc(period / 2);
  sqrtperiod := Trunc(Sqrt(period));

  WMA1[index] := WMA(halfperiod, index) * 2;
  WMA2[index] := WMA(period, index);
  WMADiff[index] := WMA1[index] - WMA2[index];

  sum := 0;
  weight := 0;
  for i:=0 to sqrtperiod - 1 do
    begin
      sum := sum + WMADiff[index + i] * (sqrtperiod - i);
      weight := weight + (sqrtperiod - i);
    end;
  HMA[index] := sum / weight;

  DSD[index] := 0.0;

  if (HMA[index] - HMA[index + 2] > 0)
    and (HMA[index + 1] - HMA[index + 3] > 0)
    then DSD[index] := 1.0;

  if (HMA[index] - HMA[index + 2] < 0)
    and (HMA[index + 1] - HMA[index + 3] < 0)
    then DSD[index] := -1.0;

end;

exports

Init, Calculate;

end.
Last edited by Tantalus on Mon May 25, 2009 7:43 pm, edited 1 time in total.

User avatar
Terranin
Site Admin
Posts: 833
Joined: Sat Oct 21, 2006 4:39 pm

#2 Postby Terranin » Fri Jan 11, 2008 9:45 pm

Screenshot would help a lot. To set scale use SetFixedMaxMinValues in init procedure.
Hasta la vista
Mike

User avatar
Tantalus
Posts: 302
Joined: Fri Mar 23, 2007 3:51 pm
Contact:

#3 Postby Tantalus » Fri Jan 11, 2008 10:10 pm

OK, here's a screenshot. Maybe you could try running it on your machine to see what it does.

I'll try the function you suggested and some more debugging...

thx.
Attachments
ind_up_top.jpg
ind_up_top.jpg (52.71 KiB) Viewed 9027 times

User avatar
Terranin
Site Admin
Posts: 833
Joined: Sat Oct 21, 2006 4:39 pm

#4 Postby Terranin » Fri Jan 11, 2008 11:08 pm

Also, empty value is 0, if you use this value in your indicator then set default to 0.000000123 for example with SetEmptyValue. Otherwise points with 0 value will not be painted at all.
Hasta la vista

Mike


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 36 guests