Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.
From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.
Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
Elite Section
Get access to private discussions, specialized support, indicators and trading systems reported every week.
Advanced Elite Section
For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
Anyway, do you know what is wrong with the code execution (see the attached picture) ?
it's just my variant
Code:
int ord;
if (C2<MA2 && C1>MA1) //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;
}
//---------------------------------------------------------
}
int ord;
if (C2<MA2 && C1>MA1) //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 ?
I updated my code. Simplified it and added your recommendations.
Code:
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,2)&&Close[1]>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 ?
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.