Hey guys,
I am just starting to learn MQL4 and working toward building an EA one step at a time, I am at the first step and can't figure out why I can't get it to open a trade. Any ideas? Code is below
//--- input parameters
extern int CCIperiod = 14;
extern double StopTradingBalance = 1000;
extern int HighTimeFrameEMA = 0;
extern int MediumTimeFrameEMA = 0;
extern int LowTimeFrameEMA = 0;
extern double LotSize = 0.01;
double PreviousCCIReading();
double CurrentCCIReading ();
extern double StopTradingBalance();
extern int MagicNumber = 1234;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---CCI function values
double PreviousCCIReading = iCCI(NULL,5,15,0,2);
double CurrentCCIReading = iCCI(NULL,5,15,0,1);
//sell order
if(PreviousCCIReading > 100 && CurrentCCIReading < 100)
if(AccountBalance() > StopTradingBalance)
if(OrdersTotal()==0)
OrderSend(Symbol(),OP_SELL, LotSize, Bid, 3,Bid,Bid,NULL, MagicNumber, 0,Green);
}
The way I started was to simplify things.
I'll comment out the if statements and try that real quick
I don't have any errors but it won't enter a trade on the tester, the only if statement I have left is the "if(PreviousCCIReading > 100)". does the order send line look right? "OrderSend(Symbol(),OP_SELL, LotSize, Bid, 3,Bid,Bid,NULL, MagicNumber, 0,Green);"
uble PreviousCCIReading();
double CurrentCCIReading ();
extern double StopTradingBalance();
Remove the parentheses . They not functions
if(AccountBalance() > StopTradingBalance)
if(OrdersTotal()==0)
Finally this will never be evaluated to true . Simply you are comparing against empty value which it very big number sure yojr balance will not be greater than it
OrderSend(Symbol(),OP_SELL, LotSize, Bid, 3,Bid,Bid,NULL, MagicNumber, 0,Green
uble PreviousCCIReading();
double CurrentCCIReading ();
extern double StopTradingBalance();
Remove the parentheses . They not functions
if(AccountBalance() > StopTradingBalance)
if(OrdersTotal()==0)
Finally this will never be evaluated to true . Simply you are comparing against empty value which it very big number sure yojr balance will not be greater than it
yeah, no errors at compile. I have changed the OrderSend line to "OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3,Bid+(StopLoss*Pips),Bid- (TakeProfit*Pips),NULL, MagicNumber, 0,Green);" Now it is entering trades when I want the EA to do so. I have taken the parenthesis out of "PreviousCCIReading" and "CurrentCCIReading". I had to comment out the "StopTradingBalance" variable because when I took out the parenthesis it gave me an error at compile saying that it had already been defined. the gaol behind the StopTradingBalance is to not allow any trades if the account balance falls below the stop trading balance as the EA will be running when I sleep. Any ideas for that?
- When you post code please use the CODE button (Alt-S)!
(For large amounts of code, attach it.)
Please edit
your (original) post.
General rules and best pratices of the Forum. - General - MQL5 programming forum
Messages Editor double PreviousCCIReading(); double CurrentCCIReading ();
These are decloration of two functions. You never define the functions and you never call them. These two lines do nothing, delete them.- Vincent Capizzo: h, no errors at compile. I
Irrelevant, extern double StopTradingBalance(); should not compile. An extern can not be a function, so the compiler is taking that as A) an extern double variable with no default value, or B) it is an declaration of a function returning a double, but you never define the function and never call it. It does nothing but confuse the compiler so line with StopTradingBalance should not compile.
All bets are off until you write correct code.
OrderSend(Symbol(),OP_SELL, LotSize, Bid, 3,Bid,Bid,NULL, MagicNumber, 0,Green);
Check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. Don't just silence the compiler, it is trying to help you.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.You can't have the SL and TP closer to the market than the minimum (MODE_STOPLEVEL * _Point / SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).)
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
- When you post code please use the CODE button (Alt-S)!
(For large amounts of code, attach it.)
Please edit
your (original) post.
General rules and best pratices of the Forum. - General - MQL5 programming forum
Messages Editor - These are decloration of two functions. You never define the functions and you never call them. These two lines do nothing, delete them.
Irrelevant, extern double StopTradingBalance(); should not compile. An extern can not be a function, so the compiler is taking that as A) an extern double variable with no default value, or B) it is an declaration of a function returning a double, but you never define the function and never call it. It does nothing but confuse the compiler so line with StopTradingBalance should not compile.
All bets are off until you write correct code.
- Check your return codes for errors, and report them including GLE/LE.
Don't look at it unless you have an error.
Don't just silence the compiler, it is trying to help you.
What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after. You can't have the SL and TP closer to the market than the minimum (MODE_STOPLEVEL * _Point / SymbolInfoInteger(SYMBOL_TRADE_STOPS_LEVEL).)
Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial
Just add this to the top of your code
#property strictIn this way the complier will tell you the error line of code. If you encounter a warning, or error description of a (string to int). You have a variable that can not be of the same type.
Edit your original post. Click where you want the code to go. Alt-S and paste your code in the pop up. Click insert.
Click image to play:
Thank you
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey guys,
I am just starting to learn MQL4 and working toward building an EA one step at a time, I am at the first step and can't figure out why I can't get it to open a trade. Any ideas? Code is below