exit after X bars

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

exit after X bars

#1 Postby pthomas82 » Sun Sep 25, 2011 6:15 am

Hi Guys,

im a bit stumpped how to do this - can someone give me a hint:

basically I want to close a trade after (lets say) 3 bars of being open

Thanks in advance

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

#2 Postby dackjaniels » Sun Sep 25, 2011 11:40 am

Hi pthomas,

You could define a global variable such as:

Code: Select all

tradeOpenBar: integer;



When you enter the trade record the entry bar:

Code: Select all

tradeOpenBar := Bars; //(current no of bars on chart)


Then check on each tick or each new bar for exit condition:

Code: Select all

if (Bars - tradeOpenBar = 3) then //add trade closing code/procedure



Hope this helps.

Steve

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

#3 Postby pthomas82 » Sun Sep 25, 2011 8:55 pm

Hi DackJaniels,

Thanks for your help - i'll give it a shot tonight, if it works i'll post the code =)

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

#4 Postby dackjaniels » Sun Sep 25, 2011 8:57 pm

Don't forget to post if it doesn't, we may be able to help :-)

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

#5 Postby pthomas82 » Mon Sep 26, 2011 4:48 am

ok, worked perfectly - here is the example as promised:

Code: Select all

 library BARS2_60;

uses
  SysUtils, Classes,  StrategyInterfaceUnit, TechnicalFunctions;

var
  Currency: PChar = nil;
  TimeFrame: integer;
  LotSize: double;
  tradeOpenBar: integer;


  OrderHandle: integer;
  OrderStyle: TTradePositionType;
 




procedure InitStrategy; stdcall;
begin
  StrategyShortName('Bars 2 60');
  StrategyDescription('2 bars 60pip move');

  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;

end;

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

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

procedure GetSingleTick; stdcall;

begin

if Symbol <> string(Currency) then exit;
SetCurrencyAndTimeframe(Symbol, TimeFrame);


if (OrderHandle = -1) and (open(2) < close(2)) and (open(1) < close(1)) then
begin
 SendInstantOrder(Symbol, op_Buy, LotSize, 0, 0, '', 0, OrderHandle);

    OrderStyle := tp_Buy;

    tradeOpenBar := Bars;
end;

if (OrderHandle = -1) and (open(2) > close(2)) and (open(1) > close(1)) then
begin
 SendInstantOrder(Symbol, op_sell, LotSize, 0, 0, '', 0, OrderHandle);

    OrderStyle := tp_sell;

    tradeOpenBar := Bars;
end;

begin

if OrderSelect(0, SELECT_BY_POS, MODE_TRADES)  THEN
if (OrderHandle <> -1) and (Bars - tradeOpenBar = 3) then

  begin

    CloseOrder(OrderHandle);

    OrderHandle := -1;
    end;
  end;
end;

exports
  InitStrategy,
  DoneStrategy,
  ResetStrategy,
  GetSingleTick,
  ReplaceStr;

end.

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

#6 Postby dackjaniels » Mon Sep 26, 2011 7:25 am

Nice one! :D


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 10 guests