[go: up one dir, main page]

0% found this document useful (0 votes)
76 views5 pages

Untitled

Uploaded by

Bouchaib Saidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views5 pages

Untitled

Uploaded by

Bouchaib Saidi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//+------------------------------------------------------------------+

//| Black Algo Basic Template 1.00.mq5


//| Copyright 2016, Lucas Liew
//| https://blackalgotechnologies.com/
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Lucas Liew"
#property link "https://blackalgotechnologies.com/"
#property version "1.00"

#include <Trade\Trade.mqh> // Get code from other places

//--- Input Variables (Accessible from MetaTrader 5)

input double lot = 1;


input int shortSmaPeriods = 10;
input int longSmaPeriods = 40;
input int slippage = 3;
input bool useStopLoss = true;
input double stopLossPips = 30;
input bool useTakeProfit = true;
input double takeProfitPips = 60;

//--- Service Variables (Only accessible from the MetaEditor)

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

shortSmaControlPanel = iMA(_Symbol, _Period, shortSmaPeriods, 0, MODE_SMA,


PRICE_CLOSE); // Getting the Control Panel/Handle for short SMA
longSmaControlPanel = iMA(_Symbol, _Period, longSmaPeriods, 0, MODE_SMA,
PRICE_CLOSE); // Getting the Control Panel/Handle for long SMA

if(_Digits == 5 || _Digits == 3 || _Digits == 1) P = 10;else P = 1; // To


account for 5 digit brokers

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];

// -------------------- Technical Requirements --------------------

// 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

stopLevelPips = (double) (SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL) +


SymbolInfoInteger(_Symbol, SYMBOL_SPREAD)) / P; // Defining minimum StopLevel

if (stopLossPips < stopLevelPips)


{
stopLossPipsFinal = stopLevelPips;
}
else
{
stopLossPipsFinal = stopLossPips;
}

if (takeProfitPips < stopLevelPips)


{
takeProfitPipsFinal = stopLevelPips;
}
else
{
takeProfitPipsFinal = takeProfitPips;
}

// -------------------- EXITS --------------------

if(PositionSelect(_Symbol) == true) // We have an open position


{
// --- Exit Rules (Long Trades) ---

// --------------------------------------------------------- //

if(shortSma2 > longSma2 && shortSma1 <= longSma1) // Rule to exit long trades

// --------------------------------------------------------- //

{
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) // If it is Buy
position
{

myTradingControlPanel.PositionClose(_Symbol); // Closes position


related to this symbol

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
{

myTradingControlPanel.PositionClose(_Symbol); // Closes position


related to this symbol

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;
}
}
}

// -------------------- ENTRIES --------------------

if(PositionSelect(_Symbol) == false) // We have no open position


{

// --- Entry Rules (Long Trades) ---

// --------------------------------------------------------- //

if(shortSma2 < longSma2 && shortSma1 >= longSma1) // Rule to enter long
trades

// --------------------------------------------------------- //

if (useStopLoss) stopLossLevel = currentAsk - stopLossPipsFinal * _Point *


P; else stopLossLevel = 0.0;
if (useTakeProfit) takeProfitLevel = currentAsk + takeProfitPipsFinal *
_Point * P; else takeProfitLevel = 0.0;

myTradingControlPanel.PositionOpen(_Symbol, ORDER_TYPE_BUY, lot,


currentAsk, stopLossLevel, takeProfitLevel, "Buy Trade. Magic Number #" + (string)
myTradingControlPanel.RequestMagic()); // Open a Buy position

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;
}

// --- Entry Rules (Short Trades) ---

// --------------------------------------------------------- //

if(shortSma2 > longSma2 && shortSma1 <= longSma1) // Rule to enter short
trades
// --------------------------------------------------------- //

if (useStopLoss) stopLossLevel = currentBid + stopLossPipsFinal * _Point *


P; else stopLossLevel = 0.0;
if (useTakeProfit) takeProfitLevel = currentBid - takeProfitPipsFinal *
_Point * P; else takeProfitLevel = 0.0;

myTradingControlPanel.PositionOpen(_Symbol, ORDER_TYPE_SELL, lot,


currentAsk, stopLossLevel, takeProfitLevel, "Sell Trade. Magic Number #" + (string)
myTradingControlPanel.RequestMagic()); // Open a Sell position

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;
}

//+------------------------------------------------------------------+

You might also like