ea keeps placing orders even after take profit reached

 

How do I code my ea so that is places only one trade each way and does not place a trade once the take profit target is hit


//+------------------------------------------------------------------+
//| easycrom.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Dominic Henry"
#property link "www.*****.com"

int MagicNumber = 10000;
//extern double Range_ref =139; // in points
extern double Lots = 0.1; // in points
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

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

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
/*
I need help creating an EA that will in order
1) Calculate the difference between high and low of the last closed candle ( Range=Prev_CandleH - PrevCandleL)
2) Compare this value to a a variable
3) If the difference between high and low of the last closed candle is equal or greater than user assigned variable
4) Execute BUY STOP at LOW of prev candle and execute SELL STOP at LOW of prev candle
5) Close pending order if not filled in 4 hours
6) Take profit of 40 pips



*/

int OrderCount(string symbol, int type)
{
int count=0;
for(int i=0;i<OrdersTotal();i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderSymbol()!=symbol)
continue;
if(OrderType()==type)
count++;
}
return(count);
}


double getRangeRef(string _symbol)
{ // this one a simple approach.
// furthermore, when attaching the EA to a char
// we can customize the external variables to the corresponding symbol.
if(_symbol=="EURUSDm" || _symbol=="EURUSD") // Attention here: depending on the broker's symbol, it can be EURUSD, eurusd, ...
return(100); // EURUSDm, here is because my broker provides these symbols for minis.
if(_symbol=="EURGBP" || _symbol=="EURGBP") // Attention here: depending on the broker's symbol, it can be EURUSD, eurusd, ...
return(100);
if(_symbol=="USDJPYm" || _symbol=="USDJPY") // Attention here: depending on the broker's symbol, it can be EURUSD, eurusd, ...
return(100); // this could be specified in a external file, or something like that.
if(_symbol=="EURJPYm" || _symbol=="EURJPY") // Attention here: depending on the broker's symbol, it can be EURUSD, eurusd, ...
return(100);
if(_symbol=="GBPJPYm" || _symbol=="GBPJPY") // Attention here: depending on the broker's symbol, it can be EURUSD, eurusd, ...
return(100);
if(_symbol=="AUDUSDm" || _symbol=="AUDUSD") // Attention here: depending on the broker's symbol, it can be EURUSD, eurusd, ...
return(100); // this could be specified in a external file, or something like that.
if(_symbol=="GBPUSDm" || _symbol=="GBPUSD") // Attention here: depending on the broker's symbol, it can be EURUSD, eurusd, ...
return(100); // this could be specified in a external file, or something like that.
if(_symbol=="USDCADm" || _symbol=="USDCAD") // Attention here: depending on the broker's symbol, it can be EURUSD, eurusd, ...
return(100);
if(_symbol=="USDCHFm" || _symbol=="USDCHF") // Attention here: depending on the broker's symbol, it can be EURUSD, eurusd, ...
return(100);
//return(139); //default value provided to every other pairs.
}


int start()
{
double Range = (High[1]-Low[1])/10;

double Range_ref=getRangeRef(Symbol());
if( Range>= Range_ref*Point) {
int digits = MarketInfo(Symbol(),MODE_DIGITS);
double buy_price = NormalizeDouble(High[1], digits);
double sell_price = NormalizeDouble(Low[1], digits);
double buy_take_profit = NormalizeDouble(High[1]+400*Point, digits);
double sell_take_profit = NormalizeDouble(Low[1]-400*Point, digits);
int ticket=0;

// if orderstotal is huge number, we could spare time here.
int longOrders = OrderCount(Symbol(),OP_BUYSTOP)+OrderCount(Symbol(),OP_BUY);
int shortOrders = OrderCount(Symbol(),OP_SELLSTOP)+OrderCount(Symbol(),OP_SELL);

if(longOrders==0)
if(buy_price > Ask) //buy stop must be above market price
ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots, buy_price,3,0,buy_take_profit,"--",MagicNumber,TimeCurrent()+4*60*60,Green);
if(ticket==-1)
Print("error buystop = ", GetLastError());

ticket=0;
if(shortOrders==0)
if(sell_price<Bid) //sell stop must be below market price
ticket = OrderSend(Symbol(),OP_SELLSTOP,Lots,sell_price,3,0,sell_take_profit,"--",MagicNumber,TimeCurrent()+4*60*60,Red);
if(ticket==-1)
Print("error sellstop = ", GetLastError());
}
return(0);
}
//+------------------------------------------------------------------+

 

int BarTraded = 0;

bool NewBar()
{
/* Update with a new bar and reset BarTraded if it's new */
static datetime lastbar;
datetime curbar = Time[0];
if(lastbar != curbar){
BarTraded = 0;
lastbar=curbar;
return (true);
}
else{
return(false);
}
}

int start()

{

if(/* whatever... */ && BarTraded == 0){

//bar hasn't been traded yet so feel free to open another

}

}

//when you enter any buy or sell order successfully

BarTraded = 1;

Hope this is clear enough,

Jon