Download MetaTrader 5
To add comments, please log in or register
Register as a seller and start offering your signals!
Chistabo
140
Chistabo 2014.10.05 17:25 

Hi!

 

I have EA which has global variable [bool Trade=false;].

I need a function that would check if a line on chart has been crossed, it will enable trade [Trade=true;] , then 'wait' for 1 order to be opened (when trading conditions are met), and then disable the trade [Trade=false;].

Currently this is the code (not working):

   if(crossedLine("trade_once")) {TradeOnce();}
// ----- TRADE ONCE FUNCTION -----------------------------------------------------------------------------------
void TradeOnce()
   {
   int currentOpenOrdersNo=getNumOpenOrders(-1,magic); Trade=true;
   while(Trade==true)
      {
      if(getNumOpenOrders(-1,magic)>currentOpenOrdersNo) Trade=false;
      break;
      }
/*      
   datetime TradeOnceTime=0;
   if(crossedLine("trade_once"))
      {
      TradeOnceTime=(TimeCurrent()); Print("Trade Once line crossed! Broker time: "+TimeToStr(TimeCurrent(),
                     TIME_DATE|TIME_MINUTES)+", "+Symbol());
      Trade=true;
      if(TradeOnceTime+9000<=TimeCurrent()) {Trade=false;}
      Print("Trade Once time passed! Broker time: "+TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES)
         +", "+Symbol());
      }
*/

 Obviously I tried with timing solution (LOL) too, also unsuccessfully.

Please advise.

 

Thank you,

Simon

S love nia 

whroeder1
13602
whroeder1 2014.10.05 19:01  
Chistabo: Currently this is the code (not working):
 while(Trade==true)
  1. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  2. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isTrading. Trade sounds like a ticket number and "if trade" is an incomplete sentence.
  3. While you're in the while loop, who is doing the opening?
  4. if(crossedLine("trade_once")) currentOpenOrdersNo=getNumOpenOrders(-1,magic);
    isTrading = getNumOpenOrders(-1,magic) == currentOpenOrdersNo;
    if(isTrading) ...

Chistabo
140
Chistabo 2014.10.06 18:03  

Thank you for your reply, WHRoeder.

I have solved the problem:

bool     TradeOnce; // global variable
int      CurrentOpenOrdersNo; // global variable
...
// ----- START FUNCTION -----
int start()
  {
...
   checkLines();
   if(TradeOnce) tradeOnce();
...
   }
// ----- CHECK LINE CROSSING FUNCTION ----------------------------------------------------------------
void checkLines()
  {
...
   if(crossedLine("once_trade")) {TradeOnce=true; CurrentOpenOrdersNo=getNumOpenOrders(-1,magic); // put Trade=true; here?
                                    Print("once_trade line crossed! CurrOpenOrders: ",CurrentOpenOrdersNo,
                                    ", Broker time: "+TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES));}
...
  }
// ----- TRADE ONCE FUNCTION -----------------------------------------------------------------------------------
void tradeOnce()
   {
   Trade=true; // put Trade=true; in checkLines() function?
   if(getNumOpenOrders(-1,magic)>CurrentOpenOrdersNo) {TradeOnce=false; Trade=false;}
//   Print("TradeOnce: "+TradeOnce+", getNumOOrders: ",getNumOpenOrders(-1,magic),", CurrentOOrders#: ", // for test purpose
//   CurrentOpenOrdersNo,", Trade: ",Trade); 
   }

This code works.

1. Tire not flat, keys present, starts, will go in gear, yes electrical...

2. After all these years helping people (= answering to newbies && wanna-be-coders - thank you, WHRoeder; bow) you are still surprised how un-educated people (like me) can be? I am not a coder, mq4 is simple enough to be learned... hit&&miss...

3. This did crossed my mind, but at the time being that was the solution (keys=!present && tire=flat LOL!).

4. ... not helpful... but thank you anyway.

Best regards,

 

Have fun,

Simon

S love nia 

Chistabo
140
Chistabo 2014.10.07 12:22  

After some testing, some code has been added, since if TradeOnce line is crossed, TradeOnce parameters are set to true, and CurrentOpenOrdersNo is set, but if i.e. one order is closed, CurrentOpenOrderNo is not updated. Solution: reset TradeOnce function (to zero) if # of open orders is less than CurrentOpenOrdersNo.

...

...
// ----- CHECK LINE CROSSING FUNCTION ----------------------------------------------------------------
void checkLines()
   {
   double PriceBuy=NormalizeDouble(Ask,Digits);
   double PriceSell=NormalizeDouble(Bid,Digits);
   string LineOrderTime=TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES);
...
   if(crossedLine("buy once"))   {TradeOnce=true; Trade=true; BuyOnly=true; SellOnly=false;
                                    CurrentOpenOrdersNo=getNumOpenOrders(-1,magic);
                                    Print("BUY ONCE line crossed! CurrOpenOrders#: "+CurrentOpenOrdersNo+
                                    ", Broker time: "+LineOrderTime+", "+Symbol()+", Price Ask: "+PriceBuy);}
   if(crossedLine("sell once"))  {TradeOnce=true; Trade=true; SellOnly=true; BuyOnly=false;
                                    CurrentOpenOrdersNo=getNumOpenOrders(-1,magic);
                                    Print("SELL ONCE line crossed! CurrOpenOrders#: "+CurrentOpenOrdersNo+
                                    ", Broker time: "+LineOrderTime+", "+Symbol()+", Price Bid: "+PriceSell);}
...
   }
// ----- TRADE ONCE FUNCTION -----------------------------------------------------------------------------------
void tradeOnce()
   {
   double PriceBuy=NormalizeDouble(Ask,Digits);
   double PriceSell=NormalizeDouble(Bid,Digits);
   string TradeOnceTime=TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES);
   
   if(getNumOpenOrders(-1,magic)>CurrentOpenOrdersNo) {TradeOnce=false; Trade=false;
      Print("Trade Once order opened. Setting TradeOnce = false & Trade = false! Broker time: "+TradeOnceTime);}
   if(getNumOpenOrders(-1,magic)<CurrentOpenOrdersNo) {TradeOnce=false; Trade=false;
      Print("Open Orders # < CurrentOpenOrders#! Setting TradeOnce = false & Trade = false! Broker time: "+TradeOnceTime); }
   }

 The rest of the code stays pretty the same.

 Best regards,

 

Simon

S love nia 

/
To add comments, please log in or register