下面是一个简单的MT4 EA示例代码:

#property strict #property indicator_chart_window

//输入变量 input int RSI_Period = 14; //RSI指标周期 input int RSI_Buy_Limit = 30; //RSI买入阈值 input int RSI_Sell_Limit = 70; //RSI卖出阈值 input int MACD_Fast_Period = 12; //MACD快速线周期 input int MACD_Slow_Period = 26; //MACD慢速线周期 input int MACD_Signal_Period = 9; //MACD信号线周期 input double Risk_Percent = 1.0; //风险百分比

//全局变量 double Lot_Size; //手数大小 double Account_Balance; //账户余额 double Account_Equity; //账户净值 int Total_Trades; //总交易数 int Profit_Trades; //盈利交易数 int Loss_Trades; //亏损交易数 double Total_Profit; //总盈亏 double Highest_Profit; //最高盈利 double Lowest_Profit; //最低亏损

//初始化函数 void OnInit() { //计算手数大小 Lot_Size = NormalizeDouble(AccountBalance() * Risk_Percent / 100 / 10000, 2); //获取账户信息 Account_Balance = AccountBalance(); Account_Equity = AccountEquity(); //初始化交易统计变量 Total_Trades = 0; Profit_Trades = 0; Loss_Trades = 0; Total_Profit = 0; Highest_Profit = 0; Lowest_Profit = 0; }

//循环函数 void OnTick() { //计算RSI指标值 double RSI_Value = iRSI(NULL, 0, RSI_Period, PRICE_CLOSE, 0); //计算MACD指标值 double MACD_Main = iMACD(NULL, 0, MACD_Fast_Period, MACD_Slow_Period, MACD_Signal_Period, PRICE_CLOSE, MODE_MAIN, 0); double MACD_Signal = iMACD(NULL, 0, MACD_Fast_Period, MACD_Slow_Period, MACD_Signal_Period, PRICE_CLOSE, MODE_SIGNAL, 0); //检查是否有足够的资金开仓 if(Account_Equity >= Lot_Size * MarketInfo(Symbol(), MODE_MARGINREQUIRED)) { //检查是否有持仓 if(OrdersTotal() == 0) { //检查RSI是否满足买入条件 if(RSI_Value < RSI_Buy_Limit) { //开多仓 int Ticket = OrderSend(Symbol(), OP_BUY, Lot_Size, Ask, 3, Bid - StopLevel * Point, Bid + TakeProfit * Point, "RSI Buy", MagicNumber, 0, Green); if(Ticket > 0) { Total_Trades++; } } //检查RSI是否满足卖出条件 if(RSI_Value > RSI_Sell_Limit) { //开空仓 int Ticket = OrderSend(Symbol(), OP_SELL, Lot_Size, Bid, 3, Ask + StopLevel * Point, Ask - TakeProfit * Point, "RSI Sell", MagicNumber, 0, Red); if(Ticket > 0) { Total_Trades++; } } } else { //检查是否有MACD信号 if((MACD_Main > 0 && MACD_Signal > 0) || (MACD_Main < 0 && MACD_Signal < 0)) { //平掉所有相反的持仓并转变方向交易 for(int i = OrdersTotal() - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if(OrderType() == OP_BUY && MACD_Main < 0 && MACD_Signal < 0) { OrderClose(OrderTicket(), OrderLots(), Bid, 3, Green); int Ticket = OrderSend(Symbol(), OP_SELL, Lot_Size, Bid, 3, Ask + StopLevel * Point, Ask - TakeProfit * Point, "MACD Sell", MagicNumber, 0, Red); if(Ticket > 0) { Total_Trades++; } } if(OrderType() == OP_SELL && MACD_Main > 0 && MACD_Signal > 0) { OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red); int Ticket = OrderSend(Symbol(), OP_BUY, Lot_Size, Ask, 3, Bid - StopLevel * Point, Bid + TakeProfit * Point, "MACD Buy", MagicNumber, 0, Green); if(Ticket > 0) { Total_Trades++; } } } } } } } }

//交易执行函数 void OnTrade() { //统计交易结果 for(int i = OrdersHistoryTotal() - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) { double Profit = OrderProfit() + OrderCommission() + OrderSwap(); Total_Profit += Profit; if(Profit > 0) { Profit_Trades++; if(Profit > Highest_Profit || Highest_Profit == 0) { Highest_Profit = Profit; } } if(Profit < 0) { Loss_Trades++; if(Profit < Lowest_Profit || Lowest_Profit == 0) { Lowest_Profit = Profit; } } } } }


原文地址: http://www.cveoy.top/t/topic/b90i 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录