MetaTraderメモ
テクニカル指標iRSIが80以上になったらsell、20以下になったらbuyの注文を出すサンプルです。
太字の部分がiRSI関数を呼び出している部分になります。
iRSIの詳しい使い方は
http://www.metasys-seeker.net/MQL4_Reference_ver1/MQL4_Reference_Contents.html
あたりを参照してください。
extern int MAGIC = 123456;
extern int Slippage = 3;
extern double Lots = 0.1;
extern int TS = 10; // TradingStop
extern int LC = 10; // LossCut
int ticket;
int start()
{
string sStatus;
if(Bars<100 || IsTradeAllowed()==false) return;
int buy_position = 0;
int sell_position = 0;
for(int i=0; i<OrdersTotal(); i++) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;
if(OrderMagicNumber() == MAGIC) {
if(OrderType() == OP_BUY) {
buy_position++;
}
if(OrderType() == OP_SELL) {
sell_position++;
}
}
}
if (buy_position==0) {
if (iRSI(NULL,0,14,PRICE_CLOSE,0)<20) {
ticket=OrderSend(Symbol(),OP_BUY, // Symbol for trading.,Operation type.
Lots, // Number of lots.
Ask, // Preferred price of the trade.
Slippage, // Maximum price slippage for buy or sell orders.
Ask-LC*Point, // Stop loss level.
Ask+TS*Point, // Take profit level.
"Buy Order ", // Order comment text.
MAGIC,0,Green); // Order magic number.
// Order expiration time
// Color of the opening arrow on the chart.
if (ticket<0) {
Print("OrderSend Failed, Error No ",GetLastError());
}
return(0);
}
}
if (sell_position==0) {
if (iRSI(NULL,0,14,PRICE_CLOSE,0)>80) {
ticket=OrderSend(Symbol(),OP_SELL, // Symbol for trading.,Operation type.
Lots, // Number of lots.
Bid, // Preferred price of the trade.
Slippage, // Maximum price slippage for buy or sell orders.
Bid+LC*Point, // Stop loss level.
Bid-TS*Point, // Take profit level.
"Sell Order ", // Order comment text.
MAGIC,0,Red); // Order magic number.
&#
160; // Order expiration time
// Color of the opening arrow on the chart.
if (ticket<0) {
Print("OrderSend Failed, Error No ",GetLastError());
}
return(0);
}
}
return(0);
}