//+------------------------------------------------------------------+
//| TEST.mq4 |
//| Copyright © 2012, MetaQuotes Software Corp. |
//| _http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
extern double lot = 1.0;
extern int step = 10;
extern string first = "EURUSD";
extern string second = "GBPUSD";
extern int slippage = 5;
int mapOrders[];
int init()
{
initMapOrders();
return(0);
}
int start()
{
info();
doTrading();
//info();
return(0);
}
void doTrading()
{
int profit1=0, profit2=0;
string type="";
int profitTotal=0;
if(OrdersTotal() == 0)
{
openNewPiar();
initMapOrders();
return;
}
for(int i = 0; i<ArraySize(mapOrders); i++)
{
if(OrderSelect(mapOrders,SELECT_BY_TICKET))
profitTotal = profitTotal + OrderProfit()/OrderLots();
i++;
if(OrderSelect(mapOrders,SELECT_BY_TICKET))
profitTotal = profitTotal + OrderProfit()/OrderLots();
}
if(OrderSelect(mapOrders[ArraySize(mapOrders)-1],SELECT_BY_TICKET))
{
profit1 = OrderProfit()/OrderLots();
}
else
Print("Error ", GetLastError());
if(OrderSelect(mapOrders[ArraySize(mapOrders)-2],SELECT_BY_TICKET))
{
profit2 = OrderProfit()/OrderLots();
}
else
Print("Error ", GetLastError());
//Comment(profit1 +" "+ profit2);
if (profitTotal > step )
{
Print("closeAll()");
closeAll();
}
else
{
if((profit1 + profit2) > step)
{
Print("closeLastProfit() && openNewPiar()");
closeLastProfit();
openNewPiar();
}
}
initMapOrders();
}
void openNewPiar()
{
OrderSend(first, OP_SELL,lot,MarketInfo(first,MODE_BID),5,0,0,"",0,0,Red);
OrderSend(second,OP_BUY, lot,MarketInfo(second,MODE_ASK),5,0,0,"",0,0,Blue);
OrderSend(first, OP_BUY, lot,MarketInfo(first,MODE_ASK),5,0,0,"",0,0,Blue);
OrderSend(second, OP_SELL,lot,MarketInfo(second,MODE_BID),5,0,0,"",0,0,Red);
}
void closeLastProfit()
{
int pos = ArraySize(mapOrders);
int ticket;
ticket = mapOrders[pos-1];
OrderSelect(ticket,SELECT_BY_TICKET);
// Print(pos-1, " ", ticket);
// Print(OrderTicket(), " ", OrderLots()," ", MarketInfo(OrderSymbol(),MODE_BID));
closeOrders();
ticket = mapOrders[pos-2];
OrderSelect(ticket,SELECT_BY_TICKET);
// Print(pos-2, " ", ticket);
// Print(OrderTicket(), " ", OrderLots()," ", MarketInfo(OrderSymbol(),MODE_BID));
closeOrders();
}
void closeOrders()
{
if(OrderType()== OP_BUY)
{
if(OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),slippage,Blue))
Print("??????");
else
Print(GetLastError());
}
if(OrderType()== OP_SELL)
{
if(OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),slippage,Red))
Print("??????");
else
Print(GetLastError());
}
}
void closeAll()
{
for(int i = 0; i<ArraySize(mapOrders); i++)
{
OrderSelect(mapOrders,SELECT_BY_TICKET);
closeOrders();
}
}
void info()
{
string s = "\n";
int profit1=0, profit2=0;
string type;
int profitTotal;
for(int i = 0; i<ArraySize(mapOrders); i++)
{
if(OrderSelect(mapOrders,SELECT_BY_TICKET))
{
profit1 = OrderProfit()/OrderLots();
profitTotal = profitTotal + profit1;
if(OrderType()==1) type = "Sell"; else type = "Buy";
}
else
Print("Error ", GetLastError());
s = s + " " + mapOrders +" "+ profit1 + " " + type +"\n";
i++;
if(OrderSelect(mapOrders,SELECT_BY_TICKET))
{
profit2 = OrderProfit()/OrderLots();
profitTotal = profitTotal + profit2;
if(OrderType()==1) type = "Sell"; else type = "Buy";
}
else
Print("Error ", GetLastError());
s = s + " " + mapOrders +" "+ profit2 + " " + type + " "+ (profit1 + profit2)+ "\n";
s = s + " _________________________________________________________" + "\n";
//s = s+ "\n";
}
s = s + " Total point " + profitTotal +"\n";
Comment(s);
}
void initMapOrders()
{
int idx=0;
ArrayResize(mapOrders, OrdersTotal());
for(int i=0 ;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS);
// if(OrderSymbol() == first || OrderSymbol() == second)
// {
mapOrders = OrderTicket();
// idx++;
// }
//Print(i," ",mapOrders);
}
if(OrdersTotal() > 0)
ArraySort(mapOrders);//, WHOLE_ARRAY,0,MODE_ASCEND);
info();
}