Please guide me regarding spread - page 4

 
deysmacro:
You must remember though. The EA closes running trades only. You have to open trade yourself.

100%, no problem there, rather thats exactly what I am trying to achieve .. i dont want EA to open any trade, thats MY job .. I just want it to close it if more than 2 orders are open at X profit and must consider spread ! Slowly I am getting towards it .. this programming thing should have been taught in schools.. hehe too bad I am from the 80s, never thought I will have to go through it, but more I read the help file, I find it more and more intriguing .. also, this forum has loads of help, thanks to guys like you.
 
tatyawinchu:

what if I can add an IF loop and then paste the rest of the code below which starts the loop?

The IF condition should check the OrdersTotal to be => 2 so that control is passed to close all open orders at X profit after spread?



I'm not sure what you really mean by "The IF condition should check the OrdersTotal to be => 2 so that control is passed to close all open orders at X profit after spread"

but I'm pretty sure nothing really bad will happen if you try. And if it doesn't work, there's nothing to lose. At least you tried.

 
tatyawinchu:

100%, no problem there, rather thats exactly what I am trying to achieve .. i dont want EA to open any trade, thats MY job .. I just want it to close it if more than 2 orders are open at X profit and must consider spread ! Slowly I am getting towards it .. this programming thing should have been taught in schools.. hehe too bad I am from the 80s, never thought I will have to go through it, but more I read the help file, I find it more and more intriguing .. also, this forum has loads of help, thanks to guys like you.

Just include spread in totalprofit.
 
thrdel:


I'm not sure what you really mean by "The IF condition should check the OrdersTotal to be => 2 so that control is passed to close all open orders at X profit after spread"

but I'm pretty sure nothing really bad will happen if you try. And if it doesn't work, there's nothing to lose. At least you tried.


I'm not sure what you really mean by "The IF condition should check the OrdersTotal to be => 2 so that control is passed to close all open orders at X profit after spread"

Hmm.. Ok what I mean by that is

IF .. total number of open orders is more than or equal to 2 .. only then go ahead and follow rest of the code..

IF total number of open orders .. [ OrdersTotal() ?? ] .. is less than 2 .. that means its either 1 or 0, then do nothing ..

Now, here is a problem for me, that mql 4 help file says OrdersTotal() counts all orders, market AND pending .. I am trying to look for some function which can get only live market orders, not pending ones, OR, it can be mathematically calculated as mostly I only have max 2 trades running at a go anyways.

Is this explanation clearer?

tx for the time you are giving me man.

 

thrdel wants you to try first and see what happen though I am pretty sure what would likely to happen.

 
deysmacro:

thrdel wants you to try first and see what happen.



yes, going through help file to find the proper function which gives only open orders.
 
Show us your code when you manage to complete it and we will help you if we can.
 
deysmacro:
Show us your code when you manage to complete it and we will help you if we can.

Roger that ! Thanks a loads..

..

and I failed to find a function that only returns open trades .. This is ok, so I added the IF loop as below..

//+------------------------------------------------------------------+
//|                    CloseTrades_After_Account_Profit_Reached.mq4  |
//|                                     Copyright © 2007, Tradinator |
//|                                          tradinator.fx@gmail.com |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2007, Tradinator"
#property link      "tradinator.fx@gmail.com"


                                       
extern double My_Money_Profit_Target=100;     //The amount of money profit at which you want to close ALL open trades.
extern string Profit_Target= "Enter above To Close all OPEN trades when amount of profit is Reached in Account and not per OPEN trade! ";
                                          
int Slippage=5;
int i;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+


int start()

//+------------------------------------------------------------------+
//| This is just the single line of code that I have added
//+------------------------------------------------------------------+
{ 
 if (OrdersTotal()>=2)

 {
   if (AccountProfit()>= My_Money_Profit_Target)
   {
    for(i=OrdersTotal()-1;i>=0;i--)
       {
       OrderSelect(i, SELECT_BY_POS);
       int type   = OrderType();
               
       bool result = false;
              
       switch(type)
          {
          //Close opened long positions
          case OP_BUY  : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Pink);
                         break;
               
          //Close opened short positions
          case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Pink);
                          
          }
          
       if(result == false)
          {
            Sleep(0);
          }  
       }
      Print ("Account Profit Reached. All Open Trades Have Been Closed");
      return(0);
   }  
   
   Comment("Balance: ",AccountBalance(),", Account Equity: ",AccountEquity(),", Account Profit: ",AccountProfit(),
           "\nMy Account Profit Target: ",My_Money_Profit_Target);
   
  return(0);
   }
 }

I added the IF condition at start which goes if (OrdersTotal()>=2) .. [ Highlighted in above code with comment ]

Now, there was a problem closing the {} as on first attempt I tried it as

{ 
 if (OrdersTotal()>=2)
}

 {
   if (AccountProfit()>= My_Money_Profit_Target)
   {
    for(i=OrdersTotal()-1;i>=0;i--)
       { .. etc etc 
     

so basically as a first time coder, I tried to put that if condition out to be checked FIRST .. didnt compile ..

So I closed the {} by getting the condition inside.. now it compiled with only 2 warnings and no errors..

return value of 'OrderSelect' should be checked Testcode 1.mq4 56 8

not all control paths return a value Testcode 1.mq4 86 2

So it compiles .. is it logically correct?

[Also, not removing any copyright info, I am just adding a line, removing his details would be totally unfair on my part .. whats the rule regarding that? ]

 
tatyawinchu:

yes, going through help file to find the proper function which gives only open orders.


Really sorry man, as I said before, I'm really not that good at explaining . I understand that you want to find a function that will count only open buy or sell orders, I asked you to look at my code, I explained it to you to the best of my abilities and wasn't as good as I hopped .

Do you remember the first step I mentioned about that code ?

It was : CountMyOrders. Now, if you'd look at the code as I asked you, here's what you 'd find

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double CountMyOrders()
  {
   myOrders=0;
   for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false)
         Print("Failed to select order, error : "+ErrorDescription(GetLastError()));
      //skip orders placed by other EA's
      if(OrderMagicNumber()>0)continue;
      if(OrderType()==OP_BUY || OrderType()==OP_SELL)
        {
         myOrders++;
        }
     }
   return(myOrders);
  }

It says :

if(OrderType()==OP_BUY || OrderType()==OP_SELL)

You must know what OP_BUY and OP_SELL means.

No pending orders count, no open buy stop, no open sell stop, no buy limit or sell limit, just open buy and open sell.

How else can I explain any better that piece of code and what it does ?

You can keep searching the documentation and get familiar with it but I can tell you, the function you're looking for isn't there.

My EA says if there's no orders yet, wait :

if(myOrders<1)

You want your to wait for 2 orders. If you paid a little bit of attention to what I said, you'd probably notice that all you need to do is :

if(myOrders<2)

Voilla. Now it waits for the 2 orders to be in place - no pending, buy or sell only !

Please understand that I tried to put the code together to suit others as well who might be looking for a way to manage open trades and not that much a custom designed for a specific purpose.

It is intended as an example, take what you think is good, leave the rest out, improve on it, change it,learn from it if you can and experiment with it.

Hope it makes sense.

 
thrdel:


Really sorry man, as I said before, I'm really not that good at explaining . I understand that you want to find a function that will count only open buy or sell orders, I asked you to look at my code, I explained it to you to the best of my abilities and wasn't as good as I hopped .

Do you remember the first step I mentioned about that code ?

It was : CountMyOrders. Now, if you'd look at the code as I asked you, here's what you 'd find

It says :

You must know what OP_BUY and OP_SELL means.

No pending orders count, no open buy stop, no open sell stop, no buy limit or sell limit, just open buy and open sell.

How else can I explain any better that piece of code and what it does ?

You can keep searching the documentation and get familiar with it but I can tell you, the function you're looking for isn't there.

My EA says if there's no orders yet, wait :

You want your to wait for 2 orders. If you paid a little bit of attention to what I said, you'd probably notice that all you need to do is :

Voilla. Now it waits for the 2 orders to be in place - no pending, buy or sell only !

Please understand that I tried to put the code together to suit others as well who might be looking for a way to manage open trades and not that much a custom designed for a specific purpose.

It is intended as an example, take what you think is good, leave the rest out, improve on it, change it,learn from it if you can and experiment with it.

Hope it makes sense.

Thanks a loads .. Firstly, its not you ! its all me ! hehe I am the newbie here .. your explanations are perfect ! just little difficult to digest right now as I am going step by step .. word by word through help files =) and asking for help as I get stuck ..Rather, now because of your code, i am reading the FUNCTION section in help file ! ! Also, could you compile the code I pasted above at your end and see if there is anything wrong with the logic of that IF condition? Lets omit right now that it counts all orders .. say if this EA is executed when no orders were open, then first order was opened and some time later other order gets triggered .. is the logic flow of that IF condition correct? Also, I am now almost sure that this code just needs a line of fixing or such [Also, I dropped the idea of posting this as a job.. ], I dont have to complicate it to scan whether or not to omit orders from other EAs because for me this will be the only EA running, so I dont have to specifically assign magic number to each order etc...now I just need to find out if the logic of my IF is correct ^^
Reason: