extern double TakeProfit = 200;
extern double StopLoss = 100;
extern double Lots = 0.03;
static int ticket = 0;
static datetime lastbar;
int init()
{
lastbar=Time[1];
return (0);
}
int start ()
{
{
static bool IsFirstTick = true;
if(IsFirstTick == true)
{
IsFirstTick = false;
bool res;
res = OrderSelect(ticket, SELECT_BY_TICKET);
if(res == true)
{
if(OrderCloseTime() == 0)
{
bool res2;
res2 = OrderClose(ticket, Lots, OrderClosePrice(), 10);
if(res2 == false)
{
Alert("Error Clossing Order #", ticket);
}
}
}
}
}
if (IsNewBar())
{
//Sell Conditions
if(Close[1] < Open[1])
{
ticket = OrderSend(Symbol(),OP_SELL, Lots, Bid, 0, 0, 0, "Set by Open Close System");
if(ticket < 0)
Alert("Error Sending Buy Order!");
}
else
{
Alert("Your ticket # is" + string(ticket));
Sleep(5000);
Alert("Modifiying the Order...");
bool res1;
res1 = OrderModify(ticket, 0,Ask+StopLoss*Point,Ask-TakeProfit*Point,0);
if(res1 == false)
{
Alert("Error modifiying order!");
}
else
{
Alert("Order modifiying successfully");
}
}
//Buy Conditions
if(Close[1] > Open[1])
{
ticket = OrderSend(Symbol(),OP_BUY, Lots, Ask, 0, 0, 0, "Set by Open Close System");
if(ticket < 0)
Alert("Error Sending Buy Order!");
}
else
{
Alert("Your ticket # is" + string(ticket));
Sleep(5000);
Alert("Modifiying the Order...");
bool res1;
res1 = OrderModify(ticket, 0,Bid-StopLoss*Point,Bid+TakeProfit*Point,0);
if(res1 == false)
{
Alert("Error modifiying order!");
}
else
{
Alert("Order modifiying successfully");
}
}
}
return (0);
}
bool IsNewBar()
{
datetime curbar = Time[0]; // Open time of current bar
if (lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
return(false);
}
Please help!!!!!
bool variable for new
[Archive!] Any rookie question,
Guys please help me with the above code. I am trying to write an EA that will set trades on the open of every candle.
1. Problem is its not closing the open trades before it opens a new trade.
2. The stoploss and take profit dont always get set.
Any help will be appreciated.
Thanx.
Please help!!!!!
How to open trade
Ea to close trades
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register