Untitled
Untitled
CTrade myTradingControlPanel;
double shortSmaData[], longSmaData[]; // You can declare multiple variables of the
same data type in the same line.
int numberOfShortSmaData, numberOfLongSmaData;
int shortSmaControlPanel, longSmaControlPanel;
int P;
double currentBid, currentAsk;
double stopLossPipsFinal, takeProfitPipsFinal, stopLevelPips;
double stopLossLevel, takeProfitLevel;
double shortSma1, shortSma2, longSma1, longSma2;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
ArraySetAsSeries(shortSmaData,true); // Setting up table/array for time series
data
ArraySetAsSeries(longSmaData,true); // Setting up table/array for time series
data
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
IndicatorRelease(shortSmaControlPanel);
IndicatorRelease(longSmaControlPanel);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// -------------------- Collect most current data --------------------
currentBid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
numberOfShortSmaData = CopyBuffer(shortSmaControlPanel, 0, 0, 3,
shortSmaData); // Collect most current SMA(10) Data and store it in the
datatable/array shortSmaData[]
numberOfLongSmaData = CopyBuffer(longSmaControlPanel, 0, 0, 3, longSmaData); //
Collect most current SMA(10) Data and store it in the datatable/array longSmaData[]
shortSma1 = shortSmaData[1];
shortSma2 = shortSmaData[2];
longSma1 = longSmaData[1];
longSma2 = longSmaData[2];
// Explanation: Stop Loss and Take Profit levels can't be too close to our order
execution price. We will talk about this again in a later lecture.
// Resources for learning more: https://book.mql4.com/trading/orders (ctrl-f
search "stoplevel"); https://book.mql4.com/appendix/limits
// --------------------------------------------------------- //
if(shortSma2 > longSma2 && shortSma1 <= longSma1) // Rule to exit long trades
// --------------------------------------------------------- //
{
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) // If it is Buy
position
{
if(myTradingControlPanel.ResultRetcode()==10008 ||
myTradingControlPanel.ResultRetcode()==10009) //Request is completed or order
placed
{
Print("Exit rules: A close order has been successfully placed with
Ticket#: ",myTradingControlPanel.ResultOrder());
}
else
{
Print("Exit rules: The close order request could not be
completed.Error: ",GetLastError());
ResetLastError();
return;
}
}
}
// --------------------------------------------------------- //
if(shortSma2 < longSma2 && shortSma1 >= longSma1) // Rule to exit short
trades
// --------------------------------------------------------- //
{
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) // If it is
Sell position
{
if(myTradingControlPanel.ResultRetcode()==10008 ||
myTradingControlPanel.ResultRetcode()==10009) //Request is completed or order
placed
{
Print("Exit rules: A close order has been successfully placed with
Ticket#: ", myTradingControlPanel.ResultOrder());
}
else
{
Print("Exit rules: The close order request could not be completed.
Error: ", GetLastError());
ResetLastError();
return;
}
}
}
// --------------------------------------------------------- //
if(shortSma2 < longSma2 && shortSma1 >= longSma1) // Rule to enter long
trades
// --------------------------------------------------------- //
if(myTradingControlPanel.ResultRetcode()==10008 ||
myTradingControlPanel.ResultRetcode()==10009) //Request is completed or order
placed
{
Print("Entry rules: A Buy order has been successfully placed with
Ticket#: ", myTradingControlPanel.ResultOrder());
}
else
{
Print("Entry rules: The Buy order request could not be completed.
Error: ", GetLastError());
ResetLastError();
return;
}
// --------------------------------------------------------- //
if(shortSma2 > longSma2 && shortSma1 <= longSma1) // Rule to enter short
trades
// --------------------------------------------------------- //
if(myTradingControlPanel.ResultRetcode()==10008 ||
myTradingControlPanel.ResultRetcode()==10009) //Request is completed or order
placed
{
Print("Entry rules: A Sell order has been successfully placed with
Ticket#: ", myTradingControlPanel.ResultOrder());
}
else
{
Print("Entry rules: The Sell order request could not be
completed.Error: ", GetLastError());
ResetLastError();
return;
}
//+------------------------------------------------------------------+