stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Please Help a Novice stop a For Loop
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=5579
Page 1 of 2
Author:  burnrubber75 [ Sun Dec 02, 2018 10:29 am ]
Post subject:  Please Help a Novice stop a For Loop

I am trying to use the code attached to open 2 Pending Orders, however once SL or TP has been hit I don't want it to open another order until I drop the EA back on the chart if this is possible

Code: Select all

//+------------------------------------------------------------------+
//|                                      Open Two Pending Orders.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property version   ""
#include <stdlib.mqh>
//---
input bool   DynamicLotSize    =true;   //Use Money Management
input double EquityPercent     = 2;     //Risk Percent
input double FixedLotSize      = 0.1;   //Fixed LotSize
input double StopLoss          = 100;   //StopLoss( in Point)
input double TakeProfit        = 300;   //TakeProfit( in  Point)
input double TrailingStop      = 50;    //TrailingStop(in Point)
input double PipsAway          = 50;    //points away from the current Bid & Ask
input double Slippage          = 30;    //Slippage
input double Magic             = 1111;  //Magic Number
double LotSize;
int ticket1;
int ticket2;
int t=0;
//---
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----

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

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

//---Detect Open or Pending Orders
   int total=OrdersTotal();
   t=0;
   for(int i=total;i>=0;i--)
     {
      OrderSelect(i,SELECT_BY_POS);
      int type=OrderType();
      if(( OrderSymbol()==Symbol()) && (OrderMagicNumber()==Magic))
        {

         switch(type)
           {
            case OP_BUY       : t=1;
            case OP_SELL      : t=1;
            case OP_BUYLIMIT  : t=1;
            case OP_BUYSTOP   : t=1;
            case OP_SELLLIMIT : t=1;
            case OP_SELLSTOP  : t=1;
           }
        }
     }
   if(t<1)
     {
      //Lot Size Calculation
      if(DynamicLotSize==true)
        {
         double RiskAmount= AccountEquity() *(EquityPercent/100);
         double TickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
         if(Point==0.001 || Point==0.00001) TickValue*=10;
         double CalcLots=(RiskAmount/StopLoss)/TickValue;
         LotSize=CalcLots;
        }
      else LotSize=FixedLotSize;
      // Lot size verification
      if(LotSize<MarketInfo(Symbol(),MODE_MINLOT))
        {
         LotSize=MarketInfo(Symbol(),MODE_MINLOT);
        }
      else if(LotSize>MarketInfo(Symbol(),MODE_MAXLOT))
        {
         LotSize=MarketInfo(Symbol(),MODE_MAXLOT);
        }
      if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.1)
        {
         LotSize=NormalizeDouble(LotSize,1);
        }
      else LotSize=NormalizeDouble(LotSize,2);
      //Open two Pending Orders simultaneously
      double t1 = Ask + PipsAway * Point; //BuyStop Entry Point
      double t2 = Bid - PipsAway * Point; //SellStop Entry Point
      double sl1 = t1 - StopLoss * Point; //BuyStop Stoploss
      double sl2 = t2 + StopLoss * Point; //SellStop Stoploss
      double tp1 = t1 + TakeProfit * Point; //BuyStop TakeProfit
      double tp2 = t2 - TakeProfit * Point; //SellStop TakeProfit
      ticket1 = OrderSend( Symbol(),OP_BUYSTOP,LotSize,t1, Slippage,sl1,tp1,"London Breakout",Magic,0,White);
      ticket2 = OrderSend( Symbol(),OP_SELLSTOP,LotSize,t2, Slippage, sl2,tp2,"London Breakout",Magic,0,White);
     }
   for(int j=0; j<OrdersTotal(); j++)
     {
      //---if opened order is "Buy", close another pending order "Sellstop" and use trailing stop for opened "Buy" order
      OrderSelect(ticket1,SELECT_BY_TICKET);
      if(OrderType()==OP_BUY)
        {
         // delete pending order
         OrderDelete(ticket2);
         // use Trailling Stop
         if(Bid-OrderOpenPrice()>Point*TrailingStop)
           {
            if(OrderStopLoss()<Bid-Point*TrailingStop)
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
              }
           }
        }
      //---if opened order is "Sell", close another pending order "Buystop" and use trailing stop for opened "Sell" order
      OrderSelect(ticket2,SELECT_BY_TICKET);
      if(OrderType()==OP_SELL)
        {
         // delete pending order
         OrderDelete(ticket1);
         // use Trailling Stop
         if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
           {
            if(OrderStopLoss()>(Ask+Point*TrailingStop))
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
              }
           }
        }
     }
     return(0);
  }
//+------------------------------------------------------------------+
Thanks in advance for your help Mr Novice!

P.S Also do you now as a new member I can't see or download any attachments i.e Scripts or EA's? :arrrg:
Author:  SteveHopwood [ Sun Dec 02, 2018 2:17 pm ]
Post subject:  Please Help a Novice stop a For Loop

The easiest way is to the the EA to remove itself when it detects a trade has closed.

Declare a couple more variables to go with int t:
bool RemoveExpert = false;
int t = 0, oldt = 0;

Pop this snippet at the top of OnStart()

Code: Select all

   if (RemoveExpert)
   {
      ExpertRemove();
      return;
   }//if (RemoveExpert)
Scroll down to "if(t<1)" and insert this block immediately above:

Code: Select all

   if (oldt > 0)//There was an open trade]
      if (t == 0)//but not any more, so remove the expert
      {
         RemoveExpert = true;
         return;//Removes the EA at the next tick.
      }
   oldt = t;
:xm: :rocket:
Author:  burnrubber75 [ Tue Dec 04, 2018 12:06 am ]
Post subject:  Please Help a Novice stop a For Loop

Excellent Thanks so much
Author:  burnrubber75 [ Tue Dec 04, 2018 12:27 am ]
Post subject:  Please Help a Novice stop a For Loop

SteveHopwood » Sun Dec 02, 2018 2:17 pm wrote:The easiest way is to the the EA to remove itself when it detects a trade has closed.

Declare a couple more variables to go with int t:
bool RemoveExpert = false;
int t = 0, oldt = 0;

Pop this snippet at the top of OnStart()

Code: Select all

   if (RemoveExpert)
   {
      ExpertRemove();
      return;
   }//if (RemoveExpert)
Scroll down to "if(t<1)" and insert this block immediately above:

Code: Select all

   if (oldt > 0)//There was an open trade]
      if (t == 0)//but not any more, so remove the expert
      {
         RemoveExpert = true;
         return;//Removes the EA at the next tick.
      }
   oldt = t;
:xm: :rocket:
Steve thanks that works how would I code it to once an order has kicked in to delete the other pending order and then stop the EA

Thanks in advance.
Author:  burnrubber75 [ Tue Dec 04, 2018 2:33 am ]
Post subject:  Please Help a Novice stop a For Loop

Sorted, put the code in wrong block


Thanks again for your help!
Author:  SteveHopwood [ Tue Dec 04, 2018 10:16 pm ]
Post subject:  Please Help a Novice stop a For Loop

burnrubber75 » Tue Dec 04, 2018 2:33 am wrote:Sorted, put the code in wrong block


Thanks again for your help!
Good to see you delving under the bonnet of this stuff. :clap: :clap: :clap:

Really good to see you sorting out a problem for yourself. This is the best way to learn. :clap: :clap: :clap:

:xm: :rocket:
Author:  burnrubber75 [ Fri Dec 07, 2018 4:38 am ]
Post subject:  Please Help a Novice stop a For Loop

Please can you help me add high & low of last n candles, then I need to add the pending orders x pips from those high/low prices?

Manage to sort of code it to place a line across the candles

Code: Select all

//+------------------------------------------------------------------+
//|                           High and low across last N candles.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window

#include <WinUser32.mqh>

extern int        NumberOfBars           = -1;
extern int        RefreshEveryXMins      = 1;

string   ccy, sym, IndiName;
int      dig, tf, tmf;
double   spr, pnt, tickval, bidp, askp, minlot, lswap, sswap;
datetime prev_time;
bool     FirstTime;

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  IndiName = "HighLow-";
  IndicatorShortName(IndiName);
  
  if (RefreshEveryXMins > 240)                             RefreshEveryXMins = 240;
  if (RefreshEveryXMins > 60 && RefreshEveryXMins < 240)   RefreshEveryXMins = 60;
  if (RefreshEveryXMins > 30 && RefreshEveryXMins < 60)    RefreshEveryXMins = 30;
  if (RefreshEveryXMins > 15 && RefreshEveryXMins < 30)    RefreshEveryXMins = 15;
  if (RefreshEveryXMins > 5  && RefreshEveryXMins < 15)    RefreshEveryXMins = 5;
  if (RefreshEveryXMins > 1  && RefreshEveryXMins < 5)     RefreshEveryXMins = 1;

  ccy     = Symbol();
  tmf     = Period();
  bidp    = MarketInfo(ccy,MODE_BID);
  askp    = MarketInfo(ccy,MODE_ASK);
  pnt     = MarketInfo(ccy,MODE_POINT);
  dig     = MarketInfo(ccy,MODE_DIGITS);
  spr     = MarketInfo(ccy,MODE_SPREAD);
  tickval = MarketInfo(ccy,MODE_TICKVALUE);
  minlot  = MarketInfo(ccy,MODE_MINLOT);
  lswap   = MarketInfo(ccy,MODE_SWAPLONG);
  sswap   = MarketInfo(ccy,MODE_SWAPSHORT);
  if (dig == 3 || dig == 5) {
    pnt     *= 10;
    spr     /= 10;
    tickval *= 10;
  }  
  prev_time = -9999;

  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  if (RefreshEveryXMins < 0)  {
    if (FirstTime)  {
      del_obj();
      plot_obj();
    }
    FirstTime = false;      
    return(0);
  }  
  if (RefreshEveryXMins == 0) {
    del_obj();
    plot_obj();    
  }
  else {
    if (prev_time != iTime(sym,RefreshEveryXMins,0))  {
      del_obj();
      plot_obj();
      prev_time = iTime(sym,RefreshEveryXMins,0);
  } }      
  return(0);
}

//+------------------------------------------------------------------+
void del_obj()  {
//+------------------------------------------------------------------+
  int k=0;
  while (k<ObjectsTotal())   {
    string objname = ObjectName(k);
    if (StringSubstr(objname,0,StringLen(IndiName)) == IndiName)  
      ObjectDelete(objname);
    else
      k++;
  }    
  return(0);
}

//+------------------------------------------------------------------+
void plot_obj()   {
//+------------------------------------------------------------------+
  if (NumberOfBars < 0) 
    int firstbar = WindowFirstVisibleBar();
  else
    firstbar = NumberOfBars;  
  int highbar  = iHighest(NULL,0,MODE_HIGH,firstbar,0);
  int lowbar   = iLowest(NULL,0,MODE_LOW,firstbar,0);
  ObjectCreate(IndiName+"high",OBJ_HLINE,0,0,High[highbar]);
  ObjectCreate(IndiName+"low",OBJ_HLINE,0,0,Low[lowbar]);
  return(0);
}
Thanks in advance.
Author:  SteveHopwood [ Fri Dec 07, 2018 9:03 pm ]
Post subject:  Please Help a Novice stop a For Loop

burnrubber75 » Fri Dec 07, 2018 4:38 am wrote:Please can you help me add high & low of last n candles, then I need to add the pending orders x pips from those high/low prices?

Manage to sort of code it to place a line across the candles

Code: Select all

//+------------------------------------------------------------------+
//|                           High and low across last N candles.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window

#include <WinUser32.mqh>

extern int        NumberOfBars           = -1;
extern int        RefreshEveryXMins      = 1;

string   ccy, sym, IndiName;
int      dig, tf, tmf;
double   spr, pnt, tickval, bidp, askp, minlot, lswap, sswap;
datetime prev_time;
bool     FirstTime;

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  IndiName = "HighLow-";
  IndicatorShortName(IndiName);
  
  if (RefreshEveryXMins > 240)                             RefreshEveryXMins = 240;
  if (RefreshEveryXMins > 60 && RefreshEveryXMins < 240)   RefreshEveryXMins = 60;
  if (RefreshEveryXMins > 30 && RefreshEveryXMins < 60)    RefreshEveryXMins = 30;
  if (RefreshEveryXMins > 15 && RefreshEveryXMins < 30)    RefreshEveryXMins = 15;
  if (RefreshEveryXMins > 5  && RefreshEveryXMins < 15)    RefreshEveryXMins = 5;
  if (RefreshEveryXMins > 1  && RefreshEveryXMins < 5)     RefreshEveryXMins = 1;

  ccy     = Symbol();
  tmf     = Period();
  bidp    = MarketInfo(ccy,MODE_BID);
  askp    = MarketInfo(ccy,MODE_ASK);
  pnt     = MarketInfo(ccy,MODE_POINT);
  dig     = MarketInfo(ccy,MODE_DIGITS);
  spr     = MarketInfo(ccy,MODE_SPREAD);
  tickval = MarketInfo(ccy,MODE_TICKVALUE);
  minlot  = MarketInfo(ccy,MODE_MINLOT);
  lswap   = MarketInfo(ccy,MODE_SWAPLONG);
  sswap   = MarketInfo(ccy,MODE_SWAPSHORT);
  if (dig == 3 || dig == 5) {
    pnt     *= 10;
    spr     /= 10;
    tickval *= 10;
  }  
  prev_time = -9999;

  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  if (RefreshEveryXMins < 0)  {
    if (FirstTime)  {
      del_obj();
      plot_obj();
    }
    FirstTime = false;      
    return(0);
  }  
  if (RefreshEveryXMins == 0) {
    del_obj();
    plot_obj();    
  }
  else {
    if (prev_time != iTime(sym,RefreshEveryXMins,0))  {
      del_obj();
      plot_obj();
      prev_time = iTime(sym,RefreshEveryXMins,0);
  } }      
  return(0);
}

//+------------------------------------------------------------------+
void del_obj()  {
//+------------------------------------------------------------------+
  int k=0;
  while (k<ObjectsTotal())   {
    string objname = ObjectName(k);
    if (StringSubstr(objname,0,StringLen(IndiName)) == IndiName)  
      ObjectDelete(objname);
    else
      k++;
  }    
  return(0);
}

//+------------------------------------------------------------------+
void plot_obj()   {
//+------------------------------------------------------------------+
  if (NumberOfBars < 0) 
    int firstbar = WindowFirstVisibleBar();
  else
    firstbar = NumberOfBars;  
  int highbar  = iHighest(NULL,0,MODE_HIGH,firstbar,0);
  int lowbar   = iLowest(NULL,0,MODE_LOW,firstbar,0);
  ObjectCreate(IndiName+"high",OBJ_HLINE,0,0,High[highbar]);
  ObjectCreate(IndiName+"low",OBJ_HLINE,0,0,Low[lowbar]);
  return(0);
}
Thanks in advance.
Indicators cannot place trades.

:xm: :rocket:
Author:  burnrubber75 [ Tue Dec 11, 2018 9:03 am ]
Post subject:  Please Help a Novice stop a For Loop

Ok knew that, is it possible to add this to an EA to get it to work with the 2 Pending Orders EA you helped me with?

Thanks

SteveHopwood » Fri Dec 07, 2018 9:03 pm wrote:
burnrubber75 » Fri Dec 07, 2018 4:38 am wrote:Please can you help me add high & low of last n candles, then I need to add the pending orders x pips from those high/low prices?

Manage to sort of code it to place a line across the candles

Code: Select all

//+------------------------------------------------------------------+
//|                           High and low across last N candles.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window

#include <WinUser32.mqh>

extern int        NumberOfBars           = -1;
extern int        RefreshEveryXMins      = 1;

string   ccy, sym, IndiName;
int      dig, tf, tmf;
double   spr, pnt, tickval, bidp, askp, minlot, lswap, sswap;
datetime prev_time;
bool     FirstTime;

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  IndiName = "HighLow-";
  IndicatorShortName(IndiName);
  
  if (RefreshEveryXMins > 240)                             RefreshEveryXMins = 240;
  if (RefreshEveryXMins > 60 && RefreshEveryXMins < 240)   RefreshEveryXMins = 60;
  if (RefreshEveryXMins > 30 && RefreshEveryXMins < 60)    RefreshEveryXMins = 30;
  if (RefreshEveryXMins > 15 && RefreshEveryXMins < 30)    RefreshEveryXMins = 15;
  if (RefreshEveryXMins > 5  && RefreshEveryXMins < 15)    RefreshEveryXMins = 5;
  if (RefreshEveryXMins > 1  && RefreshEveryXMins < 5)     RefreshEveryXMins = 1;

  ccy     = Symbol();
  tmf     = Period();
  bidp    = MarketInfo(ccy,MODE_BID);
  askp    = MarketInfo(ccy,MODE_ASK);
  pnt     = MarketInfo(ccy,MODE_POINT);
  dig     = MarketInfo(ccy,MODE_DIGITS);
  spr     = MarketInfo(ccy,MODE_SPREAD);
  tickval = MarketInfo(ccy,MODE_TICKVALUE);
  minlot  = MarketInfo(ccy,MODE_MINLOT);
  lswap   = MarketInfo(ccy,MODE_SWAPLONG);
  sswap   = MarketInfo(ccy,MODE_SWAPSHORT);
  if (dig == 3 || dig == 5) {
    pnt     *= 10;
    spr     /= 10;
    tickval *= 10;
  }  
  prev_time = -9999;

  return(0);
}

//+------------------------------------------------------------------+
int deinit()  {
//+------------------------------------------------------------------+
  del_obj();
  return(0);
}

//+------------------------------------------------------------------+
int start()  {
//+------------------------------------------------------------------+
  if (RefreshEveryXMins < 0)  {
    if (FirstTime)  {
      del_obj();
      plot_obj();
    }
    FirstTime = false;      
    return(0);
  }  
  if (RefreshEveryXMins == 0) {
    del_obj();
    plot_obj();    
  }
  else {
    if (prev_time != iTime(sym,RefreshEveryXMins,0))  {
      del_obj();
      plot_obj();
      prev_time = iTime(sym,RefreshEveryXMins,0);
  } }      
  return(0);
}

//+------------------------------------------------------------------+
void del_obj()  {
//+------------------------------------------------------------------+
  int k=0;
  while (k<ObjectsTotal())   {
    string objname = ObjectName(k);
    if (StringSubstr(objname,0,StringLen(IndiName)) == IndiName)  
      ObjectDelete(objname);
    else
      k++;
  }    
  return(0);
}

//+------------------------------------------------------------------+
void plot_obj()   {
//+------------------------------------------------------------------+
  if (NumberOfBars < 0) 
    int firstbar = WindowFirstVisibleBar();
  else
    firstbar = NumberOfBars;  
  int highbar  = iHighest(NULL,0,MODE_HIGH,firstbar,0);
  int lowbar   = iLowest(NULL,0,MODE_LOW,firstbar,0);
  ObjectCreate(IndiName+"high",OBJ_HLINE,0,0,High[highbar]);
  ObjectCreate(IndiName+"low",OBJ_HLINE,0,0,Low[lowbar]);
  return(0);
}
Thanks in advance.
Indicators cannot place trades.

:xm: :rocket:
Author:  SteveHopwood [ Tue Dec 11, 2018 9:50 am ]
Post subject:  Please Help a Novice stop a For Loop

burnrubber75 » Tue Dec 11, 2018 9:03 am wrote:Ok knew that, is it possible to add this to an EA to get it to work with the 2 Pending Orders EA you helped me with?

Thanks
Yes. Study any one of my shells for an example of how to use iHighest and to find functions that will draw lines for you. Shells are found at http://www.stevehopwoodforex.com/phpBB3 ... p=803#p803

:xm: :rocket:
All times are UTC Page 1 of 2