Problems checking for open trade

 

Hey there I have added this to my code to check if there is an open trade already. The problem is the code will execute one trade and that's it. Can anybody see a problem with this code. I am not a super expert with programing


// Check for open trades

for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
if(OrderSelect(cnt,SELECT_BY_POS))
{

if(OrderSymbol()==Symbol())
{
tradeopen=true;
}
else
{
tradeopen=false;
}

}

}

 
Your code will pickup Pending and Open trades, other than that it looks OK to me.
 

Thankyou Raptor for your promt reply


If you don't mind Ill attach the whole code, Maybe there is something I have missed elsewhere. I have hit the wall with this at the moment.

Files:
daz.mq4  3 kb
 
dazamate:
Can anybody see a problem with this code.

  1. No magic number means it's incompatible with every other EA and manual trading and itself on the same pair/different timeframes.
  2. If the first opened order was not the EA's then flag is false, even if it has open trades.
    int TotalOrderCount(){
        int count=0;
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
            count++;
        }
        return(count);
    }

 
dazamate:

Thankyou Raptor for your promt reply


If you don't mind Ill attach the whole code, Maybe there is something I have missed elsewhere. I have hit the wall with this at the moment.

A few comments . . .

You declare i . .

int i;

. . . but you don't set it anywhere, what is i ?

You set . . .

static bool tradeopen = false;

. . then check it to see if you can place a trade . . . it will always be false at this point . . . then you have code later which may set it to true

if(OrderSymbol()==Symbol())
    {
    tradeopen=true;
    }  

barcount is set to 0

static int barcount = 0;

so this will always be true if you have at least 1 bar on your chart . . .

if(Bars != barcount)

You need to go through your code line by line . . . anything you don't understand look it up and read about it . . . it's the only way you can learn.

 

Raptor, Thanks for going through it . Like I said I am no super expert. I have been reading the way other people code things and try implement it into mine. I have done lots of reading on the functions. It just gets frustrating when it doesn't work and you don't know how to diagnose your own disaster.

In response to your comments

int i;

Its gone, I was using that for something else accidentally left that there


I will explain whats going on in my head in regards to the barcounter . I am still not sure why this part of the code is no good.

static int barcount = 0;  // <- First time program is run bar counter is set to 0

     if(Bars != barcount) // <- This will be true so the if statement is allowed to run
    
       {      
         if(iBarHour == iOpenHour)
                {
                //If statement
    
                }
         barcount=Bars ; // <- At the end of the if statement, the bar counter is set to Bars so that this if statement will not be true
                         //    again until another bar opens
       }

As for the check to see if there is any open trades on the pair...

static bool tradeopen = false;                // First time program is run tradeopen is set to false. I use the static in front 
// because if I don't the programming will keep setting this to false because an ea or   // a program is just a giant loop is it not. However I should set this to true because // the ea should assume there is a trade open until it does it checks that would // probably be the more smarter approach.       for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)         {           if(OrderSelect(cnt,SELECT_BY_POS)) // <- So it scans each open and pending trade             {                           if(OrderSymbol()==Symbol()) // <- Checks to see if any of them trades are the same as the pair the ea is using                {                 tradeopen=true; // If so then set tradeopen to true                }                        else                {                 tradeopen=false; // If not set trade open to false                }                          }                         }

So at the end of the ea it does this check to see if there is a trade open on the current pair or not. Sets tradeopen to either true of false. Is it an issue that this bit off code is used at the end of the EA?

Also another problem I cant figure out...

int iBarHour;
iBarHour = TimeHour(TimeCurrent());
Print(iBarHour);
Why does iBarHour always return 1? I want it to return the current hour :(


WHRoeder


Does it matter if the EA uses a magic number or not. Can it not just check all open and pending trades and if any of them are the current pair?


Once again guys I know I am a newbie but all your input is appreciated I know I am probably going to get ripped into again about my dodgy coding :P


THanks

 
dazamate:

I will explain whats going on in my head in regards to the barcounter . I am still not sure why this part of the code is no good.

Sorry, I am probably showing my ignorance of the use of static variables . . . I use global variables instead and anything I want to set at the initialisation of the code I put it into the init function. https://docs.mql4.com/basis/functions/special

If you are running this part of the code during the weekend it will not change, it uses Server time and that updates with each tick.

int iBarHour;
iBarHour = TimeHour(TimeCurrent());
Print(iBarHour);

assuming this code works then dDayOpenPrice will only be set while iBarHour = 6, when iBarHour = 7 dDayOpenPrice will not longer be set to the correct value. If you make dDayOpenPrice a global then it's value will be saved while the code is running (making it static will probably work too). If the code is restarted nothing will happen until the next day though . . . not until the 6 am bar.

WHRoeder

Does it matter if the EA uses a magic number or not. Can it not just check all open and pending trades and if any of them are the current pair?

If you have one EA on one chart only with no manual trading . . then you can probably get away without using a Magic number, it's good practice to use it though, get your code working first then implement a magic number, it will help you learn.

 

RaptorUK thankyou once again.


I backtest the ea to see if code is working and Print() the variable to see whats going on in them to help debug the code. The ibarhour code seems to be working properly it just wasn't showing properly in the backtest because I have set it to constantly print ibarhour's value that it cant display it quick enough as the code spits it out so the backtester gets Jammed. Is there maybe someway to put a delay on the Print code?



Also yes I want it to store the value of the open of the 06:00 bar :)


I re organized the code, and I am proud of myself I did get it to be able to check if the ea had opened any trades on the current pair. I used WHRoeder 's example and turned it into a separate function :) and got the function to return true or false. Yay.



Since you are obviously an elite in the EA coding field. How many success stories have you heard from people making their own EA's? I have only heard/seen bad stories.











 
dazamate:

Since you are obviously an elite in the EA coding field. How many success stories have you heard from people making their own EA's? I have only heard/seen bad stories.











LOL . . . your comment should be directed at WHRoeder not me, he is a much, much more proficient coder than I am, take a look through some of his recent posts at the code he has shared, you can learn a lot from understanding his code, I know I have, thank you WHRoeder :-) .

I hang out here because I like to try and help people help themselves . . . and I get to learn stuff in the process.

To have a successful EA you first need a successful method . . . how many of those are there ? many things work for a short period of time, a few things will work for a month or two, very few work consistently. Even if someone has a good method (how they would know this without proper testing I don't know) they generally don't have the knowledge or ability to turn that into a viable EA. Anyone that does have a viable EA is hardly likely to advertise the fact, they will be using it and making money . . .

Glad to hear you are making progress . . . regarding your Print issue, you could use Comment(iBarHour); instead, it gets output to screen in the top left corner.

 
  1. You're welcome. "To learn something, practice. To master it, teach."
  2. Bars is unreliable (once you reach max bars on chart it won't change and code breaks.) Volume[0]==1 is unreliable, if you miss a tick the code breaks. Always use time.
    int start(){
       static datetime Time0;
       if (Time0 == Time[0]) return; Time0 = Time[0];
       // A new bar has started.

  3. probably showing my ignorance of the use of static variables . . . I use global variables instead and anything I want to set at the initialisation of the code
    If the variable needs to be initialized then it must be global. If the variable is shared between two or more functions then it must be global. Otherwise define it local to the function, static if need be.
  4. Does it matter if the EA uses a magic number or not.
    You'll probably have everything working, then you'll open another chart to test there and then suddenly everything fails. Do it right from the beginning. In my code, I check both magic number and pair just in case I put it on another chart but don't change the magic number.
 

Your right Raptor... Sorry WHRoeder that was rude not thank you are well. Well this is a beginning of a huge project for me so I really hope you guys don't mind me bugging you.


int start(){
   static datetime Time0;
   if (Time0 == Time[0]) return; Time0 = Time[0];
   // A new bar has started.

WHRoeder, This code looks so simple but I cant work out in my head how it works, won't the if statement always be true if it makes time0 equal to time[0] every time time0 is equal to time[0]? Not saying it doesn't work but I just don't understand how it works structured like that.


2 more questions.

How would I go about making a counter where if a pending order is place and it is not triggered with in x amount of bars then it is to be canceled ? All I can come up with is put a counter to count every time a new bar forms and if the amount of bars counted == the specified allowed bars before pending orders are canceled. The bar counter is reset everytime a new pending order is opened? How does that sound?

Next question.

This one I have no Idea how to go about.

I want to make a function that scans eurusd, usdchf, gbpusd, usdjpy on 1hr tf. It goes back to the last 06:00 gmt candle records the open, goes back another 24 bars to the prev 06:00 gmt candle records that open to and records the 6gmt -6gmt range for each pair. Then compares the 6gmt - 6gmt range from all pairs and returns the one which is the highest. Is it possible for an ea to do that from being attached to 1 time chart?


Thanks again for your knowledge guys and your patients

Reason: