#property indicator_chart_window
// ===== パラメータ =====
input int BB_Period = 20;
input double BB_Dev = 2.5;
input int RSI_Period = 14;
input int WaitBars = 5;
// ===== 状態 =====
int position = 0;
double entry_price = 0;
int wait_count = 0;
int win = 0;
int lose = 0;
double profit_total = 0;
// ===== インジ =====
double Upper(int i){ return iBands(NULL,0,BB_Period,BB_Dev,0,PRICE_CLOSE,MODE_UPPER,i); }
double Lower(int i){ return iBands(NULL,0,BB_Period,BB_Dev,0,PRICE_CLOSE,MODE_LOWER,i); }
double RSI(int i){ return iRSI(NULL,0,RSI_Period,PRICE_CLOSE,i); }
// ===== H1トレンド判定 =====
bool StrongTrend(int i)
{
double ma_now = iMA(NULL,PERIOD_H1,20,0,MODE_SMA,PRICE_CLOSE,i);
double ma_prev = iMA(NULL,PERIOD_H1,20,0,MODE_SMA,PRICE_CLOSE,i+5);
if(MathAbs(ma_now - ma_prev) > 0.002) // 傾き閾値(調整可)
return true;
return false;
}
// ===== 時間制限 =====
bool TradeTime(datetime t)
{
int h = TimeHour(t);
if(h >= 8 || h <= 4) return true;
return false;
}
// ===== 描画 =====
void Arrow(string name, datetime t, double price, int code, color clr)
{
ObjectCreate(0,name,OBJ_ARROW,0,t,price);
ObjectSetInteger(0,name,OBJPROP_ARROWCODE,code);
ObjectSetInteger(0,name,OBJPROP_COLOR,clr);
}
void Text(string name, datetime t, double price, string txt, color clr)
{
ObjectCreate(0,name,OBJ_TEXT,0,t,price);
ObjectSetText(name,txt,10,"Arial",clr);
}
// ===== 統計描画 =====
void DrawStats()
{
double rate = 0;
if(win+lose > 0)
rate = (double)win/(win+lose)*100.0;
Comment("勝ち:",win,
" 負け:",lose,
" 勝率:",DoubleToString(rate,1),"%\n",
"累計:",DoubleToString(profit_total,2));
}
// ===== メイン =====
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)
{
for(int i=100; i>=0; i--)
{
string id = IntegerToString(i);
if(!TradeTime(time[i])) continue;
if(wait_count > 0)
{
wait_count--;
continue;
}
double upper = Upper(i);
double lower = Lower(i);
double rsi = RSI(i);
bool trend = StrongTrend(i);
// =====================
// エントリー
// =====================
if(position == 0 && !trend)
{
// ロング
if(low[i] <= lower && rsi < 30)
{
position = 1;
entry_price = close[i];
Arrow("Buy"+id,time[i],low[i],233,clrLime);
}
// ショート
if(high[i] >= upper && rsi > 70)
{
position = -1;
entry_price = close[i];
Arrow("Sell"+id,time[i],high[i],234,clrRed);
}
}
// =====================
// 決済(ロング)
// =====================
if(position == 1)
{
if(high[i] >= upper || rsi > 50)
{
double profit = close[i] - entry_price;
profit_total += profit;
Arrow("Exit"+id,time[i],close[i],159,clrYellow);
if(profit > 0)
{
win++;
Text("W"+id,time[i],close[i],"〇",clrAqua);
}
else
{
lose++;
Text("L"+id,time[i],close[i],"×",clrRed);
wait_count = WaitBars;
}
position = 0;
}
}
// =====================
// 決済(ショート)
// =====================
if(position == -1)
{
if(low[i] <= lower || rsi < 50)
{
double profit = entry_price - close[i];
profit_total += profit;
Arrow("Exit"+id,time[i],close[i],159,clrYellow);
if(profit > 0)
{
win++;
Text("W"+id,time[i],close[i],"〇",clrAqua);
}
else
{
lose++;
Text("L"+id,time[i],close[i],"×",clrRed);
wait_count = WaitBars;
}
position = 0;
}
}
}
DrawStats();
return(rates_total);
}