Why doesn't this EA close half

Hi,

This is a trade management EA I have spent hours on (I’m no programmer by the way, just trial and error) trying to understand why it won’t close half a position when it is supposed to.

It sets the stops ok on all open trades but it won’t go the next bit and close half the order at the set number of pips I tell it to.

The idea is it will manage [B]all [/B]open trades, I just leave it on [B]one[/B] chart, it sets the initial SL when I enter, moves the SL a bit tighter after a certain number of pips, and then when say 10 pips is reached it should close half (or a third or whatever I set it to)

The SL bit is fine, but it doesn’t close out half and I’m totally lost as to why. It must be something fundamental but I can’t see it.

Many thanks
PG


//+--------------------------------------------------------+ 
//          *****************
//            | Close Half Master | 
//+--------------------------------------------------------+ 

// must be on chart before trades placed

extern bool AllPositions = true; // must be set to true to work on ALL positions
extern bool UsePartClose = true; 

// my settings -50 -200 -30 -170 50 -150 80 -120 100 10

extern int LevelProfitSL = -50; // never really figured this but it seems to ensure SL is set on trade if set to -ve value
extern int LevelMovingSL = -200; // initial SL if its not already there
extern int LevelProfit0 = 10; // this extra level added to get it to work like a trailing stop between 5 and 10 pips to minimise loss.
extern int LevelMoving0 = -170; 
extern int LevelProfit1 = 20; // 
extern int LevelMoving1 = -150; 
// could lose one of the above but it seems to run ok.
extern int LevelProfit2 = 70; 
extern int LevelMoving2 = -100; 

extern string PCsettings = "Partial settings";
extern string pc_value = "LevelProfit3 = part close value";
extern string info = "preserve_lots protects some and leaves to trail";  // so if LP3 is 10 then part close will happen EVERY 10 pips profit

extern int LevelProfit3 = 100; 
extern int LevelMoving3 = 1; //so locked in as a BE trade

extern string how_much = "2 = 50%, 3 = 33% 4=25% of order";
extern double close_percent = 2.0;   // 2 is half each time, 3 is 1/3rd, 4 is 1/4 etc etc 
extern double preserve_lots = 0.2;  // how much to stop from being closed out

extern string info2 = "trailing starts at step+TS+level3moving";
extern int TrailingStop = 140;  //14 + 1 + 10 = 16 
extern int TrailingStep = 10; 
extern int Slippage = 2; 
extern bool ShowComment = true; 
extern bool UseSound = true; 
string var_132 = "button.wav"; 
string var_133 = "meep.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++) 
// for (int i = 0; i > OrdersTotal(); i--)      doesn't work this way round
 { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 


   {
    if (AllPositions || (OrderSymbol() == Symbol())) 

      {
         ThreeLevelSystemOfOutput(); digits = MarketInfo(OrderSymbol(),MODE_DIGITS); 
         point = MarketInfo(OrderSymbol(),MODE_POINT); 

         msg = msg + OrderSymbol() + " Open Price " + DoubleToStr(OrderOpenPrice(),digits) + " SL = " + DoubleToStr(OrderStopLoss(),digits) + " (" + StopLossInPoint() + ")
"; 
      }
   }
 } 
if (ShowComment) Comment(msg);
} 

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

void ThreeLevelSystemOfOutput() 
// actually now 4 :-)
{ 
int profit = ProfitPosition(); 
int sl = StopLossInPoint(); 
int spread = MarketInfo(OrderSymbol(),MODE_SPREAD);
double lots = MathCeil(OrderLots() * 10.0) / 10.0;
// add sl level  to make the quirk of setting sl automatically work
//added 0 level to get a extra move like a trailing SL

if (lots < preserve_lots) TrailingPositions();  // so the idea is it will not close out under say 0.5 of a lot

   {
   // this one sets the first / initial SL
   if ((profit > LevelProfitSL) && (profit <= LevelProfit0) && (sl < LevelMovingSL)) ModifyStopLossInPoint(LevelMovingSL);
   // this ones moves it a bit closer if necc
   if ((profit > LevelProfit0) && (profit <= LevelProfit1) && (sl < LevelMoving0)) ModifyStopLossInPoint(LevelMoving0);
   // closer still although poss redundant
   if ((profit > LevelProfit1) && (profit <= LevelProfit2) && (sl < LevelMoving1)) ModifyStopLossInPoint(LevelMoving1);
   // closer still although redundant
   if ((profit > LevelProfit2) && (profit <= LevelProfit3) && (sl < LevelMoving2)) ModifyStopLossInPoint(LevelMoving2); 
   // now this one should close half and move the SL to BE... but it doesn't
   if ((profit > LevelProfit3) && (sl < LevelMoving3))

      {
      ModifyStopLossInPoint(LevelMoving3);  // I know it gets to here ok
      if (UsePartClose) PartClose();   // but this routine doesn't get executed
      }
   
if (profit > LevelMoving3 + TrailingStop + TrailingStep) TrailingPositions(); 
   } 
}
//+------------------------------------------------------------------+ 

//Part Close routine
void PartClose() 

{ 

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 * pips2dbl) / 10.0; 
double lots = MathCeil(OrderLots() / close_percent * 10.0) / 10.0;   // the bit to part close out 1/3, 1/2 of the order etc

if (lots > preserve_lots) // so don't keep closing above what you want to preserve on the table
{ 
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_133); 

//closedHalf = true;   just testing to see if this is stopping rest of closing of trade, should be uncommented if no difference.

} } } 
//+------------------------------------------------------------------+ 

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)); 

}

Can someone help me out a bit with this.

Why don’t you just use IBFX’s partial close EA no need to reinvent the wheel

Sorry, can’t help also.

I use MPTM for same functionality (and much more)

stevehopwoodforex.com • View topic - MPTM’s new home

Cheers

thanks for the replies guys, looked at IBFX but its platform specific, only works with IBFX. Will take another look at MPTM.