🌟
MQL5でブレイクイーブン機能をつける
input string BreakEven_Comment = "----ブレークイーブンの設定----";
input int BreakEvenTime = 60; // 建値に移動するまでの時間(分)
input double BreakEvenProfit = 10; // 建値に移動するまでの利益(ポイント)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void mode_time_break_even(int side)
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
if(PositionGetInteger(POSITION_TYPE) == side && PositionGetInteger(POSITION_MAGIC) == MagicNumber)
{
if(Symbol() == PositionGetString(POSITION_SYMBOL))
{
long openTime = PositionGetInteger(POSITION_TIME);
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double currentPrice = (side == 0) ? SymbolInfoDouble(NULL,SYMBOL_BID) : SymbolInfoDouble(NULL,SYMBOL_ASK);
datetime currentTime = TimeCurrent();
// 指定した時間が経過しているかをチェック
bool isTimeElapsed = (currentTime - openTime) >= BreakEvenTime * 60;
// 指定した利益が得られているかをチェック
bool isProfitAchieved = (side == 0) ? (currentPrice - openPrice) >= BreakEvenProfit * _Point : (openPrice - currentPrice) >= BreakEvenProfit * _Point;
if(isTimeElapsed && isProfitAchieved)
{
// ストップロスを建値に設定
position_stop_modify(side, openPrice);
}
}
}
}
}
void position_stop_modify(int side, double price)
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
if("" != PositionGetSymbol(i))
{
if(PositionGetInteger(POSITION_TYPE)==side)
{
if(Symbol()==PositionGetString(POSITION_SYMBOL))
{
if(PositionGetInteger(POSITION_MAGIC)==MagicNumber)
{
if(price > PositionGetDouble(POSITION_SL) + 1 * _Point || price < PositionGetDouble(POSITION_SL) - 1 * _Point )
position_modify(PositionGetInteger(POSITION_TICKET),price,0);
}
}
}
}
}
}
void position_modify(ulong position_ticket,double sl = 0,double tp = 0){
MqlTradeRequest req = {};
MqlTradeResult res = {};
req.action =TRADE_ACTION_SLTP;
req.symbol =Symbol();
req.magic =MagicNumber;
if(sl!=0){req.sl =sl;}
if(tp!=0){req.tp =tp;}
req.position=position_ticket;
bool order_result = OrderSend(req, res);
if(order_result == false)
{
Print(res.retcode);
}
}
Discussion