小弟根據《五分鐘動量交易系統(tǒng)》編的,請大家多多指教!- //+------------------------------------------------------------------+
- //| 五分鐘動量交易系統(tǒng).mq4 |
- //+------------------------------------------------------------------+
- extern double MAPeriod=20; //指數(shù)均線周期
- extern double StopLossSpred = 20; //初始止損離均線的點數(shù)
- extern double CloseSpred = 15; //后半倉止損離均線的點數(shù)
- extern double Lots=0.2; //持倉,必須為偶數(shù)
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- int start()
- {
- double MacdCurrent, MacdPrevious, Ma;
- int cnt, ticket, total;
- if(Bars<100)
- {
- Print("K線少于100根!");
- return(0);
- }
- MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
- MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,2);
- Ma=iMA(NULL,0,MAPeriod,0,MODE_EMA,PRICE_CLOSE,0);
-
- total=OrdersTotal();
-
- //開單檢查
- if(total<1) //在沒有持倉的情況下才能開新倉
- {
- // 檢查資金
- if(AccountFreeMargin()<(1000*Lots))
- {
- Print("資金不足: ", AccountFreeMargin());
- return(0);
- }
- // 檢查開多單的可能性
- if(MacdCurrent>0 && MacdPrevious<0 && Ask>Ma && Ask<=Ma+20*Point) //MACD首紅且匯價位于均線上20點內做多
- {
- ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ma-StopLossSpred*Point,0,"lbs-buy",197658,0,Red);
- if(ticket>0)
- {
- if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("開多單成功 : ",OrderOpenPrice());
- }
- else Print("開多單發(fā)生錯誤 : ",GetLastError());
- return(0);
- }
- // 檢查開空單的可能性
- if(MacdCurrent<0 && MacdPrevious>0 && Bid<Ma && Bid>=Ma-20*Point) //MACD首綠且匯價位于均線下20點內做空
- {
- ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ma+StopLossSpred*Point,0,"lbs-sell",197658,0,Green);
- if(ticket>0)
- {
- if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("開空單成功 : ",OrderOpenPrice());
- }
- else Print("開空單發(fā)生錯誤 : ",GetLastError());
- return(0);
- }
- return(0);
- }
-
- //減倉及平倉檢查
- for(cnt=0;cnt<total;cnt++)
- {
- OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
- if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) // check for symbol
- {
- if(OrderType()==OP_BUY) // 如果有做多單存在
- {
- //多單減倉檢查
- if(Ask-OrderOpenPrice()>=OrderOpenPrice()-OrderStopLoss() && OrderOpenPrice()>OrderStopLoss() && OrderLots()==Lots)
- {
- ticket=OrderClose(OrderTicket(),Lots/2,Ask,3,Violet); // 平掉一半倉位
- if(ticket>0)
- {
- if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("多單減倉成功 : ",OrderOpenPrice());
- }
- else Print("多單減倉發(fā)生錯誤 : ",GetLastError());
- return(0);
- }
-
- //修改后半倉位的止損價到盈虧平衡點
- if(Ask-OrderOpenPrice()>OrderOpenPrice()-OrderStopLoss() && OrderOpenPrice()>OrderStopLoss() && OrderLots()==Lots/2)
- {
- ticket=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);
- if(ticket>0)
- {
- if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("多單盈虧平衡點修改成功 : ",OrderOpenPrice());
- }
- else Print("多單盈虧平衡點修改發(fā)生錯誤 : ",GetLastError());
- return(0);
- }
-
- //根據行情發(fā)展修改剩余倉位的止損價到均線下15點
- if(Ma-CloseSpred*Point>OrderStopLoss() && OrderOpenPrice()<=OrderStopLoss() && OrderLots()==Lots/2)
- {
- ticket=OrderModify(OrderTicket(),OrderOpenPrice(),Ma-CloseSpred*Point,OrderTakeProfit(),0,Red);
- if(ticket>0)
- {
- if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("多單止損修改成功 : ",OrderOpenPrice());
- }
- else Print("多單止損修改發(fā)生錯誤 : ",GetLastError());
- return(0);
- }
- }
- else // 如果有做空單存在
- {
- //空單減倉檢查
- if(OrderOpenPrice()-Bid>OrderStopLoss()-OrderOpenPrice() && OrderOpenPrice()<OrderStopLoss() && OrderLots()==Lots)
- {
- ticket=OrderClose(OrderTicket(),Lots/2,Ask,3,Violet); // 平掉一半倉位
- if(ticket>0)
- {
- if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("空單減倉成功 : ",OrderOpenPrice());
- }
- else Print("空單減倉發(fā)生錯誤 : ",GetLastError());
- return(0);
- }
-
- //修改后半倉位的止損價到盈虧平衡點
- if(OrderOpenPrice()-Bid>OrderStopLoss()-OrderOpenPrice() && OrderOpenPrice()<OrderStopLoss() && OrderLots()==Lots/2)
- {
- ticket=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);
- if(ticket>0)
- {
- if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("空單盈虧平衡點修改成功 : ",OrderOpenPrice());
- }
- else Print("空單盈虧平衡點修改發(fā)生錯誤 : ",GetLastError());
- return(0);
- }
-
- //根據行情發(fā)展修改剩余倉位的止損價到均線下15點
- if(Ma+CloseSpred*Point<OrderStopLoss() && OrderOpenPrice()>=OrderStopLoss() && OrderLots()==Lots/2)
- {
- ticket=OrderModify(OrderTicket(),OrderOpenPrice(),Ma+CloseSpred*Point,OrderTakeProfit(),0,Green);
- if(ticket>0)
- {
- if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("空單止損修改成功 : ",OrderOpenPrice());
- }
- else Print("空單止損修改發(fā)生錯誤 : ",GetLastError());
- return(0);
- }
- }
- }
- }
- return(0);
- }
- // the end.
復制代碼
|