#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Delphi trading backtesting code


    Hi All,

    I am not sure if this is the best place to post this question, but I really need some help with regards to a relatively simple code. It is part of a DLL and when compiled I run it through a Forex trading backtesting software.

    I have the code running, I would just like it to execute orders when an indicator reaches a certain value, should I post the code here or somewhere else on the forum?

    The indicator code is:



    IndATR: integer;

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

    procedure ResetStrategy; stdcall;

    begin

    IndATR := CreateIndicator('EURUSD', 15, 'ATR', '14;Close');

    end;

    //in procedure GetSingleTick:
    atrCurrent := GetIndicatorValue(IndATR, 0, 2);



    But I am not sure how to incorporate it into the main code and how to stipulate that I want an order to be opened when the ATR value reaches 50%.

    Any help would be massively appreciated! Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    156
    Rep Power
    3
    Not really sure why you would have that kind of problem. You can run a timer to check the ATR value at specified interval and when ATR value reaches 50% (or more) you execute codes that place order.

    Use a TTimer, specify interval that you want, and in its OnTimer you check the current ATR value and when it's 50% or more you place order. Make sure the timer is enabled, otherwise the OnTimer will never be fired.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by Luthfi
    Not really sure why you would have that kind of problem. You can run a timer to check the ATR value at specified interval and when ATR value reaches 50% (or more) you execute codes that place order.

    Use a TTimer, specify interval that you want, and in its OnTimer you check the current ATR value and when it's 50% or more you place order. Make sure the timer is enabled, otherwise the OnTimer will never be fired.
    Thanks for the response. I have entered the ATR code correctly ( I believe), but it does not seem to be working, could you please tell me how to specify and use a TTimer in the code? Sorry, I am very new to Delphi.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    156
    Rep Power
    3
    If you are using a form, you can just drop a TTimer to your form (let's say that the instance is named Timer1), set its interval to 1000 (1second) and enabled to true. Double click on it and its OnTimer event handler will be generated. Of course initially the event handler will be empty. It's your job to put codes that do the ATR checking and place the order inside this event handler.

    If you want to do all in code, e.g. you don't use Form or DataModule, you can use something like this:

    Code:
    type
      TMyClass=class  // your class than handle the ATR checking and placing order
        ..
       private
         FTimer: TTimer;
         procedure HandleTimer(ASender: TObject);
       public
         constructor Create;
         destructor Destroy; override;
      end;
    
    ...
    implementation
    ..
    
    constructor TMyClass.Create;
    begin
      ...
      FTimer := TTimer.Create(nil);
      FTimer.Interval := 1000; // 1 second
      FTimer.OnTimer := HandleTimer;  // assign the event handler
      FTimer.Enabled := True;
      ...
    end;
    
    destructor TMyClass.Destroy;
    begin
      FTimer.Free;
      ...
      inherited;
    end;
    
    procedure TMyClass.HandleTimer(ASender: TObject);
    var
      vATR: Double;
    begin
      vATR := GetCurrentATR;
      if vATR >= 50 then
        PlaceOrder;
    end;
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    Thanks so much for your help. I will have to look into this and try to learn how to do it. Would you be able to point me to any resources that specifically teach how to code Delphi trading strategies?

    Thanks again.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    156
    Rep Power
    3
    Unfortunately no. In the past I have tried to win some of similar applications contracts, but never succeeded. So my experience in this particular field is nearly zero. However the programming should not be that hard or that different with other network related applications.

IMN logo majestic logo threadwatch logo seochat tools logo