My ADX Strategy

How to create strategies and indicators
Message
Author
Gen
Posts: 14
Joined: Tue Sep 21, 2010 2:19 pm

My ADX Strategy

#1 Postby Gen » Tue Oct 26, 2010 4:28 am

Hi,
I am trying to get the ADX filter into my strategy.
When SMA lines are crossed and the value of ADX is more than 30, the order should be placed.
But, it does not work for some reasons.

I do not seen any error in journal.

Can you please tell me what is missing?


//-------------------------------------------------------------------------
// ADX Strategy
//-------------------------------------------------------------------------
library ADXStrategy;

uses
Graphics, Interfaces, TechnicalFunctions, SysUtils, Classes, StrategyInterfaceUnit;

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

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


{-----Init strategy---------------------------------------------------------}
procedure InitStrategy; stdcall;
begin
StrategyShortName('SimpleADX');
StrategyDescription('Strategy based on ADX');

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

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

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

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

RegOption('SMA2 period', ot_Integer, period2);
SetOptionRange('SMA2 period', 2, MaxInt);
period2 := 32;

RegOption('ADXPeriod', ot_Integer, ADXPeriod);
ADXPeriod := 14;
end;

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

{-----Reset strategy--------------------------------------------------------}
procedure ResetStrategy; stdcall;
begin
OrderHandle := -1;
handle11 := CreateIndicator(Currency, TimeFrame, 'ADX', format('%d;Close', [ADXPeriod]));
SetIndicatorBuffStyle(handle11, 0, psSolid, 1, clYellow);
SetIndicatorBuffStyle(handle11, 1, psSolid, 1, clRed);
SetIndicatorBuffStyle(handle11, 2, psSolid, 1, clBlue);

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;

{-----Process single tick---------------------------------------------------}
procedure GetSingleTick; stdcall;
var
sma1: double;
sma2: double;
adx: double;
bip: double;
bim: 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
sma1 := GetSMA(period1);
sma2 := GetSMA(period2);

// get TimeFrame1 ADX Value
adx := GetIndicatorValue(handle11, 1, 0);
bip := GetIndicatorValue(handle11, 1, 1);
bim := GetIndicatorValue(handle11, 1, 2);


// 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
(sma1 < sma2)
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
(sma1 > sma2)
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
(sma1 < sma2) and
(adx > 30)
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
(sma1 > sma2) and
(adx > 30)
then
begin
SendInstantOrder(Symbol, op_Buy, LotSize, 0, 0, '', 0, OrderHandle);
OrderStyle := tp_Buy;
OpenTime := Time(0);
end;
end;

exports

InitStrategy,
DoneStrategy,
ResetStrategy,
GetSingleTick,
ReplaceStr,
IntrfProcsRec;

end.

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

#2 Postby FT Support » Tue Oct 26, 2010 4:00 pm

Hello,

I tried your strategy and it seems that it works (i saw some trades placed and closed).

Please check the following things:

1) If you have "Strategy execution" enabled (Testing -> Enable/Disable strategy execution menu)

2) If you have your strategy checked in the list of strategies

3) If you have valid currency selected in strategy parameters (you should select currency which has ticks generated).
Check our other product here:
http://www.forexcopier.com


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 11 guests