why won't this code work?

 

the EA should open a new trade when the "ask" price drops below a certain price. however, the EA does not open a trade until it is compiled. i am thinking it should open a trade on its own. where are i going wrong?

int start()
{

int j;

if (LotDecimal==0) LotDecimal = LotDecimal();

Information();

LongTradeNew=true;


if(timeprevMIN!=iTime(NULL,OpenNewTF,0))
{
CountTrades = CountTrades("buy");
if (CountTrades==0 && Ask<Buy_Price && LongTradeNew==true)
{
CommentTrades = "LONG ADVISOR 2012 V1.0"+Symbol()+" - Buy "+(CountTrades+1);
Buy_NewLot = NewLot("buy");

ticket=0;
for (j = 0; ticket < 1 && j < MaxAttempts; j++)
{
Print("NEW POSITION OPENED on CURRENCY PAIR: "+Symbol()+", LOT= "+Buy_NewLot+", PRICE= "+Ask+", ADVISOR= "+CommentTrades+" NUMBER= "+MagicNumber);
ticket = OrderSend(Symbol(),OP_BUY,Buy_NewLot,Ask,3,0,0,CommentTrades,MagicNumber,0,Blue);
if(ticket<0) Sleep(10000);
}
if(ticket<0)
{
Print("ERROR: ",GetLastError());
} else {
RefreshRates();
}
}
Ñorrect("buy");
timeprevMIN=iTime(NULL,OpenNewTF,0);
}

 
cavesinarizona:

however, the EA does not open a trade until it is compiled.

That is not a correct word in that context. When you write the EA, you then compile it. You can't run it before it is compiled.

Use the SRC button to get a window to paste your source code into.

 

sorry, maybe i did not make myself clear. the EA has already been compiled with no errors or warnimgs. in actual trading it will not open a trade even if the "ask < buy_Price". it will however open the trade if metatrader 4 is restarted or the EA is re-compiled. its as if the EA does not recognize the new "ask" price without being re-compiled or re-started.

 
int start()
{
   
   int j;
   
   if (LotDecimal==0) LotDecimal = LotDecimal();
   
   Information();
   
   LongTradeNew=true;
   
     
   if(timeprevMIN!=iTime(NULL,OpenNewTF,0))
   {
      CountTrades = CountTrades("buy");
      if (CountTrades==0 && Ask<Buy_Price && LongTradeNew==true)
      {
         CommentTrades = "LONG ADVISOR 2012 V1.0"+Symbol()+" - Buy "+(CountTrades+1);
         Buy_NewLot = NewLot("buy");
         
         ticket=0;
         for (j = 0; ticket < 1 && j < MaxAttempts; j++)
         {
            Print("NEW POSITION OPENED on CURRENCY PAIR: "+Symbol()+", LOT= "+Buy_NewLot+", PRICE= "+Ask+", ADVISOR= "+CommentTrades+" NUMBER= "+MagicNumber);
            ticket = OrderSend(Symbol(),OP_BUY,Buy_NewLot,Ask,3,0,0,CommentTrades,MagicNumber,0,Blue);
            if(ticket<0) Sleep(10000);
         }   
         if(ticket<0) 
         {
            Print("ERROR: ",GetLastError()); 
         } else {
            RefreshRates();
         }   
      }
      Ñorrect("buy");
      timeprevMIN=iTime(NULL,OpenNewTF,0);
   }    
 

Don't give functions and variables exactly the same name

CountTrades = CountTrades("buy")
 
         ticket=0;
         for (j = 0; ticket < 1 && j < MaxAttempts; j++)
         {
            Print("NEW POSITION OPENED on CURRENCY PAIR: "+Symbol()+", LOT= "+Buy_NewLot+", PRICE= "+Ask+", ADVISOR= "+CommentTrades+" NUMBER= "+MagicNumber);
            ticket = OrderSend(Symbol(),OP_BUY,Buy_NewLot,Ask,3,0,0,CommentTrades,MagicNumber,0,Blue);
            if(ticket<0) Sleep(10000);
         }  

You sleep for 10 seconds on a failed trade but do not RefreshRates after doing so.

 

not sure what you mean? the EA works great. i simply added the "Ask<Buy_Price" in an attemt to get it to open only below a certain point. however at that point it failed to open trades. if the price is below the required point i can re-start it and it will open the trade and begin functioning normally. obviously the inserted line it not correct. any help? how will changing the names of the variables and functions help?

 if(timeprevMIN!=iTime(NULL,OpenNewTF,0))
   {
      CountTrades = CountTrades("buy");
      if (CountTrades==0 && Ask<Buy_Price && LongTradeNew==true)
      {
         CommentTrades = "LONG ADVISOR 2012 V1.0"+Symbol()+" - Buy "+(CountTrades+1);
         Buy_NewLot = NewLot("buy");
 
cavesinarizona:

sorry, maybe i did not make myself clear. the EA has already been compiled with no errors or warnimgs. in actual trading it will not open a trade even if the "ask < buy_Price". it will however open the trade if metatrader 4 is restarted or the EA is re-compiled. its as if the EA does not recognize the new "ask" price without being re-compiled or re-started.

Ok, fair enough. Then add some print statements to find out where it is getting to. There is also loads of missing code (that we can't see) that may be the cause of the problem.
 
dabbler:
You sleep for 10 seconds on a failed trade but do not RefreshRates after doing so.
Nor do you print out WHY it failed.
 

the entire code is too long to insert. Please see attached file for entire code. i wanted to have the EA not open a trade until the "ask" price reaches a certain level. as it stands now it open trades without regard to prices or indicators. simply a "martingale" type

Files:
test_2.mq4  10 kb
 
cavesinarizona:

not sure what you mean? the EA works great. i simply added the "Ask<Buy_Price" in an attemt to get it to open only below a certain point. however at that point it failed to open trades. if the price is below the required point i can re-start it and it will open the trade and begin functioning normally. obviously the inserted line it not correct. any help? how will changing the names of the variables and functions help?

If you do not want to correct these obvious failures, try to change your inserting only, write it into the another if condition:

 if(timeprevMIN!=iTime(NULL,OpenNewTF,0) && Ask<Buy_Price)
   {
      CountTrades = CountTrades("buy");
      if (CountTrades==0 && LongTradeNew==true)
      {
         CommentTrades = "LONG ADVISOR 2012 V1.0"+Symbol()+" - Buy "+(CountTrades+1);
         Buy_NewLot = NewLot("buy");