Need help - trade management.

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

Need help - trade management.

#1 Postby pthomas82 » Sat Oct 29, 2011 6:55 am

Hi Guys,

Im trying to code up some trade management - and for some reason I can't get it working.

Firstly there are no entry rules as the object of this code is to add an automated exit to a manual entry. I believe I am having problems with orderhandle as I dont believe the code is identifying when a trade is opened. Please note the manual entry could also include buystop / sell stop - i dont know if this effects anything.

can someone cast their eye over this code and give me some pointers?

Code: Select all


library JBNJBQEXIT;


uses

SysUtils,  Classes,  StrategyInterfaceUnit, technicalfunctions;

var

// External parameters

Currency: PChar = nil;
TimeFrame: integer;
lookback:integer;
LotSize: double;
STOPLOSS: integer;
BREAKEVEN: integer;
STOCCURRENT: Double;
STOCPREV: Double;
STOC: integer;
STOCUPPER: Integer;
STOCLOWER: Integer;
LOWEST: Integer;
HIGHEST: Integer;
LOWVALUE: double;
HIGHVALUE: double;

// custom variables

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


{-----Init strategy-----------------------------------------}

procedure InitStrategy; stdcall;

begin

StrategyShortName('JBNJBQ');
StrategyDescription('Jack be nimble exit');


// Register external parameters

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

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

RegOption('Stoploss', ot_Integer, STOPLOSS);
STOPLOSS := 30;

RegOption('BREAKEVEN', ot_Integer, BREAKEVEN);
BREAKEVEN := 15;

RegOption('STOC UPPER BUY LEVEL', ot_Integer, STOCUPPER);
STOCUPPER := 80;

RegOption('STOC LOWER SELL LEVEL', ot_Integer, STOCLOWER);
STOCLOWER := 20;

RegOption('Number of bars back', ot_Integer, LOOKBACK);
LOOKBACK := 10;

end;

 

{-----Done strategy---------------------------------------}

procedure DoneStrategy; stdcall;

begin

FreeMem(Currency);

end;

 

{-----Reset strategy--------------------------------------}

procedure ResetStrategy; stdcall;

begin

Orderhandle := -1;

STOC := CreateIndicator (Currency, Timeframe, 'Stochastic', '10;5;10;close/close');


end;



{-----Process single tick----------------------------------}

procedure GetSingleTick; stdcall;


begin

STOCCURRENT := GetIndicatorValue(STOC, 0, 0);
STOCPREV := GetIndicatorValue(STOC, 1, 0);
LOWEST := iLowest(Currency, Timeframe, MODE_CLOSE, lookback, 0);
HIGHEST := iHighest(Currency, Timeframe, MODE_CLOSE, lookback, 0);
LOWVALUE :=   iLow(Currency, Timeframe, LOWEST);
HIGHVALUE :=  iHigh(Currency, Timeframe, HIGHEST);

// check our currency

if Symbol <> string(Currency) then exit;


// set currency and timeframe

SetCurrencyAndTimeframe(Symbol, TimeFrame);

// check open orders

if OrderSelect(0, SELECT_BY_POS, MODE_TRADES) then
if (OrderType = tp_Sell) or (OrderType = tp_Buy) then
  begin
  Orderhandle :=1;
  end;




// STOPLOSS BUY

if OrderSelect(0, SELECT_BY_POS, MODE_TRADES) THEN
if (OrderHandle <>-1) and (OrderStyle = tp_Buy) and

    (OrderProfitPips <=-STOPLOSS) then

  begin

    CloseOrder(OrderHandle);

  end;


// STOPLOSS SELL

if OrderSelect(0, SELECT_BY_POS, MODE_TRADES)  THEN
if (OrderHandle <>-1) and (OrderStyle = tp_Sell) and

   (OrderProfitPips <=-STOPLOSS) then

  begin

    CloseOrder(OrderHandle);

  end;

  // MOVE BREAKEVEN STOP BUY


if OrderSelect(0, SELECT_BY_POS, MODE_TRADES) THEN
if (OrderHandle <>-1) and (OrderStyle = tp_Buy) and (OrderProfitPips >= BREAKEVEN) then

  begin

    ModifyOrder(OrderHandle,0,OrderOpenprice,0);

  end;

   // MOVE BREAKEVEN STOP SELL


if OrderSelect(0, SELECT_BY_POS, MODE_TRADES) THEN
if (OrderHandle <>-1) and (OrderStyle = tp_Sell) and (OrderProfitPips >= BREAKEVEN) then

  begin

    ModifyOrder(OrderHandle,0,OrderOpenprice,0);

  end;



// TRAILING STOP BUY

if OrderSelect(0, SELECT_BY_POS, MODE_TRADES) THEN
if (OrderHandle <>-1) and (OrderStyle = tp_Buy) and (OrderProfitPips >= BREAKEVEN)  THEN
if (STOCPREV < STOCUPPER) and (STOCCURRENT > STOCUPPER) THEN


  begin

  ModifyOrder(OrderHandle,0,LOWVALUE,0);

  end;


// TRAILING STOP SELL


if OrderSelect(0, SELECT_BY_POS, MODE_TRADES)  THEN
if (OrderHandle =-1) and (OrderStyle = tp_Sell) and (OrderProfitPips >= BREAKEVEN) THEN
if (STOCPREV > STOCLOWER) and (STOCCURRENT < STOCLOWER) THEN



  begin

    ModifyOrder(OrderHandle,0,HIGHVALUE,0);

  end;

// IF ORDER CLOSED THEN RESET

 if OrderClosed(OrderHandle) then
    begin
      Orderhandle := -1;
    end;

end;




exports

InitStrategy,
DoneStrategy,
ResetStrategy,
GetSingleTick;


end.


Thanks in advance =)

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

#2 Postby FT Support » Sat Oct 29, 2011 5:40 pm

Hello,

Please try to change:

Code: Select all

  Orderhandle :=1;

with

Code: Select all

Orderhandle := OrderTicket();


If this does not work then add more "Print" instructions for debug purposes so you will be able to see what actually happens in the strategy.

Also try to debug the strategy, see details here: http://forextester.com/forum/viewtopic.php?t=1760
Check our other product here:
http://www.forexcopier.com

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

#3 Postby pthomas82 » Sat Oct 29, 2011 9:10 pm

Hi Mike,

Thanks for the idea - I simplified the code to get a working version. For everyone trying to do something like this - here is my example:

Code: Select all


library easyexit;


uses
  SysUtils,
  Classes,
  StrategyInterfaceUnit;

var

// External parameters

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



// custom variables

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


{-----Init strategy-----------------------------------------}

procedure InitStrategy; stdcall;

begin

StrategyShortName('Exit test ');
StrategyDescription('Exit Test');


// Register external parameters

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

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

RegOption('LotSize', ot_Double, LotSize);
SetOptionDigits('LotSize', 1);


end;



{-----Done strategy---------------------------------------}

procedure DoneStrategy; stdcall;

begin

FreeMem(Currency);

end;

 

{-----Reset strategy--------------------------------------}

procedure ResetStrategy; stdcall;

begin
end;


{-----Process single tick----------------------------------}

procedure GetSingleTick; stdcall;
var
i:integer;

begin

// check our currency

if Symbol <> string(Currency) then exit;

// set currency and timeframe

SetCurrencyAndTimeframe(Symbol, TimeFrame);

// look for open orders

  if (OrdersTotal > 0) then
  Begin
  for i:=0  to OrdersTotal do
  begin
  OrderSelect(i, SELECT_BY_POS, MODE_TRADES);


// inital stoploss buy

    if (Orderstoploss = 0) and (ordertype = tp_buy) then
    ModifyOrder(OrderTicket,0,OrderOpenprice -30*point ,0);


// greater than 10 - breakeven buy

    if (Orderstoploss < OrderOpenPrice) and (ordertype = tp_buy) and (OrderProfitPips >= 10) then
    ModifyOrder(OrderTicket,0,OrderOpenprice,0);


// greater than 50 - breakeven buy

    IF (Orderstoploss = OrderOpenPrice) and (ordertype = tp_buy) and (OrderProfitPips >= 50) then
    ModifyOrder(OrderTicket,0,Orderstoploss + 10*point ,0);
    end;

//inital stoploss sell

    if (Orderstoploss = 0) and (ordertype = tp_Sell) then
    ModifyOrder(OrderTicket,0,OrderOpenprice + 30*point ,0);


// greater than 10 - breakeven sell

    IF (Orderstoploss > OrderOpenPrice) and (Ordertype = tp_Sell) and (OrderProfitPips >= 10) then
    ModifyOrder(OrderTicket,0,OrderOpenprice,0);


// greater than 50 - breakeven sell


    IF (Orderstoploss = OrderOpenPrice) and (Ordertype = tp_Sell) and (OrderProfitPips >= 50) then
    ModifyOrder(OrderTicket,0,Orderstoploss  -10*point ,0);
    end;

end;

exports

InitStrategy,
DoneStrategy,
ResetStrategy,
GetSingleTick;


end.



Return to “FT API”

Who is online

Users browsing this forum: No registered users and 16 guests