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

Examples step by step how to make your own automated strategy or user indicator
Message
Author
charvo
Posts: 52
Joined: Tue Dec 04, 2012 1:15 pm

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

#1 Postby charvo » Wed Dec 05, 2012 11:04 am

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.

KelvinHand
Posts: 103
Joined: Sun Jan 02, 2011 6:05 pm

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

#2 Postby KelvinHand » Wed Dec 05, 2012 6:45 pm

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.

charvo
Posts: 52
Joined: Tue Dec 04, 2012 1:15 pm

#3 Postby charvo » Thu Dec 06, 2012 12:10 am

Kelvin, many thanks!

Your crystal demonstration is highly appreciated!

Alex123
Posts: 1
Joined: Sat Jan 25, 2014 2:18 am
Location: fsfsdf

Alex

#4 Postby Alex123 » Sat Jan 25, 2014 2:21 am

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?

User avatar
Terranin
Site Admin
Posts: 833
Joined: Sat Oct 21, 2006 4:39 pm

Re: Alex

#5 Postby Terranin » Sat Jan 25, 2014 6:17 am

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;
Hasta la vista
Mike


Return to “Programming lessons”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest