Can anyone tell me what I am doing wrong?

 

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



 
Vincent Capizzo:

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. 
Does your code open a trade without any logic at all?
That would confirm your OrderSend parameters are correct at least. 

 
andrew4789:
The way I started was to simplify things. 
Does your code open a trade without any logic at all?
That would confirm your OrderSend parameters are correct at least. 

I'll comment out the if statements and try that real quick
 
Vincent Capizzo:
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);"

 
OrderSend(Symbol(),OP_SELL, LotSize, Bid, 3,Bid,Bid,NULL, MagicNumber, 0,Green

I don't know how you don't get any errors how comes you set order price equals to sl and tp all bid ?!!!!!!


Also variables shouldn't be declared this way

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

 
Kareem Abdelhakim:
OrderSend(Symbol(),OP_SELL, LotSize, Bid, 3,Bid,Bid,NULL, MagicNumber, 0,Green

I don't know how you don't get any errors how comes you set order price equals to sl and tp all bid ?!!!!!!


Also variables shouldn't be declared this way

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?

 
  1. 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

  2. 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.

  3. 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.

  4. 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.

  5. 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

 
William Roeder:
  1. 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

  2. These are decloration of two functions. You never define the functions and you never call them. These two lines do nothing, delete them.

  3. 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.

  4. 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.

  5. 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

Thank you, I tried the Alt-S and edited my post but I don't think I did it properly. The original post has been edited and the long bit of code is no longer there. How do you use Alt-S to show code? You'll have to forgive my being a bit slow as this is new to me. I don't want to clog the thread with lots of code again so I won't post any more until I figure out how to use the Alt-S. As things stand I have the EA entering trades when I want it to. I have 4 warnings and no errors but it won't close the trade on the desired trigger. I found out that in addition to the errors tab the journal tab can give me specific error codes in the strategy tester. In that journal tab I am currently seeing two errors 1) "unknown ticket 1234 for OrderClose function" and "OrderClose error 4108. 
 

Just add this to the top of your code

#property strict
In 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. 
 
Vincent Capizzo: How do you use Alt-S to show code?

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:SRC

 
William Roeder:

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

Reason: