Expert Advisor is not opening trades - mql4

I tried to create an EA in mql4 which opens and close the trade position as per condition given, but it is not opening the trade after mating the conditions, EA Works till signal which shows buy and sell and after that nothing happens. How can I debug this?
void CloseBuyPosition()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
string Cp = OrderSymbol();
if (_Symbol == Cp)
if (OrderType() == OP_BUY)
{
OrderClose(OrderTicket(), OrderLots(), Bid, 3, NULL);
}
}
}
void CloseSellPosition()
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
string Cp = OrderSymbol();
if (_Symbol == Cp)
if (OrderType() == OP_SELL)
{
OrderClose(OrderTicket(), OrderLots(), Ask, 3, NULL);
}
}
}
void OnTick()
{
string signal = "";
double Sar = iSAR(_Symbol, _Period, 0.02, 0.2, 0);
if (Sar < Low[1] && Open[1] < Close[1])
{
signal = "buy";
}
if (Sar > High[1] && Open[1] > Close[1])
{
signal = "sell";
}
if (signal == "buy" && OrdersTotal() == 0)
OrderSend(_Symbol, OP_BUY, 0.01, Ask, 3, 20, 100, NULL, 0, 0, Green);
if (signal == "sell" && OrdersTotal() == 0)
OrderSend(_Symbol, OP_SELL, 0.01, Bid, 3, 20, 100, NULL, 0, 0, Red);
Comment("The Signal is :", signal);
if (Open[1] > Close[1] && OrdersTotal() > 0)
CloseBuyPosition();
if (Open[1] < Close[1] && OrdersTotal() > 0)
CloseSellPosition();
}

Step 0:check, whether your EA was launched inside MetaTrader4 Terminal with an active permission to actually do trades.
Step 1:check the code, for having at least some elementary self-debugging tools ( GetLastError() and Print() are way better than intermittent and self-destroying, i.e. having Zero-depth of history GUI-text inside but a last visible Comment()
Step 2:Analyze the logs, where all printed details will help you trace the root cause ( Broker-rejections, closed Market, flawed price-levels and many of the possible reasons for an OrderSend()-call to get rejected )
int OrderSend( string symbol, // symbol
int cmd, // operation
double volume, // volume
double price, // price
int slippage, // slippage
double stoploss, // stop loss <------------ PRICE-DOMAIN levels, not INT-s
double takeprofit, // take profit <---------- PRICE-DOMAIN levels, not INT-s
string comment = NULL, // comment
int magic = 0, // magic number
datetime expiration = 0, // pending order expiration
color arrow_color = clrNONE// color
);
void OnTick()
{
/* string signal = "";
double Sar = iSAR( _Symbol, _Period, 0.02, 0.2, 0 );
if ( Sar < Low[1] && Open[1] < Close[1] ) signal = "buy";
if ( Sar > High[1] && Open[1] > Close[1] ) signal = "sell";
if ( signal == "buy" && OrdersTotal() == 0 ) OrderSend( _Symbol, OP_BUY, 0.01, Ask, 3, 20, 100, NULL, 0, 0, Green );
if ( signal == "sell" && OrdersTotal() == 0 ) OrderSend( _Symbol, OP_SELL, 0.01, Bid, 3, 20, 100, NULL, 0, 0, Red );
Comment( "The Signal is :", signal );
if ( Open[1] > Close[1] && OrdersTotal() > 0 ) CloseBuyPosition();
if ( Open[1] < Close[1] && OrdersTotal() > 0 ) CloseSellPosition();
*/
static int count_of_NOPs = 0;
if ( OrdersTotal() == 0 )
{ if ( Open[1] < Close[1]
&& Low[1] > iSAR( _Symbol, _Period, 0.02, 0.2, 0 )
) {
int rc = OrderSend( _Symbol, OP_BUY, 0.01, Ask, 3, 20, 100, NULL, 0, 0, Green );
Print( StringFormat( "HAVING NO ORDERS ACTIVE... DID TRY OrderSend( OP_BUY ) -> RESULTING RetCode ( tkt# ) == %d [yield GetLastError() == %d]",
rc,
GetLastError()
)
);
count_of_NOPs = 0; //------------------------------ .RESET
return; //------------------------------------------^ J.I.T./RET
}
if ( Open[1] > Close[1]
&& High[1] < iSAR( _Symbol, _Period, 0.02, 0.2, 0 )
) {
int rc = OrderSend( _Symbol, OP_SELL, 0.01, Bid, 3, 20, 100, NULL, 0, 0, Red );
Print( StringFormat( "HAVING NO ORDERS ACTIVE... DID TRY OrderSend( OP_SELL ) -> RESULTING RetCode ( tkt# ) == %d [yield GetLastError() == %d]",
rc,
GetLastError()
)
);
if ( )
count_of_NOPs = 0; //------------------------------ .RESET
return; //------------------------------------------^ J.I.T./RET
}
/* OTHERWISE: */
Print( StringFormat( "HAVING NO ORDERS ACTIVE... DID NOP... (for %d time)",
++count_of_NOPs
)
);
// */
}
else
{ if ( Open[1] > Close[1] ) { Print( StringFormat( "HAVING %d ORDERS ACTIVE... WILL TRY CloseBuyPosition()...",
OrdersTotal()
)
);
CloseBuyPosition();
count_of_NOPs = 0; //---------- .RESET
return; //----------------------^ J.I.T./RET
}
if ( Open[1] < Close[1] ) { Print( StringFormat( "HAVING %d ORDERS ACTIVE... WILL TRY CloseSellPosition()...",
OrdersTotal()
)
);
CloseSellPosition();
count_of_NOPs = 0; //---------- .RESET
return; //----------------------^ J.I.T./RET
}
/* OTHERWISE: */
Print( StringFormat( "HAVING %d ORDERS ACTIVE... DID NOP... (for %d time)",
OrdersTotal(),
++count_of_NOPs
)
);
// */
}

Related

mql5 pick orders based on calculation

bool D1GTPrevD1()
{
// if(iClose(NULL,PERIOD_D1,1) < iClose(NULL,PERIOD_D1,0))
If(iClose(NULL,PERIOD_D1,1))
{
double
low = iLow( _Symbol, PERIOD_D1, 1 ),
close = iClose( _Symbol, PERIOD_D1, 1 ),
ov = close-low,
nv = close+ov;
return !( close==nv );
};
return(true);
return(false);
}
bool D1LTPrevD1()
{
// if(iClose(NULL,PERIOD_D1,1) > iClose(NULL,PERIOD_D1,0))
If(iClose(NULL,PERIOD_D1,1))
{
double
high = iHigh( _Symbol, PERIOD_D1, 1 ),
close = iClose( _Symbol, PERIOD_D1, 1 ),
ov = high-close,
nv = close-ov;
return !( close==nv );
};
return(true);
return(false);
}
as in the above compile, like to have calculations to the current candle. As there is no effect while running EA, help me to imply this calculation in mql5

How can I cancel a trade when another is open and keep the open trade for a given duration?

I have written the code below that opens a buy and sell trade (a certain number of pips above and below the ask and bid price) at a specific time.
How can I close/cancel one immediately when the other is opened?
How can I close the opened trade if it's say X pips in profit or after a minute (depending on which condition is reached first)?
I'm a not too sure I've done the right thing in the code below and would really appreciate some help.
double spread = Ask-Bid;
extern datetime time;
extern int pipGap = 7;
extern int lotSize = 0.01;
extern int closeTimeInSeconds = 60;
int start() {
if (TimeCurrent() >= StrToTime(time)){
OrderSend(Symbol(),OP_BUYSTOP,lotSize, Ask + Point*pipGap, 0,0,0);
OrderSend(Symbol(),OP_SELLSTOP,lotSize, Bid - Point*pipGap, 0,0,0);
}
for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
OrderSelect(pos, SELECT_BY_POS)
){
int duration = TimeCurrent() - OrderOpenTime();
if (duration >= closeTimeInSeconds)
OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),
3*Point);
}
return(0);
}
AUTOMATED NEWS TRADING
What you (#iGetIt ) are looking for is a rudimentary News Trading bot. Here's a diluted implementation for it.
In the more advanced stages, you will need to automatically download news and auto-trade base on the affected pairs, track volatility (to avoid swings), sync your PC time (Windows date/time is NOT accurate and news trading need to be synced to the millisecond level). Track your broker server response time, implement Trailing-StopLoss (for improved profit), implement slide-in-orders (multiple stop orders to catch huge spikes), calculate order-set profitability (to know when to close all the orders when price retrace), etc etc.
Anyway, here's a basic version that you can play with (based on the Bounty requirements):
Opens buy + sell stop-orders at specific time.
Configurable number of pips AWAY from current ask/bid.
Implements OCO (One Cancels the Other).
Closes executed trades with x-pip profit (aka TP level).
Closes executed or outstanding stop-orders after x-seconds.
Visual: To show that it executes in time (this is Asian time, so liquidity is low, so +- a few seconds before price ticks).
Closing: It closes automatically based on the settings.
SOURCE CODE: And this is the complete MQL4 source-code for it.
(UPDATED 15May19 1045 GMT+8) to fix bug as reported in the closing of executed orders after TTL.
//+------------------------------------------------------------------+
//| SO55930471.mq4 |
//| Copyright 2019, Joseph Lee, joseph.lee#fs.com.my |
//| TELEGRAM #JosephLee74 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Joseph Lee, TELEGRAM #JosephLee74"
#property link "http://www.fs.com.my"
#property version "1.00"
#property strict
//-------------------------------------------------------------------
// APPLICABLE PARAMETERS
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// NEWS IMPACT SELECTION
//===================================================================
extern string vsEAComment = "Telegram #JosephLee74"; //Ego trip
extern datetime vdTradeStartInGMT = D'2019.5.14 06:00'; //When to trade (GMT)
extern int viStopOrderLevelInPip = 5; // StopOrder distance from ask/bid (pips)
extern double viFixLots = 0.01; // Lot size
extern int viStopLossInPip = 20; // StopLoss (pips)
extern int viTargetProfitInPip = 100; // TargetProfit (pips)
extern int viDeleteStopOrderAfterInSec = 30; // StopOrder TTL (sec)
extern int viDeleteOpenOrderAfterInSec = 300; // Executed Order TTL (sec)
extern int viMaxSlippageInPip = 2; // Max Slippage (pip)
//-------------------------------------------------------------------
// System Variables
//-------------------------------------------------------------------
int viMagicId = 0;
double viPipsToPrice = 0.0001;
double viPipsToPoint = 1;
int viBuyStopTicket = -1;
int viSellStopTicket = -1;
int viBuyOrderTicket = -1;
int viSellOrderTicket = -1;
string vsDisplay = "EVENT-TRADER v1.01 - ";
//-------------------------------------------------------------------
//+------------------------------------------------------------------+
//| EA Initialization function
//+------------------------------------------------------------------+
int init() {
ObjectsDeleteAll(); Comment("");
// Caclulate PipsToPrice & PipsToPoints (old sytle, but works)
if((Digits == 2) || (Digits == 3)) {viPipsToPrice=0.01;}
if((Digits == 3) || (Digits == 5)) {viPipsToPoint=10;}
viMagicId = vdTradeStartInGMT;
start();
return(0);
}
//+------------------------------------------------------------------+
//| EA Stand-Down function
//+------------------------------------------------------------------+
int deinit() {
ObjectsDeleteAll();
return(0);
}
//============================================================
// MAIN EA ROUTINE
//============================================================
int start() {
//==========================================
//MANAGE ROBOT EXPIRY
//==========================================
if( TimeCurrent() > D'2020.1.1' ) {
Comment(vsDisplay + "EXPIRED. Please contact josephfhlee74 at gmail dot com"); // Who am I kidding?
return(0);
}
ResetLastError();
// Exit the routine if it is not time to trade yet.
if(TimeGMT() < vdTradeStartInGMT) {
// Show a count-down timer to the trading time.
Comment(vsDisplay +
"[" + TimeToStr(TimeGMT()) + " GMT] " +
IntegerToString(int(vdTradeStartInGMT - TimeGMT())) + " sec to [" +
TimeToStr(vdTradeStartInGMT) + " GMT]"
);
return(0);
}
viBuyStopTicket = -1;
viSellStopTicket = -1;
viBuyOrderTicket = -1;
viSellOrderTicket = -1;
//=========================================================
//FIND *OPENED* BUY/SELL PENDING ORDERS
//---------------------------------------------------------
for( int i=OrdersTotal()-1; i>=0; i-- ) {
if(OrderSelect( i, SELECT_BY_POS, MODE_TRADES ))
if( OrderSymbol() == Symbol() )
if( OrderMagicNumber() == viMagicId) {
if( OrderType() == OP_BUYSTOP )
viBuyStopTicket = OrderTicket();
if( OrderType() == OP_SELLSTOP )
viSellStopTicket = OrderTicket();
if( OrderType() == OP_BUY )
viBuyOrderTicket = OrderTicket();
if( OrderType() == OP_SELL )
viSellOrderTicket = OrderTicket();
}
}
//=========================================================
//FIND *CLOSED* BUY/SELL ORDERS FOR THIS EVENT
//---------------------------------------------------------
for(int i=OrdersHistoryTotal()-1; i>=0; i--) {
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
if(OrderSymbol() == Symbol())
if(OrderMagicNumber() == viMagicId) {
if( OrderType() == OP_BUYSTOP )
viBuyStopTicket = OrderTicket();
if( OrderType() == OP_SELLSTOP )
viSellStopTicket = OrderTicket();
if( OrderType() == OP_BUY )
viBuyOrderTicket = OrderTicket();
if( OrderType() == OP_SELL )
viSellOrderTicket = OrderTicket();
}
}
// The above 2 sections will ensure that each event will only be executed once.
// If orders are cancelled or closed for whatever reason, they will never be open again.
string vsVerbose = vsDisplay + "[GMT " + TimeToStr(TimeGMT()) + "] Executing ..."
"\nActive BUYSTOP: " + viBuyStopTicket +
" | Active SELLSTOP: " + viSellStopTicket +
"" +
"\nActive BUY: " + viBuyOrderTicket +
" | Active SELL: " + viSellOrderTicket;
Comment(vsVerbose);
//=========================================================
// HANDLES OCO (One-Cancels-the-Other)
//---------------------------------------------------------
// BUY Order EXISTS, cancels all SellStops
if( viBuyOrderTicket != -1 ) {
for( int i=OrdersTotal()-1; i>=0; i-- ) {
if(OrderSelect( i, SELECT_BY_POS, MODE_TRADES ))
if( OrderSymbol() == Symbol() )
if( OrderMagicNumber() == viMagicId)
if( OrderType() == OP_SELLSTOP )
OrderDelete(OrderTicket());
}
}
// SELL Order EXISTS, cancels all BuyStops
if( viSellOrderTicket != -1 ) {
for( int i=OrdersTotal()-1; i>=0; i-- ) {
if(OrderSelect( i, SELECT_BY_POS, MODE_TRADES ))
if( OrderSymbol() == Symbol() )
if( OrderMagicNumber() == viMagicId)
if( OrderType() == OP_BUYSTOP )
OrderDelete(OrderTicket());
}
}
//=========================================================
//CLOSE EXPIRED STOP/EXECUTED ORDERS
//---------------------------------------------------------
for( int i=OrdersTotal()-1; i>=0; i-- ) {
if(OrderSelect( i, SELECT_BY_POS, MODE_TRADES ))
if( OrderSymbol() == Symbol() )
if( OrderMagicNumber() == viMagicId) {
if( (OrderType() == OP_BUYSTOP) || (OrderType() == OP_SELLSTOP) )
if((TimeCurrent()-OrderOpenTime()) >= viDeleteStopOrderAfterInSec)
OrderDelete(OrderTicket());
if( (OrderType() == OP_BUY) || (OrderType() == OP_SELL) )
if((TimeCurrent()-OrderOpenTime()) >= viDeleteOpenOrderAfterInSec) {
// For executed orders, need to close them
double closePrice = 0;
RefreshRates();
if(OrderType() == OP_BUY)
closePrice = Bid;
if(OrderType() == OP_SELL)
closePrice = Ask;
OrderClose(OrderTicket(), OrderLots(), closePrice, int(viMaxSlippageInPip*viPipsToPoint), clrWhite);
}
}
}
//===================================================================
//OPEN STOP ORDERS IF NO EXISTING nor CLOSED NO BUY/SELL STOP/ORDERS
//-------------------------------------------------------------------
// Do NOT execute (place new orders) if it is past the trading window.
if(TimeGMT() >= (vdTradeStartInGMT+viDeleteStopOrderAfterInSec))
{
Comment(vsDisplay + "[" + TimeToStr(TimeGMT()) + " GMT] " + "Already passed execution time.");
return(0);
}
// Place BuyStop if not exists; and no executed-Buy order
if( (viBuyStopTicket == -1) && (viBuyOrderTicket == -1)) {
RefreshRates();
viFixLots = NormalizeDouble(viFixLots, 2);
double viPrice = NormalizeDouble(Ask + (viStopOrderLevelInPip*viPipsToPrice), Digits);
double viSL = viPrice - (viStopLossInPip*viPipsToPrice);
double viTP = viPrice + (viTargetProfitInPip*viPipsToPrice);
viBuyStopTicket = OrderSend(Symbol(), OP_BUYSTOP, viFixLots
, viPrice
, int(viMaxSlippageInPip*viPipsToPoint)
, viSL, viTP
, vsEAComment, viMagicId, 0, Blue);
if(viBuyStopTicket == -1)
Print("Error executing BuyStop [" + IntegerToString(GetLastError()) + "]." );
}
// Place SellStop if not exists; and no executed-Sell order
if( (viSellStopTicket == -1) && (viSellOrderTicket == -1) ) {
RefreshRates();
viFixLots = NormalizeDouble(viFixLots, 2);
double viPrice = NormalizeDouble(Bid - (viStopOrderLevelInPip*viPipsToPrice), Digits);
double viSL = viPrice + (viStopLossInPip*viPipsToPrice);
double viTP = viPrice - (viTargetProfitInPip*viPipsToPrice);
viSellStopTicket = OrderSend(Symbol(), OP_SELLSTOP, viFixLots
, viPrice
, int(viMaxSlippageInPip*viPipsToPoint)
, viSL, viTP
, vsEAComment, viMagicId, 0, Red);
if(viSellStopTicket == -1)
Print("Error executing SellStop [" + IntegerToString(GetLastError()) + "]." );
}
return(0);
}
#property strict
//---
input datetime InpTime1=D'2019.05.01 00:00'; //time to enter the trade, just as an example
input double InpPipsDist=10; //distance in pips to place BS&SS OCO-orders
input int InpCloseSeconds=60; //close time, seconds since main order is sent
input double InpProfitPips=1.; //profit in pips to close all.
input double InpLotSize=0.1; //lot size
input int InpMagicNumber=123456789; //magic number
// ---
#include <Arrays\ArrayObj.mqh>
bool firstOrderOpen;
double PIP;
CArrayObj *ordersList;
// ---
class C2Orders : public CObject
{
private:
int m_ticket1;
int m_ticket2;
datetime m_timeStart;//OrderOpenTime of the two tickets might be different,TimeCurrent() is used as time they are open.
bool m_activated;
void checkTicket(const int ticket) //if one order is open, another is deleted.
{
if(ticket>0 && OrderSelect(ticket,SELECT_BY_TICKET))
{
if(OrderType()<=OP_SELL)
{
if(ticket==m_ticket1)
{
if(OrderDelete(m_ticket2))
printf("%i: failed to delete#%d. error=%d",__LINE__,m_ticket2,_LastError);
m_ticket2=-1;
}
else
{
if(!OrderDelete(m_ticket1))
printf("%i: failed to delete#%d. error=%d",__LINE__,m_ticket1,_LastError);
m_ticket1=-1;
}
m_activated=true;
}
}
}
double getPnlPips(const int ticket)const
{
if(ticket>0 && OrderSelect(ticket,SELECT_BY_TICKET))
{
return (OrderProfit()>0 ? 1 : -1)*fabs(OrderOpenPrice()-OrderClosePrice());
}
return 0;
}
bool try2closeByPnl()const
{
const double pnl=getPnlPips(m_ticket1)+getPnlPips(m_ticket2);
if(pnl-InpProfitPips*PIP>0)
{
printf("%i : pnl=%.5f vs %.5f target. closing the tickets",__LINE__,pnl,InpProfitPips*PIP);
close(m_ticket1);
close(m_ticket2);
return(true);
}
return(false);
}
bool try2closeByTime()const
{
if(TimeCurrent()-m_timeStart-InpCloseSeconds>=0)
{
if(!OrderDelete(m_ticket1))printf("%i: failed to delete#%d. error=%d",__LINE__,m_ticket1,_LastError);
if(!OrderDelete(m_ticket2))printf("%i: failed to delete#%d. error=%d",__LINE__,m_ticket2,_LastError);
return(true);
}
return(false);
}
void close(const int ticket)const
{
if(ticket>0 && OrderSelect(ticket,SELECT_BY_TICKET))
{
RefreshRates();
if(OrderClose(ticket,OrderLots(),OrderClosePrice(),1))
printf("%i: failed to close#%d. error=%d",__LINE__,ticket,_LastError);
}
}
public:
C2Orders(const int ticket1,const int ticket2,const datetime time):
m_ticket1(ticket1),m_ticket2(ticket2),m_activated(false),m_timeStart(time){}
~C2Orders(){}
bool check() //returns FALSE if deleting the object
{
if(!m_activated)
{
checkTicket(m_ticket1);
checkTicket(m_ticket2);
}
if(m_activated)
{
if(try2closeByPnl())
return(false);
}
else
{
if(try2closeByTime())
return(false);
}
return true;
}
};
//+------------------------------------------------------------------+
int OnInit()
{
firstOrderOpen=false;
PIP=_Point*(_Digits%2==1 ? 10 : 1); //does not work for GOLD and indexes, good for FX.
ordersList=new CArrayObj();
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
delete(ordersList);
}
void OnTick()
{
if(!firstOrderOpen && TimeCurrent()>=InpTime1)
{
RefreshRates();
const int ticketBS=OrderSend(_Symbol,OP_BUYSTOP,InpLotSize,NormalizeDouble(Ask+InpPipsDist*PIP,_Digits),0,0,0,NULL,InpMagicNumber);
const int ticketSS=OrderSend(_Symbol,OP_SELLSTOP,InpLotSize,NormalizeDouble(Bid-InpPipsDist*PIP,_Digits),0,0,0,NULL,InpMagicNumber);
C2Orders *oco=new C2Orders(ticketBS,ticketSS,TimeCurrent());
ordersList.Add(oco);
firstOrderOpen=true;
}
if(firstOrderOpen)
{
C2Orders* oco;
for(int i=ordersList.Total()-1;i>=0;i--)//of course you have only one instance, but in real world you may need to loop over them.
{
oco=ordersList.At(i);
if(!oco.check())
ordersList.Delete(i);
}
if(ordersList.Total()==0)
ExpertRemove();//just to finish the test faster.
}
}

MQL4 Expert Advisor isn't doing anything

This bot is supposed to buy based on support and resistance, stop loss is based on support resistance of the moving averages. When I test it out it won't do anything trying. How do I debug this to do what I want it to do?
After viewing the code do you have any suggestions input or criticism?
I tried to comment the code the best I could to make sense.
#property strict
string BotName = "SRMATrader";
/********** SETTINGS *************/
extern int Magic = 8008;
string CandleColor;
int MaxCloseSpreadPips = 600;
int MaxTrades = 1; // was 10
int AcceptableSpread = 2;
double LotsToTrade = .2; // 0.1
double StopLoss = -3700; // 3800
double ProfitTarget = 20.00; // $280 at 2.0 trade size
int MaxOpenOrderDurationSeconds = (5 * 24 * 60 * 60); // 5 days was profitable
int TradeDelayTimeSeconds = 30; //(10 * 24 * 60 * 60); // 10 Days
int PendingOrderExpirationSeconds = (4 * 24 * 60 * 60); // 4 Days
datetime LastTradePlacedTimestamp = 0;
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
}
// MAIN LOOP
void OnTick()
{
double CurrentPrice = MarketInfo(Symbol(), MODE_BID);
double CandleResistance = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_HIGH, 0);
double CandleSupport = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_LOW, 0);
// Should we place a trade?
if (GetTotalOpenTrades() < MaxTrades)
{
if ( (TimeCurrent() - LastTradePlacedTimestamp) > TradeDelayTimeSeconds )
{
// long
if ( CurrentPrice <= CandleSupport )
{
if (CandleColor == "Green")
{
PlacePendingOrder("Green", LotsToTrade, CandleSupport, PendingOrderExpirationSeconds);
LastTradePlacedTimestamp = TimeCurrent();
}
}
// short
if ( CurrentPrice >= CandleResistance)
{
if (CheckForTradeSetup() == "Red" )
{
PlacePendingOrder("Red", LotsToTrade, CandleResistance, PendingOrderExpirationSeconds);
LastTradePlacedTimestamp = TimeCurrent();
}
}
}
}
if (GetTotalOpenTrades() > 0)
{
CloseTradeAfterAge(MaxOpenOrderDurationSeconds);
CheckForOrderClose(ProfitTarget, StopLoss);
}
} // end OnTick()
string CheckForTradeSetup()
{
double CurrentPrice = MarketInfo(Symbol(), MODE_BID);
double CandleSupport = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_LOW, 0);
double CandleResistance = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_HIGH, 0);
if ( CurrentPrice <= CandleSupport )
{
CandleColor = "Green";
}
if ( CurrentPrice >= CandleResistance )
{
CandleColor = "Red";
}
return "no-setup";
}
void PlacePendingOrder(string Trade_Type, double Lots, double At_Price, int Expiration_Seconds)
{
int TicketResult = 0;
datetime Expiration_Time = (TimeCurrent() + Expiration_Seconds);
double Price = NormalizeDouble(At_Price, Digits);
if (Trade_Type == "Green")
{
if (Ask < At_Price) return;
double StopPrice = CalculateStopLossPrice(Price, Lots, StopLoss, Trade_Type);
TicketResult = OrderSend(Symbol(), OP_BUYLIMIT, Lots, Price, 10, StopPrice, 0, " Buy", Magic, Expiration_Time, clrGreen);
}
if (Trade_Type == "Red")
{
if (Bid > At_Price) return;
double StopPrice = CalculateStopLossPrice(Price, Lots, StopLoss, Trade_Type);
TicketResult = OrderSend(Symbol(),OP_SELLLIMIT, Lots, NormalizeDouble(At_Price, Digits), 10, StopPrice, 0, " Sell", Magic, Expiration_Time, clrRed);
}
if(TicketResult < 0)
{
Print("OrderSend failed with error #",GetLastError());
}
else
{
Print("OrderSend placed successfully");
}
}
double CalculateStopLossPrice(double OrderPrice, double TradeSize, double StopLossDollars, string PositionType)
{
// Convert stop loss dollars to positive number
double CurrentSpread = MarketInfo(Symbol(), MODE_SPREAD);
Print("*** CurrentSpread: ", CurrentSpread);
double PipValue = (TradeSize * 10);
double StopLossPips = (StopLossDollars / PipValue);
double CandleSupport = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_LOW, 0);
double CandleResistance = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_HIGH, 0);
if (PositionType == "Green")
{
double StopLossPrice = CandleResistance;
}
if (PositionType == "Red")
{
double StopLossPrice = CandleSupport;
}
return 0.0;
}
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, clrGreen);
t--;
}
}
if(CloseResult < 0)
{
Print("OrderSend failed with error #", GetLastError());
}
else
{
Print("OrderSend placed successfully");
}
return;
}
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;
}
double GetTotalProfits()
{
double TotalProfits = 0.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;
TotalProfits = (TotalProfits + OrderProfit());
}
}
return TotalProfits;
}
// Close all trades if we are at profit or loss
void CheckForOrderClose(double Target, double Stop)
{
// check for profit or loss
if (GetTotalProfits() > Target)
{
CloseAllTrades();
}
}
// Close if trade is more than (n) seconds old
string CloseTradeAfterAge(int MaxOpenTradeAgeSeconds)
{
for(int t=0; t < OrdersTotal(); t++)
{
if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() != Symbol()) return "wrong symbol";
if(OrderMagicNumber() != Magic) return "magic number does not match";
if(OrderCloseTime() != 0) return "order already closed";
datetime OrderOpenTime = OrderOpenTime();
string Now = (TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS));
datetime NowTimeStamp = (StrToTime(Now));
if ((NowTimeStamp - OrderOpenTime) > MaxOpenTradeAgeSeconds) // 1 * 24 * 60 * 60
{
if ((OrderType() == 0) || (OrderType() == 1)) // Only close orders that are live (not limit ordes)
{
CloseAllTrades();
return "all trades closed";
}
}
}
}
return "trades not closed";
}
check and go over this piece of code until you are sure you understand the logic and make it working
//...
if (CandleColor == "Green") //this one is NULL
{
PlacePendingOrder("Green", LotsToTrade, CandleSupport, PendingOrderExpirationSeconds);
LastTradePlacedTimestamp = TimeCurrent();
}
}
// short
if ( CurrentPrice >= CandleResistance)
{
if (CheckForTradeSetup() == "Red" )//this fn() returns "no-setup"
//....
so edit the CheckForTradeSetup() function and call it before checking the colors, and also comparing two strings is a very bad idea because it is too slow. +1/0/-1 or enums or even colors (==int) is preferred

mql4 check if order exists executed multiple times

I'm using this code to check if orders are existing for the price buy1 and sell1 in my code.
Somehow some orders get executed twice.
Shouldn't happen because i'm checking if there is any open order with the same takeprofit.
Anyone able to see whats wrong?
bool CheckBuyOrder1(double buy1_tp){
for( int i = 0 ; i < OrdersTotal() - 1 ; i++ ) {
OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
if( OrderTakeProfit() == buy1_tp && OrderComment() == "buy" )
return(true);
}
return(false);
}
bool CheckSellOrder1(double sell1_tp){
for( int i = 0 ; i < OrdersTotal() - 1 ; i++ ) {
OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
if( OrderTakeProfit() == sell1_tp && OrderComment() == "sell" )
return(true);
}
return(false);
}
int totalOrders = OrdersTotal();
void OnTick()
{
if(totalOrders != OrdersTotal()){
double vbid = MarketInfo("EURUSD",MODE_BID);
double bid = NormalizeDouble(vbid, 3);
double market_buy_tp = bid;
double buy1= bid + 0.002;
double buy1_tp= bid + 0.003;
if(CheckOpenOrders1(market_buy_tp)==false && CheckBuyOrder1(buy1_tp)==false){
int ticket9=OrderSend(Symbol(),OP_BUYSTOP,Lots,buy1,MaxSlippage,0,buy1_tp,"buy",16380,0,clrGreen);
}
double market_sell_tp = bid;
double sell1 = bid - 0.003;
double sell1_tp= bid - 0.004;
if(CheckOpenOrdersSell1(market_sell_tp)==false && CheckSellOrder1(sell1_tp)==false ){
int ticket19=OrderSend(Symbol(),OP_SELLSTOP,Lots,sell1,MaxSlippage,0,sell1_tp,"sell",16380,0,clrGreen);
}
totalOrders = OrdersTotal();
}}
Whenever you try to compare an OrderTakeProfit() value with some other double - keep in mind rounding and float representation.
E.g. if you need to compare 0.10 with another double that you believe is 0.10 - you might be surprised that 0.10 is 0.09(9)6 or 0.10(0)4
That is why sometimes you might not find such an order.
double o = Point / 2; // initialize in OnInit(), declare in global vars
...
bool CheckOrderBuy( double tp ){
for ( int i = OrdersTotal() - 1; i >= 0; i-- ){
if ( !OrderSelect( i, SELECT_BY_POS ) )continue;
if ( fabs( OrderTakeProfit() - tp ) < o
&& OrderType() == OP_BUY
) return true;
}
return false;
}

My First MQL4 EA does not generate any Orders. Why?

I'm trying to build a first EA, code below, but it doesn't execute any trades.
I know it is very simple, but logically, I think this should Buy and Sell.
I'm trying to only use a code that I understand.
I'd appreciate it if anyone had any feedback!
//
extern int sma_short = 10;
extern int sma_long = 20;
extern double fakeout = 0.0005 ;
extern double stoploss = 150;
extern double risk = 1;
extern int slippage = 5;
extern int magicnumber = 12345;
extern bool SignalMail = false;
extern bool UseTrailingStop = true;
extern int TrailingStop = 150;
double sma_short_t3;
double sma_short_t0;
double sma_long_t3;
double sma_long_t0;
double sma_diff_t3;
double sma_diff_t0;
double lots;
double stoplosslevel;
int P = 1;
int ticket, ticket2;
int total = OrdersTotal();
bool OpenLong = false;
bool OpenShort = false;
bool CloseLong = false;
bool CloseShort = false;
bool isYenPair = false;
bool OpenOrder = false;
int OnInit()
{
if ( Digits == 5 || Digits == 3 || Digits == 1 ) P = 10; else P = 1; // To account for 5 digit brokers
if ( Digits == 3 || Digits == 2 ) isYenPair = true; // Adjust for YenPair
return( INIT_SUCCEEDED );
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit( const int reason )
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void Start()
{
sma_short_t3 = iMA( NULL, 0, sma_short, 0, MODE_SMA, PRICE_CLOSE, 3 );
sma_short_t0 = iMA( NULL, 0, sma_short, 0, MODE_SMA, PRICE_CLOSE, 0 );
sma_long_t3 = iMA( NULL, 0, sma_long, 0, MODE_SMA, PRICE_CLOSE, 3 );
sma_long_t0 = iMA( NULL, 0, sma_long, 0, MODE_SMA, PRICE_CLOSE, 0 );
sma_diff_t3 = sma_long_t3 - sma_short_t3;
sma_diff_t0 = sma_long_t0 - sma_short_t0;
if ( OpenOrder )
{
if ( CloseLong || CloseShort )
{
OrderClose( OrderTicket(), OrderLots(), Bid, slippage, MediumSeaGreen );
OpenOrder = False;
CloseLong = False;
CloseShort = False;
}
}
if ( sma_diff_t3 < 0 && sma_diff_t0 > fakeout )
{
OpenLong = True ;
CloseShort = True;
}
if ( sma_diff_t3 > 0 && sma_diff_t0 < -fakeout )
{
OpenShort = True;
CloseLong = True;
}
lots = risk * 0.01 * AccountBalance() / ( MarketInfo( Symbol(), MODE_LOTSIZE ) * stoploss * P * Point ); // Sizing Algo based on account size
if ( isYenPair == true ) lots = lots * 100; // Adjust for Yen Pairs
lots = NormalizeDouble( lots, 2 );
if ( OpenLong )
{
stoplosslevel = Ask - stoploss * Point * P;
OrderSend( Symbol(), OP_BUY, lots, Ask, slippage, stoplosslevel, 0, "Buy(#" + magicnumber + ")", magicnumber, 0, DodgerBlue );
OpenOrder = True;
}
if ( OpenShort )
{
stoplosslevel = Bid + stoploss * Point * P;
OrderSend( Symbol(), OP_SELL, lots, Ask, slippage, stoplosslevel, 0, "Buy(#" + magicnumber + ")", magicnumber, 0, DodgerBlue );
OpenOrder = True ;
}
}
//+------------------------------------------------------------------+
and why do you use (MarketInfo(Symbol(),MODE_LOTSIZE)? what is the idea of that? first try with double lots = 1.00; and if problem still exists - please add a line telling about the reason why ea failed to send. sth like int ticket = OrderSend(***); if(ticket<0)Print("error=",GetLastError()); or more complex telling about the actual prices, lots, stoploss etc.
Few things in MQL4 to rather get used to:
All PriceDOMAIN data has to be NormalizeDouble() before sending to MetaTrader 4 Server.
All EquityDOMAIN data has to follow a set of discrete values,having MathMin( aMinLOT_SIZE + N * aMinLOT_STEP, aMaxLOT_SIZE ). Normalisation of EquityDOMAIN data is broker-specificand instrument-specific, so need not be always 2.
For XTO, OrderSend(), OrderMOdify(), OrderClose(), one ought follow something like this:
if ( OpenLong )
{ stoplosslevel = NormalizeDouble( Ask - stoploss * Point * P, _Digits ); // ALWAYS NormalizeDouble()
int RetCODE = OrderSend( _Symbol,
OP_BUY,
lots,
Ask,
slippage,
stoplosslevel,
0,
"Buy(#" + magicnumber + ")",
magicnumber,
0,
DodgerBlue
);
if ( RetCODE < 0 )
{ Print( "EXC: Tried to go LONG, OrderSend() failed to get confirmed ( Errno: ", GetLastError(), " )" );
}
else
{ OpenOrder = True;
...
}
...
}

Resources