Experts: ManualTradeOnStrategyTester

 

ManualTradeOnStrategyTester:

A simple way on how EA can link a manual order command from outside to use it in MetaTrader 5 Strategy Tester.

Author: SearchSurf

 

Here's a sample of a simple panel code done on VisualBasic2010: (The filepath should resemble the location folder of your terminal's Common Files)

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim filepathOut1 As String = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\sell.txt"
        Dim filepathOut2 As String = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\close.txt"
        Dim filepathIn As String = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\buy.txt"

        If (My.Computer.FileSystem.FileExists(filepathOut1)) Then
            My.Computer.FileSystem.DeleteFile(filepathOut1)
        End If

        If (My.Computer.FileSystem.FileExists(filepathOut2)) Then
            My.Computer.FileSystem.DeleteFile(filepathOut2)
        End If

        System.IO.File.Create(filepathIn).Dispose()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim filepathOut As String = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\buy.txt"
        Dim filepathOut2 As String = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\close.txt"
        Dim filepathIn As String = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\sell.txt"

        If (My.Computer.FileSystem.FileExists(filepathOut)) Then
            My.Computer.FileSystem.DeleteFile(filepathOut)
        End If

        If (My.Computer.FileSystem.FileExists(filepathOut2)) Then
            My.Computer.FileSystem.DeleteFile(filepathOut2)
        End If

        System.IO.File.Create(filepathIn).Dispose()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim filepathOut1 As String = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\buy.txt"
        Dim filepathOut2 As String = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\sell.txt"
        Dim filepathIn As String = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\close.txt"

        If (My.Computer.FileSystem.FileExists(filepathOut1)) Then
            My.Computer.FileSystem.DeleteFile(filepathOut1)
        End If

        If (My.Computer.FileSystem.FileExists(filepathOut2)) Then
            My.Computer.FileSystem.DeleteFile(filepathOut2)
        End If

        System.IO.File.Create(filepathIn).Dispose()

    End Sub
 
SearchSurf:

Here's a sample of a simple panel code done on VisualBasic2010: (The filepath should resemble the location folder of your terminal's Common Files)

Hi there, thanks on your efforts and to sharing it!

It was a very long time I was looking for a manual tester on Mt5; I was using the old FX Blue Trading Simulator v2 (http://www.fxblue.com/appstore/39/mt4-trading-simulator)

But unfortunately it's available only on Mt4, not allowing me to test strategies on equities cfd's on my current Mt5 account.

I've installed your tester an successfully created  the template but the problem is that I can't place any trade,  since I can't get to the panel.

Then I realized it's created on Vb10, so I downloaded Visualbasic and tried to copy and paste your code to create (without any success) my panel.

I never used Vb10 and neither coded; I just copied in a new blank project your code changing the filepath to locate my terminal's common files.

I couldn't debug nor publish the Panel as Vb10 returns me with this error:

 

May you be so kind to help me in any way to get the panel built so I could try the complete and working strategy tester?

Many thanks in advance and again all my appreciation to your contribute to the community!

Matteo 

FX Blue Trading Simulator v2
FX Blue Trading Simulator v2
  • FX Blue Labs - www.fxbluelabs.com
  • www.fxblue.com
The FX Blue Trading Simulator converts the MetaTrader 4 strategy tester into a tool for practising manual trading using historic data. You can use the Simulator to test how well you would have fared under particular historic market conditions - and/or to check how well your favourite indicators would have guided you in the past. The Simulator...
 

mbertox:

Hi there, thanks on your efforts and to sharing it!

It was a very long time I was looking for a manual tester on Mt5; I was using the old FX Blue Trading Simulator v2 (http://www.fxblue.com/appstore/39/mt4-trading-simulator)

But unfortunately it's available only on Mt4, not allowing me to test strategies on equities cfd's on my current Mt5 account.

I've installed your tester an successfully created  the template but the problem is that I can't place any trade,  since I can't get to the panel.

Then I realized it's created on Vb10, so I downloaded Visualbasic and tried to copy and paste your code to create (without any success) my panel.

I never used Vb10 and neither coded; I just copied in a new blank project your code changing the filepath to locate my terminal's common files.

I couldn't debug nor publish the Panel as Vb10 returns me with this error:

 

May you be so kind to help me in any way to get the panel built so I could try the complete and working strategy tester?

Many thanks in advance and again all my appreciation to your contribute to the community!

Matteo 

Hope this helps...

 

 VB10:

1. Open visual studio 2010

2. Click on "File" section, choose NEW project, then click on Windows Forms Application 

3. A new form will appear:

 


4. Add three BUTTONS on form:

 


 You can change the text of the button to "Buy", "Sell", "Close", right click on the button then properties: (you can do this later)

 

 

5. Double click all 3 each button and you will see something like this after:


 All the Private sub buttons should be inside the Public Class Form1...

 

6. Copy the codes for each button, your form now looks like this:

 


7. Don't forget to locate your Mt5 common folder, filepaths should resemble your folder on the code.

   mine looks like this:  "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\xxx.txt"

   Fastest way to find your terminals common folder is to go your MT5 MetaEditor, Go to File, and click on "Common Data Folder":

 

Copy the path folder then to your VB10 code filepaths.

 

8. Next on VB10 Go to File, and click Save all, remember the folder where you will be saving your VB10 program. 

     You'll need this later.


 

 

 

 In this example, I named mine as "MyTradingPanel"...

 

9. Test run it, start debug, if done correctly, no error should appear on error list.

 


 

 

10. While still on debugging, locate your MT5 common folder, and see if the files appears when you click on the button of your VB10 panel.

   Fastest way to find your terminals common folder is to go your MetaEditor, Go to File, and click on common files.

11. Test your button and you should see a text file appears on folder. 

 


12. After testing your buttons delete the files created on the folder manually at this moment before leaving the folder, the EA 

    should delete the files if in use later.  

13. Stop the Debug now. 

 

 

14. Now go to "Build" section, click the project you were working with. (I save mine on the name MyTradingPanel

before.) You should see below remarks on your VB10 "build successful".


 


 

 

14. Go to where you VB10 saving folder (this is where the folder you saved your project before),

    retrieve the application on "\Bin\Debug" folder.



15. You can copy paste the application file on any folder you want or place on desktop for easy access.

16. Run the EA on MT5 strategy tester, and you can now use the panel.

17. There you go, you just finished a basic crash course on VB10 :)

 

Good luck, hope you can get it running. 

 
mbertox:

Hi there, thanks on your efforts and to sharing it!


I never used Vb10 and neither coded; I just copied in a new blank project your code changing the filepath to locate my terminal's common files.

I couldn't debug nor publish the Panel as Vb10 returns me with this error:

 

May you be so kind to help me in any way to get the panel built so I could try the complete and working strategy tester?

Many thanks in advance and again all my appreciation to your contribute to the community!

Matteo 


After Saving your project...  You might want to check your VB settings too before building:

1. On the right side on "Solution Explorer", right click on the project then go to Properties. 


 

 2.  On Properties, check under "Compile", and on "Build output path":  (by default  >>> "bin\Debug\") this should save your output to where you saved your project files, but you can browse on it and change where the output file should go.


 3. Check the Debugging section, go to "Options and Settings"... or Tools, Options... and check (double click) "Projects and Solution" too, click on "Buid and Run" and on "On Run, when projects are out of date: ", choose "Always build".

 

 

 

 

Hope this fixed your building problem. 

 
SearchSurf:


After Saving your project...  You might want to check your VB settings too before building:

1. On the right side on "Solution Explorer", right click on the project then go to Properties. 


 

 2.  On Properties, check under "Compile", and on "Build output path":  (by default  >>> "bin\Debug\") this should save your output to where you saved your project files, but you can browse on it and change where the output file should go.


 3. Check the Debugging section, go to "Options and Settings"... or Tools, Options... and check (double click) "Projects and Solution" too, click on "Buid and Run" and on "On Run, when projects are out of date: ", choose "Always build".

 

 

 

 

Hope this fixed your building problem. 

Thank you for the fast reply!

I played all day with Vb10 and I found how to put graphically the buttons on the form and then I wasn't able to debug nor to publish the panel already..

Then I casually double clicked the 1st error on the log and found that the program suggest a fix..  and that's what I did. on all the 5 errors!

You can't imagine how I was happy just a blink after hitting the debug button! :D

Just made a full test on metatrader and open the forum to let you know.. and here's your reply!

Maybe your way to fix could be more reliable and I think to create a new version following yours instruction :)

As I told you I never coded but this taste it's been very intriguing..

I'm thinking how cool shall be to implement a box to chose the lot size and a SL / TP feature (maybe draggable on chart)

These features must to be coded on the mql5 ea and then linked to the panel? Or is "only" a Vb10 thing?

I don't think I could be able to do that.. but I was wondering how much work involved and the level of coding capabilities required to succeed! :) 

 Anyway I'm so happy just by now with my new trading toy in my hands! (A long night coming.. Can already see myself simulating and researching all weekend on!!) ;D

A big thank again!

See you..

Mat 

 
mbertox:

Thank you for the fast reply!

I played all day with Vb10 and I found how to put graphically the buttons on the form and then I wasn't able to debug nor to publish the panel already..

Then I casually double clicked the 1st error on the log and found that the program suggest a fix..  and that's what I did. on all the 5 errors!

You can't imagine how I was happy just a blink after hitting the debug button! :D

Just made a full test on metatrader and open the forum to let you know.. and here's your reply!

Maybe your way to fix could be more reliable and I think to create a new version following yours instruction :)

As I told you I never coded but this taste it's been very intriguing..

I'm thinking how cool shall be to implement a box to chose the lot size and a SL / TP feature (maybe draggable on chart)

These features must to be coded on the mql5 ea and then linked to the panel? Or is "only" a Vb10 thing?

I don't think I could be able to do that.. but I was wondering how much work involved and the level of coding capabilities required to succeed! :) 

 Anyway I'm so happy just by now with my new trading toy in my hands! (A long night coming.. Can already see myself simulating and researching all weekend on!!) ;D

A big thank again!

See you..

Mat 

There's a way to embbed the button on the EA itself to use on the MT5 Strategy Tester, but would require advance programming with some complex codings.  I thought this way I could just make use of those simple basic codings on mql5 and be able to use it on strategy tester. I'm just a hoobyists not actually a professional programmer and still on the learning process here too at MQL5.  Just keep on the look out on the code base, maybe some would find a way too using but simple coding entirely done on mql5... if I happen got a chance and manage to create one I'll post the same :)   ... Have fun and hopes the practice will make you better on trading profitably :)
 

A friend of mine asked if an Excel macro button can be used on this EA, since an internal VB is already existing on Excel....thought I might as well share this, a bit long and tricky if you haven't created excel macro, but if you already did it's actually basic stuff and simple as it looks.

 

Using Excel2007:

1. Customize first the excel quick access toolbar for easy access on button.

   - Right Click on "Home", then choose "Show quick access toolbar below the ribbon"

   - Click the Quick Access, then scroll down to choose more commands, select Customize, Choose command from

     Command not from the ribbon, then scroll and select Button, click Add, then click ok.





 

 

 

 

2. Click on the button tool, then create a button on excel. A window should appear asking for macro, if it didn't appear, right click on the button and choose assign macro.  

   On the assigning macro window, type in a name (in this example I named it EApanel), then click New, then "OK"...

  

 

 

4.  After clicking "OK", an Excel Visual Basic window should appear then.

  


  - Replace the present code that appeared with the codes written below. (Just delete everything inside the edit window first then type or copy paste in the entire new codes), 

   Note: After you typed in the codes, don't save it yet or you'll get confused if you haven't done this macro thingy before. just follow the steps.

   !!!! TAKE NOTE: The MT5 COMMON FOLDER location should resemble your own folder path!!! (The filepath in the filename in the code is my location folder, edit

   it to your own path folder location).  You can easily check your path folder by opening your Mt5 MetaEditor and on "File", choose "Open Common Data Folder".


Dim FileNameIn As String

Sub Buy_Click()

FileNameIn = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\buy.txt"
If DoesFileExist(FileNameIn) = 0 Then
 Open FileNameIn For Output As #1 ' create a new file if does not exist
Else
End If
Close #1

End Sub


Sub Sell_Click()

FileNameIn = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\sell.txt"
If DoesFileExist(FileNameIn) = 0 Then
Open FileNameIn For Output As #1 ' create a new file if does not exist
Else
End If
Close #1

End Sub



Sub Close_Click()

FileNameIn = "C:\Users\Rmd user\AppData\Roaming\MetaQuotes\Terminal\Common\Files\close.txt"
If DoesFileExist(FileNameIn) = 0 Then
Open FileNameIn For Output As #1 ' create a new file if does not exist
Else
End If
Close #1
End Sub


Function DoesFileExist(FileName As String) As Byte
' returns 1 if FileName exists, otherwise 0
If Dir(FileName, vbNormal) <> "" Then
 DoesFileExist = 1
Else
 DoesFileExist = 0
End If

End Function


5. Now go back to Excel (don't close the vb window). On Excel... Save the Excel first as MACRO ENABLED EXCEL FILE... choose "Save as" then choose "Excel Macro Enabled workbook". It should be MACRO ENABLED...

  you can save this on any folder you want.  I saved mine as EApanel too as named on vb macro just for easy reference, it does not necesarilly need the same name.


 


6. Now go back to Visual Basic window... Save the macro vb, From File, click Save (your VB macro).

   (If you want also to save the module1.bas, choose the export and place it on a folder, please note that macro vb files are saved on a special file exclusive for excel use only,

    saving the module.bas helps to have a back up copy of the macro codes you've made).

 

 

7. Now go back to Excel, create two more buttons or copy the Button1 and paste create two more buttons (either you create another or just copy paste the first button).

8. Rename the buttons, right click and choose "Edit Text", name the three to each as Buy, Sell, and Close.

 

 

9  Now let's assign the macro to each... on Buy button, right click and choose assign macro, assign it to Buy_click... 



10. Do the same on the two buttons, but assign them to each of its own click assignment... Sell to Sell_click, Close to Close_click.

11. Save the Excel... again Take note: It should be saved in as MACRO ENABLED WORKBOOK (since you've saved it as before, you can just now click "Save" from here on).

12. Go back to Visual Basic window, then click on "Debug", then click Compile VBAproject.



13. Let's test it, minimize the Excel, then open the EA location folder, Test the button, at each click a file should appear on the EA's location common folder...

    It takes a bit one to two seconds but a bit faster when being used alone on the Strategy Tester.  

    At this point, manually delete the text files first when you leave the location folder,  otherwise the EA will trigger a command at first open when it see's the first text file on the folder if you left it there.


 

14. Save the Excel file again and close the excel. Close also the excel visual basic. 

15. TAKE NOTE!!!  Depending on your default Excel Macro Security Settings, you might encounter a Warning everytime you open a macro driven excel file, it's an Excel securtiy feature to warn a macro file will be running.

    (If it happens, and if asked to enable, Choose "Enable", to make your macro work, otherwise the button will not work since the macro is disabled.)

 

 


 


That's about it, you're all set, just open the Excel along with the EA running on the Strategy Tester, and start practicing :)

 

 Happy practice trading...  :)

 (Comment updated: 12Dec2016) 

 

Nice and Perfect. Excel works fine.
BUT after 2days coming this spamming message

 2016.08.16 16:19:08.052 2016.01.04 00:25:00   EA detected error: 1004 -- EA Aborted!!! Pls. close EA now and attend to your open entry/ies.

that block orders

HELP!


 
Andrasdudu:

Nice and Perfect. Excel works fine.
BUT after 2days coming this spamming message

 2016.08.16 16:19:08.052 2016.01.04 00:25:00   EA detected error: 1004 -- EA Aborted!!! Pls. close EA now and attend to your open entry/ies.

that block orders

HELP!


The error you recieved came up when the EA recieved a command order from your Excel button... somehow your EA weren't able to execute the order, "Order Not Sent" message should have appeared too:

//+------------------------------------+
//| Execute TRADE                      |
//+------------------------------------+  
bool ExecuteTrade(string Entry,double ThePrice,double lot) // Entry = buy or sell / returns true if successfull.
  {
   bool success;

   success=true;

   MqlTradeRequest mreq; // for trade send request.
   MqlTradeResult mresu; // get trade result.
   ZeroMemory(mreq); // Initialize trade send request.

   Print("Order Initialized");
   mreq.action = TRADE_ACTION_DEAL;                                   // immediate order execution
   if(Entry=="buy") mreq.price = NormalizeDouble(ThePrice,_Digits);   // should be latest bid price
   if(Entry=="sell") mreq.price = NormalizeDouble(ThePrice,_Digits);  // should be latest ask price
   mreq.symbol = _Symbol;                                             // currency pair
   mreq.volume = lot;                                                 // number of lots to trade
   mreq.magic = 11119;                                                // Order Magic Number
   if(Entry=="sell") mreq.type = ORDER_TYPE_SELL;                     // Sell Order
   if(Entry=="buy") mreq.type = ORDER_TYPE_BUY;                       // Buy Order
   mreq.type_filling = ORDER_FILLING_FOK;                             // Order execution type
   mreq.deviation=100;                                                // Deviation from current price
//--- send order
   if(!OrderSend(mreq,mresu))
     {
      Alert("Order Not Sent: ",GetLastError());
      ResetLastError();
      success=false;
     }
.
.
.
.
   if(success==false)
     {
      Alert("Error ORDER FAILED!!! - error:1004");
      Arun_error=1004;
     }
   return(success);
  }

... not sure what went wrong on your process... but the only cause of the error I could think of is something on your MT5 setup ...or ... on the source command folder where you sent the command files text, probably a text command aren't deleted properly after being used or told so by the EA.  Check that command source "common" folder if it responds properly on your excel and of that EA... if it does then there gotta be something in your MT5 strategy tester that cannot detect the order requested.

 

Hello there, thanks for the Expert Advisor.

When I hit start on the strategy tester the Visualizer opens and shows a big "Waiting for update"  message and nothing happens.

How can I solve this?


Thanks..

Reason: