Bauer Boy this is the code

 

//+------------------------------------------------------------------+
//| femi candlestick.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"

extern double lTakeProfit =600;
extern double TakeProfit=600;
extern double sTakeProfit = 600;
extern double TrailingStop = 200;
extern double sTrailingStop = 200;
extern double StopLoss =1000;
extern int otstup=20;
extern int rsibuy=90;
extern int rsisell=10;
extern int rsibuys=70;
extern int rsisells=30;
extern int rsiur=50;
extern int maxTradesPerPair=1;
extern color clOpenBuy = Blue;
extern color clCloseBuy = Aqua;
extern color clOpenSell = Red;
extern color clCloseSell = Violet;
extern color clModiBuy = Blue;
extern color clModiSell = Red;
extern string Name_Expert = "Generate from Gordago";
extern int Slippage = 2;
extern bool UseSound = False;
extern string NameFileSound = "alert.wav";
extern double Lots = 0.1;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start(){
if(Bars<100){
Print("bars less than 100");
return(0);
}
if(lTakeProfit<10){
Print("TakeProfit less than 10");
return(0);
}
if(sTakeProfit<10){
Print("TakeProfit less than 10");
return(0);
}

double dSignal;
dSignal = (Close[5]+Close[4]+Close[3]+Close[2]+Close[1])/5;
double mfi;

iMFI(NULL,0,3,1);
double RSI=iRSI(NULL,0,50,PRICE_CLOSE,1);
double RSI2=iRSI(NULL,PERIOD_M30,50,PRICE_CLOSE,2);
double bullMA5=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,1);
double bullMA5pre=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,2);
// 13-period moving average on Bar[2]
double bearMA13=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,1);
double bearMA13pre=iMA(NULL,0,13,0,MODE_EMA,PRICE_CLOSE,2);
//21-period moving average on Bar[3]
double beaMA21=iMA(NULL,PERIOD_M30,21,0,MODE_EMA,PRICE_CLOSE,1);
double beaMA21pre=iMA(NULL,PERIOD_M30,21,0,MODE_EMA,PRICE_CLOSE,1);
double bearMA80=iMA(NULL,0,80,0,MODE_EMA,PRICE_CLOSE,1);
double beasMA80=iMA(NULL,PERIOD_M30,80,0,MODE_EMA,PRICE_CLOSE,2);

if(AccountFreeMargin()<(1000*Lots)){
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (!ExistPositions()){

if ((bullMA5pre<bearMA13pre&&bullMA5>bearMA13&&RSI>rsiur&&RSI<rsibuys)){
OpenBuy();
return(0);
}

if ((bullMA5pre>bearMA13pre&&bullMA5<bearMA13&&RSI<rsiur&&RSI>rsisells)){
OpenSell();
return(0);
}
}
StopLossBuy(TrailingStop);
StopLossSell(sTrailingStop);
TrailingPositionsBuy(TrailingStop);
TrailingPositionsSell(sTrailingStop);
return (0);
}

bool ExistPositions() {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
close();
return(True);
}
}
}
return(false);
}
void StopLossBuy(int StopLossBuy) {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
if (OrderType()==OP_BUY) {
if (Bid-OrderOpenPrice()<StopLoss*Point) {
if (OrderStopLoss()<Bid-StopLoss*Point ||
OrderStopLoss()==0)
ModifyStopLoss(Bid-StopLoss*Point );
}
}
}
}
}
}
void StopLossSell(int StopLossSell) {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
if (OrderType()==OP_SELL) {
if (OrderOpenPrice()-Ask<StopLoss*Point) {
if (OrderStopLoss()<Ask+StopLoss*Point||
OrderStopLoss()==0)
ModifyStopLoss(Ask+StopLoss*Point);
}
}
}
}
}
}
void TrailingPositionsBuy(int trailingStop) {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
if (OrderType()==OP_BUY) {
if(trailingStop>0){
if (Bid-OrderOpenPrice()>trailingStop*Point&&TakeProfit*Point) {
if (OrderStopLoss()<Bid-trailingStop*Point&&TakeProfit*Point)
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);

}
}
}
}
}
}
}
void TrailingPositionsSell(int trailingStop) {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
if (OrderType()==OP_SELL) {
if(trailingStop>0){
if (OrderOpenPrice()-Ask<trailingStop*Point&&TakeProfit*Point) {
if (OrderStopLoss()<Ask+trailingStop*Point&&TakeProfit*Point)
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
}

void ModifyStopLoss(double StopLoss) {
bool fm;
fm = OrderModify(OrderTicket(),OrderOpenPrice
(),StopLoss,OrderTakeProfit(),0,CLR_NONE);
if (fm && UseSound) PlaySound(NameFileSound);
}

void OpenBuy() {
double ldLot, StopLoss, ldTake;
string lsComm;
ldLot = GetSizeLot();
ldTake = GetTakeProfitBuy();
lsComm = GetCommentForOrder();
OrderSend(Symbol
(),OP_BUY,ldLot,Ask,Slippage,StopLoss*Point,ldTake,lsComm,0,0,clOpenBuy);
if (UseSound) PlaySound(NameFileSound);
}
void OpenSell() {
double ldLot, StopLoss, ldTake;
string lsComm;

ldLot = GetSizeLot();
ldTake = GetTakeProfitSell();
lsComm = GetCommentForOrder();
OrderSend(Symbol
(),OP_SELL,ldLot,Bid,Slippage,StopLoss*Point,ldTake,lsComm,0,0,clOpenSell);
if (UseSound) PlaySound(NameFileSound);
}
string GetCommentForOrder() { return(Name_Expert); }
double GetSizeLot() { return(Lots); }
double GetTakeProfitBuy() { return(0); }
double GetTakeProfitSell() { return(0); }

void close()
{
double bullMA5,bearMA13,bullMA5pre,bearMA13pre;
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
if(OrderType()==OP_BUY){
{
if(bullMA5pre>bearMA13pre&&bullMA5<bearMA13)OrderClose(OrderTicket(),OrderLots(),Bid,2,Blue);
}
}
}
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==Symbol()) {
}
if(OrderType()==OP_SELL)
{
if(bullMA5pre<bearMA13pre&&bullMA5>bearMA13) OrderClose(OrderTicket(),OrderLots(),Ask,2,Red);
}
}
}
}
}

"I want it to close order if it didnt get to the takeprofit and the last condition is meet @ void close"

Reason: