- DEEPAK SONI: Its still opening new trade when condetions are not true .
Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
Code debugging - Developing programs - MetaEditor Help
Error Handling and Logging in MQL5 - MQL5 Articles (2015)
Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010) -
double takeProfitreward = ask - stopLoss ; double takeProfitrewards = takeProfitreward * 1.20; double takeProfits = ask + takeProfitrewards;
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close at a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
My GBPJPY shows average spread = 26 points, average maximum spread = 134.
My EURCHF shows average spread = 18 points, average maximum spread = 106.
(your broker will be similar).
Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)
-
i made an EA for bull & bear engulfing. also stated to enter new trade when no orders running on both of them seperatively.
Its still opening new trade when condetions are not true . for example entering a bear trade till it closes, then again enter new bear trade without condetions trade onw new candles . Same for bull trades .
try this
#property copyright "Forum Discussion , The" #property link "https://www.mql5.com/en/forum/449111" #property version "1.00" #include <Trade\Trade.mqh> // Constants const int BULL_ENGULFING = -1; const int BEAR_ENGULFING = 1; const double PIP_SIZE = _Point; ulong Tickets; datetime barStamp=0; // Global variables CTrade trade; int OnInit(){ barStamp=0; return(INIT_SUCCEEDED); } // Function to check for bullish engulfing pattern bool IsBullEngulfing(bool &managed_to_check_it) { managed_to_check_it=false; ResetLastError(); int error_collection=0; int prev = 2; double prevOpen = iOpen(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevClose = iClose(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevLow = iLow(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevHigh = iHigh(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double currentOpen = iOpen(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentClose = iClose(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentLow = iLow(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentHigh = iHigh(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); //if no errors if(error_collection==0){ managed_to_check_it=true; // Check if the current candle is bullish and engulfs the previous candle bool isBullish = (currentOpen < currentClose) && (currentClose > prevOpen) && (currentOpen <= prevClose); bool isEngulfing = currentHigh > prevHigh; return(isBullish && isEngulfing); } return(false); } // Function to check for bearish engulfing pattern bool IsBearEngulfing(bool &managed_to_check_it) { managed_to_check_it=false; ResetLastError(); int error_collection=0; int prev = 2; double prevOpen = iOpen(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevClose = iClose(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevLow = iLow(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevHigh = iHigh(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double currentOpen = iOpen(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentClose = iClose(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentLow = iLow(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentHigh = iHigh(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double stopLoss = currentHigh; //if no errors if(error_collection==0){ managed_to_check_it=true; // Check if the current candle is bearish and engulfs the previous candle bool isBearish = (currentOpen > currentClose) && (currentClose < prevOpen) && (currentOpen >= prevClose); bool isEngulfing = currentLow < prevLow; return(isBearish && isEngulfing); } return(false); } // Function to send a buy order // Function to send a sell order // The OnTick event handler void OnTick() { ResetLastError(); datetime now=iTime(_Symbol,_Period,0); //check on new bars only if(GetLastError()==0&&now>barStamp){ //create 2 indicators for the check completing succesfully bool isBull=false,isBear=false,bullChecked=false,bearChecked=false; //we want both checks to fire otherwise we do not register the new bar timestamp isBull=IsBullEngulfing(bullChecked); isBear=IsBearEngulfing(bearChecked); //if both checked succefussvufully :P if(bullChecked&&bearChecked) { //then we log the new bar timestamp barStamp=now; //and then we check for opening the trades if(isBull){ // Retrieve the current Ask and Bid prices double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Calculate the risk based on 0.4% of the stop loss distance double stopLoss = iLow(_Symbol, PERIOD_CURRENT, 1); double riskAmount = ask - stopLoss * 100000; double riskPercentage = 0.04; // 4% risk double accountCapital = AccountInfoDouble(ACCOUNT_BALANCE); double takeProfitreward = ask - stopLoss ; double takeProfitrewards = takeProfitreward * 1.20; double takeProfits = ask + takeProfitrewards; double tradeQuantity = AccountInfoDouble(ACCOUNT_BALANCE); double tradeqty = tradeQuantity * riskPercentage; double qty = tradeqty / riskAmount; if (PositionsTotal()==0 && OrdersTotal()==0) { trade.SetExpertMagicNumber(00002); trade.Buy(2.0,_Symbol,ask,stopLoss,takeProfits,"Bull Engulfing"); } Comment("Bullish("+TimeToString(now,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); } if(isBear){ // Retrieve the current Ask and Bid prices double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Calculate the risk based on 0.4% of the stop loss distance double stopLoss = iHigh(_Symbol, PERIOD_CURRENT, 1); double riskAmount = stopLoss - bid * 100000; // Assuming a short trade double riskPercentage = 0.04; // 4% risk double accountCapital = AccountInfoDouble(ACCOUNT_BALANCE); double takeProfitreward = stopLoss - bid; double takeProfitrewards = takeProfitreward * 1.20 ; double takeProfits= bid - takeProfitrewards; double tradeQuantity = AccountInfoDouble(ACCOUNT_BALANCE); double tradeqty = tradeQuantity * riskPercentage; double qty = tradeqty / riskAmount; if (PositionsTotal()==0 && OrdersTotal()==0){ trade.SetExpertMagicNumber(00004); trade.Sell(2.0,_Symbol,bid,stopLoss,takeProfits,"bear Engulfing"); } Comment("Bearish("+TimeToString(now,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); } } } }
Thank you for your efforts.
But after backtesting, this code is not giving any orders
try this one :
#property copyright "Forum Discussion , The" #property link "https://www.mql5.com/en/forum/449111" #property version "1.00" #include <Trade\Trade.mqh> // Constants const int BULL_ENGULFING = -1; const int BEAR_ENGULFING = 1; const double PIP_SIZE = _Point; ulong Tickets; datetime barStamp=0; // Global variables CTrade trade; int OnInit(){ barStamp=0; return(INIT_SUCCEEDED); } // Function to check for bullish engulfing pattern bool IsBullEngulfing(bool &managed_to_check_it) { managed_to_check_it=false; ResetLastError(); int error_collection=0; int prev = 2; double prevOpen = iOpen(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevClose = iClose(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevLow = iLow(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevHigh = iHigh(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double currentOpen = iOpen(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentClose = iClose(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentLow = iLow(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentHigh = iHigh(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); //if no errors if(error_collection==0){ managed_to_check_it=true; // Check if the current candle is bullish and engulfs the previous candle bool isBullish = (currentOpen < currentClose) && (currentClose > prevOpen) && (currentOpen <= prevClose); bool isEngulfing = currentHigh > prevHigh; return(isBullish && isEngulfing); } return(false); } // Function to check for bearish engulfing pattern bool IsBearEngulfing(bool &managed_to_check_it) { managed_to_check_it=false; ResetLastError(); int error_collection=0; int prev = 2; double prevOpen = iOpen(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevClose = iClose(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevLow = iLow(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevHigh = iHigh(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double currentOpen = iOpen(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentClose = iClose(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentLow = iLow(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentHigh = iHigh(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double stopLoss = currentHigh; //if no errors if(error_collection==0){ managed_to_check_it=true; // Check if the current candle is bearish and engulfs the previous candle bool isBearish = (currentOpen > currentClose) && (currentClose < prevOpen) && (currentOpen >= prevClose); bool isEngulfing = currentLow < prevLow; return(isBearish && isEngulfing); } return(false); } // Function to send a buy order // Function to send a sell order // The OnTick event handler void OnTick() { ResetLastError(); datetime now=iTime(_Symbol,_Period,0); //check on new bars only if(GetLastError()==0&&now>barStamp){ //create 2 indicators for the check completing succesfully bool isBull=false,isBear=false,bullChecked=false,bearChecked=false; //we want both checks to fire otherwise we do not register the new bar timestamp isBull=IsBullEngulfing(bullChecked); isBear=IsBearEngulfing(bearChecked); //if both checked succefussvufully :P if(bullChecked&&bearChecked) { //then we log the new bar timestamp barStamp=now; //and then we check for opening the trades if(isBull){ // Retrieve the current Ask and Bid prices double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Calculate the risk based on 0.4% of the stop loss distance double stopLoss = iLow(_Symbol,_Period, 1); double riskAmount = (ask - stopLoss) / _Point; double riskPercentage = 0.04; // 4% risk double accountCapital = AccountInfoDouble(ACCOUNT_BALANCE); double takeProfitreward = ask - stopLoss ; double takeProfitrewards = takeProfitreward * 1.20; double takeProfits =NormalizeDouble((ask + takeProfitrewards),_Digits); double tradeQuantity = AccountInfoDouble(ACCOUNT_BALANCE); double tradeqty = tradeQuantity * riskPercentage; double qty = tradeqty / riskAmount; //Tick value for one lot (movement of 1 point (_Point) is equal to what with one lot) double tvol_buy=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE); /*You know you can lose "risk percentage" by "risk amount" in other words , the 4% will be lost in distance ask->StopLoss so you want to "estimate" the lot size So you know how much one point for one lot costs and you know how many points your loss would be so you can project the lot you will also have to check many other things , lot size limit , max total volume , if the asset is tradable if you have enough margin etc.*/ double tick_value_for_your_lot=tradeqty/riskAmount; qty=NormalizeDouble(tick_value_for_your_lot/tvol_buy,2); if(PositionsTotal()==0 && OrdersTotal()==0) { trade.SetExpertMagicNumber(2); trade.Buy(qty,_Symbol,ask,stopLoss,takeProfits,"Bull Engulfing"); } Comment("Bullish("+TimeToString(now,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); } if(isBear){ // Retrieve the current Ask and Bid prices double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Calculate the risk based on 0.4% of the stop loss distance double stopLoss = iHigh(_Symbol, PERIOD_CURRENT, 1); double riskAmount = stopLoss - bid * 100000; // Assuming a short trade double riskPercentage = 0.04; // 4% risk double accountCapital = AccountInfoDouble(ACCOUNT_BALANCE); double takeProfitreward = stopLoss - bid; double takeProfitrewards = takeProfitreward * 1.20 ; double takeProfits= bid - takeProfitrewards; double tradeQuantity = AccountInfoDouble(ACCOUNT_BALANCE); double tradeqty = tradeQuantity * riskPercentage; double qty = tradeqty / riskAmount; if (PositionsTotal()==0 && OrdersTotal()==0){ trade.SetExpertMagicNumber(00004); trade.Sell(2.0,_Symbol,bid,stopLoss,takeProfits,"bear Engulfing"); } Comment("Bearish("+TimeToString(now,TIME_DATE|TIME_MINUTES|TIME_SECONDS)); } } } }
If you see the short trade it took in after the fall{ in middle} It was not as per the condetion, it went to bear trade again after previous one closed even when condetions were not true this time.
for this condition it has been instructed to find it is correct i think
// Check if the current candle is bearish and engulfs the previous candle bool isBearish = (currentOpen > currentClose) && (currentClose < prevOpen) && (currentOpen >= prevClose); bool isEngulfing = currentLow < prevLow;
try with these :
// Function to check for bullish engulfing pattern bool IsBullEngulfing(bool &managed_to_check_it) { managed_to_check_it=false; ResetLastError(); int error_collection=0; int prev = 2; double prevOpen = iOpen(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevClose = iClose(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevLow = iLow(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevHigh = iHigh(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double currentOpen = iOpen(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentClose = iClose(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentLow = iLow(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentHigh = iHigh(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); //if no errors if(error_collection==0){ managed_to_check_it=true; // Check if the current candle is bullish and engulfs the previous candle /* bool isBullish = (currentOpen < currentClose) && (currentClose > prevOpen) && (currentOpen <= prevClose); bool isEngulfing = currentHigh > prevHigh; return(isBullish && isEngulfing); */ //is the previous bar bearish ? if(prevOpen>prevClose){ //and is the current bar bullish ? if(currentClose>currentOpen){ //is the close of the previous above the current open if(prevClose>=currentOpen){ //is the open of the previous below the current close if(prevOpen<=currentClose){ return(true); } } } } } return(false); } // Function to check for bearish engulfing pattern bool IsBearEngulfing(bool &managed_to_check_it) { managed_to_check_it=false; ResetLastError(); int error_collection=0; int prev = 2; double prevOpen = iOpen(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevClose = iClose(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevLow = iLow(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double prevHigh = iHigh(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError(); double currentOpen = iOpen(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentClose = iClose(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentLow = iLow(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double currentHigh = iHigh(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError(); double stopLoss = currentHigh; //if no errors if(error_collection==0){ managed_to_check_it=true; /* // Check if the current candle is bearish and engulfs the previous candle bool isBearish = (currentOpen > currentClose) && (currentClose < prevOpen) && (currentOpen >= prevClose); bool isEngulfing = currentLow < prevLow; return(isBearish && isEngulfing); */ //is the previous bar bullish ? if(prevOpen<prevClose){ //and is the current bar bearish ? if(currentClose<currentOpen){ //is the open of the previous above the current close if(prevOpen>=currentClose){ //is the close of the previous below the current open if(prevClose<=currentOpen){ return(true); } } } } } return(false); }
Ea on Engulfing pattern, incorrect opening of more order on false condetions also.
Made an ea on Engulfing pattern.Yes exactly, I tried testing your code sir, but it wasn't opening any order, i don't know what's wrong with your code . It doesn't show any error while compiling but at time of backtest it doesn't give any orders.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
i made an EA for bull & bear engulfing. also stated to enter new trade when no orders running on both of them seperatively.
Its still opening new trade when condetions are not true . for example entering a bear trade till it closes, then again enter new bear trade without condetions trade onw new candles . Same for bull trades .