//+------------------------------------------------------------------+
//| ScalpingEA.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict
//--- input parameters
input double lots = 0.01; // 交易手数,基准手0.01
input double dailyVolumeTarget = 1.3; // 每日交易量目标(手数),大于1.3手
input int breakEvenPoints = 35; // 激活移动止损的突破点数(35点)
input int noBreakEvenTime = 60; // 没有突破时平仓的时间(秒)
input int stopLossPoints = 200; // 初始止损点数
input int takeProfitPoints = 200; // 初始止盈点数
input int magicNumber = 12345; // 订单魔术数字,用于标识EA订单
//--- global variables
double dailyVolume = 0.0; // 当日累计交易量
datetime lastDay = 0; // 记录当前日期
datetime lastTradeTime = 0; // 上次开仓时间
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// 初始化时重置交易量
dailyVolume = 0.0;
lastDay = 0;
lastTradeTime = 0;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// 清理
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// 检查新的一天
datetime currentTime = TimeCurrent();
int currentDay = TimeDay(currentTime);
if (lastDay != currentDay)
{
// 新的一天重置交易量
dailyVolume = 0.0;
lastDay = currentDay;
}
// 检查交易时间(0-23点)
int hour = TimeHour(currentTime);
if (hour < 0 || hour > 23)
return; // 非交易时间
// 如果当日交易量已达到目标,则不再开新仓
if (dailyVolume >= dailyVolumeTarget)
return;
// 检查开仓条件:使用随机指标示例
bool buyCondition = false;
bool sellCondition = false;
double k = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
double d = iStochastic(Symbol(), 0, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);
if (k < 20 && d < 20 && k > d)
buyCondition = true;
if (k > 80 && d > 80 && k < d)
sellCondition = true;
// 基于时间控制开仓频率:每60秒可开一单
if (currentTime - lastTradeTime < 60)
return;
// 开仓操作
if (buyCondition || sellCondition)
{
double openPrice = 0.0;
double stopLoss = 0.0;
double takeProfit = 0.0;
int orderType = -1;
if (buyCondition)
{
orderType = OP_BUY;
openPrice = Ask;
stopLoss = Ask - stopLossPoints * 10 * Point; // 计算止损价格
takeProfit = Ask + takeProfitPoints * 10 * Point; // 计算止盈价格
}
else if (sellCondition)
{
orderType = OP_SELL;
openPrice = Bid;
stopLoss = Bid + stopLossPoints * 10 * Point; // 计算止损价格
takeProfit = Bid - takeProfitPoints * 10 * Point; // 计算止盈价格
}
int ticket = OrderSend(Symbol(), orderType, lots, openPrice, 3, stopLoss, takeProfit, "", magicNumber, 0, clrGreen);
if (ticket > 0)
{
dailyVolume += lots; // 更新交易量
lastTradeTime = currentTime; // 更新最后交易时间
}
}
// 检查现有订单的平仓条件
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol())
{
// 计算点大小(标准点)
double pipSize = 10 * Point;
// 检查是否突破35点
bool breakEven = false;
if (OrderType() == OP_BUY)
{
if (Bid >= OrderOpenPrice() + breakEvenPoints * pipSize)
breakEven = true;
}
else if (OrderType() == OP_SELL)
{
if (Ask <= OrderOpenPrice() - breakEvenPoints * pipSize)
breakEven = true;
}
if (breakEven)
{
// 激活移动止损:将止损移动至开仓价
double newStop = OrderOpenPrice();
if (OrderType() == OP_BUY)
{
if (OrderStopLoss() < newStop)
OrderModify(OrderTicket(), OrderOpenPrice(), newStop, OrderTakeProfit(), 0, clrBlue);
}
else if (OrderType() == OP_SELL)
{
if (OrderStopLoss() > newStop || OrderStopLoss() == 0)
OrderModify(OrderTicket(), OrderOpenPrice(), newStop, OrderTakeProfit(), 0, clrBlue);
}
}
else
{
// 没有突破:检查是否已过60秒
if (currentTime - OrderOpenTime() >= noBreakEvenTime)
{
// 60秒后平仓
if (OrderType() == OP_BUY)
OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrRed);
else if (OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrRed);
}
}
}
}
}
}
//+------------------------------------------------------------------+ |