| View previous topic :: View next topic |
| Author |
Message |
swannyfx
Joined: 18 Jun 2010 Posts: 35
|
Posted: Fri May 20, 2011 1:38 pm Post subject: Change indicator color when rising or falling |
|
|
Hi,
I'd like to modify the color of the HMA indicator so that its blue when the HMA is increasing and red when it's decreasing. I know how to change the color of the indicator using SetIndexStyle but this changes the entire indicator line, how would I get it to switch between two colors based on direction?
HMA code is below. Any input is greatly appreciated.
Thanks,
Swanny.
//---------------------------------------------------------------------------
// Hull Moving average indicator
//---------------------------------------------------------------------------
library HMA;
uses
graphics,
IndicatorInterfaceUnit,
TechnicalFunctions;
var
// External variables
period: integer = 8;
// Buffers
WMA1, WMA2, WMADiff, iHMA: TIndexBuffer;
//---------------------------------------------------------------------------
// Initialize indicator
//---------------------------------------------------------------------------
procedure Init; stdcall;
begin
// define properties
IndicatorShortName('Hull Moving average');
SetOutputWindow(ow_ChartWindow);
SetEmptyValue(0);
// register options
AddSeparator('Common');
RegOption('Period', ot_Integer, period);
SetOptionRange('Period', 1, MaxInt);
// create buffers
WMA1 := CreateIndexBuffer;
WMA2 := CreateIndexBuffer;
WMADiff := CreateIndexBuffer;
iHMA := CreateIndexBuffer;
IndicatorBuffers(1);
SetIndexBuffer(0, iHMA);
SetIndexStyle(0, ds_Line, psSolid, 1, clYellow);
SetIndexLabel(0, 'HMA');
end;
//---------------------------------------------------------------------------
// Calculate WMA
//---------------------------------------------------------------------------
function WMA(per: integer; index: integer): double;
var
sum: double;
weight: integer;
i: integer;
begin
sum := 0;
weight := 0;
for i:=0 to per - 1 do
begin
sum := sum + Close(index + i) * (per - i);
weight := weight + (per - i);
end;
result := sum / weight;
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;
iHMA[index] := sum / weight;
end;
exports
Init, Calculate;
end.
|
|
| Back to top |
|
 |
FT Support
Joined: 11 Jul 2009 Posts: 901
|
Posted: Mon May 23, 2011 9:07 am Post subject: |
|
|
Hello Swanny,
you need to have 2 index buffers, one of them should be red and one of them should be blue.
Then when you populate these buffers with values you need to choose the proper buffer depending on the color which you want to see on the chart.
_________________ Check our other products here:
www.fx-metropolis.com
www.forexcopier.com |
|
| Back to top |
|
 |
swannyfx
Joined: 18 Jun 2010 Posts: 35
|
Posted: Mon May 23, 2011 10:51 am Post subject: |
|
|
Hi FT,
Thanks for the info. I've modified the code, as shown below, and it compiles successfully but when I try to import it into ForexTester, I get the error message 'Cannot install indicator, it will be deleted. Have error: Can not get 'ReplaceStr' proc address'.
I assume there is an error with my code but am not sure what it is. Have I done something wrong in my code (I've marked my modifications with //****** to make the lines easier to identify)?
Thanks,
Swanny.
//---------------------------------------------------------------------------
// Hull Moving average indicator
//---------------------------------------------------------------------------
library HMAColor;
uses
graphics,
IndicatorInterfaceUnit,
TechnicalFunctions;
var
// External variables
period: integer = 21;
// Buffers
WMA1, WMA2, WMADiff, iHMA, HMA1, HMA2: TIndexBuffer; //******
//---------------------------------------------------------------------------
// Initialize indicator
//---------------------------------------------------------------------------
procedure Init; stdcall;
begin
// define properties
IndicatorShortName('Hull Moving average');
SetOutputWindow(ow_ChartWindow);
SetEmptyValue(0);
// register options
AddSeparator('Common');
RegOption('Period', ot_Integer, period);
SetOptionRange('Period', 1, MaxInt);
// create buffers
WMA1 := CreateIndexBuffer;
WMA2 := CreateIndexBuffer;
WMADiff := CreateIndexBuffer;
iHMA := CreateIndexBuffer;
HMA1 := CreateIndexBuffer; //******
HMA2 := CreateIndexBuffer; //******
IndicatorBuffers(2); //******
SetIndexBuffer(0, HMA1); //******
SetIndexStyle(0, ds_Line, psSolid, 1, clBlue);
//******
SetIndexBuffer(1, HMA2); //******
SetIndexStyle(1, ds_Line, psSolid, 1, clRed); //******
end;
//---------------------------------------------------------------------------
// Calculate WMA
//---------------------------------------------------------------------------
function WMA(per: integer; index: integer): double;
var
sum: double;
weight: integer;
i: integer;
begin
sum := 0;
weight := 0;
for i:=0 to per - 1 do
begin
sum := sum + Close(index + i) * (per - i);
weight := weight + (per - i);
end;
result := sum / weight;
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;
HMA1[index] := sum / weight; //******
HMA2[index] := sum / weight; //******
if HMA1[index] >= HMA1[index-1] then iHMA[index] := HMA1[index]; //******
if HMA1[index] < HMA1[index-1] then iHMA[index] := HMA2[index]; //******
end;
exports
Init, Calculate;
end.
|
|
| Back to top |
|
 |
FT Support
Joined: 11 Jul 2009 Posts: 901
|
Posted: Mon May 23, 2011 11:37 am Post subject: |
|
|
Hello,
Please try to replace your API with attached libs
| Description: |
|
 Download |
| Filename: |
DelphiAPI.zip |
| Filesize: |
10.05 KB |
| Downloaded: |
500 Time(s) |
_________________ Check our other products here:
www.fx-metropolis.com
www.forexcopier.com |
|
| Back to top |
|
 |
swannyfx
Joined: 18 Jun 2010 Posts: 35
|
Posted: Mon May 23, 2011 12:40 pm Post subject: |
|
|
Thanks, compiling with those libs fixed the indicator install issue.
I'm still having a problem switching between the buffers though. I've configured two buffers with different colors and populated them with the same values, but the indicator only displays one color. I think the issue is with the code I'm using to switch between buffers (excerpt from previous post):
HMA1[index] := sum / weight; //blue HMA
HMA2[index] := sum / weight; //red HMA
if HMA1[index] >= HMA1[index-1] then
iHMA[index] := HMA1[index]
else
iHMA[index] := HMA2[index];
Is there an issue with this code or could the problem be elsewhere? (The rest of the code is in my previous post if you need to reference).
Any help is much appreciated.
Thanks,
Swanny.
|
|
| Back to top |
|
 |
swannyfx
Joined: 18 Jun 2010 Posts: 35
|
Posted: Wed May 25, 2011 6:44 am Post subject: |
|
|
Hi, I'm still having issues switching between the two IndexBuffers, does anyone know how to do this?
Thanks,
Swanny.
|
|
| Back to top |
|
 |
FT Support
Joined: 11 Jul 2009 Posts: 901
|
Posted: Thu May 26, 2011 9:36 am Post subject: |
|
|
Hello Swanny,
Please try something like this:
| Code: |
var newValue, previousValue: double;
....
newValue := sum / weight;
if (HMA1[index-1] <> 0) then
previousValue := HMA1[index-1]
else
previousValue := HMA2[index-1];
if newValue >= previousValue then
HMA1[index] := newValue //blue HMA
else
HMA2[index] := newValue; //red HMA
|
_________________ Check our other products here:
www.fx-metropolis.com
www.forexcopier.com |
|
| Back to top |
|
 |
swannyfx
Joined: 18 Jun 2010 Posts: 35
|
Posted: Sat May 28, 2011 2:07 am Post subject: |
|
|
Thanks, I never thought of doing it that way. Unfortunately though, it still displays only one IndexBuffer.
I ran through the code logic on a piece of paper and have a question about the line:
if (HMA1[index-1] <> 0) then
If the HMA is lower than the previous bar, the HMA2 buffer (red/down) would be written with the new value, but HMA1 (blue/up) would be blank. In this case, wouldn't HMA1 be Null rather than 0? How would the code react to the if statement when the value is blank? I've tried adding Null to my code instead of 0 but it crashes FT when I try to apply the indicator to a chart.
Here is the code so far:
//---------------------------------------------------------------------------
// Hull Moving average indicator
//---------------------------------------------------------------------------
library HMAColor;
uses
graphics,
IndicatorInterfaceUnit,
TechnicalFunctions;
var
// External variables
period: integer = 21;
// Buffers
WMA1, WMA2, WMADiff, HMA1, HMA2: TIndexBuffer;
newValue, previousValue: double;
//---------------------------------------------------------------------------
// Initialize indicator
//---------------------------------------------------------------------------
procedure Init; stdcall;
begin
// define properties
IndicatorShortName('HMA Colored');
SetOutputWindow(ow_ChartWindow);
SetEmptyValue(0);
// register options
AddSeparator('Common');
RegOption('Period', ot_Integer, period);
SetOptionRange('Period', 1, MaxInt);
// create buffers
WMA1 := CreateIndexBuffer;
WMA2 := CreateIndexBuffer;
WMADiff := CreateIndexBuffer;
HMA1 := CreateIndexBuffer;
HMA2 := CreateIndexBuffer;
IndicatorBuffers(2);
SetIndexBuffer(0, HMA1);
SetIndexStyle(0, ds_Line, psSolid, 1, clBlue);
SetIndexLabel(0, 'HMA up');
SetIndexBuffer(1, HMA2);
SetIndexStyle(1, ds_Line, psSolid, 1, clRed);
SetIndexLabel(1, 'HMA down');
end;
//---------------------------------------------------------------------------
// Calculate WMA
//---------------------------------------------------------------------------
function WMA(per: integer; index: integer): double;
var
sum: double;
weight: integer;
i: integer;
begin
sum := 0;
weight := 0;
for i:=0 to per - 1 do
begin
sum := sum + Close(index + i) * (per - i);
weight := weight + (per - i);
end;
result := sum / weight;
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;
newValue := sum / weight;
if (HMA1[index-1] <> 0) then
previousValue := HMA1[index-1]
else
previousValue := HMA2[index-1];
if newValue >= previousValue then
HMA1[index] := newValue //blue HMA
else
HMA2[index] := newValue; //red HMA
end;
exports
Init, Calculate;
end.
Thanks,
Swanny.
|
|
| Back to top |
|
 |
swannyfx
Joined: 18 Jun 2010 Posts: 35
|
Posted: Sat May 28, 2011 5:12 am Post subject: |
|
|
I figured it out, had the bar numbering the wrong way. Here's the final code:
//---------------------------------------------------------------------------
// Hull Moving average indicator
//---------------------------------------------------------------------------
library HMAColor;
uses
graphics,
IndicatorInterfaceUnit,
TechnicalFunctions;
var
// External variables
period: integer = 21;
// Buffers
WMA1, WMA2, WMADiff, HMA1, HMA2: TIndexBuffer;
newValue, previousValue: double;
//---------------------------------------------------------------------------
// Initialize indicator
//---------------------------------------------------------------------------
procedure Init; stdcall;
begin
// define properties
IndicatorShortName('HMA Colored');
SetOutputWindow(ow_ChartWindow);
SetEmptyValue(0);
// register options
AddSeparator('Common');
RegOption('Period', ot_Integer, period);
SetOptionRange('Period', 1, MaxInt);
// create buffers
WMA1 := CreateIndexBuffer;
WMA2 := CreateIndexBuffer;
WMADiff := CreateIndexBuffer;
HMA1 := CreateIndexBuffer;
HMA2 := CreateIndexBuffer;
IndicatorBuffers(2);
SetIndexBuffer(0, HMA1);
SetIndexStyle(0, ds_Line, psSolid, 1, clBlue);
SetIndexLabel(0, 'HMA up');
SetIndexBuffer(1, HMA2);
SetIndexStyle(1, ds_Line, psSolid, 1, clRed);
SetIndexLabel(1, 'HMA down');
end;
//---------------------------------------------------------------------------
// Calculate WMA
//---------------------------------------------------------------------------
function WMA(per: integer; index: integer): double;
var
sum: double;
weight: integer;
i: integer;
begin
sum := 0;
weight := 0;
for i:=0 to per - 1 do
begin
sum := sum + Close(index + i) * (per - i);
weight := weight + (per - i);
end;
result := sum / weight;
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;
newValue := sum / weight;
if (HMA1[index+1] <> 0) then
previousValue := HMA1[index+1]
else
previousValue := HMA2[index+1];
if newValue >= previousValue then
HMA1[index] := newValue //blue HMA
else
HMA2[index] := newValue; //red HMA
end;
exports
Init, Calculate;
end.
Thanks for your help,
Swanny.
|
|
| Back to top |
|
 |
christoff
Joined: 09 Nov 2006 Posts: 34
|
Posted: Fri Aug 12, 2011 6:54 am Post subject: Have error: Can not get 'ReplaceStr' proc address' |
|
|
| FT Support wrote: | Hello,
Please try to replace your API with attached libs |
Hi
I also get this error when I try to import my custom indicator into ForexTester.
I get the error message 'Cannot install indicator, it will be deleted. Have error: Can not get 'ReplaceStr' proc address'.
I did replace my API with attached libs by copying the 2 files into c:\lazarus\projects. But I still gets the same error.
I don't know if I have to copy the files into FT2 as well and if so where.
Please help
Christoff
Thank you but I solved the problem by using the suggestions in this post:
http://forextester.com/forum/viewtopic.php?t=2189&start=0&postdays=0&postorder=asc&highlight=
|
|
| Back to top |
|
 |
christoff
Joined: 09 Nov 2006 Posts: 34
|
Posted: Tue Aug 16, 2011 5:54 am Post subject: |
|
|
Hi Swanny,
I took you code for the HMA that you made available in this post and change it to cater for all the different MA type because I was looking for EMA that will change color on slope change.
there is just one problem and it is that the indi leaves blank spaces in between the color changes.
I currently bypass this problem by just 1st loading a normal EMA and then on top of that I load the color change one.
Maybe someone out there know how to fix it inside the MA_Color indi so that we don't need to overlay 2 MA's.
Regards,
Christoff
| Description: |
|
 Download |
| Filename: |
MovingAverageColor.zip |
| Filesize: |
1.78 MB |
| Downloaded: |
444 Time(s) |
|
|
| Back to top |
|
 |
FT Support
Joined: 11 Jul 2009 Posts: 901
|
Posted: Tue Aug 16, 2011 12:58 pm Post subject: |
|
|
Hello,
Pelase try to add one more point to one of the buffers that you use for changing MA colors.
blank spaces appear when you stop showing one buffer at some point let's say index=n and then show next buffer from index=n+1 point. so to fix the problem you need to stop showing first buffer at index=n+1 or start showing next buffer at index=n
_________________ Check our other products here:
www.fx-metropolis.com
www.forexcopier.com |
|
| Back to top |
|
 |
christoff
Joined: 09 Nov 2006 Posts: 34
|
|
| Back to top |
|
 |
sneakers
Joined: 15 Mar 2012 Posts: 1
|
Posted: Thu Mar 15, 2012 10:57 pm Post subject: |
|
|
| I will be interested to network via skype with anyone using Forex Tester to test the Hull Moving Average. Contact me by private message for skype user name.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|