Question about Code.

How to create strategies and indicators
Message
Author
david070o
Posts: 28
Joined: Mon Mar 19, 2007 8:13 pm

Question about Code.

#1 Postby david070o » Sat Mar 24, 2007 11:17 am

Alright Mike,

Sorry to bother you again. But would you please take a look an tell me what's wrong. My strategy code only execute one order and does not close it at all.

The strategy is design to sell when cci is between -10 and -20, and close other when cci pass -150. Just trying to test my code before i go into more complex design. Any help from anyone will be appreciated. :roll: :roll:


Code: Select all

library ccicallStrategy;

uses
  SysUtils,  Classes, StrategyInterfaceUnit;

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

  //Custome Global Variable
  IndHandler: integer;
  OrderHandle: integer;
  OrderStyle: TTradePositionType;
  OpenTime: TDateTime;


{-----Init strategy----------------------------------------------------------}
procedure InitStrategy; stdcall;
begin
  StrategyShortName('Simple CCI CALL');
  StrategyDescription('Test');

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

end;

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

{-----Reset strategy---------------------------------------------------------}
procedure ResetStrategy; stdcall;
begin
  //Create Indicater
  IndHandler := CreateIndicator('EURUSD', PERIOD_H1, 'CCI', '16');
  OrderHandle := -1;


end;   

{-----Process single tick----------------------------------------------------}
procedure GetSingleTick; stdcall;
Var
  CCI: 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) then exit;

//Get CCI value
  CCI := GetIndicatorValue(IndHandler, 0, 2);

// if BUY order exists and cci is >= 200
// then close order
  if (OrderHandle <> -1) and (OrderStyle = tp_Buy) and
     (OpenTime <> Time(0)) and (CCI > 200 ) then
    begin
      CloseOrder(OrderHandle);
      OrderHandle := -1;
    end;

// if SELL order exists and cci <= -200
// then close order
  if (OrderHandle <> -1) and (OrderStyle = tp_Sell) and
     (OpenTime <> Time(0)) and (CCI < -150) then
    begin
      CloseOrder(OrderHandle);
      OrderHandle := -1;
    end;

// if there is no order and cc = -10
// then open SELL order
  if (OrderHandle = -1) and (CCI > -20) and (CCI < -9) then
    begin
      SendInstantOrder(Symbol, op_Sell, LotSize, 0, 0, '', 0, OrderHandle);
      OrderStyle := tp_Sell;
      OpenTime := Time(0);
    end;



end;

exports

InitStrategy,
DoneStrategy,
ResetStrategy,
GetSingleTick;

end.
Attachments
ccicallStrategy.zip
same code as posted.
(1.12 KiB) Downloaded 872 times

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

#2 Postby Terranin » Sat Mar 24, 2007 1:47 pm

This is wrong:

CCI := GetIndicatorValue(IndHandler, 0, 2);

CCI has only 2 buffers 0 and 1 (see help "List of indicators' parameters") and it should be:

CCI := GetIndicatorValue(IndHandler, 0, 1);

to get value from buffer 1 on the 0 bar.
Hasta la vista
Mike

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

#3 Postby david070o » Sun Mar 25, 2007 10:24 pm

Thank you very much, you are the best.


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 30 guests