Need Help Spotting Coding Error

 
Files:
capture.png  34 kb
 

Why do you close all orders even trand didn't change the direction?

 

Roger09,

I use the close all trades script only if trend change direction.

If a change in trend is detected, all previous open trades are closed and a trade is opened in the direction of the current trend.

What do you mean when you say that "I close all trades even without a change in trend" ? Could you show it from the code ?

Thanks.

Anyway, do you know what is wrong with the code execution (see the attached picture) ?

 

Anyway, I use once per bar condition checking. But why is the EA executing many trades per single bar (see picture) ?

 
username1:
Anyway, do you know what is wrong with the code execution (see the attached picture) ?

it's just my variant

int ord;

if (C2MA1) //Trend change from down to up.

{

int total = OrdersTotal(); //Script to close all open orders.

for(int i=total-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS);

if(OrderSymbol()==Simbol()&& OrderType()==1)

{

result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

if(result == false)

{

Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );

Sleep(3000);

}

}

if(OrderSymbol()==Simbol()&& OrderType()==0)ord++;

}

int ticket; //Place Buy Order

RefreshRates();

if(ord==0)ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-1000*Point,Ask+1000*Point,"Buy Order",9999,0,Green);

if(ticket<0)

{

Print("Buy Order failed with error #",GetLastError());

}

return;

}

//---------------------------------------------------------

}
 

if(OrderSymbol()==Simbol()&& OrderType()==0)ord++;

if(ord==0)...

Could you explain a little on what these new additions do ?

Thank you.

 
Roger09:
it's just my variant

int ord;

if (C2MA1) //Trend change from down to up.

{

int total = OrdersTotal(); //Script to close all open orders.

for(int i=total-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS);

if(OrderSymbol()==Simbol()&& OrderType()==1)

{

result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

if(result == false)

{

Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );

Sleep(3000);

}

}

if(OrderSymbol()==Simbol()&& OrderType()==0)ord++;

}

int ticket; //Place Buy Order

RefreshRates();

if(ord==0)ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-1000*Point,Ask+1000*Point,"Buy Order",9999,0,Green);

if(ticket<0)

{

Print("Buy Order failed with error #",GetLastError());

}

return;

}

//---------------------------------------------------------

}

Thanks. This version of closing specific trades is more efficient than using script to close all open trades.

Only thing I dont really understand what or how the new variable "ord" does or controls ?

Cheers.

 

Updated Code

I updated my code. Simplified it and added your recommendations.

extern int MA_Period=5;

extern int MA_Method=1;

extern double Lots=0.1;

int init() {return(0);}

int deinit(){return(0);}

int start()

{

int BarOpen = 0; //Run Once Per Bar

if (Open[0] != BarOpen)

{

BarOpen=Open[0];

if (Close[2]iMA(NULL,0,MA_Period,0,MA_Method,0,1)) // Uptrend

{

int total = OrdersTotal(); //Close Short Positions

for(int i=total-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS);

if(OrderSymbol()==Symbol()&& OrderType()==1)

{bool result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );}

}

OrderSend (Symbol(),0,Lots,Ask,3,NULL,NULL,NULL,0,0,Green); // Open Long Position

}

if (Close[2]>iMA(NULL,0,MA_Period,0,MA_Method,0,2)&&Close[1]<iMA(NULL,0,MA_Period,0,MA_Method,0,1)) //Downtrend

{

total = OrdersTotal(); //Close Long Positions

for(i=total-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS);

if(OrderSymbol()==Symbol()&& OrderType()==0)

{result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );}

}

OrderSend (Symbol(),0,Lots,Bid,3,NULL,NULL,NULL,0,0,Green); //Open Short

}

return(0);

}

}

It seems to be working much better , except for the multiple trades per bar issue. Can you help me with that ?

Thanks.

Files:
capture_10.png  32 kb
 

Something wrong with your tester. My tester works properly.

Restart your terminal.

PS Variable ord doesn't allow to open more then one order in one direction.

Files:
chart.bmp  977 kb
 

In the updated new code in post #8, where should I put the "ord" controller ?

Because the new code is not quite the same as the code is the first post.

Could you post your test results here ? I cant test it yet.

Thanks.

 

Try this:

extern int MA_Period=5;

extern int MA_Method=1;

extern double Lots=0.1;

int init() {return(0);}

int deinit(){return(0);}

int BarOpen = 0;

int start()

{

int ord;

if (Time[0] != BarOpen)

{

BarOpen=Time[0];

if (Close[2]iMA(NULL,0,MA_Period,0,MA_Method,0,1)) // Uptrend

{

int total = OrdersTotal(); //Close Short Positions

for(int i=total-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS);

if(OrderSymbol()==Symbol()&& OrderType()==1)

{

bool result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

}

if(OrderSymbol()==Symbol()&& OrderType()==0)ord++;

}

if(ord==0)OrderSend (Symbol(),0,Lots,Ask,3,NULL,NULL,NULL,0,0,Green); // Open Long Position

}

if (Close[2]>iMA(NULL,0,MA_Period,0,MA_Method,0,2)&&Close[1]<iMA(NULL,0,MA_Period,0,MA_Method,0,1)) //Downtrend

{

total = OrdersTotal(); //Close Long Positions

for(i=total-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS);

if(OrderSymbol()==Symbol()&& OrderType()==0)

{result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );}

if(OrderSymbol()==Symbol()&& OrderType()==1)ord++;

}

if(ord==0)OrderSend (Symbol(),0,Lots,Bid,3,NULL,NULL,NULL,0,0,Green); //Open Short

}

return(0);

}

}
Reason: