جزاك الله خير صحيح 100%اقتباس:
استاذ اسامه
عدلت على الكود والان يعمل بشكل جيد
هل التعديل صحيح
كان
اصبحكود PHP:if(type>OP_SELL && order_OP!=Order_Price)
كود PHP:if(type>OP_SELL && (order_OP!=Order_Price || order_SL!=Order_SL || order_TP!=Order_TP))
كود السلاف كامل بعد التعديل
كود PHP://+------------------------------------------------------------------+
//| |
//| |
//| www.arabictrader.com/vb |
//| |
//| [email protected] |
//+------------------------------------------------------------------+
#property copyright "MR.dollarEA"
#property link "[email protected]"
#import "kernel32.dll"
int CreateFileW(string Filename,uint AccessMode,int ShareMode,int PassAsZero,int CreationMode,int FlagsAndAttributes,int AlsoPassAsZero);
int ReadFile(int FileHandle,uchar &Buffer[],int BufferLength,int &BytesRead[],int PassAsZero);
int SetFilePointer(int FileHandle,int Distance,int PassAsZero,int FromPosition);
int GetFileSize(int FileHandle,int PassAsZero);
int CloseHandle(int FileHandle);
#import
#define GENERIC_READ 0x80000000
#define WIN32_FILE_SHARE_READ 1
#define WIN32_FILE_SHARE_WRITE 2
#define OPEN_EXISTING 3
#define SEEK_FILE_BEGIN 0
#define SEEK_FILE_CURRENT 1
#define INVALID_HANDLE_VALUE -1
#define READ_BUFFER_SIZE 1024
input bool ECN_Broker=false;
input string Master_Path="";
input int MaxPipsDifference=10;
input string info1="Lot Settings ";
input bool AutoLotPercent=true;
input double MinimumLot=0.1;
input double MultiLotNumber=1;
input bool CustomLot=false;
input double Lots=0.1;
input bool MoneyManagment=false;
input double Risk=2.0;
input int OrderExpirationMinutes=2;
input string info2=" Order Managment Settings ";
input bool CopyCloseOrder=true;
input bool CopyOrdersModifications=true;
input bool CopyTakeProfit=true;
input int ManualTakeProfit=0;
input bool CopyStopLoss=true;
input int ManualStopLoss=0;
input int MagicNumber=11111;
input bool Show_Panel=true;
int Lot_Decimal=1;
int P;
string OrdersInfo[];
datetime StartTime;
string prefix;
string status="Waiting";
int LastReadedLine;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
if(StringLen(Symbol())>6)prefix=StringSubstr(Symbol(),6,0);
StartTime=TimeCurrent();
if(Digits==5||Digits==3)P=10;
else P=1;
if(MarketInfo(Symbol(),MODE_MINLOT)<0.1)Lot_Decimal=2;
if(Show_Panel)
{
CreatePanel("PPanel_info_Rec1",OBJ_RECTANGLE_LABEL,"",5,20,220,210,Black,Red,Blue,8);
DrawObjects();
}
LastReadedLine=0;
return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
for(int i=ObjectsTotal()-1;i>=0;i--)
{
string namee=ObjectName(i);
if(StringFind(namee,"PPanel",0)>=0)ObjectDelete(namee);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OrderIsOpened(int ticket)
{
int i;
for(i=OrdersTotal()-1;i>=0;i--)
{
bool select=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderComment()==IntegerToString(ticket))return(OrderTicket());
}
for(i=OrdersHistoryTotal()-1;i>=0;i--)
{
select=OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(OrderComment()==IntegerToString(ticket))return(OrderTicket());
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
if(!IsDllsAllowed())
{
Alert("Make Sure DLL Import is Allowed");
return;
}
if(Master_Path=="")
{
Alert("Make Sure you set Correct Master Path");
status="Error";
return;
}
double TP,SL;
while(IsStopped()==false)
{
status="Connected";
//Comment("Server Time : "+);
Sleep(300);
RefreshRates();
int handlee=OpenExistingFileForReading(Master_Path+"\\MQL4\\Files\\CopierMaster.txt",true,true);
if(IsValidFileHandle(handlee))
{
int NewSize=0;
ArrayResize(OrdersInfo,NewSize);
while(!IsFileAtEnd(handlee))
{
string line=ReadLineFromFile(handlee,"\n");
NewSize++;
ArrayResize(OrdersInfo,NewSize);
OrdersInfo[NewSize-1]=line;
// Alert(line);
}
CloseFile(handlee);
}
else
{
status="Error";
Alert("Error Open File");
}
for(int i=LastReadedLine;i<NewSize;i++)
{
LastReadedLine++;
int PreviousCounted=0;
//int index;
int Order_Ticket,Order_Type,Action_Type;
string Order_Symbol="";
double Order_Price,Order_SL,Order_TP,Order_Lots,Order_Time,Order_Time_GMT,Balance;
Get_Data(OrdersInfo[i]);
Action_Type=StringToInteger(Order_Data[10]);
int check_Order_Opened=OrderIsOpened(StringToInteger(Order_Data[0]));
if(check_Order_Opened!=-1 && Action_Type==0)continue;// check if the order is opened then go to next order
Order_Ticket=StringToInteger(Order_Data[0]);
Order_Symbol=Order_Data[1]+prefix;
Order_Type=StringToInteger(Order_Data[2]);
Order_Lots=StrToDouble(Order_Data[3]);
Order_Price=StrToDouble(Order_Data[4]);
Order_SL=StrToDouble(Order_Data[5]);
Order_TP=StrToDouble(Order_Data[6]);
Order_Time=StrToDouble(Order_Data[7]);
Order_Time_GMT=StrToDouble(Order_Data[8]);
Balance=StrToDouble(Order_Data[9]);
if(Action_Type==0)
{
if(Order_Ticket!=0 && MathAbs(Order_Time_GMT-TimeGMT())<=OrderExpirationMinutes*60)
{
status="Working";
RefreshRates();
double point=MarketInfo(Order_Symbol,MODE_POINT)*P;
if(Order_Type==0 && MathAbs(Order_Price-MarketInfo(Order_Symbol,MODE_ASK))<=MaxPipsDifference*point)
{
Order_Price=MarketInfo(Order_Symbol,MODE_ASK);
}
if(Order_Type==1 && MathAbs(Order_Price-MarketInfo(Order_Symbol,MODE_BID))<=MaxPipsDifference*point)
{
Order_Price=MarketInfo(Order_Symbol,MODE_BID);
}
if(CustomLot)Order_Lots=Lots;
else if(MoneyManagment)Order_Lots=LotManage();
else if(AutoLotPercent)Order_Lots=MathMax(CalculateLotPercent(Balance,Order_Lots),MinimumLot)*MultiLotNumber;
if(CopyTakeProfit)
{
TP=Order_TP;
}
else
{
if(ManualTakeProfit==0){TP=0;}else {TP=Order_Price+ManualTakeProfit*GetTP(Order_Type)*point;}
}
if(CopyStopLoss)
{
SL=Order_SL;
}
else
{
if(ManualStopLoss==0){SL=0;}else {SL=Order_Price+ManualStopLoss*GetSL(Order_Type)*point;}
}
double ECN_SL,ECN_TP;
if(ECN_Broker){ECN_SL=SL;ECN_TP=TP;SL=0;TP=0;}
int ticket=OrderSend(Order_Symbol,Order_Type,Order_Lots,Order_Price,3*P,SL,TP,IntegerToString(Order_Ticket),MagicNumber,0,White);
if(ECN_Broker && SL!=0 && TP!=0 && ticket>0)
{
bool modify=OrderModify(ticket,Order_Price,ECN_SL,ECN_TP,0);
}
if(ticket<0)Print("Error Open Order : "+GetLastError());
}
}
else if(Action_Type==1 && check_Order_Opened>0)
{
if(OrderSelect(check_Order_Opened,SELECT_BY_TICKET,MODE_TRADES))
{
int type=OrderType();
double order_OP=OrderOpenPrice();
double order_SL=OrderStopLoss();
double order_TP=OrderTakeProfit();
double order_Exp=OrderExpiration();
if(type>OP_SELL && (order_OP!=Order_Price || order_SL!=Order_SL || order_TP!=Order_TP))
{
modify=OrderModify(OrderTicket(),Order_Price,Order_SL,Order_TP,order_Exp);
if(!modify)Alert("Modify Order Error: "+GetLastError());
}
else if(type<=OP_SELL && (order_SL!=Order_SL || order_TP!=Order_TP))
{
modify=OrderModify(OrderTicket(),order_OP,Order_SL,Order_TP,0);
if(!modify)Alert("Modify Order Error: "+GetLastError());
}
}
}
else if(CopyCloseOrder && Action_Type==2 && check_Order_Opened>0)
{
if(OrderSelect(check_Order_Opened,SELECT_BY_TICKET,MODE_TRADES))
{
string symbol=OrderSymbol();
RefreshRates();
double ask=MarketInfo(symbol,MODE_ASK);
double bid=MarketInfo(symbol,MODE_BID);
if(OrderType()==OP_BUY)bool action=OrderClose(OrderTicket(),OrderLots(),bid,3*P);
if(OrderType()==OP_SELL)action=OrderClose(OrderTicket(),OrderLots(),ask,3*P);
if(OrderType()>OP_SELL)action=OrderDelete(OrderTicket());
if(!action)Alert("Close Order Error: "+GetLastError());
}
}
}
my_time=TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS);
Comment("Server Time: "+TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS));
if(Show_Panel)
DrawObjects();
}
}
string my_time;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string Order_Data[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Get_Data(string data)
{
ushort sep=StringGetCharacter("|",0);
StringSplit(data,sep,Order_Data);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CopyAutoClose(string &array[])
{
bool OrderIsClosed;
bool action;
for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
bool select=OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
int Order_Ticket=OrderTicket();
string Order_Comment=OrderComment();
int type=OrderType();
double lots=OrderLots();
string symbol=OrderSymbol();
if(OrderOpenTime()<StartTime)continue;
if(OrderMagicNumber()==MagicNumber)
{
OrderIsClosed=true;
for(int i=0;i<ArraySize(array);i++)
{
for(int j=0;j<StringLen(array[i]);j++)
{
if(StringSubstr(array[i],j,1)=="|")
{
string ticket=StringSubstr(array[i],0,j);
if(Order_Comment==ticket)OrderIsClosed=false;
break;
}
}
if(!OrderIsClosed)break;
}
if(OrderIsClosed)
{
RefreshRates();
double ask=MarketInfo(symbol,MODE_ASK);
double bid=MarketInfo(symbol,MODE_BID);
if(type==OP_BUY)action=OrderClose(Order_Ticket,lots,bid,3*P);
if(type==OP_SELL)action=OrderClose(Order_Ticket,lots,ask,3*P);
if(type>OP_SELL)action=OrderDelete(Order_Ticket);
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetTP(int type)
{
if(type==OP_BUY || type==OP_BUYLIMIT || type==OP_BUYSTOP)
{
return(1);
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetSL(int type)
{
if(type==OP_BUY || type==OP_BUYLIMIT || type==OP_BUYSTOP)
{
return(-1);
}
return(1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double LotManage()
{
double lot=MathCeil(AccountFreeMargin() *Risk/1000)/100;
if(lot<MarketInfo(Symbol(),MODE_MINLOT))lot=MarketInfo(Symbol(),MODE_MINLOT);
if(lot>MarketInfo(Symbol(),MODE_MAXLOT))lot=MarketInfo(Symbol(),MODE_MAXLOT);
return (NormalizeDouble(lot,Lot_Decimal));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double CalculateLotPercent(double balance,double lots)
{
double myLot=(AccountBalance()/balance)*lots;
return(myLot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OpenExistingFileForReading(string FileName,bool ShareForReading=true,bool ShareForWriting=false)
{
int ShareMode=0;
if(ShareForReading) ShareMode += WIN32_FILE_SHARE_READ;
if(ShareForWriting) ShareMode += WIN32_FILE_SHARE_WRITE;
return (CreateFileW(FileName, GENERIC_READ, ShareMode, 0, OPEN_EXISTING, 0, 0));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string ReadLineFromFile(int FileHandle,string Terminator="\r\n")
{
// Holds the line which is eventually returned to the caller
string Line="";
// Keep track of the file pointer before we start doing any reading
int InitialFilePointer=SetFilePointer(FileHandle,0,0,SEEK_FILE_CURRENT);
// Keep reading from the file until we get the end of the line, or the end of the file
bool bContinueRead=true;
while(bContinueRead)
{
// Receives the number of bytes read from the file. Note that MQL can only pass
// arrays as by-reference parameters to DLLs
int BytesRead[1]={0};
// Allocate a buffer
uchar ReadBuffer[];
ArrayResize(ReadBuffer,READ_BUFFER_SIZE+1);
// Do a read of up to READ_BUFFER_SIZE bytes
ReadFile(FileHandle,ReadBuffer,READ_BUFFER_SIZE,BytesRead,0);
// Check whether any data has been read...
if(BytesRead[0]!=0)
{
// Add the new data to the line we've built so far
Line=StringConcatenate(Line,CharArrayToString(ReadBuffer,0,BytesRead[0]));
// Does the line now contain the specified terminator?
int pFindTerminator = StringFind(Line, Terminator);
if(pFindTerminator != -1)
{
// The line does contain the specified terminator. Remove it from the data we're
// going to pass back to the caller
Line=StringSubstr(Line,0,pFindTerminator);
// We've almost certainly read too much data - i.e. the latest 200 byte block
// intrudes into the next line. Need to adjust the file pointer to the start
// of the next line. This must be the file pointer before we started reading, plus
// the length of the line we've read, plus the length of the terminator
SetFilePointer(FileHandle,InitialFilePointer+StringLen(Line)+StringLen(Terminator),0,SEEK_FILE_BEGIN);
// Stop reading
bContinueRead=false;
} else {
// The line read so far does not yet contain the specified terminator
bContinueRead=true;
}
} else {
// Either at the end of the file, or the file handle is not valid for reading
bContinueRead=false;
}
}
return (Line);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double LastOrderInfo(string index)
{
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
{
bool select=OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(index=="P")return(OrderProfit());
if(index=="L")return(OrderLots());
}
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsFileAtEnd(int FileHandle)
{
int CurrentFilePointer=SetFilePointer(FileHandle,0,0,SEEK_FILE_CURRENT);
return (CurrentFilePointer >= GetFileSize(FileHandle, 0));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsValidFileHandle(int FileHandle)
{
return (FileHandle != INVALID_HANDLE_VALUE);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseFile(int FileHandle)
{
CloseHandle(FileHandle);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int TotalOrders(int type)
{
int cnt=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()==type && OrderMagicNumber()==MagicNumber)
{
cnt++;
}
}
}
return(cnt);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DrawObjects()
{
int X_Shift=10,Y_Shift=20;
int Added=25;
RefreshRates();
CreatePanel("PPanel_Info_S3",OBJ_EDIT,"Server Time: ",X_Shift,Added+Y_Shift,80,20,Black,White,Black,9,true,false,0,ALIGN_LEFT);
CreatePanel("PPanel_Info_S4",OBJ_EDIT,my_time,100+X_Shift,Added+Y_Shift,110,20,Black,White,Red,8,true,false,0,ALIGN_CENTER);Added+=25;
CreatePanel("PPanel_Info_S5",OBJ_EDIT,"Copier Status: ",X_Shift,Added+Y_Shift,90,20,Black,White,Black,9,true,false,0,ALIGN_LEFT);
CreatePanel("PPanel_Info_S6",OBJ_EDIT,status,100+X_Shift,Added+Y_Shift,110,20,Black,White,Red,8,true,false,0,ALIGN_CENTER);Added+=25;
CreatePanel("PPanel_Info_Balance1",OBJ_EDIT,"Balance",X_Shift,Added+Y_Shift,80,20,Black,White,Black,10,true,false,0,ALIGN_LEFT);
CreatePanel("PPanel_Info_Balance2",OBJ_EDIT,DoubleToStr(AccountBalance(),2),100+X_Shift,Added+Y_Shift,110,20,Black,White,Red,9,true,false,0,ALIGN_CENTER);Added+=25;
CreatePanel("PPanel_Info_B1",OBJ_EDIT,"Total Copied Buy :",X_Shift,Added+Y_Shift,100,20,Black,White,Black,8,true,false,0,ALIGN_LEFT);
CreatePanel("PPanel_Info_B2",OBJ_EDIT,DoubleToStr(TotalOrders(OP_BUY),0),120+X_Shift,Added+Y_Shift,70,20,Black,White,Red,10,true,false,0,ALIGN_CENTER);Added+=25;
CreatePanel("PPanel_Info_S1",OBJ_EDIT,"Total Copied Sell :",X_Shift,Added+Y_Shift,100,20,Black,White,Black,8,true,false,0,ALIGN_LEFT);
CreatePanel("PPanel_Info_S2",OBJ_EDIT,DoubleToStr(TotalOrders(OP_SELL),0),120+X_Shift,Added+Y_Shift,70,20,Black,White,Red,10,true,false,0,ALIGN_CENTER);Added+=35;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CreatePanel(string name_,ENUM_OBJECT Type,string text,int XDistance,int YDistance,int Width,int Hight,
color BGColor_,color InfoColor,color boarderColor,int fontsize,bool readonly=false,bool Obj_Selectable=false,int Corner=0,ENUM_ALIGN_MODE Align=ALIGN_LEFT)
{
if(ObjectFind(0,name_)==-1)
{
ObjectCreate(0,name_,Type,0,0,0);
ObjectSetInteger(0,name_,OBJPROP_XDISTANCE,XDistance);
ObjectSetInteger(0,name_,OBJPROP_YDISTANCE,YDistance);
ObjectSetInteger(0,name_,OBJPROP_XSIZE,Width);
ObjectSetInteger(0,name_,OBJPROP_YSIZE,Hight);
ObjectSetString(0,name_,OBJPROP_TEXT,text);
ObjectSetString(0,name_,OBJPROP_FONT,"Arial Bold");
ObjectSetInteger(0,name_,OBJPROP_FONTSIZE,fontsize);
ObjectSetInteger(0,name_,OBJPROP_CORNER,Corner);
ObjectSetInteger(0,name_,OBJPROP_COLOR,InfoColor);
ObjectSetInteger(0,name_,OBJPROP_BORDER_TYPE,BORDER_FLAT);
ObjectSetInteger(0,name_,OBJPROP_BORDER_COLOR,boarderColor);
ObjectSetInteger(0,name_,OBJPROP_BGCOLOR,BGColor_);
ObjectSetInteger(0,name_,OBJPROP_SELECTABLE,Obj_Selectable);
if(Type==OBJ_EDIT)
{
ObjectSetInteger(0,name_,OBJPROP_ALIGN,Align);
ObjectSetInteger(0,name_,OBJPROP_READONLY,readonly);
}
}
else if(ObjectGet(name_,OBJPROP_TEXT)!=text || ObjectDescription(name_)!=text)
{
ObjectSetString(0,name_,OBJPROP_TEXT,text);
}
}
//+------------------------------------------------------------------+

