Please help, splitting a trade

 

hi

Does anyone have code or an ea that splits the trade at certain profit levels, e.i. Enter trade - when 30 pips in profit sell half of trade, when at 50 pips profit sell 30% of original, at 70 sell the last 20%.

I am getting myself into a serious knot trying to code this.

Any help is greatly appreciated.

Thanks

 
cardio:
hi

Does anyone have an ea that splits the trade at certain profit levels, e.i. Enter trade - when 30 pips in profit sell half of trade, when at 50 pips profit sell 30% of original etc.

I am getting myself into a serious knot trying to code this.

Any help is greatly appreciated.

Thanks

hi cardio.

i am also in process creating my ea and woking for same kind of functionality.

take this as an example.

double exit1 = 30;

double exit2 = 60;

double exit3 = 90;

// above three lines are there for profit levels..

//

if(OrderType() == OP_SELL) and if(OrderType() == OP_BUY)

you must have this code for buy and sell where you close your open positions right?

below these lines you need to add the condition to close the above 3 exits

lets use for sell and only 1

///

static int lots = { number of lots } // this is just to control the lots..

if{ normal stuff}

else if ( Ask < OrderOpenPrice()- exit1 ) * Point && lots >= {number} )

{

CloseOrder{}...

lots--;

}

-- more else if'sssss

////

this is one way to do it

hope this helps..!

-sonic.

 
Sonic:
hi cardio.

i am also in process creating my ea and woking for same kind of functionality.

take this as an example.

double exit1 = 30;

double exit2 = 60;

double exit3 = 90;

// above three lines are there for profit levels..

//

if(OrderType() == OP_SELL) and if(OrderType() == OP_BUY)

you must have this code for buy and sell where you close your open positions right?

below these lines you need to add the condition to close the above 3 exits

lets use for sell and only 1

///

static int lots = { number of lots } // this is just to control the lots..

if{ normal stuff}

else if ( Ask < OrderOpenPrice()- exit1 ) * Point && lots >= {number} )

{

CloseOrder{}...

lots--;

}

-- more else if'sssss

////

this is one way to do it

hope this helps..!

-sonic.

Ok to add to above desc what i am doing is to decrease a pos after 30 pip and then 60 and then 90 . but might not be exactly what you are looking for but you could get a idea from this ..

-sonic.

 

Thanks Sonic

Thanks Sonic

You reminded me of something I tried to do a while back - I will modify this code, I have not tested this - but will do so this week.

/////////////////////////////////////////////

void twostepts(string Symbol1, int magicNum, int ts1)

{

/*

So my exit is like this:

1) start trade

2) when profit is 30p move sl to be

3) form there use ts 30p

4) when profit will reach 60p move sl to 55p

5) if price still goes up and is above 60p then use ts 15p

and exit on stop.

*/

int total, cnt, ticket1;

int period2;

double n1, newsl;

total = OrdersTotal();

for(cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol1 && OrderMagicNumber()== magicNum)

{

//close any other outstanding pending OrderSelect

if (totalstop>0)

{

Print("closing all pending, totalstops = ", totalstop);

CloseAllPendingBUYSELL(Symbol1,magicNum);

}

if(OrderType()==OP_BUY) // long position is opened

{

// check for trailing stop

if(ts1>0)

{

if(Bid-OrderOpenPrice()>Point*62)

{

if(OrderStopLoss()<Bid-Point*15)

{

// Print("stage3 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",OrderStopLoss(),", new sl : ", Bid-Point*15);

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*15,OrderTakeProfit(),0,Green);

return(0);

}

return(0);

}

if(Bid-OrderOpenPrice()>Point*60)

{

if(OrderStopLoss()<OrderOpenPrice()+55*Point)

{

// Print("stage2 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",OrderStopLoss(),", new sl : ", OrderOpenPrice()+55*Point);

OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+55*Point,OrderTakeProfit(),0,Green);

return(0);

}

return(0);

}

if(Bid-OrderOpenPrice()>Point*30)

{

if(OrderStopLoss()<Bid-Point*30)

{

// Print("stage1 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",OrderStopLoss(),", new sl : ", OrderOpenPrice()+2*Point);

OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+2*Point,OrderTakeProfit(),0,Green);

return(0);

}

return(0);

}

}

}

else // go to short position

{

// check for trailing stop

if(ts1>0)

{

if((OrderOpenPrice()-Ask)>(Point*62))

{

if((OrderStopLoss()>(Ask+Point*15)) || (OrderStopLoss()==0))

{

//Print("stage1 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",Ask+Point*15,", new sl : ", OrderOpenPrice()+2*Point);

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*15,OrderTakeProfit(),0,Red);

return(0);

}

return(0);

}

if((OrderOpenPrice()-Ask)>(Point*60))

{

if((OrderStopLoss()>(OrderOpenPrice()-55*Point)) || (OrderStopLoss()==0))

{

//Print("stage1 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",OrderOpenPrice()-55*Point,", new sl : ", OrderOpenPrice()+2*Point);

OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-55*Point,OrderTakeProfit(),0,Red);

return(0);

}

return(0);

}

if((OrderOpenPrice()-Ask)>(Point*30))

{

if((OrderStopLoss()>(OrderOpenPrice()-2*Point)) || (OrderStopLoss()==0))

{

//Print("stage1 nts, Profit : ",Bid-OrderOpenPrice(),", Current sl : ",OrderOpenPrice()-2*Point,", new sl : ", OrderOpenPrice()+2*Point);

OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-2*Point,OrderTakeProfit(),0,Red);

return(0);

}

return(0);

}

}

}

}

//end trailing stop

}

}

///////////////////////////////////

Stay well

 

Here is some test code that you can butcher up to make your own. I think I might enter this one into the contest. hehehe

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

//| Trade Drop Test.mq4 |

//| Nicholishen |

//| www.tradingintl.com |

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

#property copyright "Nicholishen"

#property link "www.tradingintl.com"

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

//| expert initialization function |

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

int init()

{

//----

start();

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

//int TICK;

int start()

{

//----

while(0==0){

// TICK++;

int tt=OrdersTotal();

if(tt<1){

// TICK=0;

openorder(1);

Sleep(4000);

}

if(tt>0){

while(OrdersTotal()>0){

DropCheck();Sleep(3000);

}

}

}

//----

//Comment(TICK);

return(0);

}

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

bool DropCheck(){

static bool step1,step2;static int Last;

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

if(OrderSelect(i,SELECT_BY_POS)){

double pro= MathAbs(Ask-OrderOpenPrice());

//if(pro > 10*Point && !step1)

// if(TICK>Last){

DropLots(30,OrderTicket());

//if(pro > 20*Point && !step2 && step1)

// if(TICK==3 && TICK<4){DropLots(50,OrderTicket());step2=true;}

//if(pro > 30*Point && step2){

/*if(TICK==8){

if(OrderType()==OP_SELL){

OrderClose(OrderTicket(),OrderLots(),Ask,2,White);

}

if(OrderType()==OP_BUY){

OrderClose(OrderTicket(),OrderLots(),Bid,2,White);

}

//step1=false;step2=false;

TICK=0;

}*/

// Last=TICK;

}

}

return(true);

}

bool DropLots(int per,int ticket){

if(OrderSelect(ticket,SELECT_BY_TICKET)){

double remain=OrderLots();

if(remain>=0.2){

double drop=NormalizeDouble((remain*per)/100,2);

if(drop<=0.1) drop=OrderLots();

//if(drop>1.0)drop=MathCeil(drop);

Comment(drop);Print(drop);

}

ModifyLots(drop,ticket);

return(true);

}

return(false);

}

void ModifyLots(double drop,int ticket){

if(OrderType()==OP_BUY){

OrderClose(ticket,drop,Bid,2,White);

}

else if(OrderType()== OP_SELL){

OrderClose(ticket,drop,Ask,2,White);

}

}

extern int stoploss=20,takeprofit=20;extern double lots=3; int ID=23;

void openorder(int f){

double sl,tp;int res;

//string comm="Pro-Gen_"+DoubleToStr(ID,0);

// if(ConflictCheck){

// if( !CMC(Symbol(),f) )return;

// }

lots = LotSize();

// if(Reverse){

// if(f==1)f=2;

// else if(f==2)f=1;

// }

if(f==2){

while(res<=0){

if(stoploss==0){sl=0;}else{sl=Bid+stoploss*Point;}

if(takeprofit==0){tp=0;}else{tp=Bid-takeprofit*Point;}

//if(UseClose)tp=Bid-(takeprofit*2)*Point;

// if(!AlertOnlyMode){

res=OrderSend(Symbol(),OP_SELL,lots,Bid,2,sl,tp,"xname",ID,0,Red);

// }

if(res<=0){

int error=GetLastError();

// Print("Error = ",ErrorDescription(error));

if(error==134)break;

Sleep(1000);

RefreshRates();

}

}

// if(Alerts || AlertOnlyMode){

// Alert("PG Short Signal on ",Symbol());

// }

//lopen=false;sopen=false;

// bar=iBars(NULL,P(period));

}

else if(f==1){

while(res<=0){

if(stoploss==0){sl=0;}else{sl=Ask-stoploss*Point;}

if(takeprofit==0){tp=0;}else{tp=Ask+takeprofit*Point;}

//if(UseClose)tp=Ask+(takeprofit*2)*Point;

// if(!AlertOnlyMode){

res=OrderSend(Symbol(),OP_BUY,lots,Ask,2,sl,tp,"xname",ID,0,Blue);

// }

if(res<=0){

error=GetLastError();

// Print("Error = ",ErrorDescription(error));

if(error==134)break;

Sleep(1000);

RefreshRates();

}

}

// if(Alerts || AlertOnlyMode){

// Alert("PG Long Signal on ",Symbol());

// }

// lopen=false;sopen=false;

// bar=iBars(NULL,P(period));

}

}

extern int Risk=50;

double LotSize(){

double lotMM;

// if(PairsTraded==0){

lotMM = MathCeil(AccountFreeMargin() * Risk / 10000) / 10;

// } else {

// lotMM = MathCeil(AccountFreeMargin() * Risk / 10000 /PairsTraded) / 10 ;

// }

if (lotMM < 0.1) lotMM = lots;

if (lotMM > 1.0) lotMM = MathCeil(lotMM);

if (lotMM > 100) lotMM = 100;

return (lotMM);

}

 

Thanks

That looks very good - I will test it out.