How close order - page 2

 
smsafaee:


i have a partner that he do'nt like that i send the program code for any body

what can you suggest me to my problem?

thanks alot


Show your code we might see what's wrong. Closing orders is nothing "top secret"
 

hi all friends

this is my code without a function text that i used in my program

please help me to find the problem.

i tested it more than 6 times in real charts but it opened more than 2 orders in the same time, but it must open 1 order and no more.

i use an array to save tickets for EA orders and a counter to count the orders(send_ticket[total_order])

when i close one order i increase total_order and become ready for a new order

i use a function in M1,M5,M15 in EA and there are some if s for each time frame that i process each time_frame charts in them.

with thanks


#include <LibOrderReliable.mqh>
//frame_state define
#define m1_u 0
#define m5_u 1
#define m15_u 2
#define m30_u 3
#define m15_d 4
#define m5_d 5
#define m1_d 6
//frame_state define

//define type of order
#define buy 0
#define sell 1
//define type of order

//External Parameters


//External Parameters
extern double lots=0.1;
extern int start_time=2;
extern int finish_time=22;
//Program Parameters
double open_price=0;
int frame_state=0, order_counter;
bool order_type=0, order_close=0, b=0;
static int send_ticket[501], total_order;
//Program Parameters
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
if(total_order==0)
total_order=500;
for(order_counter=0;order_counter<=total_order;order_counter++)
{
OrderClose(send_ticket[order_counter],lots,Bid,Red);
send_ticket[order_counter]=-1;
}

if(total_order==500)
{
for(order_counter=0;order_counter<=total_order;order_counter++)
send_ticket[order_counter]=-1;
total_order=0;
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
////////////////////////////////////////
if((Hour()<finish_time && Hour()>=start_time) || send_ticket[total_order]>=0 /* no open order*/)
{
////////////////////////////////////////
if(frame_state==m1_u)
{Comment("m1_u");
if(send_ticket[total_order]<0) //check for opening an order
{
for(order_counter=0;order_counter<=total_order;order_counter++)
{
OrderClose(send_ticket[order_counter],lots,Bid,Red);
//OrderCloseBy(send_ticket[order_counter],send_ticket[order_counter],Red);
OrderCloseReliable(send_ticket[order_counter],lots,Bid,4,Red);
//send_ticket[order_counter]=-1;
}

if((arian_function(2,iBars(0,PERIOD_M1),PERIOD_M1,25,12)>0 && arian_function(1,iBars(0,PERIOD_M1),PERIOD_M1,25,12)<0))
{//sharte residan be 500 ezafe shavad
OrderCloseReliable(send_ticket[total_order-1],lots,Bid,4,Red);
send_ticket[total_order]=OrderSend(Symbol(),OP_SELL,lots,Bid,0,Bid+200*Point,Bid-500*Point,"sell order",0,0,Green);
order_type=sell;
open_price=Bid;
}
else if((arian_function(2,iBars(0,PERIOD_M1),PERIOD_M1,25,12)<0 && arian_function(1,iBars(0,PERIOD_M1),PERIOD_M1,25,12)>0))
{//sharte residan be 500 ezafe shavad
OrderCloseReliable(send_ticket[total_order-1],lots,Bid,4,Red);
send_ticket[total_order]=OrderSend(Symbol(),OP_BUY,lots,Ask,0,Ask-200*Point,Ask+500*Point,"buy order",0,0,Green);
order_type=buy;
open_price=Ask;
}
}
//#######################################
else if(send_ticket[total_order]>=0)
{
if(order_type==sell)
{
if((arian_function(2,iBars(0,PERIOD_M1),PERIOD_M1,25,12)<0 && arian_function(1,iBars(0,PERIOD_M1),PERIOD_M1,25,12)>0 && (Bid-open_price)<=(-15*Point) && arian_function(1,iBars(0,PERIOD_M5),PERIOD_M5,25,12)<0))
{
frame_state=m5_u;
}
else if((arian_function(2,iBars(0,PERIOD_M1),PERIOD_M1,25,12)<0 && arian_function(1,iBars(0,PERIOD_M1),PERIOD_M1,25,12)>0 && frame_state==m1_u))
{
//order_close=OrderClose(send_ticket[total_order],lots,Bid,Red);
order_close=OrderCloseReliable(send_ticket[total_order],lots,Bid,4,Red);
if(order_close==1)
{
total_order++;
order_close=0;
frame_state=m1_u;
}
}
}
else if(order_type==buy)
{
if((arian_function(2,iBars(0,PERIOD_M1),PERIOD_M1,25,12)>0 && arian_function(1,iBars(0,PERIOD_M1),PERIOD_M1,25,12)<0 && (Ask-open_price)>=(15*Point) && arian_function(1,iBars(0,PERIOD_M5),PERIOD_M5,25,12)>0))
{
frame_state=m5_u;
}
else if((arian_function(2,iBars(0,PERIOD_M1),PERIOD_M1,25,12)>0 && arian_function(1,iBars(0,PERIOD_M1),PERIOD_M1,25,12)<0 && frame_state==m1_u))
{
//order_close=OrderClose(send_ticket[total_order],lots,Bid,Red);
order_close=OrderCloseReliable(send_ticket[total_order],lots,Bid,4,Red);
if(order_close==1)
{
total_order++;
order_close=0;
frame_state=m1_u;
}
}
}
}
}
////////////////////////////////////////
if(frame_state==m5_u)
{Comment("m5_u");
if(order_type==sell)
{
if(arian_function(2,iBars(0,PERIOD_M5),PERIOD_M5,25,12)<0 && arian_function(1,iBars(0,PERIOD_M5),PERIOD_M5,25,12)>0 && (Bid-open_price)<=(-30*Point) && arian_function(1,iBars(0,PERIOD_M15),PERIOD_M15,25,12)<0)
{
frame_state=m15_u;
}
else if(arian_function(2,iBars(0,PERIOD_M5),PERIOD_M5,25,12)<0 && arian_function(1,iBars(0,PERIOD_M5),PERIOD_M5,25,12)>0 && frame_state==m5_u)
{
//order_close=OrderClose(send_ticket[total_order],lots,Bid,Red);
order_close=OrderCloseReliable(send_ticket[total_order],lots,Bid,4,Red);
if(order_close==1)
{
total_order++;
order_close=0;
frame_state=m1_u;
}
}
}
else if(order_type==buy)
{
if(arian_function(2,iBars(0,PERIOD_M5),PERIOD_M5,25,12)>0 && arian_function(1,iBars(0,PERIOD_M5),PERIOD_M5,25,12)<0 && (Ask-open_price)>=(30*Point) && arian_function(1,iBars(0,PERIOD_M15),PERIOD_M15,25,12)>0)
{
frame_state=m15_u;
}
else if(arian_function(2,iBars(0,PERIOD_M5),PERIOD_M5,25,12)>0 && arian_function(1,iBars(0,PERIOD_M5),PERIOD_M5,25,12)<0 && frame_state==m5_u)
{
//order_close=OrderClose(send_ticket[total_order],lots,Bid,Red);
order_close=OrderCloseReliable(send_ticket[total_order],lots,Bid,4,Red);
if(order_close==1)
{
total_order++;
order_close=0;
frame_state=m1_u;
}
}
}
}
////////////////////////////////////////
if(frame_state==m15_u)
{Comment("m15_u");
if(order_type==sell)
{
if(arian_function(2,iBars(0,PERIOD_M15),PERIOD_M15,25,12)<0 && arian_function(1,iBars(0,PERIOD_M15),PERIOD_M15,25,12)>0 && arian_function(1,iBars(0,PERIOD_M30),PERIOD_M30,25,12)<0 && 1==2)
{
frame_state=m30_u;
}
else if(arian_function(2,iBars(0,PERIOD_M15),PERIOD_M15,25,12)<0 && arian_function(1,iBars(0,PERIOD_M15),PERIOD_M15,25,12)>0 && frame_state==m15_u)
{
//order_close=OrderClose(send_ticket[total_order],lots,Bid,Red);
order_close=OrderCloseReliable(send_ticket[total_order],lots,Bid,4,Red);
if(order_close==1)
{
total_order++;
order_close=0;
frame_state=m1_u;
}
}
}
else if(order_type==buy)
{
if(arian_function(2,iBars(0,PERIOD_M15),PERIOD_M15,25,12)>0 && arian_function(1,iBars(0,PERIOD_M15),PERIOD_M15,25,12)<0 && arian_function(1,iBars(0,PERIOD_M30),PERIOD_M30,25,12)>0 && 1==2)
{
frame_state=m30_u;
}
else if(arian_function(2,iBars(0,PERIOD_M15),PERIOD_M15,25,12)>0 && arian_function(1,iBars(0,PERIOD_M15),PERIOD_M15,25,12)<0 && frame_state==m15_u)
{
//order_close=OrderClose(send_ticket[total_order],lots,Bid,Red);
order_close=OrderCloseReliable(send_ticket[total_order],lots,Bid,4,Red);
if(order_close==1)
{
total_order++;
order_close=0;
frame_state=m1_u;
}
}
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
double arian_function(int i1,int bars,int time_frame,int MA1,int RSIperiod1)

{

.....

}

 

There are still many places where Order*() functions have not been replaced by the Order*Reliable() functions. The whole code is a complete mess, there is code duplication all over the place. Tell your top-secret super "programmer" he should clean up and refactor the code first.

Dumping such a load of badly written and incomplete code that does not even compile into the forum without specifying where exactly it fails and expecting us to debug it for you (which should be the job of your programmer) without even allowing us to compile and test the code is an impertinence.

Provide a minimal example that demonstrates the error.

 

Your order closing loop counts UP...

It should always be

for(order_counter=total_order; order_counter>=0; order_counter--)

 
phy:

Your order closing loop counts UP...

It should always be

for(order_counter=total_order; order_counter>=0; order_counter--)

Actually it should be two loops with the inner one counting up (FIFO!) and the outer one repeating this until no more orders are left to close. Something like this:

/**
* Close all open orders or trades that match type and magic number.
* This function won't return until all positions are closed
* type = -1 means all types, magic = -1 means all magic numbers
*/
void closeOpenOrders(int type, int magic) {
   int total, cnt;
   double price;
   color clr;
   int order_type;

   Print("closeOpenOrders(" + type + "," + magic + ")");

   while (getNumOpenOrders(type, magic) > 0) {
      while (IsTradeContextBusy()) {
         Print("closeOpenOrders(): waiting for trade context.");
         Sleep(MathRand() / 10);
      }
      total = OrdersTotal();
      RefreshRates();
      if (type == OP_BUY) {
         price = Bid;
         clr = CLR_SELL_ARROW;
      } else {
         price = Ask;
         clr = CLR_BUY_ARROW;
      }
      for (cnt = 0; cnt < total; cnt++) {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if ((type == -1 || OrderType() == type) && (magic == -1 || OrderMagicNumber() == magic)) {
            if (IsTradeContextBusy()) {
               break; // something else is trading too, back to the while loop.
            }
            order_type = OrderType();
            if (order_type == OP_BUYSTOP || order_type == OP_SELLSTOP || order_type == OP_BUYLIMIT || order_type == OP_SELLLIMIT) {
               orderDeleteReliable(OrderTicket());
            } else {
               orderCloseReliable(OrderTicket(), OrderLots(), price, 999, clr);
            }
            break; // restart the loop from 0 (hello FIFO!)
         }
      }
   }
}
 

Well, ok, hadn't considered the new fifo stuff, sometimes that is handled transparently in the back office.

 

hi

7bit


i tested these codes but when i compiled it i found 3 errors for these lines


 clr = CLR_SELL_ARROW;
 clr = CLR_BUY_ARROW;
 
getNumOpenOrders(type, magic)
i tested Get...
but it was false again
what is the problem?

 
This is part of a bigger function library, it references some other functions that I did not include in the post, it was only meant as an example to illustrate the logic. It is found in this library: https://sites.google.com/site/prof7bit/common_functions but this lib is GPL, you are not allowed to use it for closed source code. You must not sell the EAs compiled with it, you must release them under GPL.
Reason: