#include <Trade\Trade.mqh> // Get code from other places
//--- Input Variables (Accessible from MetaTrader 5)
input double lot = 0.01;
input int slippage = 3;
input bool useStopLoss = true;
input double stopLossPips = 100;
input bool useTakeProfit = true;
input double takeProfitPips = 300;
input int MAperiodShort=5;
input int DonchianPeriod=100;
input int InverseFisherPeriodsshort=40;
input bool useTrailingStop=true;
input double trailingStopPips=40;
//--- Service Variables (Only accessible from the MetaEditor)
CTrade myTradingControlPanel;
double MaDataShort[];
int MaControlPanelShort;
double numberofMaDataShort;
double MaDataShort1,MaDataShort2,MaDataShort3;
double DonchianDataHigh[],DonchianDataLow[];
int DonchianControlPanel;
double numberofDonchianDataHigh,numberofDonchianDataLow;
double DonchianDataHigh1,DonchianDataHigh2,DonchianDataHigh3;
double DonchianDataLow1,DonchianDataLow2,DonchianDataLow3;
int P;
double currentBid, currentAsk;
double stopLossPipsFinal, takeProfitPipsFinal, stopLevelPips;
double stopLossLevel, takeProfitLevel;
int inverseFisherTransformPanelshort;
string ehlers_inverse_fisher_transform_2short;
double inverseFisherDatashort1[],inverseFisherDatashort2[];
double eInverseFisherDataNormalizeshort1, eInverseFisherDataNormalizeshort2;
int numberofInverseFisherDatashort1,numberofInverseFisherDatashort2;
double DS;
double newStopLossPips;
double newTrailingStopPrice;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
ArraySetAsSeries(MaDataShort,true);
MaControlPanelShort =iMA(_Symbol,_Period,MAperiodShort,0,MODE_SMA,PRICE_CLOSE);
ArraySetAsSeries(inverseFisherDatashort1,true);
ArraySetAsSeries(inverseFisherDatashort2,true);
inverseFisherTransformPanelshort = iCustom(_Symbol,_Period,"ehlers_inverse_fisher_transform_2",InverseFisherPeriodsshort);
ArraySetAsSeries(DonchianDataHigh,true);
ArraySetAsSeries(DonchianDataLow,true);
DonchianControlPanel=iCustom(_Symbol,_Period,"donchian_channel",DonchianPeriod);
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(MaControlPanelShort);
IndicatorRelease(DonchianControlPanel);
IndicatorRelease(inverseFisherTransformPanelshort);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// -------------------- Collect most current data --------------------
currentBid = SymbolInfoDouble(_Symbol,SYMBOL_BID); // Get latest Bid Price
currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Get latest Ask Price
numberofMaDataShort=CopyBuffer(MaControlPanelShort,0,0,5,MaDataShort);
MaDataShort1=MaDataShort[1];
MaDataShort2=MaDataShort[2];
MaDataShort3=MaDataShort[3];
numberofDonchianDataHigh=CopyBuffer(DonchianControlPanel,0,0,5,DonchianDataHigh);
numberofDonchianDataLow=CopyBuffer(DonchianControlPanel,1,0,5,DonchianDataLow);
DonchianDataHigh1=DonchianDataHigh[1];
DonchianDataHigh2=DonchianDataHigh[2];
DonchianDataHigh3=DonchianDataHigh[3];
DonchianDataLow1=DonchianDataLow[1];
DonchianDataLow2=DonchianDataLow[2];
DonchianDataLow3=DonchianDataLow[3];
numberofInverseFisherDatashort1 =CopyBuffer(inverseFisherTransformPanelshort,0,0,3,inverseFisherDatashort1);
numberofInverseFisherDatashort2 = CopyBuffer(inverseFisherTransformPanelshort,0,0,3,inverseFisherDatashort2);
eInverseFisherDataNormalizeshort1=NormalizeDouble(inverseFisherDatashort1[1],3);
eInverseFisherDataNormalizeshort2=NormalizeDouble(inverseFisherDatashort2[2],3);
DS=DonchianDataHigh1/DonchianDataLow1;
// -------------------- 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) ---
/*
Exits:
- Exit the long trade when SMA(10) crosses SMA(40) from top
- Exit the short trade when SMA(10) crosses SMA(40) from bottom
*/
// TDL 3: Enter exit rule for long trades
if (DonchianDataLow1<DonchianDataLow2&&
eInverseFisherDataNormalizeshort1<eInverseFisherDataNormalizeshort2)
{
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;
}
}
}
// TDL 4: Enter exit rule for short trades
// --------------------------------------------------------- //
// --------------------------------------------------------- //
if (DonchianDataHigh1>DonchianDataHigh2&&
eInverseFisherDataNormalizeshort1>eInverseFisherDataNormalizeshort2)
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;
}
}
}
if(useTrailingStop==true)
{
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
newTrailingStopPrice = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID) - trailingStopPips*(_Point*P), _Digits);
if(newTrailingStopPrice > PositionGetDouble(POSITION_PRICE_OPEN))
{ //if price has moved in favour of trade by more than trailingStopPips
if(newTrailingStopPrice > PositionGetDouble(POSITION_SL))
{ //if existing SL is not as tight as newTrailingStopPrice
myTradingControlPanel.PositionModify(_Symbol, newTrailingStopPrice, PositionGetDouble(POSITION_TP));
Print("Trailing Stop has moved to ", newTrailingStopPrice);
}
}
}
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
newTrailingStopPrice = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK) + trailingStopPips*(_Point*P), _Digits);
if(newTrailingStopPrice < PositionGetDouble(POSITION_PRICE_OPEN))
{ //if price has moved in favour of trade by more than trailingStopPips
if(newTrailingStopPrice < PositionGetDouble(POSITION_SL) || PositionGetDouble(POSITION_SL) == 0)
{ //if existing SL is not as tight as newTrailingStopPrice
myTradingControlPanel.PositionModify(_Symbol, newTrailingStopPrice, PositionGetDouble(POSITION_TP));
Print("Trailing Stop has moved to ", newTrailingStopPrice);
}
}
}
}
// -------------------- ENTRIES --------------------
if(PositionSelect(_Symbol) == false) // We have no open position
{
// --- Entry Rules (Long Trades) ---
/*
Entries:
- Enter a long trade when SMA(10) crosses SMA(40) from bottom
- Enter a short trade when SMA(10) crosses SMA(40) from top
*/
// TDL 1: Enter entry rule for long trades
// --------------------------------------------------------- //
if (DonchianDataHigh1>DonchianDataHigh2&&MaDataShort1>MaDataShort2
&&DonchianDataLow1>=DonchianDataLow2&&eInverseFisherDataNormalizeshort1>eInverseFisherDataNormalizeshort2&&DS>1.0097)
// &&eInverseFisherDataNormalizeshort1>eInverseFisherDataNormalizeshort2&&
//eInverseFisherDataNormalizeshort1>0&&eInverseFisherDataNormalizeshort2>0)
{
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) ---
/*
Exit:
- Exit the long trade when SMA(10) crosses SMA(40) from top
- Exit the short trade when SMA(10) crosses SMA(40) from bottom
*/
// TDL 2: Enter entry rule for short trades
// --------------------------------------------------------- //
else if (DonchianDataLow1<DonchianDataLow2&&MaDataShort1<MaDataShort2
&&DonchianDataHigh1<=DonchianDataHigh2&&eInverseFisherDataNormalizeshort1<eInverseFisherDataNormalizeshort2&&DS>1.0097)
// &&eInverseFisherDataNormalizeshort1<eInverseFisherDataNormalizeshort2&&
// eInverseFisherDataNormalizeshort1<0&&eInverseFisherDataNormalizeshort2<0)
{
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, currentBid, 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;
}
}
}
}
//+------------------------------------------------------------------+