
EA自作企画第8弾!ざっくり勝てそうな戦略で…前回までにEA自体の設定を行いました。今回は本当に簡単なロジックでエントリーするようにします。
なるべく簡単なやつで…
maxポジションが4で、riskが10%でした。ってことは、10万円の証拠金で、1ドル150円としたら千通貨。ドル円的にはロングだけで入るので、1時間足、SMA20を上抜けたらロング、利益1%で利確とかにしてみましょう。ちょっとだけ工夫してSMAの計算をHighでやってみます。
void EntryTriggerLong(double lots)//引数にロットの大きさを取る
{
double SMA20_1 = iMA(NULL,0,20,0,MODE_SMA,PRICE_HIGH,1);//ひとつ前の20期間Highの平均
double SMA20_2 = iMA(NULL,0,20,0,MODE_SMA,PRICE_HIGH,2);//ふたつ前の20期間Highの平均
double close1 = Close[1];//ひとつ前の終値
double close2 = Close[2];//ふたつ前の終値
if(SMA20_1 < close1 && SMA20_2 >close2)//ふたつ前からひとつ前にかけてローソクのゴールデンクロス
{
int ticket = OrderSend(NULL,OP_BUY,lots,Ask,5,0,0,"",0,0,clrBlue);//ロングする
}
}
こんな感じで実装してみました。
全体のコードです。設定も少しいじりました。
//+------------------------------------------------------------------+
//| OnewayTrader.mq4 |
//| Copyright 2025, FPshima |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, FPshima"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
extern bool LongOn = true; //ロングを許可
extern bool ShortOn = false;//ショートを許可
extern int zone = 100; //エントリー不可範囲
extern int maxPosition = 4; //最大ポジション数
extern int LongMAX = 4; // ロングポジションの最大数
extern int ShortMAX = 2; // ショートポジションの最大数
extern double risk = 5; //取りうる最大リスク
extern double profitRate = 1;//利確
double calculateLotSize()
{
double accountBalance = AccountBalance();
double totalRiskAmount = accountBalance * risk/100;
double eachPositionRisk = totalRiskAmount/maxPosition;
double minLot = MarketInfo(NULL,MODE_MINLOT);
double marginRequire = MarketInfo(NULL,MODE_MARGINREQUIRED)*minLot;
double lotSize = (eachPositionRisk / marginRequire)*minLot;
if(lotSize <= minLot)
{
lotSize = minLot;
}
if(lotSize <= 20.0)
{
lotSize = NormalizeDouble(lotSize,2);
}
if(lotSize > 20.0)
{
lotSize = 20.0;
}
return lotSize;
}
bool EntryJudge(int tradeType)
{
double zonePips = zone *Point*10;
int currentPositions = 0;
int currentLongPositions =0;
int currentShortPositions = 0;
for(int i = 0 ; i < OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
currentPositions++;
if(OrderType() == tradeType && MathAbs(OrderOpenPrice()-Ask) <= zonePips && OrderType() == OP_BUY)
{
return false;
}
if(OrderType() == tradeType && MathAbs(OrderOpenPrice()-Bid) <= zonePips && OrderType() == OP_SELL)
{
return false;
}
if(OrderType() == OP_BUY) currentLongPositions++;
if(OrderType() == OP_SELL) currentShortPositions++;
}
}
if(currentPositions < maxPosition )
{
if(tradeType == OP_BUY && currentLongPositions < LongMAX)
{
return true;
}else{
return false;
}
if(tradeType == OP_SELL && currentShortPositions < ShortMAX)
{
return true;
}else{
return false;
}
}else{
return false;}
}
void CheckAndClosePositions(double profitPercent)
{
double accountBalance = AccountBalance();
int totalPositions = 0;
double totalProfit = 0;
for(int i = 0; i< OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
double positionProfit = OrderProfit() + OrderSwap() - OrderCommission();
totalProfit += positionProfit;
totalPositions++;
}
}
double eachProfit = accountBalance * profitPercent/100;
double targetTotalProfit = accountBalance * profitPercent/100;
if(totalPositions >1)
{
targetTotalProfit /= totalPositions;
}
for(int i = OrdersTotal() -1 ; i>= 0 ; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
double positionProfit = OrderProfit() + OrderSwap() - OrderCommission();
if(totalProfit >= targetTotalProfit)
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,clrBlue);
}
if(positionProfit >= eachProfit)
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,clrBlue);
}
}
}
}
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
CheckAndClosePositions(profitRate);
double tradeLot = calculateLotSize();
if(EntryJudge(OP_BUY) && LongOn == true)
{
EntryTriggerLong(tradeLot);
}
}
void EntryTriggerLong(double lots)
{
double SMA20_1 = iMA(NULL,0,20,0,MODE_SMA,PRICE_HIGH,1);
double SMA20_2 = iMA(NULL,0,20,0,MODE_SMA,PRICE_HIGH,2);
double close1 = Close[1];
double close2 = Close[2];
if(SMA20_1 < close1 && SMA20_2 >close2)
{
int ticket = OrderSend(NULL,OP_BUY,lots,Ask,5,0,0,"",0,0,clrBlue);
}
}

まぁ増えてはいますが、思いのほか伸びませんね!EAの難しいところだと思います!エントリーロジックや、資金管理を工夫すればもっと良い戦略もとれるかも!色々工夫してみてください!

おすすめFXアイテム
過去検証や分析はMT4かFT4、Trading viewがおすすめです。
無料が良い人はMT4で、MT4を使わせてくれる口座を使用すると良いです。おすすめはFXTF
チャート分析に毎月課金してもいいよって人はTrading viewがおすすめです。無料もあります


