双线MACD金死叉开平仓源码
#property description "MACD金死叉开仓止盈"
#property description "蓝色的泪
#property strict
extern double 下单手数=0.1;
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
extern int 止损点数=0;
extern int 止盈点数=3000;
extern int 最大持仓单数 =100;
extern int 移动止损=0;
extern int magic=1279092;
extern string 注释="macd金死叉EA";
int time=200;
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
}
void OnTick()
{
double macd1_1=iMACD(NULL,0,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,1);
double macd1_2=iMACD(NULL,0,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_MAIN,2);
double macd2_1=iMACD(NULL,0,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_SIGNAL,1);
double macd2_2=iMACD(NULL,0,FastEMA,SlowEMA,SignalSMA,PRICE_CLOSE,MODE_SIGNAL,2);
bool buy,sell;
if(macd1_2<macd2_2&&macd1_1>macd2_1)
{
buy=true;
}
if(macd1_2>macd2_2&&macd1_1<macd2_1)
{
sell=true;
}
double stp_b,stk_b,stp_s,stk_s;
if(止损点数!=0)
{
stp_b=Ask-止损点数*Point;
stp_s=Bid+止损点数*Point;
}
if(止盈点数!=0)
{
stk_b=Ask+止盈点数*Point;
stk_s=Bid-止盈点数*Point;
}
if(移动止损!=0)
{
mstoploss_1(移动止损);
}
int res5;
static datetime opentime1;
if( buy&& opentime1!=Time &&danshu()<=最大持仓单数)
{
res5= OrderSend(Symbol(),OP_BUY,下单手数,Ask,30,stp_b,stk_b,"1",magic,0,Red);
if(res5>0)
{
opentime1=Time;
}
if (res5<=0)
{
Sleep(time);
}
}
if( sell&& opentime1!=Time&&danshu()<=最大持仓单数)
{
res5= OrderSend(Symbol(),OP_SELL,下单手数,Bid,30, stp_s,stk_s,"1",magic,0,Lime);
if(res5>0)
{
opentime1=Time;
}
if (res5<=0)
{
Sleep(time);
}
}
}
int danshu()
{
int tat=0;
for (int i=OrdersTotal()-1; i>=0; i--)
{
if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES)==false ) break;
if ( OrderType()<=1 && OrderSymbol()==Symbol()&& OrderMagicNumber()==magic )
{
tat=tat+1;
}
}
return(tat);
}
bool cheakorders()
{int i;
bool order_switch=true;
if (OrdersTotal( ) > 0 )
{
for(i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if( ( OrderType()==OP_SELL || OrderType()==OP_BUY ) && OrderSymbol()==Symbol()&& OrderMagicNumber()==magic)
{
order_switch=false;
break;
}
}
}
return(order_switch);
}
void closebuy()
{
bool res;
int i;
if (OrdersTotal() > 0 )
{
for(i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderType()==OP_BUY&& OrderSymbol()==Symbol()&& OrderMagicNumber()==magic)
{
res=OrderClose(OrderTicket(),OrderLots(),Bid,30,White);
if(res)
{
}
if (!res)
{
RefreshRates();
Sleep(time);
}
}
}
}
}
void closesell()
{int i;
bool res;
if (OrdersTotal( ) > 0 )
{
for(i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&& OrderMagicNumber()==magic )
{
res=OrderClose(OrderTicket(),OrderLots(),Ask,30,White);
if(res)
{
}
if(!res)
{
RefreshRates();
Sleep(time);
}
}
}
}
}
void mstoploss_1(int stoploss)
{
bool modifys = false;
for (int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if (OrderType()==OP_BUY &&OrderMagicNumber()==magic&& OrderSymbol()==Symbol())
{
if ((Bid -OrderOpenPrice()) > stoploss*MarketInfo(OrderSymbol(), MODE_POINT))
{
if ( (OrderStopLoss() < (Bid -stoploss*MarketInfo(OrderSymbol(), MODE_POINT))) || (OrderStopLoss()==0))
{
modifys = OrderModify(OrderTicket(),
OrderOpenPrice(),
Bid - stoploss*MarketInfo(OrderSymbol(), MODE_POINT),
OrderTakeProfit(),
0,
clrNONE);
}
}// iOpen
}
if ( OrderType()==OP_SELL &&OrderMagicNumber()==magic&& OrderSymbol()==Symbol())
{
if ((OrderOpenPrice() -Ask) > (stoploss*MarketInfo(OrderSymbol(), MODE_POINT)))
{
if ((OrderStopLoss()) > (Ask + stoploss*MarketInfo(OrderSymbol(), MODE_POINT)) || (OrderStopLoss()==0))
{
modifys = OrderModify(OrderTicket(),
OrderOpenPrice(),
Ask + stoploss*MarketInfo(OrderSymbol(),MODE_POINT),
OrderTakeProfit(),
0,
clrNONE);
}
}
}
}
}
bool eg_his(double &lot)
{int i;
bool eg=true;
for(i=OrdersHistoryTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) break;
if( OrderType()==OP_BUY || OrderType()==OP_SELL )
if( OrderMagicNumber()==magic&& OrderSymbol()==Symbol() )
{
lot=OrderLots();
if(OrderProfit()<0 && StringSubstr(OrderComment(),0,4)!="from")
{
eg=false;
}
break;
}
}
return(eg);
}
顶下 支持下 {:1_181:} {:1_179:} 顶下 顶下 谢谢 支持下 支持下