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;

 承諾與聲明

兄弟財經是全球歷史最悠久,信譽最好的外匯返佣代理。多年來兄弟財經兢兢業業,穩定發展,獲得了全球各地投資者的青睞與信任。歷經十餘年的積澱,打造了我們在業内良好的品牌信譽。

本文所含内容及觀點僅為一般信息,並無任何意圖被視為買賣任何貨幣或差價合約的建議或請求。文中所含内容及觀點均可能在不被通知的情況下更改。本文並未考 慮任何特定用戶的特定投資目標、財務狀況和需求。任何引用歷史價格波動或價位水平的信息均基於我們的分析,並不表示或證明此類波動或價位水平有可能在未來 重新發生。本文所載信息之來源雖被認為可靠,但作者不保證它的準確性和完整性,同時作者也不對任何可能因參考本文内容及觀點而產生的任何直接或間接的損失承擔責任。

外匯和其他產品保證金交易存在高風險,不適合所有投資者。虧損可能超出您的賬戶註資。增大槓桿意味著增加風險。在決定交易外匯之前,您需仔細考慮您的財務目標、經驗水平和風險承受能力。文中所含任何意見、新聞、研究、分析、報價或其他信息等都僅 作與本文所含主題相關的一般類信息.

同時, 兄弟財經不提供任何投資、法律或稅務的建議。您需向合適的顧問徵詢所有關於投資、法律或稅務方面的事宜。