Page 1 of 1

Lesson X - Where to Declare 'Global Variable's?

Posted: Wed Dec 05, 2012 11:04 am
by charvo
Hi, Just thought that my question would complete the lessons for the future users :-)

the delphi strategy in FT2 has similar structure of that in MT4.

FT2's delphi strategy has generally four parts:
Init, Done, Reset, and 'GetSingleTick',

MT4's strategy has 3 parts: init(), deinit(), and start().

now the question:

Where to declare the variables that are used all through the library? or in other words, where to define/declare the 'global' variables? Should a user define them immediately following the 'external' variables or inside 'GetSingleTick'?

this question would be complement to the Lesson 1's contents.

Re: Lesson X - Where to Declare 'Global Variable's?

Posted: Wed Dec 05, 2012 6:45 pm
by KelvinHand
charvo wrote:Hi, Just thought that my question would complete the lessons for the future users :-)

the delphi strategy in FT2 has similar structure of that in MT4.

FT2's delphi strategy has generally four parts:
Init, Done, Reset, and 'GetSingleTick',

MT4's strategy has 3 parts: init(), deinit(), and start().

now the question:

Where to declare the variables that are used all through the library? or in other words, where to define/declare the 'global' variables? Should a user define them immediately following the 'external' variables or inside 'GetSingleTick'?

this question would be complement to the Lesson 1's contents.



//--- Code Sample ----

library YourAnswer;


uses
SysUtils, Classes, StrategyInterfaceUnit,
//You can create your global variables in another file and compile to .dcu can be uses by many files here
Your_Global_Variable_In_DCU;


var

//---- From Here :Your Global Variables for this file ---

// External parameters
Currency: PChar = nil;
TimeFrame: integer;
LotSize: double;
period1: integer;
period2: integer;

// custom variables
OrderHandle: integer;
OrderStyle: TTradePositionType;
OpenTime: TDateTime;

//----To here : Your Global Variables any place you like ----
'Global' variables above all procedures and functions in your current file. can only see in the current file.

{-----Init strategy---------------------------------------------------------}
procedure InitStrategy; stdcall;
begin
end;

{-----Done strategy---------------------------------------------------------}
procedure DoneStrategy; stdcall;
begin
end;

//This global variables here only seen by procedures /functions below it, not the above

var
z: integer;


{-----Reset strategy--------------------------------------------------------}
procedure ResetStrategy; stdcall;
begin
end;

{-----Process single tick---------------------------------------------------}
procedure GetSingleTick; stdcall;
//inside 'GetSingleTick' is global variables inside the 'GetSingleTick.

begin

procedure x;
begin
end;

function y(): integer;
begin
end;
end;

exports
InitStrategy,
DoneStrategy,
ResetStrategy,
GetSingleTick;

end.

Posted: Thu Dec 06, 2012 12:10 am
by charvo
Kelvin, many thanks!

Your crystal demonstration is highly appreciated!

Alex

Posted: Sat Jan 25, 2014 2:21 am
by Alex123
I have tried different time functions, but am just not able to open an order after a certain number of hours have passed after the previous order has closed. Could anyone please help?

Re: Alex

Posted: Sat Jan 25, 2014 6:17 am
by Terranin
Alex123 wrote:I have tried different time functions, but am just not able to open an order after a certain number of hours have passed after the previous order has closed. Could anyone please help?



Code: Select all

var
  // global vars
  tm: TDateTime;
  handle: integer;


// initialize variables in ResetStrategy
tm := 0;
handle := 0;

...

// track that order is closed and store close time
if handle <> 0 then
  if not(GetOrderInfo(handle, info)) then
    begin
      handle := 0;
      tm := TimeCurrent;
    end;
 
// if order is closed (handle = 0) check time difference
// OneHour = 1/24
if (handle = 0) and (time <> 0) then
  if (TimeCurrent - tm) > OneHour*X then 
     begin
        // open new order here and store its handle
     end;