Im new to coding but does anyone know why my sell trade is opening and closing on entry. Ive put my code below feel free to stick it into MT4.
#property strict//Always Keep// FIND OUT WHY SELL ARE NOT WORKING AS EXPECTED //.............Input Variables........................................................string Botname = "Model Q GBPUSD" ; //string is a variable nameint Magic = 1234 ; //int integer number Magic number is Unique ID Code for the Robot int MaxTrades = 1;
int MaxCloseSpreadPips = 7;
double LotsToTrade= 0.1 ; //double is a float number fraction decimal ect..double StopLoss= -1000.00 ;
double TakeProfit= 50.00 ;
int SMAslow=100;
int SMAfast=50;
bool LongSetup1 = false ; // need to define LongSetup as false bool LongSetup2 = false ;
bool ShortSetup1 = false ;
bool ShortSetup2 = false ;
bool ExitRuleLong = false;
bool ExitRuleShort = false;
// Trade Time Delayint TradeDelayTimeSec = (1* 24* 60* 60); //Trade delay 1day // Time In betweem Trade in Sec (1 * 24* 60* 60) for eg this means wait 1 day inbetween trade or evaluate market every day in secsdatetime LastTradePlacedTimeStamp = 0;
//.............Input Variables........................................................intOnInit()
{
return(INIT_SUCCEEDED);
}
voidOnDeinit(constint reason)
{
}
// MAIN CODE HERE..................................................................... voidOnTick()
{
// Add Indicator Here ..............................................................................double PlusDI = iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,0);
double PlusDIone = iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,3);
double PlusDItwo = iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,5);
double MinusDI = iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,0);
double MinusDIone = iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,3);
double MinusDItwo = iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,5);
// Add Indicator Here............................................................................// TradeSetup Code.................................................................................. if ( GetTotalopenTrades () < MaxTrades ) // This were we put our order in since GetTotalopenTrades is 0 and 0<1 the code inside will run
{
// Trade Delay Code........................................................................................................... if ( (TimeCurrent() - LastTradePlacedTimeStamp) < TradeDelayTimeSec) return; // so if the current time - the "last time we took a trade" time is less < than our trade delay which is 1day retart the loop which is shown by return function. "Return" says to the loop if true start from begining. //....................ENTERING BUY ORDERS CODE FOR "LONG SETUP 1" AND "LONG SETUP 2"......................................... // Check for long trade setup 1 .........................................................if ((PlusDIone<MinusDIone)&&(PlusDItwo<MinusDItwo)&& (PlusDI>MinusDI))
{
int OrderResult = OrderSend(Symbol(), OP_BUY,LotsToTrade,Ask,10,0,0,"Buy Order",Magic,0,clrBlue) ; // Ordersend is function for inputting Buy or Sell
LastTradePlacedTimeStamp= TimeCurrent(); // note the time you send the trade used in trade delay code
ExitRuleLong = True;
}
// Long Setup 2 code................................................................................ elseif ((PlusDIone>MinusDIone)&&(PlusDItwo>MinusDItwo)&& ( PlusDI>MinusDI))
{
int OrderResult = OrderSend(Symbol(), OP_BUY,LotsToTrade,Ask,10,0,0,"Buy Order",Magic,0,clrBlue) ; // Ordersend is function for inputting Buy or Sell
LastTradePlacedTimeStamp= TimeCurrent();
ExitRuleLong = True;
}
//.......................ENTERING SELL ORDERS FOR "SHORT SETUP 1" AND "SHORT SETUP 2".....................................// SHORT SETUP 1elseif ((PlusDIone>MinusDIone)&&(PlusDItwo>MinusDItwo)&& ( PlusDI<MinusDI))
{
int OrderResult = OrderSend(Symbol(), OP_SELL,LotsToTrade,Bid,10,0,0,"Sell Order",Magic,0,clrBlack) ; // Ordersend is function for inputting Buy or Sell
LastTradePlacedTimeStamp= TimeCurrent(); // note the time you send the trade used in trade delay code
ExitRuleShort = False ;
}
// Short Setup 2 elseif ((PlusDIone<MinusDIone)&&(PlusDItwo<MinusDItwo) && ( PlusDI<MinusDI))
{
int OrderResult = OrderSend(Symbol(), OP_SELL,LotsToTrade,Bid,10,0,0,"Sell Order",Magic,0,clrBlack) ; // Ordersend is function for inputting Buy or Sell
ExitRuleShort = False ;
LastTradePlacedTimeStamp= TimeCurrent();
}
}
// EXIT CODE ...........................................................................................................// EXIT LONGif ( ExitRuleLong == True)
{
if ( (PlusDIone>MinusDIone)&& (PlusDItwo>MinusDItwo)&& (PlusDI<MinusDI)) CloseAllTrades();
}
// EXIT SHORTif ( ExitRuleShort == True)
{
if ( (PlusDIone<MinusDIone)&& (PlusDItwo<MinusDItwo)&&(PlusDI>MinusDI)) CloseAllTrades();
}
// TAKE PROFIT AND STOPLOSSif ( GetTotalProfits() > TakeProfit) CloseAllTrades(); // Take Profit Code //Nested Conditional if "current TP" > "TP" Close Trade if ( GetTotalProfits()< StopLoss) CloseAllTrades(); // StopLoss Code// EXIT CODE ...........................................................................................................// Statement in journel to display number of Open Trades...........................................int Trades= GetTotalopenTrades();
// Referencing the GetTotalopenTrades function below Print ("*********Our Total Number of Trades: ", Trades); // Statement to Printed
}
// Total Open Trades .............................................. int GetTotalopenTrades ()
{
int TotalTrades = 0 ;
for ( int t=0; t<OrdersTotal(); t++)
{
if ( OrderSelect(t, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol() != Symbol()) continue;
if ( OrderMagicNumber() !=Magic) continue;
if ( OrderCloseTime() !=0) continue;
TotalTrades = (TotalTrades+1) ;
}
}
return TotalTrades;
}
// Code For Closeing Trades.............................................................void CloseAllTrades()
{
int CloseResult= 0 ;
for (int t=0; t<OrdersTotal(); t++)
{
if (OrderSelect( t, SELECT_BY_POS, MODE_TRADES))
{
if (OrderMagicNumber() !=Magic) continue ;
if (OrderSymbol() !=Symbol()) continue ;
if (OrderType() ==OP_BUY) CloseResult= OrderClose(OrderTicket(), OrderLots(), Bid, MaxCloseSpreadPips,clrRed);
if (OrderType() ==OP_SELL) CloseResult= OrderClose(OrderTicket(), OrderLots(), Ask, MaxCloseSpreadPips,clrBlack);
t -- ;
}
}
return ;
}
//Check TotalProfits for all Trades.................................................................................double GetTotalProfits()
{
double TotalProfits = 0.0 ;
for (int t=0; t<OrdersTotal(); t++)
{
if (OrderSelect( t, SELECT_BY_POS, MODE_TRADES))
{
if (OrderMagicNumber() !=Magic) continue ;
if (OrderSymbol() !=Symbol()) continue ;
if (OrderCloseTime() != 0) continue ;
TotalProfits = (TotalProfits + OrderProfit());
}
}
return TotalProfits ;
}
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.
Hi Guys
Im new to coding but does anyone know why my sell trade is opening and closing on entry. Ive put my code below feel free to stick it into MT4.