请帮我使用MQL4文件编辑器,编写一个ea,添加下多单按钮,下空单按钮,下挂多单按钮,下挂空单按钮,并点击按钮后,可以输入下单的仓位。请帮我写出具体的代码,并注释是每句的含义
时间: 2024-03-25 17:41:20 浏览: 135
好的,我可以为您提供一些MQL4代码来满足您的需求。
首先,我们需要定义一些全局变量来保存按钮的状态和交易方向:
```MQL4
bool buyButtonPressed = false;
bool sellButtonPressed = false;
bool buyLimitButtonPressed = false;
bool sellLimitButtonPressed = false;
int lotSize = 0;
```
然后,我们可以在初始化函数中创建按钮和设置按钮的坐标和大小:
```MQL4
int init() {
// 创建按钮
ObjectCreate("buyButton", OBJ_BUTTON, 0, 0, 0);
ObjectCreate("sellButton", OBJ_BUTTON, 0, 0, 0);
ObjectCreate("buyLimitButton", OBJ_BUTTON, 0, 0, 0);
ObjectCreate("sellLimitButton", OBJ_BUTTON, 0, 0, 0);
// 设置按钮的坐标和大小
ObjectSet("buyButton", OBJPROP_XDISTANCE, 10);
ObjectSet("buyButton", OBJPROP_YDISTANCE, 10);
ObjectSet("buyButton", OBJPROP_WIDTH, 100);
ObjectSet("buyButton", OBJPROP_HEIGHT, 30);
ObjectSet("buyButton", OBJPROP_TEXT, "Buy");
ObjectSet("sellButton", OBJPROP_XDISTANCE, 120);
ObjectSet("sellButton", OBJPROP_YDISTANCE, 10);
ObjectSet("sellButton", OBJPROP_WIDTH, 100);
ObjectSet("sellButton", OBJPROP_HEIGHT, 30);
ObjectSet("sellButton", OBJPROP_TEXT, "Sell");
ObjectSet("buyLimitButton", OBJPROP_XDISTANCE, 230);
ObjectSet("buyLimitButton", OBJPROP_YDISTANCE, 10);
ObjectSet("buyLimitButton", OBJPROP_WIDTH, 100);
ObjectSet("buyLimitButton", OBJPROP_HEIGHT, 30);
ObjectSet("buyLimitButton", OBJPROP_TEXT, "Buy Limit");
ObjectSet("sellLimitButton", OBJPROP_XDISTANCE, 340);
ObjectSet("sellLimitButton", OBJPROP_YDISTANCE, 10);
ObjectSet("sellLimitButton", OBJPROP_WIDTH, 100);
ObjectSet("sellLimitButton", OBJPROP_HEIGHT, 30);
ObjectSet("sellLimitButton", OBJPROP_TEXT, "Sell Limit");
return(0);
}
```
接下来,我们需要在OnChartEvent()函数中检测按钮是否被按下,并弹出一个输入框让用户输入下单的仓位:
```MQL4
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam) {
if (id == CHARTEVENT_OBJECT_CLICK) { // 检测按钮是否被点击
if (sparam == "buyButton") { // 多单按钮
buyButtonPressed = true;
sellButtonPressed = false;
buyLimitButtonPressed = false;
sellLimitButtonPressed = false;
lotSize = (int)MessageBox("Please enter lot size:", "Buy", MB_OKCANCEL | MB_ICONINFORMATION);
} else if (sparam == "sellButton") { // 空单按钮
buyButtonPressed = false;
sellButtonPressed = true;
buyLimitButtonPressed = false;
sellLimitButtonPressed = false;
lotSize = (int)MessageBox("Please enter lot size:", "Sell", MB_OKCANCEL | MB_ICONINFORMATION);
} else if (sparam == "buyLimitButton") { // 挂多单按钮
buyButtonPressed = false;
sellButtonPressed = false;
buyLimitButtonPressed = true;
sellLimitButtonPressed = false;
lotSize = (int)MessageBox("Please enter lot size:", "Buy Limit", MB_OKCANCEL | MB_ICONINFORMATION);
} else if (sparam == "sellLimitButton") { // 挂空单按钮
buyButtonPressed = false;
sellButtonPressed = false;
buyLimitButtonPressed = false;
sellLimitButtonPressed = true;
lotSize = (int)MessageBox("Please enter lot size:", "Sell Limit", MB_OKCANCEL | MB_ICONINFORMATION);
}
}
}
```
最后,我们可以在OnTick()函数中下单:
```MQL4
void OnTick() {
if (buyButtonPressed) { // 多单按钮被按下
OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, 0, 0, "Buy Order", 0, 0, Green);
buyButtonPressed = false;
} else if (sellButtonPressed) { // 空单按钮被按下
OrderSend(Symbol(), OP_SELL, lotSize, Bid, 3, 0, 0, "Sell Order", 0, 0, Red);
sellButtonPressed = false;
} else if (buyLimitButtonPressed) { // 挂多单按钮被按下
OrderSend(Symbol(), OP_BUYLIMIT, lotSize, Ask - 10 * Point, 3, Ask - 20 * Point, Ask + 20 * Point, "Buy Limit Order", 0, 0, Green);
buyLimitButtonPressed = false;
} else if (sellLimitButtonPressed) { // 挂空单按钮被按下
OrderSend(Symbol(), OP_SELLLIMIT, lotSize, Bid + 10 * Point, 3, Bid + 20 * Point, Bid - 20 * Point, "Sell Limit Order", 0, 0, Red);
sellLimitButtonPressed = false;
}
}
```
以上就是完整的代码,下单时会弹出一个输入框让用户输入下单的仓位,然后根据按钮的状态下单。需要注意的是,这只是一个简单的示例代码,您需要根据您自己的需求进行修改和完善。