/+----------------------------------------------- -------------------+
//| start-example.mq5 |
//| Copyright 2012, MetaQuotes Software Corp. |
//| |
//+----------------------------------------------- -------------------+
#property copyright ""
#property link ""
#property version "1.00"
//+----------------------------------------------- -------------------+
//| EA初始化函數 |
//+----------------------------------------------- -------------------+
#include <Trade\Trade.mqh> //包含執行交易的庫
#include <Trade\PositionInfo.mqh> //包含獲取持倉信息的庫
int iMA_handle; //存儲指標句柄的變量
double iMA_buf[]; //存儲指標值的動態數組
double Close_buf[]; //存儲每個柱形收盤價格的動態數組
string my_symbol; //存儲交易品種的變量
ENUM_TIMEFRAMES my_timeframe; //存儲時間框架的變量
CTrade m_Trade; //執行交易的結構體
CPositionInfo m_Position; //獲取持倉信息的結構體
//+----------------------------------------------- -------------------+
//| |
//+----------------------------------------------- -------------------+
int OnInit()
{
my_symbol=Symbol(); //保存當前圖表的交易品種,用於此EA對其進一步的操作。
my_timeframe=PERIOD_CURRENT; //保存圖表的當前時間框架,用於此EA對其的進一步操作。
iMA_handle=iMA(my_symbol,my_timeframe,40,0,MODE_SMA,PRICE_CLOSE); //應用此指標並獲取指標句柄
if(iMA_handle==INVALID_HANDLE) //檢查指標句柄是否可用
{
Print("Failed to get the indicator handle"); //如果句柄沒有獲取到,打印相關報錯信息到日誌文件中
return(-1); //完成報錯處理
}
ChartIndicatorAdd(ChartID(),0,iMA_handle); //將指標添加到價格圖表中
ArraySetAsSeries(iMA_buf,true); //將iMA_buf數組的索引設置為時間序列
ArraySetAsSeries(Close_buf,true); //將Close_buf數組的索引設置為時間序列
return(0); //返回0,初始化結束
}
//+----------------------------------------------- -------------------+
//| EA去初始化函數 |
//+----------------------------------------------- -------------------+
void OnDeinit (const int r<a href="http://www.520fx.com/forum.php?mod=forumdisplay&fid=25" style="color:" target="_blank">ea</a>son)
{
IndicatorRel<a href="http://www.520fx.com/forum.php?mod=forumdisplay&fid=25" style="color:" target="_blank">ea</a>se(iMA_handle); //刪除指標句柄並釋放分配給它的存儲空間
ArrayFree(iMA_buf); //釋放動態數組iMA_buf的數據
ArrayFree(Close_buf); //釋放動態數組Close_buf的數據
}
//+----------------------------------------------- -------------------+
//| EA的tick函數 |
//+----------------------------------------------- -------------------+
void OnTick()
{
int err1=0; //用於存儲指標緩存處理結果的變量
int err2=0; //用於存儲價格圖表處理結果的變量
err1=CopyBuffer(iMA_handle,0,1,2,iMA_buf); //將數據從指標數組中拷貝到動態數組iMA_buf中,用於進一步處理
err2=CopyClose(my_symbol,my_timeframe,1,2,Close_buf); //將價格圖表數據拷貝到動態數Close_buf中,用於進一步處理
if(err1<0 || err2<0) //如果出錯
{
Print("Failed to copy data from the indicator buffer or price chart buffer"); //打印相關錯誤信息到日誌文件
return; //並退出函數
}
if(iMA_buf[1]>Close_buf[1] && iMA_buf[0]<Close_buf[0]) //如果指標值大於收盤價並且開始變小
{
if(m_Position.Select(my_symbol)) //如果該交易品種的持倉已經存在
{
if(m_Position.PositionType()==POSITION_TYPE_SELL) m_Trade.PositionClose(my_symbol); //並且這是一個賣出持倉,那麼平倉
if(m_Position.PositionType()==POSITION_TYPE_BUY) return; //或者,如果是一個買入持倉,那麼退出
}
m_Trade.Buy(0.1,my_symbol); //如果到這裡,說明沒有持倉;那麼我們開倉
}
if(iMA_buf[1]<Close_buf[1] && iMA_buf[0]>Close_buf[0]) //如果指標值小於收盤價並且在變大
{
if(m_Position.Select(my_symbol)) //如果該交易品種的持倉已經存在
{
if(m_Position.PositionType()==POSITION_TYPE_BUY) m_Trade.PositionClose(my_symbol); //並且這是一個買入持倉,那麼平倉
if(m_Position.PositionType()==POSITION_TYPE_SELL) return; //或者,如果這是一個賣出持倉,那麼退出
}
m_Trade.Sell(0.1,my_symbol); //如果我們到這裡,說明沒有持倉;那麼我們開倉
}
}
//+----------------------------------------------- -------------------+
oanda2016 發表在 痞客邦 留言(0) 人氣(117)
先列出需求,創建一條垂直線,價格漲時,顏色為白色,跌時為紅色。這樣這個實例裡會包括創建一條線、if語句、順帶用一下顏色數據類型。
新建一個自定義指標:
oanda2016 發表在 痞客邦 留言(0) 人氣(173)
//+------------------------------------------------------------------+
//| SimpleOpen.mq5 |
//| Copyright Wayne Xu |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Wayne Xu"
#property link "http://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int a;
double b[];
int OnInit()
{
//---
a=iRSI(Symbol(),0,6,PRICE_CLOSE);
//---
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
MqlTradeRequest one;
MqlTradeRequest one1;
MqlTradeResult two;
//------
one.action=TRADE_ACTION_DEAL;
one.symbol=Symbol();
one.volume=1;
one.price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
one.sl=one.price+0.003;
one.tp=one.price-0.003;
one.type=ORDER_TYPE_SELL;
//------
one1.action=TRADE_ACTION_DEAL;
one1.symbol=Symbol();
one1.volume=1;
one1.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
one1.sl=one.price-0.003;
one1.tp=one.price+0.003;
one1.type=ORDER_TYPE_BUY;
//double a=iRSI(Symbol(),0,14,PRICE_CLOSE);
int c = CopyBuffer(a,0,0,1,b);
//Print(b[0]);
if(PositionsTotal()<1&&b[0]>70)
{OrderSend(one,two);}
if(PositionsTotal()<1&&b[0]<30)
{OrderSend(one1,two);}
oanda2016 發表在 痞客邦 留言(0) 人氣(133)
下面代碼為講解只用,不能直接複製到mql5文件裡,會運行出錯,後面我會附件附上源代碼文件:
//+----------------------------------------------- -------------------+
//| 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; 這裡定義短週期均線的句柄,有了句柄,以後就可以做相關的操作。
oanda2016 發表在 痞客邦 留言(0) 人氣(248)
//+----------------------------------------------- -------------------+
//| start-example.mq5 |
//| Copyright 2012, MetaQuotes Software Corp. |
//| |
//+----------------------------------------------- -------------------+
#property copyright ""
#property link ""
#property version "1.00"
//+----------------------------------------------- -------------------+
//| EA初始化函數 |
//+----------------------------------------------- -------------------+
#include <Trade\Trade.mqh> //包含執行交易的庫
#include <Trade\PositionInfo.mqh> //包含獲取持倉信息的庫
int iMA_handle; //存儲指標句柄的變量
double iMA_buf[]; //存儲指標值的動態數組
double Close_buf[]; //存儲每個柱形收盤價格的動態數組
string my_symbol; //存儲交易品種的變量
ENUM_TIMEFRAMES my_timeframe; //存儲時間框架的變量
CTrade m_Trade; //執行交易的結構體
CPositionInfo m_Position; //獲取持倉信息的結構體
//+----------------------------------------------- -------------------+
//| |
//+----------------------------------------------- -------------------+
int OnInit()
{
my_symbol=Symbol(); //保存當前圖表的交易品種,用於此EA對其進一步的操作。
my_timeframe=PERIOD_CURRENT; //保存圖表的當前時間框架,用於此EA對其的進一步操作。
iMA_handle=iMA(my_symbol,my_timeframe,40,0,MODE_SMA,PRICE_CLOSE); //應用此指標並獲取指標句柄
if(iMA_handle==INVALID_HANDLE) //檢查指標句柄是否可用
{
Print("Failed to get the indicator handle"); //如果句柄沒有獲取到,打印相關報錯信息到日誌文件中
return(-1); //完成報錯處理
}
ChartIndicatorAdd(ChartID(),0,iMA_handle); //將指標添加到價格圖表中
ArraySetAsSeries(iMA_buf,true); //將iMA_buf數組的索引設置為時間序列
ArraySetAsSeries(Close_buf,true); //將Close_buf數組的索引設置為時間序列
return(0); //返回0,初始化結束
}
//+----------------------------------------------- -------------------+
//| EA去初始化函數 |
//+----------------------------------------------- -------------------+
void OnDeinit (const int r<a href="http://www.520fx.com/forum.php?mod=forumdisplay&fid=25" style="color:" target="_blank">ea</a>son)
{
IndicatorRel<a href="http://www.520fx.com/forum.php?mod=forumdisplay&fid=25" style="color:" target="_blank">ea</a>se(iMA_handle); //刪除指標句柄並釋放分配給它的存儲空間
ArrayFree(iMA_buf); //釋放動態數組iMA_buf的數據
ArrayFree(Close_buf); //釋放動態數組Close_buf的數據
}
//+----------------------------------------------- -------------------+
//| EA的tick函數 |
//+----------------------------------------------- -------------------+
void OnTick()
{
int err1=0; //用於存儲指標緩存處理結果的變量
int err2=0; //用於存儲價格圖表處理結果的變量
err1=CopyBuffer(iMA_handle,0,1,2,iMA_buf); //將數據從指標數組中拷貝到動態數組iMA_buf中,用於進一步處理
err2=CopyClose(my_symbol,my_timeframe,1,2,Close_buf); //將價格圖表數據拷貝到動態數Close_buf中,用於進一步處理
if(err1<0 || err2<0) //如果出錯
{
Print("Failed to copy data from the indicator buffer or price chart buffer"); //打印相關錯誤信息到日誌文件
return; //並退出函數
}
if(iMA_buf[1]>Close_buf[1] && iMA_buf[0]<Close_buf[0]) //如果指標值大於收盤價並且開始變小
{
if(m_Position.Select(my_symbol)) //如果該交易品種的持倉已經存在
{
if(m_Position.PositionType()==POSITION_TYPE_SELL) m_Trade.PositionClose(my_symbol); //並且這是一個賣出持倉,那麼平倉
if(m_Position.PositionType()==POSITION_TYPE_BUY) return; //或者,如果是一個買入持倉,那麼退出
}
m_Trade.Buy(0.1,my_symbol); //如果到這裡,說明沒有持倉;那麼我們開倉
}
if(iMA_buf[1]<Close_buf[1] && iMA_buf[0]>Close_buf[0]) //如果指標值小於收盤價並且在變大
{
if(m_Position.Select(my_symbol)) //如果該交易品種的持倉已經存在
{
if(m_Position.PositionType()==POSITION_TYPE_BUY) m_Trade.PositionClose(my_symbol); //並且這是一個買入持倉,那麼平倉
if(m_Position.PositionType()==POSITION_TYPE_SELL) return; //或者,如果這是一個賣出持倉,那麼退出
}
m_Trade.Sell(0.1,my_symbol); //如果我們到這裡,說明沒有持倉;那麼我們開倉
}
oanda2016 發表在 痞客邦 留言(0) 人氣(880)
//+------------------------------------------------------------------+
//| start-example.mq5 |
//| Copyright 2012, MetaQuotes Software Corp. |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
#include <Trade\Trade.mqh> //包含执行交易的库
#include <Trade\PositionInfo.mqh> //包含获取持仓信息的库
int iMA_handle; //存储指标句柄的变量
double iMA_buf[]; //存储指标值的动态数组
double Close_buf[]; //存储每个柱形收盘价格的动态数组
string my_symbol; //存储交易品种的变量
ENUM_TIMEFRAMES my_timeframe; //存储时间框架的变量
CTrade m_Trade; //执行交易的结构体
CPositionInfo m_Position; //获取持仓信息的结构体
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
my_symbol=Symbol(); //保存当前图表的交易品种,用于此EA对其进一步的操作。
my_timeframe=PERIOD_CURRENT; //保存图表的当前时间框架,用于此EA对其的进一步操作。
iMA_handle=iMA(my_symbol,my_timeframe,40,0,MODE_SMA,PRICE_CLOSE); //应用此指标并获取指标句柄
if(iMA_handle==INVALID_HANDLE) //检查指标句柄是否可用
{
Print("Failed to get the indicator handle"); //如果句柄没有获取到,打印相关报错信息到日志文件中
return(-1); //完成报错处理
}
ChartIndicatorAdd(ChartID(),0,iMA_handle); //将指标添加到价格图表中
ArraySetAsSeries(iMA_buf,true); //将iMA_buf数组的索引设置为时间序列
ArraySetAsSeries(Close_buf,true); //将Close_buf数组的索引设置为时间序列
return(0); //返回0,初始化结束
}
//+------------------------------------------------------------------+
//| EA去初始化函数 |
//+------------------------------------------------------------------+
void OnDeinit (const int r<a href="http://www.520fx.com/forum.php?mod=forumdisplay&fid=25" style="color:" target="_blank">ea</a>son)
{
IndicatorRel<a href="http://www.520fx.com/forum.php?mod=forumdisplay&fid=25" style="color:" target="_blank">ea</a>se(iMA_handle); //删除指标句柄并释放分配给它的存储空间
ArrayFree(iMA_buf); //释放动态数组iMA_buf的数据
ArrayFree(Close_buf); //释放动态数组Close_buf的数据
}
//+------------------------------------------------------------------+
//| EA的tick函数 |
//+------------------------------------------------------------------+
void OnTick()
{
int err1=0; //用于存储指标缓存处理结果的变量
int err2=0; //用于存储价格图表处理结果的变量
err1=CopyBuffer(iMA_handle,0,1,2,iMA_buf); //将数据从指标数组中拷贝到动态数组iMA_buf中,用于进一步处理
err2=CopyClose(my_symbol,my_timeframe,1,2,Close_buf); //将价格图表数据拷贝到动态数Close_buf中,用于进一步处理
if(err1<0 || err2<0) //如果出错
{
Print("Failed to copy data from the indicator buffer or price chart buffer"); //打印相关错误信息到日志文件
return; //并退出函数
}
if(iMA_buf[1]>Close_buf[1] && iMA_buf[0]<Close_buf[0]) //如果指标值大于收盘价并且开始变小
{
if(m_Position.Select(my_symbol)) //如果该交易品种的持仓已经存在
{
if(m_Position.PositionType()==POSITION_TYPE_SELL) m_Trade.PositionClose(my_symbol); //并且这是一个卖出持仓,那么平仓
if(m_Position.PositionType()==POSITION_TYPE_BUY) return; //或者,如果是一个买入持仓,那么退出
}
m_Trade.Buy(0.1,my_symbol); //如果到这里,说明没有持仓;那么我们开仓
}
if(iMA_buf[1]<Close_buf[1] && iMA_buf[0]>Close_buf[0]) //如果指标值小于收盘价并且在变大
{
if(m_Position.Select(my_symbol)) //如果该交易品种的持仓已经存在
{
if(m_Position.PositionType()==POSITION_TYPE_BUY) m_Trade.PositionClose(my_symbol); //并且这是一个买入持仓,那么平仓
if(m_Position.PositionType()==POSITION_TYPE_SELL) return; //或者,如果这是一个卖出持仓,那么退出
}
m_Trade.Sell(0.1,my_symbol); //如果我们到这里,说明没有持仓;那么我们开仓
}
}
//+------------------------------------------------------------------+
oanda2016 發表在 痞客邦 留言(0) 人氣(82)
Onada 2015 最佳cfd 公司
優惠活動中 開戶入金200美金 獲得獎金100美金 歡迎詢問
oanda2016 發表在 痞客邦 留言(0) 人氣(829)
新建自定義指標後,會看到如下代碼,約40行。
//+----------------------------------------------- -------------------+
//| telihai-2.mq5 |
//| Copyright 2011, telihai. |
//| http://www.telihai.com/ |
//+----------------------------------------------- -------------------+
#property copyright "Copyright 2011, telihai."
#property link "http://www.telihai.com/"
#property version "1.00"
#property indicator_chart_window
//+----------------------------------------------- -------------------+
//| Custom indicator initialization function |
//+----------------------------------------------- -------------------+
int OnInit()
{
//--- indicator buffers mapping
//---
return(0);
}
//+----------------------------------------------- -------------------+
//| Custom indicator iteration function |
//+----------------------------------------------- -------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
//--- return value of prev_calculated for next call
return(rates_total);
}
//+----------------------------------------------- -------------------+
oanda2016 發表在 痞客邦 留言(0) 人氣(107)
//+----------------------------------------------- -------------------+
//| 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; 這裡定義短週期均線的句柄,有了句柄,以後就可以做相關的操作。
oanda2016 發表在 痞客邦 留言(0) 人氣(129)
Onada 2015 最佳cfd 公司
優惠活動中 開戶入金200美金 獲得獎金100美金 歡迎詢問
oanda2016 發表在 痞客邦 留言(0) 人氣(321)