【MetaTrader编程入门教程】---指标文件构成
小明学外汇外汇技术面 2019-10-21 14:49:19 交流微信号:FX263cn MetaTrader编程入门
Sleep(5000);
}
}
}
}
//判断是否买或卖或平仓
int buyorsell() //在这个函数计算设置你的交易信号
{
double MacdCurrent, MacdPrevious, SignalCurrent;
double SignalPrevious, MaCurrent, MaPrevious;
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);
if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious && MaCurrent>MaPrevious)
return (1); // 买 Ma在上升,Macd在0线上,并且两线上交叉
if(MacdCurrent>0 && MacdCurrentSignalPrevious
&& MaCurrent return (-1); // 卖
return (0); //不交易
}
int nowbuyorsell = 0;
void CheckForOpen()
{
if (Time[0] == lasttime ) return; //每时间周期检查一次
lasttime = Time[0];
nowbuyorsell = buyorsell(); //获取买卖信号
if (nowbuyorsell == 1) //买 先结束已卖的
CloseSell();
if (nowbuyorsell == -1) //卖 先结束已买的
CloseBuy();
if (TimeDayOfWeek(CurTime()) == 1)
{
if (TimeHour(CurTime()) < 3 ) return; //周一早8点前不做
}
if (TimeDayOfWeek(CurTime()) == 5)
{
if (TimeHour(CurTime()) > 19 ) return; //周五晚11点后不做
}
if (OrdersTotal( ) >= maxOpen) return ;
//如果已持有开仓数达到最大,不做
if (nowbuyorsell==0) return; //不交易
TradeOK(); //去下单交易
}
void TradeOK() //去下单交易
{
int error ;
if (nowbuyorsell == 1) //买
{
switch (whichmethod)
{
case 1: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);break;
case 2: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-StopLoss*Point,0,"",MAGICMA,0,Blue); break;
case 3: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,Ask+TakeProfit*Point,"",MAGICMA,0,Blue);break;
case 4: res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",MAGICMA,0,Blue);break;
default : res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);break;
}
if (res <=0)
{
error=GetLastError();
if(error==134)Print("Received 134 Error after OrderSend() !! "); // not enough money
if(error==135) RefreshRates(); // prices have changed
}
Sleep(5000);
return ;
}
if (nowbuyorsell == -1) //卖
{
switch (whichmethod)
{
case 1: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red); break;
case 2: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+StopLoss*Point,0,"",MAGICMA,0,Red); break;
case 3: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,Bid-TakeProfit*Point,"",MAGICMA,0,Red); break;
case 4: res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",MAGICMA,0,Red); break;
default : res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red); break;
}
if (res <=0)
{
error=GetLastError();
【版权声明】
本文仅代表作者观点,不代表本站立场。Fx263.com对文中陈述、观点判断保持中立,不对所包含内容的准确性、可靠性或完整性提供任何明示或暗示的保证,且不构成任何投资建议,请读者仅作参考,并自行承担全部风险与责任。Fx263.com作为信息内容发布平台,页面展示内容的目的在于传播更多信息,不代表Fx263.com立场;本站会员及自媒体人所发的稿件所载明的信息与本网无关,如文章涉及版权,请联系本站处理。
【风险提示】
请通过正规渠道参与外汇保证金交易。目前通过网络平台提供、参与外汇保证金交易均属非法。请提高意识,谨防损失!外汇、贵金属和差价合约(OTC场外交易)是杠杆产品,存在较高的风险,可能会导致亏损您的投资本金,请理性投资。