Order problem

How to create strategies and indicators
Message
Author
woodrat
Posts: 3
Joined: Wed Jul 01, 2009 6:58 pm

Order problem

#1 Postby woodrat » Tue Jul 07, 2009 2:06 am

I'm having problems because I don't know the programming language.

What I want is to make an order at the price when the current bar breaks the highest point of the latest 20 bars.

After that order is closed and that specific bar is complete, I need to know how to search for a new set of 20 bars and to make an order.

If that doesn't work out, the following problem happens.

Image


(orderstotal = 0) can't stop the numerous orders tangled up in the same bar.

Also, I did some research and found the following function:

SetCurrencyAndTimeframe(symbol,SearchTimeFrame);

if time(0) <> PrevTime then
begin
PrevTime := Time(0);

// here it is new bar appears

This also is unable to prevent making multiple orders in the same bar. However, when I set the SearchTimeFrame as PERIOD_M15

begin
imax:=0;
for i:= 1 to 20 do
If imax < iHigh(Symbol,TimeFrame,i) then imax:=iHigh(Symbol,TimeFrame,i);
if (imax < iHigh(Symbol,TimeFrame,0)) and (orderstotal = 0)
then
SendInstantOrder(symbol, op_Buy, LotSize, Ask - StopLoss*Point, Ask + TakeProfit*Point, '', 0, OrderHandle);

AND in the consecutive function set the TimeFrame as PERIOD_M14 the "multiple orders made in the same bar" problem disappeared. (But what I want is NOT M14)

The biggest problem is that "if time(0) <> PrevTime then" shifts all orders by one bar. The order is made in the bar after the original bar that the order was supposed

to be made.

At times, an order isn't made when it has to be made and vice versa.

In the original approach withouth the use of the PrevTime fuction, it was complicated and twisted, but the order signals were always almost correct. But after adding

the PrevTime function, the orders are irregular, and are shifted to the next bar.

Image

The whole function follows:

Code: Select all

library RND_0305;

uses
  SysUtils,
  Classes,
  DateUtils,
  StrategyInterfaceUnit,
  TechnicalFunctions;

var

  Timeframe:integer=15;
  Currency: PChar;
  Searchtimeframe: integer=15;
  StopLoss: integer = 30;
  TakeProfit: integer = 30;
  LotSize: double = 1;
  PrevTime: TDateTime;
  iMAX:double=0;
  jLOW:double=8000;
procedure InitStrategy; stdcall;
begin
  StrategyShortName('4h_15m_test_stv_331');
  StrategyDescription('Strategy opens orders at 8 a.m');
  RegOption('Currency', ot_Currency, Currency);
  RegOption('Timeframe',ot_integer,Timeframe);
  RegOption('SearchTimeframe',ot_integer,SearchTimeframe);
  RegOption('StopLoss', ot_Integer, StopLoss);
  RegOption('TakeProfit', ot_Integer, TakeProfit);
  RegOption('LotSize', ot_Double, LotSize);
  SetOptionDigits('LotSize', 1);
  end;


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

procedure ResetStrategy; stdcall;
begin
 PrevTime:= 0;

    end;

procedure GetSingleTick; stdcall;
var
OrderHandle: integer;
i:Integer;
begin
  if Symbol <> Currency then
    exit;

   SetCurrencyAndTimeframe(symbol,SearchTimeFrame);




   if time(0) <> PrevTime then
    begin
    PrevTime := Time(0);

        // here it is new bar appears
// Buy

       begin
        imax:=0;
        for i:= 1 to 20 do
        If imax < iHigh(Symbol,TimeFrame,i) then imax:=iHigh(Symbol,TimeFrame,i);
        if (imax < iHigh(Symbol,TimeFrame,0)) and (orderstotal = 0)
        then
        SendInstantOrder(symbol, op_Buy, LotSize, Ask - StopLoss*Point, Ask + TakeProfit*Point, '', 0, OrderHandle);







//Sell



        jlow:=8000;
        for i := 1 to 20 do
        If jlow > iLow(Symbol,TimeFrame,i) then jlow:=iLow(Symbol,TimeFrame,i);
        if (jlow > iLow(Symbol,TimeFrame,0)) and (orderstotal = 0)
        then SendInstantOrder(symbol, op_Sell, LotSize, Bid + StopLoss*Point, Bid - TakeProfit*Point, '', 0, OrderHandle);

        end
    end;
end;





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




It's the first time I'm using the programming language, so it would be nice for you to help me out a little.

Thank you so much!

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

#2 Postby dackjaniels » Tue Jul 07, 2009 3:59 am

Hi woodrat,

The problem lies in your use of the PrevTime variable.

You currently use this...

Code: Select all

if time(0) <> PrevTime then
  begin
     PrevTime := Time(0);

     (check for entries)

End


What the above does is checks if the current candle ( time(0) ) has the same timestamp that the PrevTime variable contains. If they are NOT equal then they are immediately set to be equal ( PrevTime := time(0) ) and the strategy checks for a valid entry.

Of course on the next tick, unless a new candle is started, time(0) and PrevTime will still be equal and your entry rules will not be executed.

The overall result is that your entry rules are only executed on the first tick of each candle. Most entries do not occur on the first tick! If a valid entry condition occurs half way through a candle it will not be recognised until the first tick of the next candle, this is why the entries are one candle late on your chart.

Good news is you are almost there :)

Amend your code to look like this:

Code: Select all

if (time(0) <> PrevTime) and (orderstotal = 0) then
    begin
      // here it is new bar appears
      // Buy
       begin
        imax:=0;
        for i:= 1 to 20 do
        If imax < iHigh(Symbol,TimeFrame,i) then imax:=iHigh(Symbol,TimeFrame,i);
        if (imax < iHigh(Symbol,TimeFrame,0)) then
          begin
            SendInstantOrder(symbol, op_Buy, LotSize, Ask - StopLoss*Point, Ask + TakeProfit*Point, '', 0, OrderHandle);
            PrevTime := time(0);
          end;
      //Sell
        jlow:=8000;
        for i := 1 to 20 do
        If jlow > iLow(Symbol,TimeFrame,i) then jlow:=iLow(Symbol,TimeFrame,i);
        if (jlow > iLow(Symbol,TimeFrame,0)) then
          begin
            SendInstantOrder(symbol, op_Sell, LotSize, Bid + StopLoss*Point, Bid - TakeProfit*Point, '', 0, OrderHandle);
            PrevTime := time(0);
          end;
        end
    end;



Notice it's been changed to set the PrevTime variable equal to the current candle ( time(0) ) only once a valid entry has been found. This means that if no valid entry is found on the first tick it will check again on the second tick, and so on until a valid entry is found.

Also, I changed it to check that orderstotal = 0 at the very beginning. This makes your strategy more efficient as it only runs the entry rules portion of the code if no orders are already open.

Once you have copied and tested the above try changing this line...(in both the buy and sell routines)

if (imax < iHigh(Symbol,TimeFrame,0)) then

to this

if (imax < iClose(Symbol,TimeFrame,0)) then

Look carefully at the entries and exits and see if you can see why comparing the Close of the current candle to the previous 20 highs may be a better option for you.

Regards,
Steve


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 12 guests