Im stuck - need help!

How to create strategies and indicators
Message
Author
pthomas82
Posts: 60
Joined: Tue Aug 10, 2010 8:39 pm

Im stuck - need help!

#1 Postby pthomas82 » Tue Jul 26, 2011 9:27 pm

Hi Guys,

im currently working on a code which does the following:

1) buys when the close price is above the parabolic sar and if the CCI is above 200.

2) sells when the close price is below the parabolic sar and if the cci is below -200

then once an order is open

3) closes a buy order if the parabolic sar goes above the price

4) closes a sell order if the parabolic sar goes below the price.



My code is:

Code: Select all

 library CCIPSAR2;

uses
  SysUtils,
  Classes,
  StrategyInterfaceUnit,
  TechnicalFunctions;

var
  Currency: PChar = nil;
  TimeFrame: integer;
  LotSize: double;
  CCICURRENT: Double;
  PSARCURRENT: Double;
  CCIPREVIOUS: Double;
  CCI: integer;
  IndPSAR: integer;
  CCIUPPER: Integer;
  CCILOWER: Integer;
  PSARABOVE: Boolean;
  PSARBELOW: Boolean;

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




procedure InitStrategy; stdcall;
begin
  StrategyShortName('CCI PSAR 2');
  StrategyDescription('CCI PSAR TREND FOLLOWER');

  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('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', '400');
   IndPSAR := CreateIndicator ('EURUSD', Period_M1, 'PARABOLIC', '0.01; 0.01');
   PSARABOVE := false;
   PSARBELOW := false;

end;


procedure GetSingleTick; stdcall;

begin

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

if (OrderHandle <> -1) then
begin
  if PSARCURRENT > Close(0) then
      PSARABOVE := True;
  if PSARCURRENT < Close(0) then
      PSARABOVE := False;
end;

if (OrderHandle <> -1) then
begin
  if PSARCURRENT < Close(0) then
      PSARBELOW := True;
  if PSARCURRENT > Close(0) then
      PSARBELOW := False;
end;




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

end;

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

if (OrderHandle <> -1) and (OrderStyle = tp_Buy) and (PSARABOVE = True) then
begin
CloseOrder(OrderHandle);
OrderHandle := -1;
End;

if (OrderHandle <> -1) and (OrderStyle = tp_Sell) and (PSARBELOW = True) then
begin
CloseOrder(OrderHandle);
OrderHandle := -1;
End;




end;

exports
  InitStrategy,
  DoneStrategy,
  ResetStrategy,
  GetSingleTick,
  ReplaceStr;

end.



I am having the following problems:

1) The exit when the parabolic goes from below to above the price is not working (or reverse for a sell order)

2) The sell order for when the CCI is below -200 is not working.

Any hints or ideas would be appriciated!!! I have changed my code so many times now!!

Thanks in advance! :)

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

#2 Postby FT Support » Wed Jul 27, 2011 2:52 am

Hello,

Please try to add some logging to your code (you can use "Print" command). This really helps to define bugs in code. also you can try to debug the code using these reccomendations: http://forextester.com/forum/viewtopic.php?t=1760

if nothing helps then let us know and we'll help you with code
Check our other product here:
http://www.forexcopier.com

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

#3 Postby pthomas82 » Wed Jul 27, 2011 4:51 am

Thanks mate - i'll give all that a try and see how I go!

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

#4 Postby pthomas82 » Wed Jul 27, 2011 6:59 pm

hmmm im trying to use the debugger for this one. It keeps coming up: "can not get price".

does that mean that it isnt geting the tic value - i.e getsingletick?

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

#5 Postby pthomas82 » Thu Jul 28, 2011 5:13 am

ok, so im still trying to get this right - I have changed my approach and tried to just tackle one problem at a time. First I want to work out why the trade isnt closing.

So this code is supposed to buy when the parabolic sar is below the price, then close the trade and sell when the parabolic sar crosses above and visa versa.

Except the closing of the trade still doesnt work.



Anyone got any clues where im going wrong? Thanks in advance!

Code: Select all

library PSAR1;

 

uses

  SysUtils,

  Classes,

  StrategyInterfaceUnit,

  TechnicalFunctions;

 

var

  Currency: PChar = nil;

  TimeFrame: integer;

  LotSize: double;

  PSARCURRENT: Double;

  IndPSAR: integer;

 

  OrderHandle: integer;

  OrderStyle: TTradePositionType;

  OpenTime: TDateTime;

 

 

 

 

procedure InitStrategy; stdcall;

begin

  StrategyShortName('PARABOLIC');

  StrategyDescription('Buy Or sell Parabolic');

 

  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;

end;

 

 

 

procedure DoneStrategy; stdcall;

begin

   FreeMem(Currency)

end;

 

 

 

procedure ResetStrategy; stdcall;

begin

   OrderHandle := -1;

   IndPSAR := CreateIndicator ('EURUSD', Period_M1, 'PARABOLIC', '0.01; 0.01');



end;

 

 

procedure GetSingleTick; stdcall;

 

begin

 

if Symbol <> string(Currency) then exit;

SetCurrencyAndTimeframe(Symbol, TimeFrame);

PSARCURRENT := GetIndicatorValue(IndPSAR, 0, 1);






if (OrderHandle = -1) and (PSARCURRENT < Close(0)) then

begin

SendInstantOrder(Symbol, op_Buy, LotSize, 0, 0, '', 0, OrderHandle);

OrderStyle := tp_Buy;

OpenTime := Time(0)

 

end;




if (OrderHandle = -1) and (PSARCURRENT > Close(0)) 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 (OrderStyle = tp_Buy) and (PSARCURRENT > Close(0)) then

begin

CloseOrder(OrderHandle);

OrderHandle := -1;

End;

if OrderSelect(0, SELECT_BY_POS, MODE_TRADES) THEN
if (OrderHandle <> -1) and (OrderStyle = tp_Sell) and (PSARCURRENT < Close(0)) and (OpenTime <> Time(0))  then

begin

CloseOrder(OrderHandle);

OrderHandle := -1;

End;



end;

 

exports

  InitStrategy,

  DoneStrategy,

  ResetStrategy,

  GetSingleTick,

  ReplaceStr;

 

end.


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

#6 Postby pthomas82 » Fri Jul 29, 2011 5:15 am

Ok , I think I have figured this out - but im unsure. I am starting to think the value returned from

Code: Select all

GetIndicatorValue(IndPSAR, 0, 1);


can not compare to a value returned from

Code: Select all

close(0)
.

i.e the parabolic on the screen is showing value: 1.0470 and the close price of the bar is: 1.0460 therefore the statement parabolic > close(0) should be true.

Currently this is not happening. Could this be because the indicator is not retuning the value to 0.0000 ??

or is it because I can not compare the indicator value with the close value?

Thanks in advance!

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

#7 Postby pthomas82 » Sun Jul 31, 2011 4:35 am

or should I be using:

Code: Select all

iclose(0)


???

:?:

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

#8 Postby dackjaniels » Sun Jul 31, 2011 8:14 am

Hi pthomas,

I tested your code using the Parabolic-SAR (PRSAR.dll) indicator that comes with ForexTester and it works fine once I changed the number specifiying the TIndexBuffer in the call to GetIndicatorValue.

Code: Select all

PSARCURRENT := GetIndicatorValue(IndPSAR, 0, 0);


I'm not sure if your parabolic indicator is the same but perhaps you just need to change the same value from 1 to 0.

You can place a debug line in your code just after the GetIndicatorValue call to print the value of PSARCURRENT to the journal so you can see if the values look correct...

Code: Select all

Print('PSAR = ' + FloatToStr(PSARCURRENT));


Regards,
Steve

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

#9 Postby pthomas82 » Sun Jul 31, 2011 6:35 pm

Yo Dack!,

Thanks very much for the help - I'll do some testing tonight when I get home!

I was really stumped with that one - and im still learning both delphi and the ft api - so the assistance is really appriciated!

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

#10 Postby dackjaniels » Sun Jul 31, 2011 7:34 pm

No worries. Post back here if you still have problems with it.

Steve

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

#11 Postby dackjaniels » Tue Aug 02, 2011 10:26 am

Try this one pthomas: Make sure you compile it as PSAR1.dll or change the name next to library below to one you would prefer.
Also, download the new Parabolic FTSupport posted in your other thread (it works with 0.001 now).

Code: Select all

library PSAR1;

uses
  SysUtils,
  Classes,
  StrategyInterfaceUnit,
  TechnicalFunctions;

var
  Currency: PChar = nil;
  TimeFrame: integer;
  LotSize: double;
  CCICURRENT: Double;
  PSARCURRENT: Double;
  CCIPREVIOUS: Double;
  CCI: integer = -1;
  IndPSAR: integer = -1;
  CCIUPPER: Integer;
  CCILOWER: Integer;
  PSARABOVE: Boolean;
  PSARBELOW: Boolean;
  LastBarTime: TDateTime;
  period: integer = 15;

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

procedure InitStrategy; stdcall;
begin
  StrategyShortName('CCI PSAR 2');
  StrategyDescription('CCI PSAR TREND FOLLOWER');

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

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

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

  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;
   PSARABOVE := false;
   PSARBELOW := false;
   CCI := -1;
   IndPSAR := -1;
end;

procedure GetSingleTick; stdcall;

begin
//if (Bars < period) then exit;
if Symbol <> string(Currency) then exit;
SetCurrencyAndTimeframe(Symbol, TimeFrame);

if (CCI = -1) then CCI := CreateIndicator(Symbol, TimeFrame, 'CCI', '15');
if (IndPSAR = -1) then IndPSAR := CreateIndicator (Symbol, TimeFrame, 'Parabolic', '0.010;0.010');

if (LastBarTime <> Time(0)) then
begin
  CCICURRENT := GetIndicatorValue(CCI, 0, 1);
  PSARCURRENT := GetIndicatorValue(IndPSAR, 0, 0);
  CCIPREVIOUS := GetIndicatorValue(CCI, 1, 1);

  //Print('PSAR = ' + FloatToStr(PSARCURRENT));
  //Print('CCI Current = ' + FloatToStr(CCICURRENT));
  //Print('CCI Previous = ' + FloatToStr(CCIPREVIOUS));
end;
LastBarTime := Time(0);


if PSARCURRENT > Close(0) then PSARABOVE := True;
if PSARCURRENT < Close(0) then PSARABOVE := False;
if PSARCURRENT < Close(0) then PSARBELOW := True;
if PSARCURRENT > Close(0) then PSARBELOW := False;

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

end;

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

if (OrderHandle <> -1) and (OrderStyle = tp_Buy) and (PSARABOVE = True) then
begin
CloseOrder(OrderHandle);
OrderHandle := -1;
End;

if (OrderHandle <> -1) and (OrderStyle = tp_Sell) and (PSARBELOW = True) then
begin
CloseOrder(OrderHandle);
OrderHandle := -1;
End;

end;

exports
  InitStrategy,
  DoneStrategy,
  ResetStrategy,
  GetSingleTick,
  ReplaceStr;
end.


When creating indicators pay particular attention to the parameter string used, '0.010;0.010' in this case. If it isn't exactly right, correct parameters and no spaces, a new indicator will be created every time you start the strategy resulting in multiple indicators on the chart and may slow things down significantly. I just edited mine in the above code so make sure you have this latest version.

Code: Select all

if (IndPSAR = -1) then IndPSAR := CreateIndicator (Symbol, TimeFrame, 'Parabolic', '0.010;0.010');



Regards,
Steve


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 6 guests