MetaTrader编程入门

2011-06-11 14:46:58

extern double MaximumRisk = 0.3;

extern double TrailingStop =25;

extern int maxOpen = 3; //最多持仓限制

extern int maxLots = 5; //最多单仓限制

extern int bb = 0; //非零就跟踪止赢

extern double MATrendPeriod=26;//使用26均线

int i, p2, xxx,p1, res;

double Lots;

datetime lasttime;

int init() //初始化

{

Lots = 1;

lasttime = NULL;

return(0);

}

int deinit() { return(0); } //反初始化

//| expert start function

int start()

{

CheckForOpen();

if (bb>0) CTP(); //跟踪止赢

return(0);

}

//+------------------------------------------------------------------+

double LotsOptimized() //确定下单量,开仓调用

{

double lot=Lots;

int orders=HistoryTotal(); // history orders total

int losses=0; // number of losses orders without a break

//MarketInfo(Symbol(),MODE_MINLOT);

//MarketInfo(Symbol(),MODE_MAXLOT);

//MarketInfo(Symbol(),MODE_LOTSTEP);

lot=NormalizeDouble(MaximumRisk * AccountBalance()/AccountLeverage(),1);

if(lot<0.1) lot=0.1;

if(lot>maxLots) lot=maxLots;

return(lot);

}

//平仓持有的买单

void CloseBuy()

{

if (OrdersTotal( ) > 0 )

{

for(i=OrdersTotal()-1;i<0;i++)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderType()==OP_BUY)

{

OrderClose(OrderTicket(),OrderLots(),Bid,3,White);

Sleep(5000);

}

}

}

}

//平仓持有的卖单

void CloseSell()

{

if (OrdersTotal( ) > 0 )

{

for(i=OrdersTotal()-1;i<0;i++)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderType()==OP_SELL)

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,White);

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;

 承诺与声明

兄弟财经是全球历史最悠久,信誉最好的外汇返佣代理。多年来兄弟财经兢兢业业,稳定发展,获得了全球各地投资者的青睐与信任。历经十余年的积淀,打造了我们在业内良好的品牌信誉。

本文所含内容及观点仅为一般信息,并无任何意图被视为买卖任何货币或差价合约的建议或请求。文中所含内容及观点均可能在不被通知的情况下更改。本文并未考 虑任何特定用户的特定投资目标、财务状况和需求。任何引用历史价格波动或价位水平的信息均基于我们的分析,并不表示或证明此类波动或价位水平有可能在未来 重新发生。本文所载信息之来源虽被认为可靠,但作者不保证它的准确性和完整性,同时作者也不对任何可能因参考本文内容及观点而产生的任何直接或间接的损失承担责任。

外汇和其他产品保证金交易存在高风险,不适合所有投资者。亏损可能超出您的账户注资。增大杠杆意味着增加风险。在决定交易外汇之前,您需仔细考虑您的财务目标、经验水平和风险承受能力。文中所含任何意见、新闻、研究、分析、报价或其他信息等都仅 作与本文所含主题相关的一般类信息.

同时, 兄弟财经不提供任何投资、法律或税务的建议。您需向合适的顾问征询所有关于投资、法律或税务方面的事宜。