inputuchar Smooth=9;
inputuchar Length=9;
inputfloat Min_Threshold=0.0;
inputuchar Stop_Loss=100;
inputuchar Take_Profit=100;
int Trigger_Handle;
int EA_Magic = 19288;
double Trigger[];
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
Trigger_Handle = iCustom(NULL, 0, "MyMACross", Smooth, Length, Min_Threshold);
if ( Trigger_Handle < 0 )
Print("Failed to get Handles");
ArraySetAsSeries(Trigger,true);
return(0);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
IndicatorRelease(Trigger_Handle);
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
// We will use the static Old_Time variable to serve the bar time.// At each OnTick execution we will check the current bar time with the saved one.// If the bar time isn't equal to the saved time, it indicates that we have a new tick.staticdatetime Old_Time;
datetime New_Time[1];
bool IsNewBar=false;
// copying the last bar time to the element New_Time[0]int copied=CopyTime(_Symbol,_Period,0,1,New_Time);
if(copied>0) // ok, the data has been copied successfully
{
if(Old_Time!=New_Time[0]) // if old time isn't equal to new bar time
{
IsNewBar=true; // if it isn't a first call, the new bar has appearedif(MQL5InfoInteger(MQL5_DEBUGGING)) Print("We have new bar here ",New_Time[0]," old time was ",Old_Time);
Old_Time=New_Time[0]; // saving bar time
}
}
else
{
Print("Error in copying historical times data, error =",GetLastError());
ResetLastError();
return;
}
//--- EA should only check for new trade if we have a new barif(IsNewBar==false)
return;
int BarCount = Bars(NULL,0);
MqlTick m_price; // To be used for getting recent/latest price quotesMqlTradeRequest m_request; // To be used for sending our trade requestsMqlTradeResult m_result; // To be used to get our trade resultsMqlRates m_rate[]; // To be used to store the prices, volumes and spread of each barCopyRates(_Symbol,_Period,0,3,m_rate);
CopyBuffer(Trigger_Handle, 0, 0, 30, Trigger);
bool Buy_opened=false; // variable to hold the result of Buy opened positionbool Sell_opened=false; // variable to hold the result of Sell opened positionif (PositionSelect(_Symbol) ==true) // we have an opened position
{
if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
Buy_opened = true; //It is a Buy
}
elseif(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
Sell_opened = true; // It is a Sell
}
}
bool Buy_Condition = (Trigger[1] == -1);
bool Sell_Condition = (Trigger[1] == 1);
double MyLots = 0.1;
if ( Buy_Condition == true)
{
if (!Buy_opened)
{
if(Sell_opened == true)
MyLots = 0.2;
m_request.action = TRADE_ACTION_DEAL; // immediate order execution
m_request.price = NormalizeDouble(m_price.ask,_Digits); // latest Bid price
m_request.sl = NormalizeDouble(m_price.ask - Stop_Loss*_Point,_Digits); // Stop Loss
m_request.tp = NormalizeDouble(m_price.ask + Take_Profit*_Point,_Digits); // Take Profit
m_request.symbol = _Symbol; // currency pair
m_request.volume = MyLots; // number of lots to trade
m_request.magic = EA_Magic; // Order Magic Number
m_request.type= ORDER_TYPE_BUY; // Buy Order
m_request.type_filling = ORDER_FILLING_AON; // Order execution type
m_request.deviation=100; // Deviation from current price//--- send orderOrderSend(m_request, m_result);
// get the result codeif(m_result.retcode==10009 || m_result.retcode==10008) //Request is completed or order placed
{
Print("A Buy order has been successfully placed with Ticket#:",m_result.order,"!!");
}
else
{
Print("The Buy order request could not be completed -error:",GetLastError());
ResetLastError();
return;
}
}
}
if ( Sell_Condition == true)
{
if (!Sell_opened)
{
if(Buy_opened == true)
MyLots = 0.2;
m_request.action = TRADE_ACTION_DEAL; // immediate order execution
m_request.price = NormalizeDouble(m_price.bid,_Digits); // latest Bid price
m_request.sl = NormalizeDouble(m_price.bid + Stop_Loss*_Point,_Digits); // Stop Loss
m_request.tp = NormalizeDouble(m_price.bid - Take_Profit*_Point,_Digits); // Take Profit
m_request.symbol = _Symbol; // currency pair
m_request.volume = MyLots; // number of lots to trade
m_request.magic = EA_Magic; // Order Magic Number
m_request.type= ORDER_TYPE_SELL; // Sell Order
m_request.type_filling = ORDER_FILLING_AON; // Order execution type
m_request.deviation=100; // Deviation from current price//--- send orderOrderSend(m_request, m_result);
if(m_result.retcode==10009 || m_result.retcode==10008) //Request is completed or order placed
{
Print("A Sell order has been successfully placed with Ticket#:",m_result.order,"!!");
}
else
{
Print("The Sell order request could not be completed -error:",GetLastError());
ResetLastError();
return;
}
}
}
return;
}
The Expert Advisors programming in MQL5 is simple, and you can learn it easy. In this step by step guide, you will see the basic steps required in writing a simple Expert Advisor based on a developed trading strategy. The structure of an Expert Advisor, the use of built-in technical indicators and trading functions, the details of the Debug mode and use of the Strategy Tester are presented.
Hello Everyone,
I cant seem to figure out this EA, when I place it on a chart it works as intended. When I backtest it no trades are made.
Can you eyeball this code and tell me what I am doing wrong?
Most of the code is from this article
https://www.mql5.com/en/articles/100
Thank you
EK