//+------------------------------------------------------------------+//+---//+------------------------------------------------------------------+//| TSI_CD EA.mq5//| Kamau Muchiri//| https://www.mql5.com//+------------------------------------------------------------------+#property copyright"Kamau Muchiri"#property link"https://www.mql5.com"#property version"1.00"//--- input parametersinputint Stoploss = 46; //Stoploss in pipsinputint TakeProfit = 96; //Takeprofit in pipsinputint MA_Period1 = 12; // Short term MA periodinputint MA_Period2 = 54; //Long term MA periodinputint ema1 = 9; //First MA smoothing of TSIinputint ema2 = 5; //Second smoothing of TSIinputint sMAp = 5; //Signal line MA periodinputENUM_MA_METHOD MAmode = MODE_EMA; //M0ving Average typeinputint Magic = 12345; // EA Magic numberinputdouble Lotsize = 0.05; //Lotsize//Other Global parametersint TSICDHandle; //Handle for our TSICD indicatorint maHandle1; // Handle for our first MAint maHandle2, // Handle for our second MA
TSI_mline[], TSI_sline[]; //Arrays to hold the values of the Mainline and Signal line from TSIint maVal1[], maVal2[]; //Arrays to hold the values of moving averagesdouble p_close; //Variable to hold the values the closing priceint STP, TKP; //Variables to store our Stoploss and Takeprofit//+------------------------------------------------------------------+////Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//....Getting the indicator handles
TSICDHandle = (iCustom(_Symbol, _Period, "TSI_CD", ema1, ema2, sMAp, MAmode));
maHandle1 = (iMA(_Symbol, _Period, MA_Period1, 0, MODE_EMA, PRICE_CLOSE));
maHandle2 = (iMA(_Symbol, _Period, MA_Period2, 0, MODE_EMA, PRICE_CLOSE));
//Check to see if valid Handles are returnedif((TSICDHandle < 0) || (maHandle1 < 0) || (maHandle2 < 0))
{
Alert("Error creating handles for indicators-error:", GetLastError(), " !!");
}
//......Standardise the currency digits for different pairs
STP = Stoploss;
TKP = TakeProfit;
if(_Digits == 5 || _Digits == 3)
{
STP = STP * 10;
TKP = TKP * 10;
}
{
//....Checking the adequecy of the number of Bars in Historyif(Bars(_Symbol, _Period) < 500) //If the bars are less than 500
{
}
}
Alert("We have less than 500 bars,EA will now exit");
return(0);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
{
{
//---Releasing indicator handlesIndicatorRelease(TSICDHandle);
IndicatorRelease(maHandle1);
IndicatorRelease(maHandle2);
return;
}
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+//+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+voidOnTick(){
//---Check if we have a new barstaticdatetime Oldtime;// Save data about the time of the candlestick of the previous tickdatetime Newtime[1]; //Save data about time of the candlestick of the current tickbool IsNewBar = false; // Compare Oldtime with Newtimeint copied = (CopyTime(_Symbol, _Period, 0, 1, Newtime));
if(copied > 0)
{
if(Oldtime != Newtime[0])
{
IsNewBar = true; //If New time and Oldtime are different,there is a newbar
Oldtime = Newtime[0];
}
}
if(Oldtime == Newtime[0])
{
Alert("Error in copying Historical time data,error= ", GetLastError());
ResetLastError();
return;
}
if(IsNewBar == false)
{
return;
}
// Checking the number of bars againint MyBars = (Bars(_Symbol, _Period));
if(MyBars < 500)
{
Alert("We have less than 500 Bars,EA will now Exit!!");
return;
}
//...Defining some MQL5 structures to be used for our tradesMqlTick latest_price;//To be used to get the letest information about the larest priceMqlTradeRequest mrequest;// To be used for sending out trade requestsMqlTradeResult mresult; //To be used for getting trade resultsMqlRates mrate[];//To be used to store data about each barZeroMemory(mrequest); // To be used for the initialisation of mrequest structure//Setting out Arrays to the ArraySetAsSeries FlagArraySetAsSeries(mrate, true);
ArraySetAsSeries(TSI_mline, true);
ArraySetAsSeries(TSI_sline, true);
ArraySetAsSeries(maVal1, true);
ArraySetAsSeries(maVal2, true);
//...Getting and Checking if we got the latest price quote using the mqlTick structureif(!SymbolInfoTick(_Symbol, latest_price))
{
Alert("Error getting the latest price quote-Error: ", GetLastError(), "!!");
return;
}
//Getting and Checking the details about the latest 3 Barsif(CopyRates(_Symbol, _Period, 0, 3, mrate) < 0)
{
Alert("Error copying rates data-error: ", GetLastError(), " !!");
return;
}
//...Copying and Checking the new values of the indicatorsto buffers(arrays) using the handlesif(CopyBuffer(TSICDHandle, 1, 0, 3, TSI_mline) < 0)
{
if(CopyBuffer(TSICDHandle, 02, 0, 3, TSI_mline) < 0)
{
Alert("Error copying the TSICD indicator buffers -error: ", GetLastError(), " !!");
return;
}
}
if(CopyBuffer(maHandle1, 0, 0, 0, 3, maVal1) < 0)
{
{
if(CopyBuffer(maHandle2, 0, 0, 3, maVal2) < 0)
{
Alert("Error copying the TSICD indicator buffers -error: ", GetLastError(), " !!");
return;
}
}
}
//....Checking for the presence of open positionsbool Buy_opened = false; //Variable to store information whether a buy is open or notbool Sell_opened = false; //Variable to store information whether a sell is open or noif(PositionSelect(_Symbol) == true)
{
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
Buy_opened = true;
}
}
else
{
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
Sell_opened = true;
}
}
// Copy the bar close price for the previous bar prior to the current bar, that is bar 1
p_close = mrate[1].close; //Check for conditions for our Buy entrybool Buycondition1 = ((TSI_mline[0] > TSI_sline[0]) && (TSI_mline[1] < TSI_sline[1]) && (p_close > maVal2[1]));
bool Buycondition2 = ((maVal1[1] - maVal2[1]) > (maVal1[2] - maVal2[2]));
if(Buycondition1 && Buycondition2)
{
if(Buy_opened)
{
Alert("We already have a buy position");
return;
}
}
mrequest.action = TRADE_ACTION_DEAL;
mrequest.price = NormalizeDouble(latest_price.ask, _Digits);
mrequest.sl = NormalizeDouble(latest_price.ask - STP * _Point, _Digits);
mrequest.tp = NormalizeDouble(latest_price.ask + TKP * _Point, _Digits);
mrequest.symbol = _Symbol;
mrequest.volume = Lotsize;
mrequest.magic = EA_Magic;
mrequest.type = ORDER_TYPE_BUY;
mrequest.type_filling = ORDER_FILLING_FOK;
mrequest.deviation = 100;
//Sending Buy orderbool buyorder = OrderSend(mrequest, mresult);
//Getting the result codeif(mresult.retcode == 10009 || mresult.retcode == 10000)
{
Alert("A buy order has been successfully placed with Tickets# : ", mresult.order, "!!");
}
else
{
Alert("A buy order could not be placed-Error: ", GetLastError());
ResetLastError();
return;
}
//....Check for conditions for our Sell entrybool Sellcondition1 = ((TSI_mline[0] < TSI_sline[0]) && (TSI_mline[1] > TSI_sline[1]) && (p_close < maVal2[1]));
bool Sellcondition2 = ((maVal2[1] - maVal1[1]) > (maVal2[2] - maVal1[2]));
if(Sellcondition1 && Sellcondition2)
{
if(Sell_opened)
{
Alert("We already have a sell position");
return;
}
}
mrequest.action = TRADE_ACTION_DEAL;
mrequest.price = NormalizeDouble(latest_price.bid, _Digits);
mrequest.sl = NormalizeDouble(latest_price.bid - STP * _Point, _Digits);
mrequest.tp = NormalizeDouble(latest_price.bid + TKP * _Point, _Digits);
mrequest.symbol = _Symbol;
mrequest.volume = Lotsize;
mrequest.magic = EA_Magic;
mrequest.type = ORDER_TYPE_SELL;
mrequest.type_filling = ORDER_FILLING_FOK;
mrequest.deviation = 100;
//Sending Sell order
buyorder = OrderSend(mrequest, mresult);
//Getting the result codeif(mresult.retcode == 10009 || mresult.retcode == 10000)
{
Alert("A sell order has been successfully placed with Tickets# : ", mresult.order, "!!");
}
else
{
{
Alert("A sell order could not be placed-Error: ", GetLastError());
ResetLastError();
}
return;
}
}
}
Hi there?This code on True Strength Index Convergence and Divergence Expert Advisor gives out 1 error.
Error:OnTick function can only be declared on a global,namaespace or class only.Please how I can fix this error?
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.