iRSI関数は、MQL4でRSI(相対強度指数)を計算するために使用される関数です。以下に、iRSI関数の使い方をサンプルコードとともに詳しく解説します。
目次
iRSI関数の構文
double iRSI(
string symbol, // シンボル名
int timeframe, // 時間枠
int period, // RSIの期間
int applied_price, // 適用価格
int shift // シフト
);パラメーターの説明
| パラメーター | 説明 |
|---|---|
| symbol | RSIを計算する対象のシンボル(通貨ペア)。NULLを指定すると、現在のチャートのシンボルになります。 |
| timeframe | RSIを計算する時間枠(例: PERIOD_H1)。0を指定すると、現在のチャートの時間枠になります。 |
| period | RSIの計算に使用する期間。 |
| applied_price | RSIの計算に使用する価格(例: PRICE_CLOSE)。 |
| shift | RSIの値を取得するバーのシフト。0は現在のバーを指します。 |
サンプルコード
以下は、RSIを計算し、その値に基づいて売買シグナルを出すシンプルなサンプルコードです。
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// RSIの計算
double rsiValue = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);
// RSIの値を表示
Comment("Current RSI: ", rsiValue);
// RSIに基づく売買シグナル
if(rsiValue > 70)
{
Print("Overbought - Consider selling");
}
else if(rsiValue < 30)
{
Print("Oversold - Consider buying");
}
}EAのサンプルコード
MQL4でiRSI関数を使って70以上で逆張りショート、30以下で逆張りロングを行うサンプルソースを以下に示します。
#property copyright "Copyright 2024, Your Name"
#property link "https://www.example.com"
#property version "1.00"
#property strict
extern int RSI_Period = 14; // RSI期間
extern double Lots = 0.1; // 取引ロット数
extern int StopLoss = 50; // ストップロス(ポイント)
extern int TakeProfit = 100; // 利益確定(ポイント)
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
}
void OnTick()
{
if(OrdersTotal() == 0) // ポジションがない場合のみエントリー
{
double rsi = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, 0);
if(rsi >= 70) // RSIが70以上の場合、逆張りショート
{
double entry = Bid;
double sl = entry + StopLoss * Point;
double tp = entry - TakeProfit * Point;
int ticket = OrderSend(Symbol(), OP_SELL, Lots, entry, 3, sl, tp, "RSI Reverse Short", 0, 0, Red);
if(ticket > 0)
{
Print("ショートポジションを開きました。チケット番号: ", ticket);
}
else
{
Print("エラー: ショートポジションを開けませんでした。エラーコード: ", GetLastError());
}
}
else if(rsi <= 30) // RSIが30以下の場合、逆張りロング
{
double entry = Ask;
double sl = entry - StopLoss * Point;
double tp = entry + TakeProfit * Point;
int ticket = OrderSend(Symbol(), OP_BUY, Lots, entry, 3, sl, tp, "RSI Reverse Long", 0, 0, Blue);
if(ticket > 0)
{
Print("ロングポジションを開きました。チケット番号: ", ticket);
}
else
{
Print("エラー: ロングポジションを開けませんでした。エラーコード: ", GetLastError());
}
}
}
}

