Open new orders instead of stoploss

 

Hi all,

I am trying to write script for opening additional orders instead of setting stoploss. For example:   1 Buy Order is placed and it is going loss, if openprice is reaches the distance (eg:300pips), it will open another buy order. Then, Order 2 is making profit more than account balance or certain percentage, both orders will get closed on profit. My script is placing consecutive orders, but it is not closing the orders. Please help me correct it. 

//+------------------------------------------------------------------+
//|                                              movingaverageEA.mq5 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//---------------------------
//Choose stragegy
//--------------------------
extern int Choose_stragegy=8;
extern int MagicNumber = 1234;
extern double LotSize = 0.1;
extern int Maxorders=5;
extern int TakeProfit=300;
extern int StopLoss=200;
//extern int Distance=300;
//extern double Multiplier = 1.5;

extern bool AllowBuy=true;
extern bool AllowSell=true;

//---------------------------
//RSI input
//---------------------------
extern int RSI_Period=14;
extern int RSI_TopLevel=75;
extern int RSI_BottomLevel=15;



double pips;





//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE);
   if (ticksize == 0.0001 || ticksize == 0.001)
   pips = ticksize*10;
   else pips = ticksize;

  }

//+------------------------------------------------------------------+
bool IsNewCandle()
{
    static int BarsOnChart=0;
    if (Bars == BarsOnChart)
    return(false);
    BarsOnChart = Bars;
    return(true);
}

//+------------------------------------------------------------------+
int OpenOrdersThisPair(string pair)
{
int total=0;
    for(int i=OrdersTotal()-1; i>=0; i--)
        {
        OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
        if(OrderSymbol() == pair) total++;
        }
        return (total);
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
void CloseallOrders()
{
while(AccountEquity()>(AccountBalance()+(TakeProfit*pips)))
{
OrderClose(OrderTicket(), OrderLots(), Bid,3,Blue);
}
 }

void drawdown()
{
     // int totals  = OrdersTotal();
      if (OrdersTotal()>=0)
      {
        for (int i=0;OrdersTotal()>i; i++)
 {
        OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);
        if(OrderProfit())
        {
        if(OrderType()==OP_BUY)
        {
        double ddBuy=0;
        ddBuy= OrderOpenPrice()- (300*pips);
            if(!ddBuy)continue;
                    if(Ask<=ddBuy)
                    {
                    int buyticketDD= OrderSend(Symbol(),0,(LotSize*1.5),Ask,3,0,0,NULL,MagicNumber,0,Green);
                    CloseallOrders();
                    }
                 
         }

        if(OrderType()==OP_SELL)
        {
        double ddSell=0;
        ddSell= OrderOpenPrice()+ (300*pips);
            if(!ddSell)continue;
                    if(Bid>=ddSell)
                     {
                        int sellticketDD= OrderSend(Symbol(),1,(LotSize*1.5),Bid,3,0,0,NULL,MagicNumber,0,Red);
                        CloseallOrders();
                     }
                
        }
        }
      }
}
}


//+------------------------------------------------------------------+

void OrderEntry(int direction)
{
    if(direction==0)
    if(AllowBuy)
    {
        double bsl=0;
        double btp=0;
        if(StopLoss!=0)bsl=Ask-(StopLoss*pips);
        if(TakeProfit!=0)btp=Ask+(TakeProfit*pips);

        if(OpenOrdersThisPair(Symbol())<=0)int buyticket = OrderSend(Symbol(),0,LotSize,Ask,3,0,0,NULL,MagicNumber,0,Green);
        if(buyticket>0)OrderModify(buyticket,OrderOpenPrice(),bsl,btp,0,CLR_NONE);
        drawdown();


    }

    if(direction==1)
    if(AllowSell)
    {
        double ssl=0;
        double stp=0;
        if(StopLoss!=0)ssl=Bid+(StopLoss*pips);
        //else ssl=drawdown();
        if(TakeProfit!=0)stp=Bid-(TakeProfit*pips);

        if(OpenOrdersThisPair(Symbol())<=0)int sellticket= OrderSend(Symbol(),1,LotSize,Bid,3,0,0,NULL,MagicNumber,0,Red);
        if(sellticket>0)OrderModify(sellticket,OrderOpenPrice(),ssl,stp,0,CLR_NONE);
        drawdown();
    }
}

//---------------------------
//RSI 
//---------------------------
void CheckforRSI()
{
    double RSIValue= iRSI(NULL,0,RSI_Period,PRICE_CLOSE,0);

            if(RSIValue <=35)
               OrderEntry(0);


               if(RSIValue >=65)
                  OrderEntry(1);



}


//+------------------------------------------------------------------+
//| Deinitialization function                                            |
//+------------------------------------------------------------------+
int denist()
{
    Comment("");
    return(0);
}

//+------------------------------------------------------------------+
//| Expert start function                                             |
//+------------------------------------------------------------------+
int start()
{
   
   if(Choose_stragegy==8)
   {
   if(IsNewCandle())CheckforRSI();
   }


//----
   return(0);
 }
 
//+------------------------------------------------------------------+
//|                                              movingaverageEA.mq5 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

mq5 ????

 

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk.

Hedging, grid trading, same as Martingale.
          Martingale, Hedging and Grid : MHG - General - MQL5 programming forum 2016.12.20

Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
          Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum 2015.02.11

Why it won't work: Calculate Loss from Lot Pips - MQL5 programming forum 2017.07.11

 
Naresh kumar:
I'm working on mql4. May be I can set the stop loss for 1000pips. But I want to open a consecutive order,  based off distance, followed by the first order (without closing the first order) and close all orders when it makes profits. How do I do it? 
 
Keith Watford:

mq5 ????

It's for mql4
Reason: