Log in

MT4 + C# + MongoDB + ENCOG EA

By unclepips 2858 days ago Comments (47)

Ok, so I have the communication working between MT4 and VS (c#). This gives me the ability to store info into MongoDB (a nosql database).  There is a free C# library for Encog (NN library).  Has anyone successfully combined Encog with MT4 and shown sucess?


  • jaguar1637 2858 days ago

    Whaouuuuh .

    How does it work ? , please, explain it to me.

    Dio you have any URL links or documentations to share with us ?

    thank in advance

  • unclepips 2858 days ago

    Part 1:

    MT4 EA will be used to only marshal information to C#, and for opening orders of course.  C# then stores data (candles, indicators, Trade History, etc) in MongoDB. 


    Part 2:

    Data is pulled from MongoDB and evaluated by ENCOG for possible trades to open.  If found, Trade is stored in MongoDB to be read back by the marshalling program.  Money management, trade direction and trade modifications are all calculated in C#.


    There is a DLL for ENCOG to MT4, but I am much more comfortable with C# and my experience with MT4 has killed my faith in it's stability as a programming language.  I feel better about offloading the data to c#.


    Here is a link of someone working on a similar concept, he is using Named Pipes instead of a DB.


    I will post as it comes togeather.

  • unclepips 2858 days ago

    TradeMarshal C# project TradeMarshal.mq4Files under current development.

  • tovim 2858 days ago

    I look forward to further development. 

  • JohnLast 2858 days ago

    This looks very interesting. Thanks for sharing unclepips.

  • fxez 2856 days ago

    Interesting development unclepips! I'm not familiar with Encog or MongoDB. I've used the nnet package in R (single layer perceptron), and have linked R to MT4 / C# using 7Bit's connecter / DCOM Statconnecter respectively.

    I'm actually working quite a bit with R and C# right now using (D)Com Statconnector available in R and Friends download via http://rcom.univie.ac.at/download.html but am not doing anything with MT4 at the moment.

  • fxez 2856 days ago

    Do you have the link to the person using Named Pipes instead of a DB?

  • unclepips 2856 days ago

    Here is a link to the forum with the Named Pipes:  http://www.heatonresearch.com/node/2659


  • JohnLast 2851 days ago

    francisfinley thanks for that link. But it is worth a separate bookmark.

  • francisfinley 2850 days ago

    ok have installed all the stuff how is proput on the heaton site getting it to write back to his MA EA?

  • jaguar1637 2849 days ago

    Give me a few days for this test, pls.

  • JohnLast 2849 days ago

    As I read the MongoDB is a perfect match for trading because it outperforms the SQL for statistical or real-time analyses.

    That combination with encog is a killer application. The only problem for me is the lack of knowledge and skills.

  • unclepips 2848 days ago

    Update  I have created a video to show the process or where the first portion is.  I apologize for the speed of the screen capture.  You may want to pause often to see what is going on.




  • unclepips 2848 days ago

    Files have also been updated to those used in the video.

  • JohnLast 2848 days ago

    That is great, I really want to thank you for the effort to make a video on you tube.



  • fxez 2848 days ago

    The video is a very good visual step by step on how this can be of use in an external program. Very nice job!

  • jaguar1637 2848 days ago

    Yes, It's great and I would like to thank you, Uncle pips and fo course, all members from this forum too, to bring up new stuff and new ideas

    if everyone could bring a little piece like this, strong and reliable solutions would be easily found and realized soon or sooner

  • unclepips 2847 days ago

    I have uploaded a new version that saves indicators.  Of course you can expand the fields saved by adding to the object.  The great thing about MongoDB is you can add to the object, and it will just start saving the extra values for you.  Current object setup is Symbol, Name, Time and Value.  You might want to add Time Frame, but I think I will make that part of the Name in my implementation.  In the sample EA I am only storing IVAR, and 2 different MA's.


    I will now get started on the ENCOG portion of this project.

  • francisfinley 2847 days ago

    yep unclepips is great hes one of the very few people i trust!

  • jaguar1637 2839 days ago


    As far as our test regarding the correlation to price gave us some results, it would be fine if you could try to store HP indicator 's values  first.

    Due to the last message from John Last, telling us, iVAR deletes too often entries, I am thinking about an another indicator, i.e Shanon Entropy.

    instead of MA, it could be better to store Perceptron based with a mix of PFE, WPR, may be fisher ..

    Well, this opportunity is very important

  • unclepips 2839 days ago

    Maybe you could modify the Version I posted that stores Indis, and push it back out to us (or me).  Also any trading rules for trading would be awsome to have.  Right now I am getting ready to create scripts that train based on trading rules - then determine if the trade was profitable.


  • Jasonforex 2832 days ago

    I have named pipes working with C# to MT5 via a custom dll.  I would like to contribute and am also familiar with Encog (jasonforex on heatonresearch as well).  I'm currently trying to catch up on MongoDB.  I'm not a DB guy but this is pretty good stuff I must say.

    Currently, I'm trying to export ticks directly to MongoDB via my dll but keep getting an exception when trying to call from the metatrader platform.  Rough code is below (suggestions appreciated):

    [DllExport("WriteTickToMongo", CallingConvention.StdCall)]
            public static bool WriteTickToMongo(int id, [In, MarshalAs(UnmanagedType.LPWStr)] StringBuilder symbol, long time, double bid, double ask, double last, long volume)
                bool success = false;
                    string connectionString = "mongodb://localhost/?safe=true";
                    MongoServer server = MongoServer.Create(connectionString);
                    MongoDatabase EURUSD_Ticks_Database = server.GetDatabase("EURUSD_Ticks");
                    MongoCollection collection = EURUSD_Ticks_Database.GetCollection<BsonDocument>("ticks");
                    MessageBox.Show("gets to here"); // using to attempt to find error.  code does not reach here.
                    BsonDocument document = new BsonDocument()
                        { "_id", id },
                        { "SYMBOL", symbol.ToString() },
                        { "TIME", time },
                        { "BID", bid },
                        { "ASK", ask },
                        { "LAST", last },
                        { "VOLUME", volume }
                    success = true;
                catch (Exception ex)
                    success = false;
                    MessageBox.Show("Error writing to mongo database: " + ex.Message);
                return success;
  • Jasonforex 2832 days ago

    and calling code from MT5:

         bool WriteTickToMongo(int id, string &symbol, long time, double bid, double ask, double last, ulong volume);
    int id = 1;
    void OnTick()
          string symbol = Symbol();
          long time = SymbolInfoInteger(Symbol(), SYMBOL_TIME);
          double myAsk = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
          double myBid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
          double last = 0;
          long volume = 0;
          Print(id, ", ", symbol, ", ", time, ", ", myBid, ", ", myAsk, ", ", last, ", ", volume);  // prints correctly, updates correctly
          if(WriteTickToMongo(id, symbol, time, myBid, myAsk, last, volume)) // exception happening when WriteTickToMongo is called