Simple EMA strategy help + request.

How to create strategies and indicators
Message
Author
Tomp.G
Posts: 3
Joined: Mon Oct 17, 2011 7:51 pm

Simple EMA strategy help + request.

#1 Postby Tomp.G » Tue Oct 18, 2011 8:16 pm

I have created a simple EMA strategy based on your simple SMA strategy. I have plotted the EMA on the chart to confirm that the trades are taken according to the EMA crossovers unfortunately it seems that the trades are still taken according to SMA.

Please mind that I have almost no programming experience.

Also if anyone have basic strategies like RSI(buy if price>50...), and CCI (buy if price>0....) I would really appreciate if you could share them with me.

I’m writing my dissertation about technical analysis and i need perform these test to get a data for further calculation.

Thank you in advance,

Tomas

Code: Select all

//-------------------------------------------------------------------------
// Example of strategy based on 2 crossing EMA
//-------------------------------------------------------------------------
library EMAcorss;

uses
  SysUtils,  Classes,  StrategyInterfaceUnit;

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

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


{-----Init strategy---------------------------------------------------------}
procedure InitStrategy; stdcall;
begin
  StrategyShortName('SimpleEMA');
  StrategyDescription('Strategy based on 2 EMA');

  // 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('EMA1 period', ot_Integer, period1);
  SetOptionRange('EMA1 period', 2, MaxInt);
  period1 := 9;

  RegOption('EMA2 period', ot_Integer, period2);
  SetOptionRange('EMA2 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 EMA---------------------------------------------------------}
function GetEMA(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
  ema1, ema2: double;
begin
  // check our currency
  if Symbol <> string(Currency) then exit;

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

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

  // calculate EMA
  ema1 := GetEMA(period1);
  ema2 := GetEMA(period2);

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

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

  // if there is no order and fast EMA crosses slow EMA from top
  // then open SELL order
  if (OrderHandle = -1) and (ema1 < ema2) 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 EMA crosses slow EMA from bottom
  // then open BUY order
  if (OrderHandle = -1) and (ema1 > ema2) 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 » Wed Oct 19, 2011 6:12 am

Did you try to debug your strategy? please see instructions here:
http://forextester.com/forum/viewtopic.php?t=1760
Check our other product here:
http://www.forexcopier.com

dackjaniels
Posts: 151
Joined: Tue Feb 24, 2009 1:03 pm

#3 Postby dackjaniels » Fri Oct 21, 2011 5:16 pm

Hi,

In your code you call a function called GetEMA...

Code: Select all

{-----Calculate EMA---------------------------------------------------------}
function GetEMA(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;


You have clearly changed the name from GetSMA to GetEMA but you haven't changed the calculation that is performed within the function.

You must change the above function so it calculates EMA, instead of SMA.

Regards,
Steve

Tomp.G
Posts: 3
Joined: Mon Oct 17, 2011 7:51 pm

#4 Postby Tomp.G » Fri Nov 18, 2011 10:09 am

Hi,
Sorry for the late response.

Could you explain this slightly more? Would really appreciate if you could show me the code for that.

Unfortunately with my lack of programming knowledge I don’t even know where to really look.

Thank You.

Regards,
Tomas

Tomp.G
Posts: 3
Joined: Mon Oct 17, 2011 7:51 pm

#5 Postby Tomp.G » Tue Nov 22, 2011 11:50 am

Not even one person can answer my question? Could someone please have a look and help me with this rather basic problem. Thank you.

Regards,

Tom

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

#6 Postby FT Support » Wed Nov 23, 2011 3:31 pm

Hello Tom,

Your EMA calculations are incorrect, you actually calculate SMA but not EMA.
So please try to use MA indicator with EMA parameter instead of GetSMA function call.

See "using indicators" section in "Help -> strategies API"
Check our other product here:
http://www.forexcopier.com


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 17 guests