FT just halts when attaching my own-written indicator.

Examples step by step how to make your own automated strategy or user indicator
Message
Author
Masher
Posts: 3
Joined: Thu Dec 30, 2010 7:36 pm

FT just halts when attaching my own-written indicator.

#1 Postby Masher » Thu Oct 18, 2012 9:07 pm

Hello everyone, I wrote an indicator to add a time filter to display indicator values between the specified time period.
But when I attached it to FT, it just halts.
Could you pls help me check where is the problem?

Here is the code.

Code: Select all

//---------------------------------------------------------------------------
// Crossover_Signal
//---------------------------------------------------------------------------
library Crossover_Signal;

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

var
  MA1Mode: integer;
  MA2Mode: integer;
  MA1: integer;
  MA2: integer;
  Range: integer;
  Start_Hours: integer;
  Final_Hours: integer;
 
 
  ApplyToPrice: integer;

  // Buffers
  UpBuffer: TIndexBuffer;
  DwBuffer: TIndexBuffer;
  ma_1: TIndexBuffer;
  ma_2: TIndexBuffer;



//---------------------------------------------------------------------------
// Initialize
//---------------------------------------------------------------------------
procedure Init; stdcall;
begin
  // define properties
  IndicatorShortName('Crossover Signals');
  SetOutputWindow(ow_ChartWindow);

  // register options
  AddSeparator('Common');

  RegMATypeOption(MA1Mode, 'Mode for MA1');
  RegOption('The shortest Period', ot_Integer, MA1);
  SetOptionRange('The shortest  Period', 1, MaxInt);
  MA1:=5;

  RegMATypeOption(MA2Mode, 'Mode for MA2');
  RegOption('The shorter Period', ot_Integer, MA2);
  SetOptionRange('The shorter Period', 1, MaxInt);
  MA2:=10;

  RegOption('Range var', ot_Integer, Start_Hours);
  SetOptionRange('Range var', 1, MaxInt);
  Start_Hours:= 15;

  RegOption('Range var', ot_Integer, Final_Hours);
  SetOptionRange('Range var', 1, MaxInt);
  Final_Hours:= 21;

  RegApplyToPriceOption(ApplyToPrice);

  // create buffers
  IndicatorBuffers(2);
  UpBuffer:= CreateIndexBuffer;
  DwBuffer:= CreateIndexBuffer;
  ma_1:= CreateIndexBuffer;
  ma_2:= CreateIndexBuffer;


  SetIndexBuffer(0, UpBuffer);
  SetIndexStyle(0, ds_Symbol, psSolid, 1, clGreen);
  SetIndexSymbol(0, 233, 0, 0);

  SetIndexBuffer(1, DwBuffer);
  SetIndexStyle(1, ds_Symbol, psSolid, 1, clRed);
  SetIndexSymbol(1, 234, 0, 0);
end;

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

end;

//---------------------------------------------------------------------------
// Calculate requested bar
//---------------------------------------------------------------------------
procedure Calculate(index: integer); stdcall;
var
   Range, AvgRange: double;
   SSP, i1: integer;
   hh,nn,ss,ms:Word;


begin

  SSP:= 9;
  AvgRange:= 0;
  DecodeTime(TimeCUrrent, hh, nn, ss, ms);


 while ((hh>=Start_Hours) and (hh<=Final_Hours))  do   
 begin

   for i1:= index to (index + SSP) do AvgRange:= AvgRange + Abs(High(i1) - Low(i1));
   Range:= AvgRange / (SSP + 1);

  ma_1[index]:= GetMA(index, 0, MA1, TMAType(MA1Mode), pt_Close, ma_1[index+1]);
  ma_2[index]:= GetMA(index, 0, MA2, TMAType(MA2Mode), pt_Close, ma_2[index+1]);


  if  (ma_1[index]>ma_2[index])   then
    UpBuffer[index]:= Low(index) - Range * 1
  else
    UpBuffer[index]:= 0;


  if  (ma_1[index]<ma_2[index])   then   
    DwBuffer[index]:= High(index) + Range * 1
  else
    DwBuffer[index]:= 0;

 end;

end;

exports

Init, Done, Calculate;

end.

FT Support
Posts: 905
Joined: Sat Jul 11, 2009 10:54 am

#2 Postby FT Support » Mon Oct 22, 2012 10:21 am

do you change "hh" variable inside while loop? it seems that it is not changed there so there is no exit from the loop
Check our other product here:
http://www.forexcopier.com

KelvinHand
Posts: 103
Joined: Sun Jan 02, 2011 6:05 pm

Re: FT just halts when attaching my own-written indicator.

#3 Postby KelvinHand » Tue Oct 23, 2012 1:21 am

The following statement wrong, how can be 'range var' for both

Code: Select all

RegOption('Range var', ot_Integer, Start_Hours);
  SetOptionRange('Range var', 1, MaxInt);
  Start_Hours:= 15;

  RegOption('Range var', ot_Integer, Final_Hours);
  SetOptionRange('Range var', 1, MaxInt);
  Final_Hours:= 21;



Your logic and program are wrong in the Calculate():

Here the Working Code

Code: Select all

//---------------------------------------------------------------------------
// Crossover_Signal
//---------------------------------------------------------------------------
library Crossover_Signal;

uses
  graphics,
  IndicatorInterfaceUnit,
  TechnicalFunctions,
  classes,
  windows,
  Dateutils,
  Math;

var
  MA1Mode: integer;
  MA2Mode: integer;
  MA1: integer;
  MA2: integer;
  Start_Hours,
  Final_Hours: Word;

 
  ApplyToPrice: integer;

  // Buffers
  UpBuffer: TIndexBuffer;
  DwBuffer: TIndexBuffer;
  ma_1: TIndexBuffer;
  ma_2: TIndexBuffer;



//---------------------------------------------------------------------------
// Initialize
//---------------------------------------------------------------------------
procedure Init; stdcall;
begin
  // define properties
  IndicatorShortName('Crossover Signals');
  SetOutputWindow(ow_ChartWindow);

  // register options
  AddSeparator('Common');

  RegMATypeOption(MA1Mode, 'Mode for MA1');
  RegOption('The shortest Period', ot_Integer, MA1);
  SetOptionRange('The shortest  Period', 1, MaxInt);
  MA1:=5;

  RegMATypeOption(MA2Mode, 'Mode for MA2');
  RegOption('The shorter Period', ot_Integer, MA2);
  SetOptionRange('The shorter Period', 1, MaxInt);
  MA2:=10;

  RegOption('Start_Hours', ot_Integer, Start_Hours);
  SetOptionRange('Start_Hours', 1, MaxInt);
  Start_Hours:= 15;

  RegOption('Final_Hours', ot_Integer, Final_Hours);
  SetOptionRange('Final_Hours', 1, MaxInt);
  Final_Hours:= 21;

  RegApplyToPriceOption(ApplyToPrice);

  // create buffers
  IndicatorBuffers(2);
  UpBuffer:= CreateIndexBuffer;
  DwBuffer:= CreateIndexBuffer;
  ma_1:= CreateIndexBuffer;
  ma_2:= CreateIndexBuffer;


  SetIndexBuffer(0, UpBuffer);
  SetIndexStyle(0, ds_Symbol, psSolid, 1, clGreen);
  SetIndexSymbol(0, 233, 0, 0);

  SetIndexBuffer(1, DwBuffer);
  SetIndexStyle(1, ds_Symbol, psSolid, 1, clRed);
  SetIndexSymbol(1, 234, 0, 0);
end;

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

end;

//---------------------------------------------------------------------------
// Calculate requested bar
//---------------------------------------------------------------------------
procedure Calculate(index: integer); stdcall;
var
   Range, AvgRange: double;
   SSP, i : integer;
   hh     :Word;


begin


  if (index + MA2 >= Bars) or (index + MA1 >= Bars)
   then  exit;

 //---- you must let the MA to fill data not during start and end hours

  ma_1[index]:= GetMA(index, 0, MA1, TMAType(MA1Mode), pt_Close, ma_1[index+1]);
  ma_2[index]:= GetMA(index, 0, MA2, TMAType(MA2Mode), pt_Close, ma_2[index+1]);


  hh:=HourOf(Time(index));


  //---- no while loop is required
  if (hh>=Start_Hours) and (hh<Final_Hours)then
  begin
    SSP:= 9;
    AvgRange:= 0;

    for i:= index to (index + SSP) do
      AvgRange:= AvgRange + Abs(High(i) - Low(i));
    Range:= AvgRange / (SSP + 1);

   //-- Cross (MA_1, MA_2)
    if  (ma_1[index]>ma_2[index]) and (ma_1[index+1]<ma_2[index+1]) then
      UpBuffer[index]:= Low(index) - Range * 1
    else
      UpBuffer[index]:= 0;



    //-- Cross (MA_2, MA_1)

    if  (ma_1[index]<ma_2[index]) and (ma_1[index+1]>ma_2[index+1]) then
      DwBuffer[index]:= High(index) + Range * 1
    else
      DwBuffer[index]:= 0;
  end;

end;

exports

Init, Done, Calculate;

end.


Return to “Programming lessons”

Who is online

Users browsing this forum: No registered users and 24 guests