//+------------------------------------------------------------------+
//| 加仓模板.mq4 |
//| Copyright 2009, Weewas |
//| 欢迎加我探讨ea QQ:104206237 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2009, Weewas"
#property link "QQ:104206237"
extern double Lots = 0.1;
extern double TakeProfit = 10;
extern double StopProfit = 500;
extern double wind=15;
datetime time=0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CalculateBuyOrders() {
int l_count_8 = 0;
for (int l_pos_12 = 0; l_pos_12 < OrdersTotal(); l_pos_12++) {
if (OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == 12345 && OrderCloseTime()==0 )
{if (OrderType() == OP_BUY) l_count_8++;else l_count_8 = 0;}
}
return (l_count_8);
}
int CalculateSellOrders() {
int l_count_8 = 0;
for (int l_pos_12 = 0; l_pos_12 < OrdersTotal(); l_pos_12++) {
if (OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol()&& OrderMagicNumber() == 54321 && OrderCloseTime()==0)
{if (OrderType() == OP_SELL) l_count_8++;else l_count_8 = 0;}
}
return (l_count_8);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double buyprice() {
double op;
int i, n, ticket;
datetime max=0, x;
n = OrdersTotal();
if (n == 0)return;
for (i=0; i<n; i++) {
if ((OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == 12345 && OrderCloseTime()==0 )== false)continue;
x = OrderOpenTime();
if (x > max) {
ticket = OrderTicket();
max = x;
}
}
if (OrderSelect(ticket, SELECT_BY_TICKET) == false && OrderCloseTime()==0) return;
op = OrderOpenPrice();
return(op);
}
double sellprice() {
double op;
int i, n, ticket;
datetime max=0, x;
n = OrdersTotal();
if (n == 0)return;
for (i=0; i<n; i++) {
if ((OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == 54321)== false&& OrderCloseTime()==0)continue;
x = OrderOpenTime();
if (x > max) {
ticket = OrderTicket();
max = x;
}
}
if (OrderSelect(ticket, SELECT_BY_TICKET) == false&& OrderCloseTime()==0) return;
op = OrderOpenPrice();
return(op);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void profit(double &buyprofit,double &sellprofit)
{
for (int l_pos_12 = 0; l_pos_12 < OrdersTotal(); l_pos_12++) {
if (OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
if (OrderSymbol() == Symbol()&& OrderCloseTime()==0)
{if (OrderMagicNumber() == 54321) sellprofit+=OrderProfit();}
{if (OrderMagicNumber() == 12345) buyprofit+=OrderProfit();}
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CheckForOpen(int type)
{
int l_ticket_104;
int buy_ticket = CalculateBuyOrders();
int sell_ticket= CalculateSellOrders();
if (type > 0)if ( time!=Time[0])
{l_ticket_104 =OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0, 0, "", 12345, 0, Green);
time=Time[0];}
if (type < 0)if ( time!=Time[0])
{l_ticket_104 = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, 0, 0, "", 54321, 0, Red);
time=Time[0];}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CheckForadd(int type)
{
int l_ticket_104;
double point = MarketInfo(Symbol(),MODE_POINT);
if(point==0.00001)point=0.0001;
if(point==0.001)point=0.01;
int buy_ticket = CalculateBuyOrders();
int sell_ticket= CalculateSellOrders();
if (type > 0)if ( time!=Time[0] && buyprice()-Ask>wind*point)
{l_ticket_104 =OrderSend(Symbol(), OP_BUY, Lots*MathPow(2,buy_ticket), Ask, 3, 0, 0, "", 12345, 0, Green);
time=Time[0];}
if (type < 0)if ( time!=Time[0] && Bid-sellprice()>wind*point)
{l_ticket_104 = OrderSend(Symbol(), OP_SELL, Lots*MathPow(2,sell_ticket), Bid, 3, 0, 0, "", 54321, 0, Red);
time=Time[0];}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int Signal_OpenOrder()
{
int x=0;
return(x);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int Signal_OpenAdd()
{
int x=0;
return(x);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int Signal_CloseOrder()
{
int x=0;
return(x);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CloseAll()
{
int l_error_8;
int l_ord_total_0 = OrdersTotal();
if (l_ord_total_0 > 0) {
for (int l_pos_4 = l_ord_total_0 - 1; l_pos_4 >= 0; l_pos_4--) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol()) {
if (OrderType() == OP_BUY) {
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 3, Yellow);
l_error_8 = GetLastError();
if (l_error_8 > 1/* NO_RESULT */) Print("Error message line 47 , error: ", l_error_8);
} else {
if (OrderType() == OP_SELL) {
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 3, Yellow);
l_error_8 = GetLastError();
if (l_error_8 > 1/* NO_RESULT */) Print("Error message line 56 , error: ", l_error_8);
} else {
if (OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP || OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT ) {
OrderDelete(OrderTicket());
l_error_8 = GetLastError();
if (l_error_8 > 1/* NO_RESULT */) Print("Error message line 65 , error: ", l_error_8);
}
}
}
}
}
}
return (0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Closebuy()
{
int l_error_8;
int l_ord_total_0 = OrdersTotal();
if (l_ord_total_0 > 0) {
for (int l_pos_4 = l_ord_total_0 - 1; l_pos_4 >= 0; l_pos_4--) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber()==12345) {
if (OrderType() == OP_BUY ) {
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 3, Yellow);
l_error_8 = GetLastError();
if (l_error_8 > 1) Print("Error message line 47 , error: ", l_error_8);
}
}
}
}
return (0);
}
void Closesell()
{
int l_error_8;
int l_ord_total_0 = OrdersTotal();
if (l_ord_total_0 > 0) {
for (int l_pos_4 = l_ord_total_0 - 1; l_pos_4 >= 0; l_pos_4--) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber()==54321) {
if (OrderType() == OP_SELL ) {
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 3, Yellow);
l_error_8 = GetLastError();
if (l_error_8 > 1) Print("Error message line 47 , error: ", l_error_8);
}
}
}
}
return (0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void start()
{
double buyprofit,sellprofit;
profit(buyprofit,sellprofit);
int li_0 = CalculateBuyOrders() + CalculateSellOrders();
int li_4 = CalculateSellOrders();
int li_8 = CalculateBuyOrders();
if (li_4 == 0 && Signal_OpenOrder()==-1) CheckForOpen(-1);
if (li_4 > 0 && Signal_OpenAdd() ==-1) CheckForadd (-1);
if (li_8 == 0 && Signal_OpenOrder()== 1) CheckForOpen( 1);
if (li_8 > 0 && Signal_OpenAdd() == 1) CheckForadd ( 1);
if (li_4 > 0 && Signal_CloseOrder()==-1) Closesell();
if (li_8 > 0 && Signal_CloseOrder()== 1) Closebuy();
if((buyprofit+sellprofit)>TakeProfit)CloseAll();
if((buyprofit>=TakeProfit ) || buyprofit <-StopProfit)Closebuy();
if((sellprofit>=TakeProfit) || sellprofit<-StopProfit)Closesell();
return(0);
}