ไฟล์ตัวนี้มันเปิดได้คู่เดียวครับ อยากให้เปิดได้หลายคู่ครับ ไม่รู้แก้อย่างไร ครับ
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
int OpenTradesForMNandPairType(double iMN, string sOrderSymbol)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
// check for opened position, symbol & MagicNumber
if (OrderSymbol()== sOrderSymbol)
{
if (OrderMagicNumber()==iMN)
{
retval++;
}
} // sOrderSymbol
} // for loop
return(retval);
}
int TotalTradesForMNandPairType(string sOrderSymbol)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
// check for opened position, symbol & MagicNumber
if (OrderSymbol()== sOrderSymbol)
{
retval++;
} // sOrderSymbol
} // for loop
return(retval);
}
int CloseTradesForMNandPairType(double iMN, string sOrderSymbol)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersHistoryTotal();
for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_HISTORY);
// check for opened position, symbol & MagicNumber
if (OrderSymbol()== sOrderSymbol)
{
if (OrderMagicNumber()==iMN)
{
retval++;
}
} // sOrderSymbol
} // for loop
return(retval);
}
int CloseAll(double iMN)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
int type = OrderType();
if(type == OP_BUY){
Print(OrderMagicNumber());
if (OrderMagicNumber()==iMN)
{
OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3);
}
}
else{}
} // for loop
return(0);
}
int CloseTrade(double iMN, string cmm)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt = itotal - 1; icnt >= 0 ; icnt--)
//for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
int type = OrderType();
if(type == OP_BUY){
//Print(OrderMagicNumber());
if ((OrderMagicNumber()==iMN)&&(OrderComment()==cmm))
{
OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3);
}
}
else{}
} // for loop
return(0);
}
int CloseTrade1(double iMN, string cmm)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt = itotal - 1; icnt >= 0 ; icnt--)
//for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
int type = OrderType();
if(type == OP_SELL){
if ((OrderMagicNumber()==iMN)&&(OrderComment()==cmm))
{
OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3);
}
}
else{}
} // for loop
return(0);
}
int CloseTrade2(double iMN)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
int type = OrderType();
if(type == OP_BUYSTOP){
if (OrderMagicNumber()==iMN)
{
OrderDelete(OrderTicket());
}
}
else{}
} // for loop
return(0);
}
int CloseTrade3(double iMN)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
int type = OrderType();
if(type == OP_SELLSTOP){
if (OrderMagicNumber()==iMN)
{
OrderDelete(OrderTicket());
}
}
else{}
} // for loop
return(0);
}
int TotalBuy(double iMN, string sOrderSymbol)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
// check for opened position, symbol & MagicNumber
if( (OrderType()==OP_BUY) ) {
if (OrderMagicNumber()==iMN)
{
retval++;
}
} // sOrderSymbol
} // for loop
return(retval);
}
int TotalSell(double iMN, string sOrderSymbol)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
// check for opened position, symbol & MagicNumber
if( (OrderType()==OP_SELL) ) {
if (OrderMagicNumber()==iMN)
{
retval++;
}
} // sOrderSymbol
} // for loop
return(retval);
}
int TotalBuyStop(double iMN, string sOrderSymbol)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
// check for opened position, symbol & MagicNumber
if( (OrderType()==OP_BUYSTOP) ) {
if (OrderMagicNumber()==iMN)
{
retval++;
}
} // sOrderSymbol
} // for loop
return(retval);
}
int TotalSellStop(double iMN, string sOrderSymbol)
{
int icnt, itotal, retval;
retval=0;
itotal=OrdersTotal();
for(icnt=0;icnt<itotal;icnt++) // for loop
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
// check for opened position, symbol & MagicNumber
if( (OrderType()==OP_SELLSTOP) ) {
if (OrderMagicNumber()==iMN)
{
retval++;
}
} // sOrderSymbol
} // for loop
return(retval);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int bs = 0;
int start()
{
datetime tcur = TimeCurrent();
datetime opentime,closetime;
string ordertype;
double max;
double min ;
double lot = Lots;
int spread = MarketInfo(Symbol(),MODE_SPREAD);
double profit = Ask + Point*TakeProfit;
double stoploss = Ask - Point*StopLoss;
double sprofit = Bid - Point*TakeProfit;
double sstoploss = Bid + Point*StopLoss;
int TS = 0;
//double stoploss1 = 0;
if(TakeProfit == 0)
{
profit = 0;
sprofit = 0;
}
if(StopLoss == 0)
{
stoploss = 0;
sstoploss = 0;
}
int zz = 0;
int l3 = 0;
int ticket;
int total,total1;
if(0.01 == MarketInfo(0,MODE_MINLOT))
{
lot = NormalizeDouble(lot,2);
if(lot < 0.01){lot = 0.01;}
else{}
}
else if(0.1 == MarketInfo(0,MODE_MINLOT))
{
lot = NormalizeDouble(lot,1);
if(lot < 0.1){lot = 0.1;}
else{}
}
else if(1.0 == MarketInfo(0,MODE_MINLOT))
{
lot = NormalizeDouble(lot,0);
if(lot < 1.0){lot = 1.0;}
else{}
}
else{}
dshow();
//---
ResetLastError();
int file_handle=FileOpen(fileName,FILE_READ|FILE_TXT|FILE_ANSI);
if(file_handle!=INVALID_HANDLE)
{
int str_size;
string str;
//--- read data from the file
int count = 0;
while(!FileIsEnding(file_handle))
{
str_size=FileReadInteger(file_handle,INT_VALUE);
str=FileReadString(file_handle,str_size);
if(count > 0)
{
string to_split=str;string sep=",";ushort u_sep;string result[];string comment;
u_sep=StringGetCharacter(sep,0);
int k = StringSplit(to_split,u_sep,result);
if(k == 3)
{
opentime = StrToTime(result[1]);
closetime = StrToTime(result[2]);
ordertype = result[0];
comment=result[1];
}
total=OpenTradesForMNandPairType(Magic_Number, Symbol());
if((total < 1)&&(k == 3)&&(tcur >= opentime)&&(tcur <= opentime+300))
{
if((ordertype == "BUY")&&(getlastordercloseprofit() >= 0))
{
OrderSend(Symbol(),OP_BUY,lot,Ask,3,stoploss,profit,comment,Magic_Number,0,Green);
}
else if((ordertype == "SELL")&&(getlastordercloseprofit() >= 0))
{
OrderSend(Symbol(),OP_SELL,lot,Bid,3,sstoploss,sprofit,comment,Magic_Number,0,Red);
}
else if((ordertype == "BUY")&&(getlastordercloseprofit() < 0))
{
OrderSend(Symbol(),OP_BUY,OrderLots()*LotsMultiply,Ask,3,stoploss,profit,comment,Magic_Number,0,Green);
}
else if((ordertype == "SELL")&&(getlastordercloseprofit() < 0))
{
OrderSend(Symbol(),OP_SELL,OrderLots()*LotsMultiply,Bid,3,sstoploss,sprofit,comment,Magic_Number,0,Red);
}
else{}
}
else
{
if((tcur >= closetime))
{
CloseTrade(Magic_Number,comment);
CloseTrade1(Magic_Number,comment);
}
else{}
}
}
count++;
}
FileClose(file_handle);
}
else{PrintFormat("Failed to open %s file, Error code = %d",fileName,GetLastError());}
//----
return(0);
}
//+------------------------------------------------------------------+
void dshow() {
if (ObjectFind("BG") < 0) {
ObjectCreate("BG", OBJ_LABEL, 0, 0, 0);
ObjectSetText("BG", "g", 130, "Webdings", DeepSkyBlue);
ObjectSet("BG", OBJPROP_CORNER, 0);
ObjectSet("BG", OBJPROP_BACK, TRUE);
ObjectSet("BG", OBJPROP_XDISTANCE, 0);
ObjectSet("BG", OBJPROP_YDISTANCE, 15);
}
if (ObjectFind("BG1") < 0) {
ObjectCreate("BG1", OBJ_LABEL, 0, 0, 0);
ObjectSetText("BG1", "g", 130, "Webdings", DimGray);
ObjectSet("BG1", OBJPROP_BACK, FALSE);
ObjectSet("BG1", OBJPROP_XDISTANCE, 0);
ObjectSet("BG1", OBJPROP_YDISTANCE, 42);
}
if (ObjectFind("BG2") < 0) {
ObjectCreate("BG2", OBJ_LABEL, 0, 0, 0);
ObjectSetText("BG2", "g", 130, "Webdings", DimGray);
ObjectSet("BG2", OBJPROP_CORNER, 0);
ObjectSet("BG2", OBJPROP_BACK, TRUE);
ObjectSet("BG2", OBJPROP_XDISTANCE, 0);
ObjectSet("BG2", OBJPROP_YDISTANCE, 42);
}
if (ObjectFind("NAME") < 0) {
ObjectCreate("NAME", OBJ_LABEL, 0, 0, 0);
ObjectSetText("NAME", "Leo Timing EA - " + Symbol(), 9, "Arial Bold", Black);
ObjectSet("NAME", OBJPROP_CORNER, 0);
ObjectSet("NAME", OBJPROP_BACK, FALSE);
ObjectSet("NAME", OBJPROP_XDISTANCE, 5);
ObjectSet("NAME", OBJPROP_YDISTANCE, 23);
}
if (ObjectFind("BG3") < 0) {
ObjectCreate("BG3", OBJ_LABEL, 0, 0, 0);
ObjectSetText("BG3", "g", 110, "Webdings", DimGray);
ObjectSet("BG3", OBJPROP_CORNER, 0);
ObjectSet("BG3", OBJPROP_BACK, TRUE);
ObjectSet("BG3", OBJPROP_XDISTANCE, 0);
ObjectSet("BG3", OBJPROP_YDISTANCE, 73);
}
if (ObjectFind("BG5") < 0) {
ObjectCreate("BG5", OBJ_LABEL, 0, 0, 0);
ObjectSetText("BG5", "g", 130, "Webdings", DimGray);
ObjectSet("BG5", OBJPROP_CORNER, 0);
ObjectSet("BG5", OBJPROP_BACK, FALSE);
ObjectSet("BG5", OBJPROP_XDISTANCE, 0);
ObjectSet("BG5", OBJPROP_YDISTANCE, 73);
}
Comment(""
+ "\n"
+ "\n"
+ "\n"
+ "EXPERT VERSION: 1.0 "
+ "\n"
+ "======================="
//+ "\n"
//+ "-----------------------------------------------------------------------------------"
//+ "\n"
//+ "AUTHENTICATION STATUS"
//+ "\n"
//+ "-----------------------------------------------------------------------------------"
//+ "\n"
//+ "STATUS MESSAGE: " + gs_396
//+ "\n"
//+ "-----------------------------------------------------------------------------------"
+ "\n"
+ "ACCOUNT INFORMATION"
+ "\n"
+ "-----------------------------------------------------"
+ "\n"
//+ "Account Name: " + AccountName()
//+ "\n"
+ "Account Number: " + AccountNumber()
+ "\n"
//+ "Account Type: " + gs_live_380
//+ "\n"
+ "Account Leverage: 1:" + DoubleToStr(AccountLeverage(), 0)
+ "\n"
+ "Account Balance: " + DoubleToStr(AccountBalance(), 2)
+ "\n"
+ "Account Equity: " + DoubleToStr(AccountEquity(), 2)
+ "\n"
+ "Server Time: " + TimeToStr(TimeCurrent(), TIME_SECONDS)
+ "\n"
+ "=======================" + "\n"
+ "Successfully"
);
}
void DrawPriceHHLine(datetime x1, datetime x2, double y1,
double y2, color lineColor, double style)
{
string label = "MACD_DivergenceHHLine.0# " + DoubleToStr(x2, 0);
ObjectDelete(label);
ObjectCreate(label, OBJ_HLINE, 0, x2, y2, 0, 0);
ObjectSet(label, OBJPROP_RAY, 0);
ObjectSet(label, OBJPROP_COLOR, lineColor);
ObjectSet(label, OBJPROP_STYLE, style);
}
double getlastordercloseprofit()
{
for(int i = (OrdersHistoryTotal()-1); i >= 0; i --)
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
if((OrderMagicNumber()==Magic_Number)) {
if((OrderType() == OP_BUY)||(OrderType() == OP_SELL))
{
return(OrderProfit());
}
else{}
}
else{}
}
return(0.0);
}