Solved! Borland Delphi 7--Compile Help, Please!

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

Solved! Borland Delphi 7--Compile Help, Please!

#1 Postby charvo » Tue Dec 04, 2012 11:55 pm

Updates: question solved

I put the 'global variables' in the wrong location (within GetSingleTick). I should've put it in the beginning of the library coding following the external variables.


Hello, Mike, or any experienced programmer:

Could you please take a quick look at my case? it should be an easy fix but i just don't know how or why. although my code is long, my question seems in fact simple.

I attached the complete code below, it is an strategy I coded in Delphi 7. When I compile, I keep getting "undeclared" errors. I don't understand these errors, since I do declare them in either 'GetSingleTick' or the routines: for example, if you try to compile the attached code, the four errors below will be on the top 4 errors:

[Error]: Undeclared identifier: 'CheckPrevBids'
[Error]: Undeclared identifier: 'CloseAllTradesIfNeeded'
[Error]: Undeclared identifier: 'OpenPhantomTrades'
[Error]: Undeclared identifier: 'CalPhantomPL'

but clearly in my code, you can see that I declared these procedure/functions.

Code: Select all


library RC_4questions;

uses
  SysUtils, DateUtils, StrategyInterfaceUnit, TechnicalFunctions;


//external parameters

var
  SpecificLot:    double = 0.01;
  FreeLot:        double = 0.01;
  ProfitPct:      double = 1.0;
  FailurePct:     double = 1.0;

  MagicNumber:    integer= 912;

  RunFreeLot:     boolean= true;

  Pair1:          PChar  = 'GBPUSD';
  Pair2:          PChar  = 'EURJPY';
  Pair3:          PChar  = 'AUDUSD';
  Pair4:          PChar  = 'NUDJPY';
  Pair5:          PChar  = 'EURAUD';


procedure InitStrategy; stdcall;
begin
  StrategyShortName('RepeatStrategy');
  StrategyDescription('Repeat strategy that does nothing');

  RegOption('Specific Lot', ot_Double, SpecificLot);
  RegOption('Free Lot', ot_Double, FreeLot);
  RegOption('Profit%', ot_Double, ProfitPct);
  RegOption('Failure%', ot_Double, FailurePct);
  RegOption('Magic Number', ot_Integer, MagicNumber);

  RegOption('Pair 1', ot_String, Pair1);
  RegOption('Pair 2', ot_String, Pair2);
  RegOption('Pair 3', ot_String, Pair3);
  RegOption('Pair 4', ot_String, Pair4);
  RegOption('Pair 5', ot_String, Pair5);

end;

procedure DoneStrategy; stdcall;
begin

end;

procedure ResetStrategy; stdcall;
begin

end;

// Found a New Bar
function  NewBar(timeframe:integer): boolean;
var
  found: boolean;
begin
  found := true;
  NewBar := found;  // Or use 'Result'
end;


procedure GetSingleTick; stdcall;
var
  sym:        array[1..10] of Pchar;
  pairAsk:    array[1..5] of Double;
  pairBid:    array[1..5] of Double;
  PhantomPL:  array[1..10] of Double;
  PairPL:     array[1..10] of Double;
  PairBuys:   array[1..10] of integer;
  PairSells:  array[1..10] of integer;
  prevBid:    array[1..10] of double;
  prevAsk:    array[1..10] of double;
  pairPoint:  array[1..10] of double;
  pairTickVal:array[1..10] of double;
  SymbolMarks:array[1..10] of boolean;


  i, OrderSendResult, SysTF, CutInterval: integer;
  NOofSells, NOofBuys, TotalTrades: integer;
  RealPhantomWinnerType, Real2ndPhantomWinnerType: integer;
  RealPhantomWinnerTicket, Real2ndPhantomWinnerTicket, BigPhantomWinnerTicket, BigPhantomLoserTicket: integer;
  RealPhantomWinnerPL, Real2ndPhantomWinnerPL, BigPhantomWinnerPL, BigPhantomLoserPL: double;

  UseLots, DollarProfitTarget, DollarLossFail, RealisedLoss, EAfProfitPeak, EAfLossPeak: double;

  CloseAll, PriceChanged, SymbolMarksCleared, SymbolMarked, PhantomsReset, NewBarFormed: boolean;


// 888888  start 888888
begin

  PriceChanged := CheckPrevBids();
  if TotalTrades > 0 then CloseAllTradesIfNeeded();

  OpenPhantomTrades();
  CalPhantomPL();

  if TotalTrades = 0 then
  begin

    CloseAll := false;
    EAfProfitPeak := 0;
    EAfLossPeak := 0;
    RealisedLoss := 0;

    if Hour(TimeCurrent) < StartHour or Hour(TimeCurrent) > FinishHour then
    PhantomsReset := true
    else
      begin
        ResetPhantoms();
        PhantomsReset := false;
      end
  end

  AddIfNeeded();

  if not CloseAll and ( EAPL>(DollarProfitTarget + Floor(Abs(RealisedLoss))) or (EAPL+RealisedLoss)<DollarLossFail ) then
  begin
     CloseAll := true;
     ResetPhantoms();
  end

  ExamineUpdatePLfloat();

  UpdatePrevBids();

  if TotalTrades > 5 then
  begin
    if TotalTrades <= 9 then  CutInterval := 22;
    if TotalTrades > 9 and TotalTrades <= 15 then  CutInterval := 16;
    if TotalTrades > 15 and TotalTrades <= 22 then  CutInterval := 11;
    if TotalTrades > 22 then  CutInterval := 7;
  end


end;
// 888888  start 888888

procedure CloseAllTradesIfNeeded();
var
  n:  integer;
begin
  if PriceChanged and CloseAll then
  begin
    for n := 0 to OrdersTotal() do
    begin
      OrderSelect(n,SELECT_BY_POS,MODE_TRADES);
      if OrderMagicNumber() = MagicNumber do
      begin
        if OrderType() = OP_BUY do
          OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_BID),Slippage,White);
        if OrderType() = OP_SELL do
          OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_ASK),Slippage,White);
      end
    end
  end
end

function CheckPrevBids(): boolean;
var
  n:  integer;
begin
  for n := 1 to 5 do
  begin
   if prevAsk[n] <> 0 and prevAsk[n] <> MarketInfo(sym[n], MODE_ASK) then CheckPrevBids := true;
   if prevBid[i] <> 0 and prevBid[i] <> MarketInfo(sym[n+5], MODE_BID) then CheckPrevBids := true;
  end
end

procedure SetSymbols();

begin
  sym[1] := Pair1;
  sym[2] := Pair2;
  sym[3] := Pair3;
  sym[4] := Pair4;
  sym[5] := Pair5;
  sym[6] := Pair6;
  sym[7] := Pair7;
  sym[8] := Pair8;
  sym[9] := Pair9;
  sym[10] := Pair10;
end

procedure OpenPhantomTrades();
var
  i: integer;
begin   // ??? learn about variable, internal, local, global???
  for i:= 1 to 5 do
    if  pairAsk[i] == 0.0 then
        pairAsk[i] := MarketInfo(sym[i], MODE_ASK);
  for i:= 1 to 5 do
    if  pairBid[i] == 0.0 then
        pairBid[i] := MarketInfo(sym[i+5], MODE_BID);
end

function SetPoint(aSymbol: string): double;
var
  apoint, adigits: double;
begin
  adigits := MarketInfo(aSymbol, mode_digits);
  if adigits < 4 then
    apoint := 0.01;
  else
    apoint := 0.001;
  SetPoint := apoint;
end

function SetTickValue(aSymbol: string, aPoint: double): double;
var
  aTickval: double;
begin
  if aPoint/10 = MarketInfo(aSymbol, mode_point) then
    aTickval  := MarketInfo(aSymbol, mode_tickvalue) * 10;
  else
    aTickval  := MarketInfo(aSymbol, mode_tickvalue);
  SetTickValue:= aTickval;
end

procedure SetPointsAndTickvalues();
var
  n: integer;
begin
  for n := 1 to 10 do
  begin
   pairPoint[n]   := SetPoint(sym[n]);
   pairTickVal[n] := SetTickValue(sym[n], pairPoint[n]);
  end
end

procedure CalcPhantomPL();
var
  n: integer;
begin
  SetPointsAndTickvalues();

  BiggestPhantomWinnerPL = 0.0;
  BiggestPhantomWinnerType = 0;
  BiggestPhantomWinnerSymbol = '';

  BiggestPhantomLoserPL = 0.0;
  BiggestPhantomLoserType = 0;
  BiggestPhantomLoserSymbol = '';

  for n := 1 to 10 do
  begin
    if pricePoint[n] <> 0 then
    begin
       if n <=5 then
         PhantomPL[n] := (MarketInfo(sym[n], mode_bid) - priceAsk[i])/pairPoint[i];
       else
         PhantomPL[n] := (priceBid[i] - MarketInfo(sym[n], mode_ask))/pairPoint[i];

         PhantomPL[n] := 0.1 * PhantomPL[n] * TickvalArray[n];

       if PhantomPL[n] > BigPhantomWinnerPL then
       begin
          BigPhantomWinnerPL      = PhantomPL[n];
          BigPhantomWinnerSymbol  = sym[n]
          if n <= 5 then
          BigPhantomWinnerType    = 1; // 1 means long
          else
          BigPhantomWinnerType    = 0; // 0 means short
       end
       if PhantomPL[n] < BigPhantomLoserPL then
       begin
          BigPhantomLoserPL      = PhantomPL[n];
          BigPhantomLoserSymbol  = sym[n]
          if n <= 5 then
          BigPhantomLoserType    = 1; // 1 means long
          else
          BigPhantomLoserType    = 0; // 0 means short
       end
    end
  end

  if BigPhantomWinnerPL > Abs(BigPhantomLoserPL) then
  begin
     RealPhantomWinnerPL      = BigPhantomWinnerPL;
     RealPhantomWinnerSymbol  = BigPhantomWinnerSymbol;
     RealPhantomWinnerType    = BigPhantomWinnerType;
  end
  else
  begin
     RealPhantomWinnerPL      = Abs(BigPhantomLoserPL);
     RealPhantomWinnerSymbol  = BigPhantomLoserSymbol;
     if BigPhantomLoserType = 1 then
       RealPhantomWinnerType  = 0;
     if BigPhantomLoserType = 0 then
       RealPhantomWinnerType  = 1;
  end

  // later to add: LocateReal2ndPhantomWinner(RealPhantomWinnerSymbol);

end

procedure ClearSymbolMarks();
var

begin

end

function SymbolMarked(pairsym:PChar): boolean;
var

begin

end

procedure MarkSymbol(pairsym:PChar);
var

begin

end

procedure AddIfNeeded();
var
  n: integer;
  pairsym: PChar;
begin

  if Minute <> 5 and Minute <> 35 then  ClearSymbolMarks();

  if PriceChanged and CloseAll = false then
  begin
    PhantomsReset := false;

    if SymbolMarked(RealPhantomWinnerSymbol) and ( (SysTF = 60 and Minute = 5) or (SysTF = 30 and (Minute = 5 or Minute = 35)) or (SysTF = 120 and Mod(Hour(),2) = 0 and Minute = 5) or (SysTF = 240 and Mod(Hour(),4) = 0 and Minute = 5))  ) then
    begin

      for n := 1 to 10 do
      begin
        pairsym := sym[n];
        if pairsym = RealPhantomWinnerSymbol then
        begin
          if RealPhantomWinnerType = 1 then
          OrderSendResult := OrderSend(sym[n],OP_BUY,UseLots,MarketInfo(sym[n], MODE_ASK), Slippage,0,0,WindowExpertName(),MagicNumber,Blue);
          if RealPhantomWinnerType = 0 then
          OrderSendResult := OrderSend(sym[n],OP_SELL,UseLots,MarketInfo(sym[n], MODE_BID), Slippage,0,0,WindowExpertName(),MagicNumber,Blue);

          if OrderSendResult > 0 then MarkSymbol(RealPhantomWinnerSymbol)
          else
          Alert(WindowExpertName()+" FAILED to buy a REAL " + SymbolArray[i] + ".");

        end
      end
    end
  end
end

procedure Count_Trades_PL();
var
  n:  integer;
begin
  NOofSells   :=    0;
  NOofBuys    :=    0;
  TotalTrades :=    0;
  EAPL        :=    0;

  BiggestWinnerPL     :=    0;
  BiggestWinnerTicket :=    0;
  BiggestWinnerType   :=    '';
  BiggestWinnerSymbol :=    '';
  BiggestLoserPL      :=    0;
  BiggestLoserTicket  :=    0;
  BiggestLoserType    :=    '';
  BiggestLoserSymbol  :=    '';

  for n := 0 to OrdersTotal - 1 do
  begin
    OrderSelect(n, Select_By_Pos, mode_trades)
    if OrderMagicNumber = MagicNumber and OrderLots = SpecificLot then
    begin
       EAPL = EAPL + OrderProfit;
       TotalTrades = TotalTrades + 1;

       if OrderProfit > BiggestWinnerPL then
       begin
          BiggestWinnerPL     :=    OrderProfit;
          BiggestWinnerTicket :=    OrderTicket;
          BiggestWinnerType   :=    OrderType;
          BiggestWinnerSymbol :=    OrderSymbol;
       end

       if OrderProfit < BiggestLoserPL then
       begin
          BiggestLoserPL     :=    OrderProfit;
          BiggestLoserTicket :=    OrderTicket;
          BiggestLoserType   :=    OrderType;
          BiggestLoserSymbol :=    OrderSymbol;
       end

    end

  end

end


exports
  InitStrategy,
  DoneStrategy,
  ResetStrategy,
  GetSingleTick;

end.


is this really due to my coding error or my mistake in using Borland Delphi 7? When I compile, the code is '.dpr' format.

Many thanks in advance! Looking forward to any suggestions!
Last edited by charvo on Thu Dec 06, 2012 12:08 am, edited 1 time in total.

Phil_Trade
Posts: 94
Joined: Tue Jan 31, 2012 5:14 am
Contact:

#2 Postby Phil_Trade » Wed Dec 05, 2012 1:46 am

Hi

you declare CheckPrevBids function after using it !

Just move it before GetSingleTick.

Phil
my live account - 8 based pairs with optimized parameters : +188%
http://www.myfxbook.com/members/Philipp ... jpg/519044
TradeSlide : http://bit.ly/14R9Nf6

to be informed about Windev/MT4 services : a04486-tradingsignal@yahoo.fr

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

#3 Postby charvo » Wed Dec 05, 2012 9:17 am

no, it still gives me error

it still says "CheckPrevBids' is 'Undeclared'.

but this position before or after 'getsingletick' does cause different errors

if i put all routines before 'GetSingleTick', then i get more errors of 'undeclared identifier' for many variables that i declare in 'GetSingleTick' but use in the routines that are put before 'GetSingleTick', why is that?

Thanks for help.

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

#4 Postby KelvinHand » Thu Dec 06, 2012 1:09 am

charvo wrote:no, it still gives me error

it still says "CheckPrevBids' is 'Undeclared'.

but this position before or after 'getsingletick' does cause different errors

if i put all routines before 'GetSingleTick', then i get more errors of 'undeclared identifier' for many variables that i declare in 'GetSingleTick' but use in the routines that are put before 'GetSingleTick', why is that?

Thanks for help.


There are too much errors to deal with due to your own problem

1. Procedure, functions, variables used without defined

Code: Select all


//--- Missing Variables

StartHour,FinishHour : integer;
EAPL: integer;

Pair6;
Pair7;
Pair8;
Pair9;
Pair10;
Minute;

BigPhantomWinnerSymbol,
BigPhantomLoserSymbol,
BigPhantomWinnerType,
BigPhantomLoserType;

pricePoint;
priceAsk;
priceBid;
TickvalArray;

BiggestPhantomWinnerType,
  BiggestPhantomLoserType 
  BiggestPhantomWinnerPL,
  BiggestPhantomLoserPL     
  BiggestPhantomWinnerSymbol,
  BiggestPhantomLoserSymbol


  BiggestWinnerPL     
  BiggestWinnerTicket
  BiggestWinnerType 
  BiggestWinnerSymbol
  BiggestLoserPL     
  BiggestLoserTicket
  BiggestLoserType   
  BiggestLoserSymbol


//-- Missing Procedures /Functions
procedure CalPhantomPL();
begin
end;

procedure ResetPhantoms();
begin
end;

procedure ExamineUpdatePLfloat();
begin
end;

procedure UpdatePrevBids();
begin
end;

//-- Use without declare
if n <=5 then
         PhantomPL[n] := (MarketInfo(sym[n], mode_bid) - priceAsk[i])/pairPoint[i];  //<========== i
       else
         PhantomPL[n] := (priceBid[i] - MarketInfo(sym[n], mode_ask))/pairPoint[i]; // <================ i

2. Call Procedure/functions with declaration after the called, in such case you need forward declaration
eg.

Code: Select all

library RC_4questions;

uses
  SysUtils, DateUtils, Math,
  StrategyInterfaceUnit, TechnicalFunctions;

function CheckPrevBids(): boolean; forward;
procedure CloseAllTradesIfNeeded(); forward;
procedure OpenPhantomTrades(); forward;
procedure CalPhantomPL(); forward;
procedure ResetPhantoms(); forward;
procedure AddIfNeeded();   forward;
procedure ExamineUpdatePLfloat();  forward;
procedure UpdatePrevBids();   forward;

function CheckPrevBids(): boolean; forward;
procedure CloseAllTradesIfNeeded(); forward;
procedure OpenPhantomTrades(); forward;
procedure CalPhantomPL(); forward;
procedure ResetPhantoms(); forward;
procedure AddIfNeeded();   forward;
procedure ExamineUpdatePLfloat();  forward;
procedure UpdatePrevBids();   forward;



3. If then else not done properly
eg. IF X then Y(no ; here) else Z;

Code: Select all

if n <=5 then
         PhantomPL[n] := (MarketInfo(sym[n], mode_bid) - priceAsk[i])/pairPoint[i];   <=========================
       else
         PhantomPL[n] := (priceBid[i] - MarketInfo(sym[n], mode_ask))/pairPoint[i];

4. Statement Assignment is :=, = cause error

Code: Select all

eg.
if BigPhantomWinnerPL > Abs(BigPhantomLoserPL) then
  begin
     RealPhantomWinnerPL      = BigPhantomWinnerPL;   <===
     RealPhantomWinnerSymbol  = BigPhantomWinnerSymbol;  <===
     RealPhantomWinnerType    = BigPhantomWinnerType; <==
  end
  else
  begin
     RealPhantomWinnerPL      = Abs(BigPhantomLoserPL);    <==
     RealPhantomWinnerSymbol  = BigPhantomLoserSymbol;  <===
     if BigPhantomLoserType = 1 then
       RealPhantomWinnerType  = 0;     <==
     if BigPhantomLoserType = 0 then
       RealPhantomWinnerType  = 1;   <===
  end


5. Empty Procedure/function cause problem
- if empty, don't place VAR before begin.
- Always end with semicolon

Code: Select all

    procedure MarkSymbol(pairsym:PChar);
   var   {<====}

   begin

   end  {<====}


6. If then begin.. end loop causing problem, Always end with semicolon

Code: Select all

if OrderProfit > BiggestWinnerPL then
       begin
          BiggestWinnerPL     :=    OrderProfit;
          BiggestWinnerTicket :=    OrderTicket;
          BiggestWinnerType   :=    OrderType;
          BiggestWinnerSymbol :=    OrderSymbol;
       end {<=====================}

       if OrderProfit < BiggestLoserPL then
       begin
          BiggestLoserPL     :=    OrderProfit;
          BiggestLoserTicket :=    OrderTicket;
          BiggestLoserType   :=    OrderType;
          BiggestLoserSymbol :=    OrderSymbol;
       end {<==========================}


7. Floor() complained because never use math

8. Alert complained because you use Alert(""), should be ' '

9. Order() ...etc command error, likely you use the MT4 version not tally FT2 strategy api. check the help guide.

Code: Select all

    if OrderType() = OP_BUY then ==>    if OrderType() = TP_BUY then



10. MarketInfo is for Bid n Ask only, no Mode_Digits and Mode Point

Code: Select all

 adigits := Digits();//MarketInfo(aSymbol, mode_digits);


 if aPoint/10 = Point() {MarketInfo(aSymbol, mode_point) } then





11. There is no Hour() function, should be HourOf() for delphi

12. Syntax Error

Code: Select all

 
   *  Mod(Hour(),2)   ==> HourOf(Time(0)) mod 2

function CheckPrevBids(): boolean;
var
  n:  integer;
begin
  result:=false;  { <==== }
  for n := 1 to 5 do
  begin
   if prevAsk[n] <> 0 and prevAsk[n] <> MarketInfo(sym[n], MODE_ASK) then {==> CheckPrevBids ==>} result := true;
   if prevBid[i] <> 0 and prevBid[i] <> MarketInfo(sym[n+5], MODE_BID) then {==> CheckPrevBids ==>} result := true;
  end

end
   


.... etc


Too much errors and no experience programmer here can help you to clear it. too time consuming. And unlikely, the program will work properly.


My advise is to clear error by component level ( eg. procedure/function) one by one. instead of assemble all components together.
it is more difficult to isolate the problem.

Also try to attend to each error reported, some errors are easy to handle by you.

Good Luck to you.

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

#5 Postby charvo » Thu Dec 06, 2012 10:46 am

Thank you very much for your help, Kelvin:

I am aware of many errors are from my MT4 code that's not been converted yet. I'll process them step by step.

I was just trying to see if my current delphi code has any structure mistakes. now that you've solved my 'structure'/'position' problems, i am confident the rest of the compiling errors are easier to solve.

thanks again.

charvo


Return to “Programming lessons”

Who is online

Users browsing this forum: No registered users and 16 guests