I am new to mql5 and i am watching a lot of videos on how to create a ea.
I started to build a trix rsi ea but i am struggling to get the ea to place a order when the trix goes above the 0 and is below the rsi 70 and above the rsi 30 levels.
The ea also needs to close the trade in 50 seconds after it was opened.
Any help would greatly be appreciated.
I want the ea to open a trade when the trix crosses the 0 line and is between the rsi 70 and 30 lines.
Then if the order was opened it must stay open for 50 seconds then close automatically.
I was watching a few videos on how to build a ea and this is how far i got with the script, i am learning how to code and this is my first ea.
//+------------------------------------------------------------------+//| include |//+------------------------------------------------------------------+#include<Trade\trade.mqh>
MqlTick currentTick,previouseTime,currentTime;
CTrade trade;
//+------------------------------------------------------------------+//| inputs |//+------------------------------------------------------------------+inputint InpPeriodRSI = 14; // Period for RSIinputint InpPeriodTriX = 5; // Period for TriXstaticinputdouble InpLots = 0.20; // lotsinputint InpRSILevelUp = 70; // RSI Level (Upper)inputint InpRSILevelDown = 30; // RSI Level (lower)inputint InpStopLoss = 200; // Stop loss in pointsinputENUM_APPLIED_PRICE
enumInpPriceRSI = PRICE_CLOSE, // Applied Price for RSI
enumInpPriceTriX = PRICE_CLOSE; // Applied Price for TriX//+------------------------------------------------------------------+//| Global Variables |//+------------------------------------------------------------------+int HandleRSI; // Handle for RSI Indicatorint HandleTriX; // Handle for TriX Indicatordouble ArrayRSI[]; // Dynamic array for RSI datadouble ArrayTriX[]; // Dynamic array for Trix datadatetime openTimeBuy = 0;
intOnInit()
{
// Set Magic Number to Trade Object
trade.SetExpertMagicNumber(20230714);
// Create Handle for RSI Indicator
HandleRSI = iRSI( _Symbol, _Period, InpPeriodRSI, enumInpPriceRSI );
if( HandleRSI == INVALID_HANDLE )
{
Print("Error creating iRSI handle: ", _LastError );
return( INIT_FAILED );
}
// Create Handle for Trix Indicator
HandleTriX = iTriX( _Symbol, _Period, InpPeriodTriX, enumInpPriceTriX );
if( HandleTriX == INVALID_HANDLE )
{
Print("Error creating iTriX handle: ", _LastError );
return( INIT_FAILED );
}
// Initialisation Succeededreturn( INIT_SUCCEEDED );
}
//normalize pricebool NormalizePrice (double &price){
double tickSize=0;
if(!SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE,tickSize)){
Print("Failed to get tick size");
returnfalse;
}
price = NormalizeDouble(MathRound(price/tickSize)*tickSize,_Digits);
returntrue;
}
voidOnTick()
{
// get current tickif(!SymbolInfoTick(_Symbol,currentTick)){Print("Failed to get current tick");return;}
//check for new bar open ticksif(!IsNewBar()){return;}
// Get RSI value of current bar/candleif( CopyBuffer( HandleRSI, 0, 0, 2, ArrayRSI ) > 0 )
Print( "RSI value at current bar: ", DoubleToString( ArrayRSI[0], 2 ) );
// Get & Print TriX value of current bar/candleif( CopyBuffer( HandleTriX, 0, 0, 2, ArrayTriX ) > 0 )
Print( "TriX value at current bar: ", DoubleToString( ArrayTriX[0], _Digits ) );
// count open positionsint cntBuy,cntSell;
if(!CountOpenPositions(cntBuy,cntSell)){return;}
//Check for buy positionif(cntBuy==0 && ArrayTriX[0]<=(InpRSILevelDown) && ArrayTriX[0]>(InpRSILevelUp) && openTimeBuy!=iTime(_Symbol,PERIOD_CURRENT,0)){
//calculate stop loss double sl = InpStopLoss==0 ? 0 :currentTick.bid - InpStopLoss * _Point;
if(!NormalizePrice(sl)){return;}
openTimeBuy=iTime(_Symbol,PERIOD_CURRENT,0);
//double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,InpLots,currentTick.ask,sl,0,"test");
}
}
//+------------------------------------------------------------------+//| Functions |//+------------------------------------------------------------------+//check if we have a bar open tickbool IsNewBar(){
staticdatetime previouseTime = 0;
datetime currentTime = iTime(_Symbol,PERIOD_CURRENT,0);
if(previouseTime!=currentTime){
previouseTime=currentTime;
returntrue;
}
returnfalse;
}
//count open positionsbool CountOpenPositions(int &cntBuy, int &cntSell){
cntBuy = 0;
//cntSell = 0;int total = PositionsTotal();
for(int i=total-1; i>=0; i--){
ulong ticket = PositionGetTicket(i);
if(ticket<=0){Print("Failed to get position ticket"); returnfalse;}
if(!PositionSelectByTicket(ticket)){Print("Failed to select position"); returnfalse;}
long magic;
if(!PositionGetInteger(POSITION_MAGIC,magic)){Print("Failed to get position magic number"); returnfalse;}
if(magic==20230714){
long type;
if(!PositionGetInteger(POSITION_TYPE,type)){Print("Failed to get position type"); returnfalse;}
if(type==POSITION_TYPE_BUY){cntBuy++;}
//if(POSITION_TYPE_SELL){cntSell++;}
}
}
returntrue;
}
I am new to mql5 and i am watching a lot of videos on how to create a ea.
I started to build a trix rsi ea but i am struggling to get the ea to place a order when the trix goes above the 0 and is below the rsi 70 and above the rsi 30 levels.
The ea also needs to close the trade in 50 seconds after it was opened.
Any help would greatly be appreciated.