Help Needed : manage all orders not just one chart

To add comments, please log in or register
kevin
113
kevin  

Hi,

this EA is basic but useful to me, its restriction is it will only monitor the order that corresponds to the chart it is attached to. It is designed to partial close at first TP level then step trail SL but no more partial close.

Is it possible for the EA to monitor ALL open orders and apply the partial close, SL etc to them all

I imagine it is a routine to scan open positions? Use one account does not make any difference whether true or false.

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

//| close_half |

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


extern bool UseOneAccount = false;

extern bool UseCloseOneThird = true;

extern int LevelProfit1 = 100;

extern int LevelMoving1 = 11;

extern int LevelProfit2 = 150;

extern int LevelMoving2 = 50;

extern int LevelProfit3 = 250;

extern int LevelMoving3 = 150;

extern int TrailingStop = 300;

extern int TrailingStep = 50;

extern int Slippage = 2;

extern bool ShowComment = true;

extern bool UseSound = true;

string var_132 = "email.wav";

bool closedHalf = false;


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


void deinit()

{

Comment("");

}


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


void start()

{

double point;

int digits;

string msg = "";

if (closedHalf == true && OrdersTotal() == 0) closedHalf = false;

for (int i = 0; i < OrdersTotal(); i++)

{

if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

{

if (UseOneAccount || (OrderSymbol() == Symbol()))

{

ThreeLevelSystemOfOutput();

digits = MarketInfo(OrderSymbol(),MODE_DIGITS);

point = MarketInfo(OrderSymbol(),MODE_POINT);

msg = msg + OrderSymbol() + " Öåíà: " + DoubleToStr(OrderOpenPrice(),digits) + " SL = " + DoubleToStr(OrderStopLoss(),digits) + " (" + StopLossInPoint() + ")\n";

}

}

}


if (ShowComment) Comment(msg);

}


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


void ThreeLevelSystemOfOutput()

{

int profit = ProfitPosition();

int sl = StopLossInPoint();

int spread = MarketInfo(OrderSymbol(),MODE_SPREAD);


if ((profit > LevelProfit1) && (profit <= LevelProfit2) && (sl < LevelMoving1))

{

ModifyStopLossInPoint(LevelMoving1);

if (UseCloseOneThird && closedHalf == false) CloseOneThird();

}

if ((profit > LevelProfit2) && (profit <= LevelProfit3) && (sl < LevelMoving2)) ModifyStopLossInPoint(LevelMoving2);

if ((profit > LevelProfit3) && (sl < LevelMoving3)) ModifyStopLossInPoint(LevelMoving3);

if (profit > LevelMoving3 + TrailingStop + TrailingStep) TrailingPositions();

}


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


void CloseOneThird()

{

bool result = false;


//2.0 is half, 3.0 is 1/3, 4.0 is a quarter and so on. Should only close out once.

double lots = MathCeil(OrderLots() / 2.0 * 10.0) / 10.0;


if (lots > 0.0)

{

if (OrderType() == OP_BUY)

{

result = OrderClose(OrderTicket(),lots,Bid,Slippage,CLR_NONE);

}

if (OrderType() == OP_SELL)

{

result = OrderClose(OrderTicket(),lots,Ask,Slippage,CLR_NONE);

}

if (result) {

if (UseSound) PlaySound(var_132);

closedHalf = true;

}

}

}


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


void TrailingPositions()

{

double bid;

double ask;

double point = MarketInfo(OrderSymbol(),MODE_POINT);


if (OrderType() == OP_BUY)

{

bid = MarketInfo(OrderSymbol(),MODE_BID);

if (bid - OrderOpenPrice() > TrailingStop * point)

{

if (OrderStopLoss() < bid - (TrailingStop + TrailingStep - 1) * point)

{

ModifyStopLoss(bid - TrailingStop * point);

return;

}

}

}


if (OrderType() == OP_SELL)

{

ask = MarketInfo(OrderSymbol(),MODE_ASK);

if (OrderOpenPrice() - ask > TrailingStop * point)

{

if ((OrderStopLoss() > ask + (TrailingStop + TrailingStep - 1) * point) || (OrderStopLoss() == 0))

{

ModifyStopLoss(ask + TrailingStop * point);

}

}

}

}


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


void ModifyStopLoss(double sl)

{

bool result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);

if (result && UseSound) PlaySound(var_132);

}


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


void ModifyStopLossInPoint(int stoploss)

{

bool result;

double sl = 0;

double point = MarketInfo(OrderSymbol(),MODE_POINT);


if (OrderType() == OP_BUY) sl = OrderOpenPrice() + stoploss * point;

if (OrderType() == OP_SELL) sl = OrderOpenPrice() - stoploss * point;


result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);

if (result && UseSound) PlaySound(var_132);

}


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


int ProfitPosition()

{

double bid;

double ask;

double point = MarketInfo(OrderSymbol(),MODE_POINT);

double profit = 0;


if (OrderType() == OP_BUY)

{

bid = MarketInfo(OrderSymbol(),MODE_BID);

profit = (bid - OrderOpenPrice()) / point;

}

if (OrderType() == OP_SELL)

{

ask = MarketInfo(OrderSymbol(),MODE_ASK);

profit = (OrderOpenPrice() - ask) / point;

}

return(MathRound(profit));

}


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


int StopLossInPoint()

{

double point = MarketInfo(OrderSymbol(),MODE_POINT);

double sl = 0;


if (OrderType() == OP_BUY) sl = (OrderStopLoss() - OrderOpenPrice()) / point;

if (OrderType() == OP_SELL) sl = (OrderOpenPrice() - OrderStopLoss()) / point;

if (OrderStopLoss() == 0.0) sl = -OrderOpenPrice() / point;

return(MathRound(sl));

}


William Roeder
20208
William Roeder  
pgtips: Is it possible for the EA to monitor ALL open orders and apply the partial close, SL etc to them all

  1. For large amounts of code, attach it
  2. Possible yes, but you can NOT use those predefined variables like Bid/Ask,Point etc.
    result = OrderClose(OrderTicket(),lots,Bid,Slippage,CLR_NONE);

  3. for (int i = 0; i < OrdersTotal(); i++)
    Always count down.
  4. double lots = MathCeil(OrderLots() / 2.0 * 10.0) / 10.0;
    Don't hard code numbers. Help ... how to know broker's accepted volume ? - MQL4 forum
  5. bool result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
    if (result && UseSound) PlaySound(var_132);
    What are Function return values ? How do I use them ? - MQL4 forum
  6. sl = (OrderStopLoss() - OrderOpenPrice()) / point;
    Not adjust for 4/5 digit brokers for each pair



kevin
113
kevin  
WHRoeder:

  1. For large amounts of code, attach it
  2. Possible yes, but you can NOT use those predefined variables like Bid/Ask,Point etc.
  3. Always count down.
  4. Don't hard code numbers. Help ... how to know broker's accepted volume ? - MQL4 forum
  5. What are Function return values ? How do I use them ? - MQL4 forum
  6. Not adjust for 4/5 digit brokers for each pair




Thank you for your reply, your level of knowledge is amazing and well beyond my stage right now.

1. ok, I see that

2. Would I need to pre define the variables at the start of the code to replace those

3. Count down, yes I can see and understand that now, thank you for the link. i > OrdersTotal(); i--

4. Yes I can see that, I did that for my own ease of use, but noted

5. This one got me I'm afraid. I read the link but still couldn't understand how to apply it to rectify the original problem.

6. This (4/5 digit broker) I am still learning this.

Thank you for your help with this.

PG

Simon Gniadkowski
17213
Simon Gniadkowski  
pgtips:


2. Would I need to pre define the variables at the start of the code to replace those

5. This one got me I'm afraid. I read the link but still couldn't understand how to apply it to rectify the original problem.


2. Use MarketInfo() to get the correct Ask and Bid for the symbol you are interested in

5. You should report the Error to the log if your OrderModify fails . . . then you will know why it failed. Currently you do nothing . . .

result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);

if (result && UseSound) PlaySound(var_132);
kevin
113
kevin  

Hi,

I've been struggling to get this right, re writing it a bit because I was not totally happy with the first layup, but the code is still not correct.

Any more insight into this would be appreciated, I wonder if the syntax of the partial close routine is acceptable.

Thank you

PG

//+--------------------------------------------------------+
//|                  close_half                      |
//+--------------------------------------------------------+

extern   bool     UseOneAccount = true;
extern   bool     UseCloseOneThird = true;
extern   int      LevelProfit1 = -50;
extern   int      LevelMoving1 = -200;
extern   int      preserve_lots = 1.0;
extern   int      LevelProfit2 = 50;
extern   int      LevelMoving2 = -150;
extern   int      LevelProfit3 = 100;
extern   int      LevelMoving3 = -100;
extern   int      close_how_many_lots = 1.0;
extern   int      TrailingStop = 200;
extern   int      TrailingStep = 20;
extern   int      Slippage = 2;
extern   bool     ShowComment = true;
extern   bool     UseSound = true;
string   var_132 = "email.wav";
bool   closedHalf = false;

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

void deinit()
{
Comment("");
}

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

void start()
{
double point;
int    digits;
string msg = "";
if (closedHalf == true && OrdersTotal() == 0) closedHalf = false;
for (int i = 0; i > OrdersTotal(); i--)
   {
   if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
      if (UseOneAccount || (OrderSymbol() == OrderSymbol()))  //this was orig symbol specific so made it equal itself, i.e was (OrderSymbol() == Symbol(), enables all charts / position now
         {
         ThreeLevelSystemOfOutput();  //so we jump to ThreeLevelSystemOfOutput function ok
         digits = MarketInfo(OrderSymbol(),MODE_DIGITS);
         point = MarketInfo(OrderSymbol(),MODE_POINT);
         msg = msg + OrderSymbol() + "  Open Price: " + DoubleToStr(OrderOpenPrice(),digits) + "  SL = " + DoubleToStr(OrderStopLoss(),digits) + " (" + StopLossInPoint() + ")\n";
         }
      }
   }

if (ShowComment) Comment(msg);
}

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

void ThreeLevelSystemOfOutput()
{
int profit = ProfitPosition();
int sl = StopLossInPoint();
int spread = MarketInfo(OrderSymbol(),MODE_SPREAD);

if ((profit > LevelProfit1) && (profit <= LevelProfit2) && (sl < LevelMoving1)) ModifyStopLossInPoint(LevelMoving1); // set first SLs
if ((profit > LevelProfit2) && (profit <= LevelProfit3) && (sl < LevelMoving2)) ModifyStopLossInPoint(LevelMoving2); // second
if ((profit > LevelProfit3) && (sl < LevelMoving3)) 
   {
   ModifyStopLossInPoint(LevelMoving3); // set third SL
   if (UseCloseOneThird && closedHalf == false) CloseOneThird(); //jump to close 1 third routine
   Alert(" inside three level routine Closed Half ",Symbol());  // gives me a screen heads up on where it is, and it gets here.
   }

if (profit > LevelMoving3 + TrailingStop + TrailingStep) TrailingPositions();  // use the trailing feature/element  , why not just use this from the off??

}

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

//routine to partal close the trade
void CloseOneThird()
{
bool   result = false;

//2.0 is half, 3.0 is 1/3, 4.0 is a quarter and so on. Should only close out once.
double lots = MathCeil(OrderLots() / 2.0 * 10.0) / 10.0;

if (lots > preserve_lots)  // so ensures not closed repetitively, preserves at a certain amount.
   {
   if (OrderType() == OP_BUY)
      {
      result = OrderClose(OrderTicket(),close_how_many_lots,Bid,Slippage,CLR_NONE);  //specify how much to close.. wonder is this correct??
      }
   if (OrderType() == OP_SELL)
      {
      result = OrderClose(OrderTicket(),close_how_many_lots,Ask,Slippage,CLR_NONE);
      }
   if (result) {
      if (UseSound) PlaySound(var_132); Alert(" Closed Half ",Symbol());
      closedHalf = true; 
   }
   }
}

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

void TrailingPositions()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);

if (OrderType() == OP_BUY)
   {
   bid = MarketInfo(OrderSymbol(),MODE_BID);
   if (bid - OrderOpenPrice() > TrailingStop * point)
      {
      if (OrderStopLoss() < bid - (TrailingStop + TrailingStep - 1) * point)
         {
         ModifyStopLoss(bid - TrailingStop * point);
         return;
         }
      }
   }

if (OrderType() == OP_SELL)
   {
   ask = MarketInfo(OrderSymbol(),MODE_ASK);
   if (OrderOpenPrice() - ask > TrailingStop * point)
      {
      if ((OrderStopLoss() > ask + (TrailingStop + TrailingStep - 1) * point) || (OrderStopLoss() == 0))
         {
         ModifyStopLoss(ask + TrailingStop * point);
         }
      }
   }
}

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

void ModifyStopLoss(double sl)
{
bool result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}

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

void ModifyStopLossInPoint(int stoploss)
{
bool   result;
double sl = 0;
double point = MarketInfo(OrderSymbol(),MODE_POINT);

if (OrderType() == OP_BUY) sl = OrderOpenPrice() + stoploss * point;
if (OrderType() == OP_SELL) sl = OrderOpenPrice() - stoploss * point;

result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}

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

int ProfitPosition()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double profit = 0;

if (OrderType() == OP_BUY)
   {
   bid = MarketInfo(OrderSymbol(),MODE_BID);
   profit = (bid - OrderOpenPrice()) / point;
   }
if (OrderType() == OP_SELL)
   {
   ask = MarketInfo(OrderSymbol(),MODE_ASK);
   profit = (OrderOpenPrice() - ask) / point;
   }
return(MathRound(profit));
}

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

int StopLossInPoint()
{
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double sl = 0;

if (OrderType() == OP_BUY) sl = (OrderStopLoss() - OrderOpenPrice()) / point;
if (OrderType() == OP_SELL) sl = (OrderOpenPrice() - OrderStopLoss()) / point;
if (OrderStopLoss() == 0.0) sl = -OrderOpenPrice() / point;
return(MathRound(sl));
}
William Roeder
20208
William Roeder  
pgtips: I wonder if the syntax of the partial close routine is acceptable.
double lots = MathCeil(OrderLots() / 2.0 * 10.0) / 10.0;

You are ASSUMING here that lot step is 0.1.

Do it right. See my code

kevin
113
kevin  

Thank you, you are correct it was a assumption and I 'think' I have fixed that but I still do not think the partial is working, unfortunately markets are dead now so I must wait until tomorrow to see if that worked.

Thanks you again,

PG

//+--------------------------------------------------------+
//|                  close_half                      |
//+--------------------------------------------------------+

extern   bool     UseOneAccount = true;  //has to be true to get the routine to work
extern   bool     UseCloseOneThird = true;
extern   int      LevelProfit1 = -50;
extern   int      LevelMoving1 = -200;
extern   int      preserve_lots = 1.0;
extern   int      LevelProfit2 = 50;
extern   int      LevelMoving2 = -150;
extern   int      LevelProfit3 = 100;
extern   int      LevelMoving3 = -100;
extern   int      close_how_many_lots = 1.0;
extern   int      TrailingStop = 200;
extern   int      TrailingStep = 20;
extern   int      Slippage = 2;
extern   bool     ShowComment = true;
extern   bool     UseSound = true;
string   var_132 = "email.wav";
bool   closedHalf = false;

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

void deinit()
{
Comment("");
}

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

void start()
{
double point;
int    digits;
string msg = "";
if (closedHalf == true && OrdersTotal() == 0) closedHalf = false;
for (int i = 0; i > OrdersTotal(); i--)
   {
   if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {
      if (UseOneAccount || (OrderSymbol() == OrderSymbol()))  //this was orig symbol specific so made it equal itself, i.e was (OrderSymbol() == Symbol(), enables all charts / position now
         {
         ThreeLevelSystemOfOutput();  //so we jump to ThreeLevelSystemOfOutput function ok
         digits = MarketInfo(OrderSymbol(),MODE_DIGITS);
         point = MarketInfo(OrderSymbol(),MODE_POINT);
         msg = msg + OrderSymbol() + "  Open Price: " + DoubleToStr(OrderOpenPrice(),digits) + "  SL = " + DoubleToStr(OrderStopLoss(),digits) + " (" + StopLossInPoint() + ")\n";
         }
      }
   }

if (ShowComment) Comment(msg);
}

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

void ThreeLevelSystemOfOutput()
{
int profit = ProfitPosition();
int sl = StopLossInPoint();
int spread = MarketInfo(OrderSymbol(),MODE_SPREAD);

if ((profit > LevelProfit1) && (profit <= LevelProfit2) && (sl < LevelMoving1)) ModifyStopLossInPoint(LevelMoving1); // set first SLs
if ((profit > LevelProfit2) && (profit <= LevelProfit3) && (sl < LevelMoving2)) ModifyStopLossInPoint(LevelMoving2); // second
if ((profit > LevelProfit3) && (sl < LevelMoving3)) 
   {
   ModifyStopLossInPoint(LevelMoving3); // set third SL
   if (UseCloseOneThird && closedHalf == false) CloseOneThird(); //jump to close 1 third routine
   Alert(" inside three level routine Closed Half ",Symbol());  // gives me a screen heads up on where it is, and it gets here.
   }

if (profit > LevelMoving3 + TrailingStop + TrailingStep) TrailingPositions();  // use the trailing feature/element  , why not just use this from the off??

}

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

//routine to partial close the trade
void CloseOneThird() 

{ 

bool result = false; 
//2.0 is half, 3.0 is 1/3, 4.0 is a quarter and so on. Should only close out once.

//++++ These are adjusted for 5 digit brokers.
int     pips2points;    // slippage  3 pips    3=points    30=points
double  pips2dbl;       // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
     if (Digits % 2 == 1){  // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
    // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    
//double lots = MathCeil(OrderLots() / 2.0 * 10.0) / 10.0;
// pips2points multiplier - credit to WHRoeder for correct approach  :  https://forum.mql4.com/46133

double lots = MathCeil(OrderLots() / 2.0 * pips2dbl) / 10.0; 


if (lots > 0.0) 
{ 
if (OrderType() == OP_BUY) { result = OrderClose(OrderTicket(),lots,Bid,Slippage,CLR_NONE); } 
if (OrderType() == OP_SELL) { result = OrderClose(OrderTicket(),lots,Ask,Slippage,CLR_NONE); } 
if (result) { if (UseSound) PlaySound(var_132); closedHalf = true; } } } 

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

void TrailingPositions()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);

if (OrderType() == OP_BUY)
   {
   bid = MarketInfo(OrderSymbol(),MODE_BID);
   if (bid - OrderOpenPrice() > TrailingStop * point)
      {
      if (OrderStopLoss() < bid - (TrailingStop + TrailingStep - 1) * point)
         {
         ModifyStopLoss(bid - TrailingStop * point);
         return;
         }
      }
   }

if (OrderType() == OP_SELL)
   {
   ask = MarketInfo(OrderSymbol(),MODE_ASK);
   if (OrderOpenPrice() - ask > TrailingStop * point)
      {
      if ((OrderStopLoss() > ask + (TrailingStop + TrailingStep - 1) * point) || (OrderStopLoss() == 0))
         {
         ModifyStopLoss(ask + TrailingStop * point);
         }
      }
   }
}

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

void ModifyStopLoss(double sl)
{
bool result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}

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

void ModifyStopLossInPoint(int stoploss)
{
bool   result;
double sl = 0;
double point = MarketInfo(OrderSymbol(),MODE_POINT);

if (OrderType() == OP_BUY) sl = OrderOpenPrice() + stoploss * point;
if (OrderType() == OP_SELL) sl = OrderOpenPrice() - stoploss * point;

result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}

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

int ProfitPosition()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double profit = 0;

if (OrderType() == OP_BUY)
   {
   bid = MarketInfo(OrderSymbol(),MODE_BID);
   profit = (bid - OrderOpenPrice()) / point;
   }
if (OrderType() == OP_SELL)
   {
   ask = MarketInfo(OrderSymbol(),MODE_ASK);
   profit = (OrderOpenPrice() - ask) / point;
   }
return(MathRound(profit));
}

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

int StopLossInPoint()
{
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double sl = 0;

if (OrderType() == OP_BUY) sl = (OrderStopLoss() - OrderOpenPrice()) / point;
if (OrderType() == OP_SELL) sl = (OrderOpenPrice() - OrderStopLoss()) / point;
if (OrderStopLoss() == 0.0) sl = -OrderOpenPrice() / point;
return(MathRound(sl));
}
Pecks
5
Pecks  

Hey Kevin,

This is a really old post.  I am also using this code with the intention of re-writing (taking out parts) for it to manage just the stop loss. Somehow I managed to happen across your thread here.

Did you manage to get this code working? 

Was interested to know as this is the first I've done in terms of editing coding for this application, did this part of your adjusted code work and the EA was managing all the open positions? and did you have to load the EA to all charts?

 if (UseOneAccount || (OrderSymbol() == OrderSymbol()))  //this was orig symbol specific so made it equal itself, i.e was (OrderSymbol() == Symbol(), enables all charts / position now
         {

I guess because this was quite some years ago now that you may have moved on from this code? however if you did manage to get it working let me know.

Many thanks

To add comments, please log in or register