//+------------------------------------------------------------------+ //| SotoumaReceiver.mq4 |Script //| Copyright (c) 2010, alohafx |MT5 to MT4 Trade copier for ATC2010 //| http://alohafx.blog36.fc2.com/ |Use with SotoumaTrancemitter.mq5(EA) //+------------------------------------------------------------------+ #property copyright "Copyright (c) 2010, alohafx" #property link "http://alohafx.blog36.fc2.com/" #property show_inputs #include //Here to * // constants for function _lopen #define OF_READ 0 #define OF_WRITE 1 #define OF_READWRITE 2 #define OF_SHARE_COMPAT 3 #define OF_SHARE_DENY_NONE 4 #define OF_SHARE_DENY_READ 5 #define OF_SHARE_DENY_WRITE 6 #define OF_SHARE_EXCLUSIVE 7 #import "kernel32.dll" int _lopen (string path, int of); int _lcreat (string path, int attrib); int _llseek (int handle, int offset, int origin); int _lread (int handle, string buffer, int bytes); int _lwrite (int handle, string buffer, int bytes); int _lclose (int handle); int CreateDirectoryA(string path, int atrr[]); #import // * here are from xFile.mq4 ( ex. http://articles.mql4.com/720 ) extern string AccountNum = "630191"; extern string BasePath = "D:\\Program Files\\MetaTrader 5-1\\MQL5\\Files\\"; extern int manualTP = 0; extern int manualSL = 0; extern double LotsMultipier = 0.5; //extern double manualLots = 0; extern double minLots = 0.1; extern double maxLots = 15; extern int maxDev = 999; //Max deviation in point extern int Slippage = 3; bool OrderBuy,OrderSell; int i,j,k, ticket, MagicNumber, POS_n_total = 0; double OpenPrice, TakeProfit, StopLoss, TakeP, StopL, Lots; string commstr,SYMBOL,buffer,a= ""; string pos[12],symbol[12],SymbolMirror[12]; int type[12],TypeMirror[12]; double lots[12],op[12],tp[12],sl[12],symbollots[12],DoubleMirror[12][4]; //+------------------------------------------------------------------+ //| script program start function | //+------------------------------------------------------------------+ int start() { if (!IsDllsAllowed()) MessageBox("You need to turn on \'Allow DLL imports\'"); if (AccountNum=="0") MessageBox("AccountNum needed"); Print("SotoumaReceiver is working."); MagicNumber = 201000000000+StrToInteger(AccountNum); SymbolMirror[0]="USDCHF"; SymbolMirror[1]="GBPUSD"; SymbolMirror[2]="EURUSD"; SymbolMirror[3]="USDJPY"; SymbolMirror[4]="USDCAD"; SymbolMirror[5]="AUDUSD"; SymbolMirror[6]="EURGBP"; SymbolMirror[7]="EURAUD"; SymbolMirror[8]="EURCHF"; SymbolMirror[9]="EURJPY"; SymbolMirror[10]="GBPJPY"; SymbolMirror[11]="GBPCHF"; //---- while(!IsStopped()) { int start = GetTickCount(); string path = StringConcatenate(BasePath,"mt524-", AccountNum, ".txt"); buffer=ReadFile(path); int middle = GetTickCount(); int count=StringLen(buffer); // Print("Bytes read:",count); // Print(buffer); WriteFile(path,""); WriteFile(path,"0"); int finish = GetTickCount(); // Print("File size is ",count," bytes. Reading:",(middle-start)," ms. Writing:",(finish-middle)," ms.");///---- if(buffer!=a && buffer!="0") { Back2Double(); FindTrade(); } Comment(buffer,"\n",commstr); a=buffer; Sleep(100); } //---- Comment(""); return(0); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ void Back2Double() { commstr=""; for(i=0; i<12; i++) //initiarize Arrays { pos[i]=""; symbol[i]=""; } ArrayInitialize(type,0); ArrayInitialize(lots,0); ArrayInitialize(op,0); ArrayInitialize(tp,0); ArrayInitialize(sl,0); ArrayInitialize(TypeMirror,-1); ArrayInitialize(DoubleMirror,0); if(buffer=="no_pos") return(0); POS_n_total=StringLen(buffer)/32; for(i=0; i= 0 ; i-- ) { OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); if( OrderMagicNumber() != MagicNumber ) continue; if( OrderSymbol()!=SymbolMirror[k] ) continue; if( OrderType()==0 ) { if( TypeMirror[k]==1 ) overlots=1; ticket=OrderClose(OrderTicket(),NormalizeDouble(OrderLots()*overlots,1),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Violet); if (ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Buy order closed : ",OrderOpenPrice()," TakeProfit:",OrderTakeProfit()," StopLoss:",OrderStopLoss()); } else { Print("Error closing Buy order : ",GetLastError()); } } if( OrderType()==1 ) { if( TypeMirror[k]==0 ) overlots=1; ticket=OrderClose(OrderTicket(),NormalizeDouble(OrderLots()*overlots,1),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Violet); if (ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Sell order closed : ",OrderOpenPrice()," TakeProfit:",OrderTakeProfit()," StopLoss:",OrderStopLoss()); } else { Print("Error closing Sell order : ",GetLastError()); } } } // Print(" cut"+commstr);//"b=",b); } //-----------------counter symbollots[k]=0; for( i = 0 ; i < OrdersTotal() ; i++ ) { if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) break; if( OrderMagicNumber() != MagicNumber ) continue; if( OrderSymbol()==SymbolMirror[k] ) symbollots[k]=symbollots[k]+OrderLots(); // Print(SymbolMirror[k],"1 : ",symbollots[k]); } //2nd order more if the symbol position exists or new if( DoubleMirror[k][0]*LotsMultipier>symbollots[k] ) { if( TypeMirror[k]==0 ) { OrderBuy=true; OrderSell=false; } if( TypeMirror[k]==1 ) { OrderBuy=false; OrderSell=true; } OpenPrice=NormalizeDouble(DoubleMirror[k][1],MarketInfo(SYMBOL,MODE_DIGITS)); Lots=DoubleMirror[k][0]*LotsMultipier-symbollots[k]; //合計ロットが足りない場合増やす。 // Call_MM(); Call_Trade(); // Print(" plus"+commstr);//"b=",b); } //3rd if lots are same, check TP/SL for( i = 0 ; i < OrdersTotal() ; i++ ) { if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) break; if( OrderMagicNumber() != MagicNumber ) continue; if( OrderSymbol()==SymbolMirror[k] ) { if( TakeProfit!=OrderTakeProfit() || StopLoss!=OrderStopLoss() ) { ticket=OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,TakeProfit,0,Blue); if (ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Order modified : ",OrderOpenPrice()," TakeProfit:",TakeProfit," StopLoss:",StopLoss); } else { Print("Error- Order modify failed! : ",GetLastError()); } // Print(" modify"+commstr);//"b=",b); } } } } return(0); } //+------------------------------------------------------------------+ /*void Call_MM() { Lots=Lots*LotsMultipier; // if (LotsMultipier == 0) Lots=manualLots; Lots=MathMin(maxLots,MathMax(minLots,Lots)); if(minLots<0.1) Lots=NormalizeDouble(Lots,2); // 1k else { if(minLots<1) Lots=NormalizeDouble(Lots,1); // 10k else Lots=NormalizeDouble(Lots,0); // 100k } return(0); } */ //+------------------------------------------------------------------+ void Call_Trade() { if ( OrderBuy ) { if(OpenPriceMarketInfo(SYMBOL,MODE_ASK)+maxDev*MarketInfo(SYMBOL,MODE_POINT)) { Print(SYMBOL,", Price away from OpenPrice ",OpenPrice); return(0); } if(TakeProfit>MarketInfo(SYMBOL,MODE_ASK)+MarketInfo(SYMBOL,MODE_STOPLEVEL)*MarketInfo(SYMBOL,MODE_POINT)) TakeP=TakeProfit; if(manualTP>0) TakeP=MarketInfo(SYMBOL,MODE_ASK)+manualTP *MarketInfo(SYMBOL,MODE_POINT); else TakeP=0; if(StopLoss>MarketInfo(SYMBOL,MODE_ASK)-MarketInfo(SYMBOL,MODE_STOPLEVEL)*MarketInfo(SYMBOL,MODE_POINT)) StopL=StopLoss; if(manualSL>0) StopL=MarketInfo(SYMBOL,MODE_ASK)-manualSL *MarketInfo(SYMBOL,MODE_POINT); else StopL=0; ticket=OrderSend(SYMBOL,OP_BUY,Lots,MarketInfo(SYMBOL,MODE_ASK),Slippage,StopL,TakeP,"Buy",MagicNumber,0,Blue); if (ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Buy order opened : ",OrderOpenPrice()," Free Margin = ", AccountFreeMargin()," TakeProfit:",TakeP," StopLoss:",StopL); } else { Print("Error opening Buy order : ",GetLastError()); } } if ( OrderSell ) { if(OpenPrice>MarketInfo(SYMBOL,MODE_BID)+maxDev*MarketInfo(SYMBOL,MODE_POINT) || OpenPriceMarketInfo(SYMBOL,MODE_BID)-MarketInfo(SYMBOL,MODE_STOPLEVEL)*MarketInfo(SYMBOL,MODE_POINT)) TakeP=TakeProfit; if(manualTP>0) TakeP=MarketInfo(SYMBOL,MODE_ASK)+manualTP *MarketInfo(SYMBOL,MODE_POINT); else TakeP=0; if(StopLoss>MarketInfo(SYMBOL,MODE_BID)+MarketInfo(SYMBOL,MODE_STOPLEVEL)*MarketInfo(SYMBOL,MODE_POINT)) StopL=StopLoss; if(manualSL>0) StopL=MarketInfo(SYMBOL,MODE_ASK)-manualSL *MarketInfo(SYMBOL,MODE_POINT); else StopL=0; ticket=OrderSend(SYMBOL,OP_SELL,Lots,MarketInfo(SYMBOL,MODE_BID),Slippage,StopL,TakeP,"Sell",MagicNumber,0,Red); if (ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Sell order opened : ",OrderOpenPrice()," Free Margin = ", AccountFreeMargin()," TakeProfit:",TakeP," StopLoss:",StopL); } else { Print("Error opening Sell order : ",GetLastError()); } } return(0); } //+------------------------------------------------------------------+ /* All below are from xFile.mq4 http://articles.mql4.com/720 */ //+------------------------------------------------------------------+ //| read the file and return a string with its contents | //+------------------------------------------------------------------+ string ReadFile (string path) { int handle=_lopen (path,OF_READ); int read_size = 50; string char50="01234567890123456789012345678901234567890123456789"; if(handle<0) { Print("Error opening file ",path); return (""); } int result=_llseek (handle,0,0); if(result<0) { Print("Error placing the pointer" ); return (""); } string buffer=""; int count=0; int last; result=_lread (handle,char50,read_size); while(result>0 && result == read_size) { buffer=buffer + char50; count++; result=_lread (handle,char50,read_size); // Print("result= ",result," : read_size= ",read_size); //," : gfs= ",gfs); } last = result; // Print("The last read block has the size of, in bytes:", last); char50 = StringSubstr(char50,0,last); buffer = buffer + char50; if(buffer=="01234567890123456789012345678901234567890123456789") buffer=""; // Print(buffer); result=_lclose (handle); if(result!=0) Print("Error closing file ",path); return (buffer); } //+------------------------------------------------------------------+ //| write the buffer contents to the given path | //+------------------------------------------------------------------+ void WriteFile (string path, string buffer) { int count=StringLen (buffer); int result; int handle=_lopen (path,OF_WRITE); if(handle<0) { handle=_lcreat (path,0); if(handle<0) { Print ("Error creating file ",path); if (!CreateFullPath(path)) { Print("Failed creating folder:",path); return; } else handle=_lcreat (path,0); } result=_lclose (handle); handle = -1; } if (handle < 0) handle=_lopen (path,OF_WRITE); if(handle<0) { Print("Error opening file ",path); return; } result=_llseek (handle,0,0); if(result<0) { Print("Error placing the pointer"); return; } result=_lwrite (handle,buffer,count); if(result<0) Print("Error writing to file ",path," ",count," bytes"); result=_lclose (handle); if(result<0) Print("Error closing file ",path); return; } //+------------------------------------------------------------------+ //| It creates all necessary folders and subfolders | //+------------------------------------------------------------------+ bool CreateFullPath(string path) { bool res = false; if (StringLen(path)==0) return(false); Print("Create path=>",path); //---- string folders[]; if (!ParsePath(folders, path)) return(false); Print("Total subfolders:", ArraySize(folders)); int empty[]; int i = 0; while (CreateDirectoryA(folders[i],empty)==0) i++; Print("Create folder:",folders[i]); i--; while (i>=0) { CreateDirectoryA(folders[i],empty); Print("Created folder:",folders[i]); i--; } if (i<0) res = true; //---- return(res); } //+------------------------------------------------------------------+ //| break apart the path into an array of subdfolders | //+------------------------------------------------------------------+ bool ParsePath(string & folder[], string path) { bool res = false; int k = StringLen(path); if (k==0) return(res); k--; Print("Parse path=>", path); int folderNumber = 0; //---- int i = 0; while ( k >= 0 ) { int char = StringGetChar(path, k); if ( char == 92) // back slash "\" { if (StringGetChar(path, k-1)!= 92) { folderNumber++; ArrayResize(folder,folderNumber); folder[folderNumber-1] = StringSubstr(path,0,k); Print(folderNumber,":",folder[folderNumber-1]); } else break; } k--; } if (folderNumber>0) res = true; //---- return(res); } //+------------------------------------------------------------------+EOF