寫程式應該多多使用函數,這樣能夠提高程式設計的效率,下面是曾經用到的函數,将他們儲存下來,供以後直接複制過去使用,這就不必每次都要重寫了。
//---顯示資訊函數,因為建立了兩個對象,是以在退出時,應該删除對象-----------------
void showinfo(){
ObjectCreate("p1",OBJ_LABEL,0,0,0);
ObjectSetText("p1",Symbol()+"
帳戶餘額:"+DoubleToStr(AccountBalance(),0)+"
帳戶淨值:"+DoubleToStr(AccountEquity(),0),18,"華文細黑",Yellow);
ObjectSet("p1",OBJPROP_XDISTANCE,10);
ObjectSet("p1",OBJPROP_YDISTANCE,40);
ObjectCreate("p2",OBJ_LABEL,0,0,0);
ObjectSetText("p2","EA-yunkaicn
自動交易系統已經啟動。時間:"+Month()+"月"+Day()+"日"+Hour()+"點"+Minute()+"分",18,"華文細黑",Yellow);
ObjectSet("p2",OBJPROP_XDISTANCE,10);
ObjectSet("p2",OBJPROP_YDISTANCE,10);
}
//---計算目前貨币訂單數函數。
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
for(int i=0;i
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() &&
OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
if(buys>0) return(buys);
else return(-sells);
}
//---計算交易手數的函數。
double LotsOptimized()
{
if(AccountEquity()<300) return(0.1);
if(AccountEquity()>=300 && AccountEquity()<500)
return(0.2);
if(AccountEquity()>=500 && AccountEquity()<1000)
return(0.4);
if(AccountEquity()>=1000 && AccountEquity()<5000)
return(0.6);
if(AccountEquity()>=5000 && AccountEquity()<10000)
return(1);
if(AccountEquity()>=10000) return(5);
}
//---開倉函數。
void CheckForOpen()
{
Lots=0.1;
if(zo=-ccipt)
{
res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid + StopLoss*Point,Bid
- TakeProfit*Point,"",MAGICMA,0,Red);
if(res>0)
{
if(OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES))
Alert(Symbol(),"賣單已經開了,價格是:",OrderOpenPrice());
}
else
Alert(Symbol(),"賣單開倉出錯:",ErrorDescription(GetLastError()));
return;
return;
}
if(zo>maxfan && ccip1>ccipt &&
ccip2<=ccipt)
{
res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask - StopLoss*Point, Ask
+ TakeProfit*Point,"",MAGICMA, 0, Red);
if(res>0)
{
if(OrderSelect(res,SELECT_BY_TICKET,MODE_TRADES))
Alert(Symbol(),"買單已經開了,價格是:",OrderOpenPrice());
}
else
Alert(Symbol(),"買單開倉出錯:",ErrorDescription(GetLastError()));
return;
}
}
//---平倉函數。
void CheckForClose()
{
Print("目前訂單的利潤:",OrderProfit(),"目前交易量(手):",LotsOptimized());
for(int i=0;i
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol())
continue;
if(OrderType()==OP_BUY)
{
if(Open[3]>=Close[3] && Open[2]>=Close[2] &&
Open[1]>=Close[1] )
OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
}
if(OrderType()==OP_SELL)
{
if(Open[3]<=Close[3] && Open[2]<=Close[2] &&
Open[1]<=Close[1] )
OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
}
}
}