我正在尝试使用 EMA 构建自己的 EA。当程序启动时,它将根据慢速和快速 EMA 的相对位置进行买入/卖出。然后程序应该在下一个 EMA 交叉时关闭交易。我的第一个障碍是找到一种方法来计算蜡烛图收盘时的 EMA,所以我请 chatgpt 来帮助我。经过多次尝试,他给我带来了一些使用 currenTime 和 prevTime 值 + if 条件的东西。尽管我不太明白为什么,但计算仍然有效。如果有人可以向我解释它,我会很高兴。现在我正在努力关闭我的订单,当打印 EMA 值时,我看到了交叉,但程序仍然不会关闭交易。我正在分享代码,我在 M15 上从 4 月 24 日到 4 月 25 日对英镑/日元进行回测,有 2 个交叉点(图片),程序应该在当天运行 3 笔交易。有什么帮助吗?下面是我的代码。
//+------------------------------------------------------------------+
//| EACrossover.mq4 |
//| Copyright 2024, Company Name |
//| http://www.company.net |
//+------------------------------------------------------------------+
#property strict
// Global variables
input int fastEMA_Length = 9; // Length of fast EMA
input int slowEMA_Length = 21; // Length of slow EMA
input double lotSize = 0.1; // Lot size
input int slippage = 3; // slippage
input int magicNumber = 123456; // magic number
int buy_ticket, sell_ticket; // variables that store the OrderSend values
bool buying_order=false;
bool selling_order=false;
int i = 0; // bar counter
datetime prevTime = 0; // Variable to store the time of the previous tick (Created by ChatGPT)
// It is used for the calculation of EMA at the closure of candlestick
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int OnInit()
{
Print("EA Initialization");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
datetime currentTime = Time[0]; // Get the time of the current tick
// Check if the current tick is on a new bar by comparing its time with the previous tick
if(currentTime != prevTime)
{
// Update the previous tick time with the current tick time
prevTime = currentTime;
i++; // Increment the bar counter
// Calculate the EMAs for the current bar
double currentFastEMA = iMA(NULL, 0, fastEMA_Length, 0, MODE_EMA, PRICE_CLOSE, 0);
double currentSlowEMA = iMA(NULL, 0, slowEMA_Length, 0, MODE_EMA, PRICE_CLOSE, 0);
if(OrdersTotal() == 0) // We open a trade when there is no trade actually opened
{
// Check EMA position to buy or sell
if(currentFastEMA > currentSlowEMA)
{
buy_ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, slippage, 0, 0, "", magicNumber, 0, Green);
buying_order=true;
Print("I'm buying !");
}
else
{
sell_ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, slippage, 0, 0, "", magicNumber, 0, Green);
selling_order=true;
Print("I'm selling !");
}
}
else
{
Print("FastEMA=",currentFastEMA," slowEMA=",currentSlowEMA," iteration n°",i);
// If a trade is opened we decide to close it at the next EMA crossover
if(buying_order==true) //closing buying order
{
if(currentFastEMA < currentSlowEMA)
{
OrderClose(buy_ticket,lotSize, PRICE_CLOSE,slippage,clrNONE);
}
buying_order=false;
}
if(selling_order==true) //closing selling order
{
if(currentFastEMA > currentSlowEMA)
{
OrderClose(sell_ticket,lotSize,PRICE_CLOSE,slippage,clrNONE);
}
selling_order=false;
}
}
}
}
有时我会遇到错误 138,具体取决于 TF。
要关闭买单,必须以买入价关闭。 要关闭卖单,必须以要价关闭。
这意味着我们重写代码:
if(buying_order && currentFastEMA < currentSlowEMA) //closing buy order
{
OrderClose(buy_ticket,lotSize, **Bid**,slippage,clrNONE);
buying_order=false;
}
if(selling_order && currentFastEMA > currentSlowEMA) //closing sell order
{
OrderClose(sell_ticket,lotSize,**Ask**,slippage,clrNONE);
selling_order=false;
}