//+----------------------------------------------- -------------------+
//| MAcross.mq5 |
//| Copyright 2009, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+----------------------------------------------- -------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
#property indicator_chart_window 畫在主圖上
#property indicator_buffers 4 定義4個畫線位置
#property indicator_plots 4 畫4條線
//---- plot long
#property indicator_label1 "long" 這條線的名稱
#property indicator_type1 DRAW_LINE 這條線的類型是線性
#property indicator_color1 Red 這條線的顏色
#property indicator_style1 STYLE_SOLID 這條線是實線
#property indicator_width1 1 這條線的寬度是1
//---- plot small
#property indicator_label2 "small"
#property indicator_type2 DRAW_LINE
#property indicator_color2 Yellow
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//---- plot up
#property indicator_label3 "up"
#property indicator_type3 DRAW_ARROW 這裡我們要畫箭頭
#property indicator_color3 White
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//---- plot down
#property indicator_label4 "down"
#property indicator_type4 DRAW_ARROW
#property indicator_color4 MediumBlue
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
//--- input parameters
input int longma=50; 長均線週期(也就是上面圖中我們輸入的)
input int smallma=10; 短均線週期(也就是上面圖中我們輸入的)
//--- indicator buffers
double longBuffer[]; (也就是上面圖中我們輸入的畫線是long的時候,這裡就會產生一個longBuffer[]數組)
double smallBuffer[]; 同上
double upBuffer[]; 同上
double downBuffer[]; 同上
int longma_handle; 這裡定義長周期均線的句柄,有了句柄,以後就可以做相關的操作。
int smallma_handle; 這裡定義短週期均線的句柄,有了句柄,以後就可以做相關的操作。
//+----------------------------------------------- -------------------+
//| Custom indicator initialization function |
//+----------------------------------------------- -------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,longBuffer,INDICATOR_DATA);
SetIndexBuffer(1,smallBuffer,INDICATOR_DATA);
SetIndexBuffer(2,upBuffer,INDICATOR_DATA);
SetIndexBuffer(3,downBuffer,INDICATOR_DATA);
PlotIndexSetInteger(2,PLOT_DRAW_TYPE,DRAW_ARROW);
PlotIndexSetInteger(3,PLOT_DRAW_TYPE,DRAW_ARROW);
PlotIndexSetInteger(2,PLOT_ARROW,1001); 箭頭類型是1001號箭頭
PlotIndexSetInteger(3,PLOT_ARROW,1002); 箭頭類型是1002號箭頭
longma_handle=iMA(Symbol(),0,longma,0,MODE_SMA,PRICE_CLOSE); 初始化長周期均線函數
smallma_handle=iMA(Symbol(),0,smallma,0,MODE_SMA,PRICE_CLOSE); 初始化短週期均線函數
//---
return(0);
}
//+----------------------------------------------- -------------------+
//| Custom indicator iteration function |
//+----------------------------------------------- -------------------+
int OnCalculate(const int rates_total, K線總數,會隨著K線的增加而增加
const int prev_calculated, 本根K線前一根K線的序號,通常來說prev_calculated=rates_total-1
const datetime& time[], 存儲所有K線的時間數組
const double& open[], 存儲所有K線的開盤價數組
const double& high[], 存儲所有K線的最高價數組
const double& low[], 存儲所有K線的最低價數組
const double& close[], 存儲所有K線的收盤價數組
const long& tick_volume[],
const long& volume[],
const int& spread[])
{
int malong=CopyBuffer(longma_handle,0,0,rates_total,longBuffer); 使用longma_handle長周期均線數組句柄,把均線的值複製到longBuffer數組中
int masmall=CopyBuffer(smallma_handle,0,0,rates_total,smallBuffer); 使用smallma_handle長周期均線數組句柄,把均線的值複製到smallBuffer數組中
for(int i=10;i<rates_total;i++)
{
if((longBuffer[i-1]>smallBuffer[i-1])&&(longBuffer<smallBuffer)) 判斷兩均線產生金叉了
{
upBuffer=longBuffer;在金叉處畫上白色箭頭
if(i==prev_calculated)只針對當前K線報警歷史K線就不用報警了
{Alert("up");}彈出窗口報警
}
if((longBuffer[i-1]<smallBuffer[i-1])&&(longBuffer>smallBuffer)) 判斷兩均線產生了死叉
{
downBuffer=longBuffer;在死叉處畫上青色箭頭
if(i==prev_calculated)只針對當前K線報警歷史K線就不用報警了
{Alert("down");}彈出窗口報警
}
}
//---
//--- return value of prev_calculated for next call
return(rates_total);
}
//+----------------------------------------------- -------------------+
