Lets start coding a simple Trade Manager together. It shall display all open positions on chart and allow you to close them with 1 simple click. Lets develop this as an EA because EAs are designed to manage with Trades.
Lets create a new Expert Advisor template and start with following simple code:
int init() { ObjectsDeleteAll(); hwnd = WindowHandle(Symbol(),Period()); // Lets remove all Objects from Chart before we start guiRemoveAll(hwnd); return(0); }
This is kind of classic start.
We will use button objects for this very simple Trade Manager EA. Of course we must generate the buttons dynamically based on Trades activate. Its good to make Button Width and Height configurable thats why we should define global variables like:
// GUI Object Handles // Settings int GUIX = 50; int GUIY = 100; int ButtonWidth = 200; int ButtonHeight = 25; int ButtonDistance = 5; // Array Buffer to store buttons int Buttons[100]; // Lets support maximum 100 trades int Orders[100]; // Lets store Ticker IDs in this array
These button configuration parameters can be made external or kept global internal, you can define this yourself. As you see we want to support maximum 100 buttons at once. Buttons[] Array will be used to store button gui handles and Orders[] Array will be used to store the Order Ticked corresponding to index.
In BuildInterface() function we enumarate all available trades, filter only BUY & SELL ones (Market Trades) which belongs to current Symbol and draw the buttons on Chart programatically. As you see we save the button gui Handles inside Button[] Array and Order Ticket inside Orders[] Array. We will need them later!
Its also important to colorize buttons depending on direction of trade, BUY or SELL.
void BuildInterface() { // Lets remove everything before redrawing guiRemoveAll(hwnd); ArrayInitialize(Buttons,0); int x=GUIX,y=GUIY,idx=0; // Lets Enumerate all open trades for(int i=0;i<OrdersTotal();i++) { if (OrderSelect(i,SELECT_BY_POS)) if (OrderSymbol()==Symbol()) if (idx<100) if (OrderType()==OP_BUY || OrderType()==OP_SELL) { // Create a button Buttons[idx]=guiAdd(hwnd,"button",x,y,ButtonWidth,ButtonHeight ,"Close #"+OrderTicket()+" x "+DoubleToStr(OrderLots(),2)); // Lets remember OrderID by Index Orders[idx]=OrderTicket(); // Colorize the buttons based on LONG or SHORT Position if (OrderType()==OP_BUY) guiSetBgColor(hwnd,Buttons[idx],Lime); else guiSetBgColor(hwnd,Buttons[idx],Tomato); // Lets move the x,y to keep the interface moving y+=ButtonHeight+ButtonDistance; idx++; } } }
As you see in code we do also play a sound by closing the trades.
Of course our code must detect new trades automatically. Here is a simple way todo this: of course this is not 100% secure way but it works for our basic case.
// Lets detect Change in Orders and rebuild interface if (LastKnownOrdersCount!=OrdersTotal()) { BuildInterface(); LastKnownOrdersCount=OrdersTotal(); }
As you see in code we compare Last Known amount of OrdersTotal() and current OrdersTotal(), if there is a discrepancy we assume there is some change in Orders and Rebuild the Interface and save current OrdersTotal state in our global variable LastKnownOrdersCount.
There is not much to say about this part:
int deinit() { // Very important to cleanup and remove all gui items from chart if (hwnd>0) { guiRemoveAll(hwnd); guiCleanup(hwnd ); } return(0); }
Now we have all parts together. See it in action:
Please use Downloads Area to Download all parts, search for mt4gui-SimpleTM.mq4
Comments are closed