help Please

Examples step by step how to make your own automated strategy or user indicator
Message
Author
pthomas82
Posts: 60
Joined: Tue Aug 10, 2010 8:39 pm

help Please

#1 Postby pthomas82 » Tue Jun 07, 2011 3:55 am

Hi Guys,

Im looking for some help - currently im learning delphi and I throught it try code up something simple like when an indicator hits a certain level the script places a buy order, and if an indicator hits another level it places a sell order.

This code compiles ok - but it doesnt place buy and sell orders.

Can some kind member of this community point out where I have gone wrong =)

Thanks in advance,
Pete.


Code: Select all

 library CCITRY;

uses
  SysUtils, Classes,  StrategyInterfaceUnit, TechnicalFunctions;

var
  Currency: PChar = nil;
  TimeFrame: integer;
  LotSize: double;
  CCI1: integer;
  CCIUPPER: integer;
  CCILOWER: integer;

  OrderHandle: integer;
  OrderStyle: TTradePositionType;
  OpenTime: TDateTime;




procedure InitStrategy; stdcall;
begin
  StrategyShortName('CCITRY');
  StrategyDescription('CCI level strategy');

  RegOption('Currency', ot_Currency, Currency);
  ReplaceStr(Currency, 'EURUSD');

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

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

  RegOption('CCI1', ot_Integer, CCI1);
  CCI1 := 30;

  RegOption('CCIUPPER', ot_Integer, CCIUPPER);
  CCIUPPER := 250;

  RegOption('CCILOWER', ot_Integer, CCILOWER);
  CCIUPPER := -250;
end;

procedure DoneStrategy; stdcall;
begin
   FreeMem(Currency)
end;

procedure ResetStrategy; stdcall;
begin
   OrderHandle := -1
   
end;

procedure GetSingleTick; stdcall;
var
CCI: Double;
begin

if Symbol <> string(Currency) then exit;
SetCurrencyAndTimeframe(Symbol, TimeFrame);
CCI :=   GetIndicatorValue(CCI1, 1,0);

if (OrderHandle = 0) and (CCI > CCIUPPER) then
begin
SendInstantOrder(Symbol, op_Buy, LotSize, Ask - 20*Point, Ask + 50*Point, '',0, OrderHandle);
OrderStyle := tp_Buy;
OpenTime := Time(0)
end;

if (OrderHandle = 0) and (CCI < CCILOWER) then
begin
SendInstantOrder(Symbol, op_Sell, LotSize, Ask + 20*Point, Ask - 50*Point, '', 0, OrderHandle);
OrderStyle := tp_Sell;
OpenTime := Time(0)
end;


     
end;

exports
  InitStrategy,
  DoneStrategy,
  ResetStrategy,
  GetSingleTick,
  ReplaceStr;

end.

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

#2 Postby FT Support » Tue Jun 07, 2011 2:28 pm

Hello,

It seems that the problem is:

- you have "OrderHandle := -1" in "ResetStrategy" procedure

- you also have "if (OrderHandle = 0)" condition which will never be "true" because OrderHandle is always -1
Check our other product here:
http://www.forexcopier.com

pthomas82
Posts: 60
Joined: Tue Aug 10, 2010 8:39 pm

#3 Postby pthomas82 » Tue Jun 07, 2011 3:33 pm

Awesome, thanks support! I'll get trying that. Also any code I build i'll throw up here. I believe the community will find it easier to learn with more code to copy from!

Thanks for your help!

pthomas82
Posts: 60
Joined: Tue Aug 10, 2010 8:39 pm

#4 Postby pthomas82 » Wed Jun 08, 2011 4:55 am

Ok - so I got it working, a mixture of problems - the main one was not creating the indicator.

So below is the code for shareing! Basically it just goes long or short when the CCI hits a certain level.


Code: Select all

 library CCIOPTIONS;

uses
  SysUtils,
  Classes,
  StrategyInterfaceUnit,
  TechnicalFunctions;

var
  Currency: PChar = nil;
  TimeFrame: integer;
  LotSize: double;
  CCICURRENT: Double;
  CCI: integer;
  STOPLOSS: Integer;
  TAKEPROFIT: Integer;
  CCIUPPER: Integer;
  CCILOWER: Integer;

  OrderHandle: integer;
  OrderStyle: TTradePositionType;
  OpenTime: TDateTime;




procedure InitStrategy; stdcall;
begin
  StrategyShortName('CCI OPTIONS');
  StrategyDescription('CCI level strategy');

  RegOption('Currency', ot_Currency, Currency);
  ReplaceStr(Currency, 'EURUSD');

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

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

  RegOption('STOPLOSS (PIPS)', ot_Integer, STOPLOSS);
  STOPLOSS := 20;

  RegOption('TAKE PROFIT (PIPS)', ot_Integer, TAKEPROFIT);
  TAKEPROFIT := 20;

  RegOption('CCI UPPER BUY LEVEL', ot_Integer, CCIUPPER);
  CCIUPPER := 200;

  RegOption('CCI LOWER SELL LEVEL', ot_Integer, CCILOWER);
  CCILOWER := 200;

end;

procedure DoneStrategy; stdcall;
begin
   FreeMem(Currency)
end;

procedure ResetStrategy; stdcall;
begin
   OrderHandle := -1;
   CCI := CreateIndicator('EURUSD', Period_M1, 'CCI', '100');

end;

procedure GetSingleTick; stdcall;

begin

if Symbol <> string(Currency) then exit;
SetCurrencyAndTimeframe(Symbol, TimeFrame);
CCICURRENT := GetIndicatorValue(CCI, 0, 1);

     
if (OrderHandle = -1) and (CCICURRENT >CCIUPPER) then
begin
SendInstantOrder(Symbol, op_Buy, LotSize, 0, 0, '',0, OrderHandle);
OrderStyle := tp_Buy;
OpenTime := Time(0)
end;

if (OrderHandle = -1) and (CCICURRENT <-CCILOWER) then
begin
SendInstantOrder(Symbol, op_Sell, LotSize, 0, 0, '', 0, OrderHandle);
OrderStyle := tp_Sell;
OpenTime := Time(0)
end;

if OrderSelect(0, SELECT_BY_POS, MODE_TRADES) THEN
if (OrderHandle <> -1) and (OrderProfitPips >= TAKEPROFIT) or (OrderProfitPips <=-STOPLOSS) then
begin
CloseOrder(OrderHandle);
OrderHandle := -1;
End;


     
end;

exports
  InitStrategy,
  DoneStrategy,
  ResetStrategy,
  GetSingleTick,
  ReplaceStr;

end.



Now one thing I can not do is get the CCI value (in this case 100) as a variable

Code: Select all

CCI := CreateIndicator('EURUSD', Period_M1, 'CCI', '100');


I've tried defining a variable for '100' but, the problem is that the variable is an integer and the code needs a string. Anyone have any ideas?

pthomas82
Posts: 60
Joined: Tue Aug 10, 2010 8:39 pm

#5 Postby pthomas82 » Wed Jun 08, 2011 6:49 am

Also, How can i look back to the previous bar,

for example: If (CCI value 1
bar ago < 200) and (CCI value current bar > 200) then ... ... ...

Thanks again :)

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

#6 Postby FT Support » Fri Jun 10, 2011 6:29 am

use GetIndicatorValue(CCI, 1, 1); to get previous CCI value
Check our other product here:
http://www.forexcopier.com

pthomas82
Posts: 60
Joined: Tue Aug 10, 2010 8:39 pm

#7 Postby pthomas82 » Fri Jun 10, 2011 7:08 am

Thanks again Mike,

IM learning heaps every day! Your software is great and the language seems pretty easy!


Return to “Programming lessons”

Who is online

Users browsing this forum: No registered users and 19 guests