//+------------------------------------------------------------------+
//| DUMMYEA-三均线关系.mq4 |
//| Copyright ?2010, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2010, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
extern int LotsPersent= 10;
extern int MaxProfit= 10;//百分比
extern int MaxLoss= 10;//百分比
extern int Slippage = 3;
extern int MagicNumber = 123456789;
extern int MA1 = 13;
extern int MA2 = 34;
extern int MA3 = 55;
int Ticket;
int OrderTime;
bool OrderOpen=false;
double Equity = 0;
double HistoryBuyProfit;
double HistorySellProfit;
double NewHistoryBuyProfit;
double NewHistorySellProfit;
int init()
{
ObjectCreate("comment_label", OBJ_LABEL, 0, 0, 0);
ObjectSet("comment_label", OBJPROP_XDISTANCE, 50);
ObjectSet("comment_label", OBJPROP_YDISTANCE, 20);
ObjectSetText("comment_label", "DUMMYEA-三均线关系", 20, "Arial", Red);
return(0);
}
int deinit()
{
ObjectDelete("comment_label");
return(0);
}
int start()
{
//----
double MA10=iMA(NULL,0,MA1,0,MODE_EMA,PRICE_CLOSE,0);
double MA11=iMA(NULL,0,MA1,0,MODE_EMA,PRICE_CLOSE,1);
double MA20=iMA(NULL,0,MA2,0,MODE_EMA,PRICE_CLOSE,0);
double MA21=iMA(NULL,0,MA2,0,MODE_EMA,PRICE_CLOSE,1);
double MA30=iMA(NULL,0,MA3,0,MODE_EMA,PRICE_CLOSE,0);
double MA31=iMA(NULL,0,MA3,0,MODE_EMA,PRICE_CLOSE,1);
//Print("MA=", MA0);
if (Time[0] != OrderTime && Time[1] != OrderTime) OrderOpen = false;
double Lots =NormalizeDouble(AccountFreeMargin() / 1000.0 * (LotsPersent/ 100.0),1);//AccountFreeMargin() AccountBalance()
int HoldingOrders = 0;
if( OrdersTotal() != 0) {
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber){
HoldingOrders++;
}
}
}
if(HoldingOrders==0)Equity=AccountEquity();
if(MA11<MA21 && MA10>=MA20 && MA10>MA30 && OrderOpen==false){
if(NewLotsCount(OP_SELL)>0){NewHistorySellProfit+=NewHoldingSellProfit(); NewCloseAllSell();}
if(LotsCount(OP_SELL)>0 && NewLotsCount(OP_BUY)==0){
Ticket=OrderSend(Symbol(),OP_BUY,LotsCount(OP_SELL),NormalizeDouble(Ask, Digits),Slippage,0,0,"newdummy-buy",MagicNumber+1,0,Yellow);
}
Ticket=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask, Digits),Slippage,0,0,"dummy-buy",MagicNumber,0,Red);
if(Ticket>0){
if (OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)){
OrderOpen=true;
OrderTime=Time[0];
Print("Pivot dummy-buy order opened : ",OrderOpenPrice());
}
else{
Print("Error opening Pivot dummy-buy order : ",GetLastError());
return(0);
}
}
}else
if(MA11>MA21 && MA10<=MA20 && MA10<MA30 && OrderOpen==false){
if(NewLotsCount(OP_BUY)>0){NewHistoryBuyProfit+=NewHoldingBuyProfit(); NewCloseAllBuy();}
if(LotsCount(OP_BUY)>0 && NewLotsCount(OP_SELL)==0){
Ticket=OrderSend(Symbol(),OP_SELL,LotsCount(OP_BUY),NormalizeDouble(Bid, Digits),Slippage,0,0,"newdummy-sell",MagicNumber+1,0,Green);
}
Ticket=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid, Digits),Slippage,0,0,"dummy-sell",MagicNumber,0,Blue);
if(Ticket>0){
if (OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)){
OrderOpen=true;
OrderTime=Time[0];
Print("Pivot dummy-sell order opened : ",OrderOpenPrice());
}
else{
Print("Error opening Pivot dummy-sell order : ",GetLastError());
return(0);
}
}
}
if(AccountEquity()-Equity>=AccountEquity()*MaxProfit/100){
CloseAllSell();CloseAllBuy(); NewCloseAllSell();NewCloseAllBuy();
NewHistorySellProfit=0; HistoryBuyProfit=0; NewHistoryBuyProfit=0; HistorySellProfit=0;
}
// if(Equity-AccountEquity()>=AccountEquity()*MaxLoss/100){CloseAllSell();CloseAllBuy();}
// CloseAllLossBuy(); CloseAllLossSell();
if(MA11<MA21 && MA10>=MA20)CloseAllWinSell();
if(MA11>MA21 && MA10<=MA20)CloseAllWinBuy();
if(iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,0)<MA11 && iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,0)>MA10)CloseAllWinSell();
if(iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,0)>MA11 && iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,0)<MA10)CloseAllWinBuy();
// if(High[1]<MA11 && High[0]>MA10)CloseAllWinSell();
// if(Low[1]>MA11 && Low[0]<=MA10)CloseAllWinBuy();
if(HoldingBuyProfit()+HistoryBuyProfit+NewHoldingSellProfit()+NewHistorySellProfit > AccountEquity()*MaxProfit/100/2){
CloseAllBuy(); NewCloseAllSell(); NewHistorySellProfit=0; HistoryBuyProfit=0;
}
if(HoldingSellProfit()+HistorySellProfit+NewHoldingBuyProfit()+NewHistoryBuyProfit > AccountEquity()*MaxProfit/100/2){
CloseAllSell(); NewCloseAllBuy(); NewHistoryBuyProfit=0; HistorySellProfit=0;
}
/*
double HistoryBuyProfit;
double HistorySellProfit;
double NewHistoryBuyProfit;
double NewHistorySellProfit;
for (int t=0; t<OrdersTotal(); t++){
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderMagicNumber() == MagicNumber && OrderStopLoss() == 0)
if (OrderType() == OP_BUY && OrderProfit()>50*OrderLots())OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+10*Point,0,0,Yellow);
if (OrderType() == OP_SELL && OrderProfit()>50*OrderLots())OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-10*Point,0,0,Yellow);
}
*/
//----
return(0);
}
//+------------------------------------------------------------------+
void CloseAllSell()
{
bool CAS = FALSE;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber )
CAS = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(OrderClosePrice(), Digits), Slippage, Yellow);
}
}
void CloseAllBuy()
{
bool CAB = FALSE;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber)
CAB = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(OrderClosePrice(), Digits), Slippage, Yellow);
}
}
void CloseAllWinSell()
{
bool CAWS = FALSE;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber && OrderProfit() > 0.0 ){
HistorySellProfit+=OrderProfit();
CAWS = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(OrderClosePrice(), Digits), Slippage, Yellow);
}
}
}
void CloseAllWinBuy()
{
bool CAWB = FALSE;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber && OrderProfit() > 0.0){
HistoryBuyProfit+=OrderProfit();
CAWB = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(OrderClosePrice(), Digits), Slippage, Yellow);
}
}
}
double HoldingBuyProfit()
{
double BuyProfit = 0;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber)
BuyProfit += OrderProfit();
}
return (BuyProfit);
}
double HoldingSellProfit()
{
double SellProfit = 0;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber)
SellProfit += OrderProfit();
}
return (SellProfit);
}
double NewHoldingBuyProfit()
{
double NewBuyProfit = 0;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber+1)
NewBuyProfit += OrderProfit();
}
return (NewBuyProfit);
}
double NewHoldingSellProfit()
{
double NewSellProfit = 0;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber+1)
NewSellProfit += OrderProfit();
}
return (NewSellProfit);
}
double LotsCount(int type)
{
double BuyLots=0;
double SellLots=0;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber )BuyLots+=OrderLots();
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber )SellLots+=OrderLots();
}
switch(type)
{
case OP_BUY: return (BuyLots);
break;
case OP_SELL: return (SellLots);
break;
}
}
double NewLotsCount(int type)
{
double BuyLots=0;
double SellLots=0;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber+1 )BuyLots+=OrderLots();
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber+1 )SellLots+=OrderLots();
}
switch(type)
{
case OP_BUY: return (BuyLots);
break;
case OP_SELL: return (SellLots);
break;
}
}
void NewCloseAllSell()
{
bool CAS = FALSE;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber+1 )
CAS = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(OrderClosePrice(), Digits), Slippage, Yellow);
}
}
void NewCloseAllBuy()
{
bool CAB = FALSE;
for (int t=0; t<OrdersTotal(); t++)
{
OrderSelect(t, SELECT_BY_POS, MODE_TRADES);
if (OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber+1)
CAB = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(OrderClosePrice(), Digits), Slippage, Yellow);
}
}
|