Calling another indicator Val

How to create strategies and indicators
Message
Author
stockwet
Posts: 27
Joined: Wed Dec 06, 2006 10:50 am

Calling another indicator Val

#1 Postby stockwet » Thu Feb 01, 2007 1:20 pm

Mike,

I noticed that you have a GetMA function. Is it possible to call other indicators as well? For instance, I'd like to create an indicator that derives from an ATR value. Do I need to do the ATR calculations in my code, or is there something akin to the iATR() function in MetaTrader?

Thanks.

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

Re: Calling another indicator Val

#2 Postby Terranin » Fri Feb 02, 2007 4:00 pm

stockwet wrote:Mike,

I noticed that you have a GetMA function. Is it possible to call other indicators as well? For instance, I'd like to create an indicator that derives from an ATR value. Do I need to do the ATR calculations in my code, or is there something akin to the iATR() function in MetaTrader?

Thanks.


From indicator you can not call other indicators, from strategy you can. So yes, in indicator you should include the code inside.
Hasta la vista
Mike

david070o
Posts: 28
Joined: Mon Mar 19, 2007 8:13 pm

#3 Postby david070o » Wed Mar 21, 2007 8:42 am

Can you give an example of calling a CCI indicator from a strategy.
The following is my modification of your SMA strategy & CCI indicator. I can't compile it, error on TIndexBuffer. I am just started to learn programming... so any help will be appreciated. I want to finally implement Woodies' Zero Line Reject, but i am just starting out with cci crosssing.


library CCIStrategy;

uses
SysUtils, windows, Classes, IndicatorInterfaceUnit, StrategyInterfaceUnit;

var
// External parameters
Currency: PChar = nil;
TimeFrame: integer;
LotSize: double;
period1: integer;
period2: integer;
CCIPeriod: integer;

// custom variables
OrderHandle: integer;
OrderStyle: TTradePositionType;
OpenTime: TDateTime;

// Index buffers
CCIBuffer, MAbuff: TIndexBuffer;

{-----Init strategy---------------------------------------------------------}
procedure InitStrategy; stdcall;
begin
StrategyShortName('CCI ZLR');
StrategyDescription('Zero Line Reject CCI20');

SetEmptyValue(MaxInt);

// Register external parameters
RegOption('Currency', ot_Currency, Currency);
ReplaceStr(Currency, 'EURUSD');

RegOption('Timeframe', ot_Timeframe, TimeFrame);
TimeFrame := PERIOD_H1;

RegOption('LotSize', ot_Double, LotSize);
SetOptionDigits('LotSize', 1);
lotSize := 0.1;

RegOption('cci1 period', ot_Integer, period1);
SetOptionRange('cci1 period', 2, MaxInt);
period1 := 16;

RegOption('cci2 period', ot_Integer, period2);
SetOptionRange('cci2 period', 2, MaxInt);
period2 := 20;
end;

{-----Done strategy---------------------------------------------------------}
procedure DoneStrategy; stdcall;
begin
FreeMem(Currency);
end;

{-----Reset strategy--------------------------------------------------------}
procedure ResetStrategy; stdcall;
begin
OrderHandle := -1;
end;

{-----Calculate SMA---------------------------------------------------------}
{function GetSMA(period: integer): double;
var
i: integer;
sum: double;
begin
sum := 0;
for i:=0 to period - 1 do
sum := sum + Close(i);
result := sum/period;
end;}

//---------------------------------------------------------------------------
// Calculate single bar
//---------------------------------------------------------------------------
function Calculate(index: integer): double;
var
sum: double;
i: integer;

function tprice(i: integer): double;
begin
result := (High(i) + Low(i) + Close(i))/3;
end;

begin
if index + CCIPeriod >= Bars then
exit;

// count moving average
sum := 0;
for i := 0 to CCIPeriod - 1 do
sum := sum + tprice(index + i);

MAbuff[index] := sum/CCIPeriod;

sum := 0;
for i := 0 to CCIPeriod - 1 do
sum := sum + abs(tprice(index + i) - MAbuff[index + i]);

CCIBuffer[index] := (tprice(index) - MAbuff[index])/0.015/(sum/CCIPeriod);
result := CCIBuffer[index];

end;

{function Getcci(period: integer): double;
var
i: integer;
sum: double;
begin
result := Calculate(period);
end; }

{-----Process single tick---------------------------------------------------}
procedure GetSingleTick; stdcall;
var
cci1, cci2: double;
begin
// check our currency
if Symbol <> string(Currency) then exit;

// set currency and timeframe
SetCurrencyAndTimeframe(Symbol, TimeFrame);

// check number of bars and SMA period
if (Bars < period1) or (Bars < period2) then exit;

// calculate SMA
cci1 := Calculate(period1);
cci2 := Calculate(period2);

// if BUY order exists and fast SMA crosses slow SMA from top
// then close order
if (OrderHandle <> -1) and (OrderStyle = tp_Buy) and
(OpenTime <> Time(0)) and (cci1 < cci2) then
begin
CloseOrder(OrderHandle);
OrderHandle := -1;
end;

// if SELL order exists and fast SMA crosses slow SMA from bottom
// then close order
if (OrderHandle <> -1) and (OrderStyle = tp_Sell) and
(OpenTime <> Time(0)) and (cci1 > cci2) then
begin
CloseOrder(OrderHandle);
OrderHandle := -1;
end;

// if there is no order and fast SMA crosses slow SMA from top
// then open SELL order
if (OrderHandle = -1) and (cci1 < cci2) then
begin
SendInstantOrder(Symbol, op_Sell, LotSize, 0, 0, '', 0, OrderHandle);
OrderStyle := tp_Sell;
OpenTime := Time(0);
end;

// if there is no order and fast SMA crosses slow SMA from bottom
// then open BUY order
if (OrderHandle = -1) and (cci1 > cci2) then
begin
SendInstantOrder(Symbol, op_Buy, LotSize, 0, 0, '', 0, OrderHandle);
OrderStyle := tp_Buy;
OpenTime := Time(0);
end;
end;

exports

InitStrategy,
DoneStrategy,
ResetStrategy,
GetSingleTick;

end.
Last edited by david070o on Thu Mar 22, 2007 2:30 pm, edited 2 times in total.

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

#4 Postby Terranin » Thu Mar 22, 2007 11:18 am

You should not implement the code of indicator directly inside the strategy. Instead you should create indicator with CreateIndicator(...) function (see help) and request its values by GetIndicatorValue(...)
Hasta la vista

Mike

david070o
Posts: 28
Joined: Mon Mar 19, 2007 8:13 pm

#5 Postby david070o » Thu Mar 22, 2007 2:28 pm

Yeap.... I just figured it out myself. I have more questions though.... and thank your for replying.


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 15 guests