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
exit after X bars
-
- Posts: 151
- Joined: Tue Feb 24, 2009 1:03 pm
Hi pthomas,
You could define a global variable such as:
When you enter the trade record the entry bar:
Then check on each tick or each new bar for exit condition:
Hope this helps.
Steve
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
-
- Posts: 151
- Joined: Tue Feb 24, 2009 1:03 pm
-
- Posts: 60
- Joined: Tue Aug 10, 2010 8:39 pm
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.
Who is online
Users browsing this forum: No registered users and 18 guests