Last active
August 29, 2015 13:56
-
-
Save caub/9014444 to your computer and use it in GitHub Desktop.
Small MT4 (http://docs.mql4.com/) script for orders copying between 2 accounts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <http51.mqh> | |
extern string url = "http://localhost:8080/orders"; | |
//this scripts sends an http post containing orders info each tick to this url | |
int start() { | |
string params [0,2]; | |
int status[1]; // HTTP Status code | |
int total=OrdersTotal(); | |
ArrayResize( params, 0); // Flush old data | |
for(int i=total-1;pos>=0;pos--) { | |
if(OrderSelect(pos,SELECT_BY_POS)==false) continue; | |
addParam("ticket[]",OrderTicket(),params); | |
addParam("profit[]",OrderProfit(),params); | |
//... | |
} | |
//create URLEncoded string from parameters array | |
string req = ArrayEncode(params); | |
//Send Request | |
string res = httpPOST(url, req, status); | |
if (status[0] != 200){ | |
Print("HTTP:",status[0]," ", res); | |
} | |
return (0); | |
} | |
int init() { | |
Comment("init Receiver"); | |
return (0); | |
} | |
int deinit() { | |
Comment("deinit Receiver"); | |
return (0); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern int maxRetries = 10; | |
static int handle = -1; | |
// the file MT4_Directory/expert/files/orders.csv is used | |
// this code synchronises MT4 orders i.e. all inserted or removed entries in orders.csv | |
// are processed in the local account | |
int init() { | |
Comment("init Receiver"); | |
int h = FileOpen("orders.csv", FILE_WRITE); | |
FileWrite(h, ""); | |
FileClose(h); | |
return (0); | |
} | |
int start() { | |
static int size = -1; | |
string str; | |
while (!IsStopped()) { | |
if (handle == -1) { | |
handle = FileOpen("orders.csv", FILE_CSV | FILE_READ, ','); | |
if (handle > 0) { | |
size = FileSize(handle); | |
Comment("init " + size); | |
} else { | |
Comment("cant find orders.csv"); | |
} | |
} else { | |
Comment("else " + size); | |
int newsize = FileSize(handle); | |
if (newsize != size) { | |
int pos = FileTell(handle); | |
Print(".. ", pos, " ", newsize); | |
while (FileTell(handle) < newsize) { | |
int action = FileReadNumber(handle); | |
Print("action ", action); | |
if (action == 0) { | |
int mn = FileReadNumber(handle); | |
int cmd = FileReadNumber(handle); | |
string symbol = FileReadString(handle); | |
double lots = StrToDouble(FileReadString(handle)); | |
Print("open ", symbol, " ", cmd, " ", lots, " from: ", mn); | |
int ticket = tryOrderSend(mn, symbol, cmd, lots); | |
if (ticket < 0) { | |
Print("OrderSend failed with error #", GetLastError()); | |
} | |
} else if (action == 1) { | |
int tickt = FileReadNumber(handle); | |
for (int i = 0; i < OrdersTotal(); i++) { | |
OrderSelect(i, SELECT_BY_POS); | |
if (OrderMagicNumber() == tickt) { | |
Print("close ", tickt, OrderTicket(), OrderSymbol(), OrderType(), OrderLots()); | |
bool b = tryOrderClose(OrderTicket(), OrderSymbol(), OrderType(), OrderLots()); | |
if (!b) { | |
Print("OrderClose failed with error #", GetLastError()); | |
} | |
break; | |
} | |
} | |
} | |
} | |
size = newsize; | |
} | |
Sleep(20); | |
} | |
} | |
return (0); | |
} | |
int tryOrderSend(int mn, string symbol, int cmd, double lots, int retryCount = 0) { | |
/*double ts = MarketInfo(symbol, MODE_DIGITS+1); | |
Print("before: ", price); | |
price = NormalizeDouble(price, ts); | |
Print("after: ", price); | |
*/ | |
Print("open? m", mn, " s:", symbol, " c", cmd, " l;", lots); | |
double price; | |
RefreshRates(); | |
if (cmd == 0) | |
price = MarketInfo(symbol, MODE_ASK); | |
else | |
price = MarketInfo(symbol, MODE_BID); | |
int ticket = OrderSend(symbol, cmd, lots, price, 10000, 0, 0, "", mn); | |
if (ticket < 0 && retryCount < maxRetries) { | |
Print("OrderSend failed " + price + " with error #", GetLastError()); | |
retryCount++; | |
Sleep(100); | |
return (tryOrderSend(mn, symbol, cmd, lots, retryCount + 1)); | |
} else { | |
return (ticket); | |
} | |
} | |
int tryOrderClose(int ticket, string symbol, int cmd, double lots, int retryCount = 0) { | |
bool ret; | |
double price; | |
RefreshRates(); | |
if (cmd == 0) | |
price = MarketInfo(symbol, MODE_BID); | |
else | |
price = MarketInfo(symbol, MODE_ASK); | |
ret = OrderClose(ticket, lots, price, 5); | |
if (!ret && retryCount < maxRetries) { | |
Print("OrderSend failed " + price + " with error #", GetLastError()); | |
retryCount++; | |
Sleep(100); | |
return (tryOrderClose(ticket, symbol, cmd, lots, retryCount + 1)); | |
} else { | |
return (1); | |
} | |
} | |
int deinit() { | |
if (handle > 0) { | |
//FileWrite(handle, ""); | |
FileClose(handle); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment