|
这是用mt4里的macd智能交易样本程序改的,用来学习一下。现在是只作多的,就是当ama出现作多箭头时买入,收盘价低于8周期均线时卖出。问题是只能作一单,只有当手头的一单子被平了才接受新的信号,而不是有信号就买一单,我把if(total<1)改成<2,确实当出现2个信号能进2单了,但是平的时候却只能平一单而不能同时平。怎么把程序改成有信号就买一单,当出现平仓信号时就一起平掉?
还有一个问题是,在同时加一个止损50点的平仓信号?我用 if(Close[1]<iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,3)||Close[0]<OrderOpenPrice()-50*Point),但是跑了一下还是不起作用。
谢谢指点!
extern double TakeProfit = 10000; extern double Lots = 0.1; extern double MATrendPeriod=8; int start() { int cnt, ticket, total; if(Bars<100) { Print("bars less than 100"); return(0); } if(TakeProfit<10) { Print("TakeProfit less than 10"); return(0); // check TakeProfit }
total=OrdersTotal(); if(total<1) { // no opened orders identified if(AccountFreeMargin()<(1000*Lots)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } // check for long position (BUY) possibility if(iCustom(NULL, 0, "wellxAMA",9,1,1)==iCustom(NULL, 0, "wellxAMA",9,0,1)) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"lein",16384,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice()); } else Print("Error opening BUY order : ",GetLastError()); return(0); } return(0); } // it is important to enter the market correctly, // but it is more important to exit it correctly... for(cnt=0;cnt<total;cnt++) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(OrderType()<=OP_SELL && // check for opened position OrderSymbol()==Symbol()) // check for symbol { if(OrderType()==OP_BUY) // long position is opened { // should it be closed? if(Close[1]<iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,3)) { OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position return(0); // exit } } } } return(0); }
|