Forex
Google

Go Back   Forex Trading > Discussion Areas > Setup Questions
Forex Forum Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-24-2006, 02:32 PM
cardio cardio is offline
Senior Member
 
Join Date: Sep 2005
Location: St Louis, MO, USA
Posts: 176
cardio is an unknown quantity at this point
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

Last edited by cardio : 04-24-2006 at 04:02 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-24-2006, 03:59 PM
Sonic Sonic is offline
Junior Member
 
Join Date: Apr 2006
Posts: 7
Sonic is on a distinguished road
Arrow

Quote:
Originally Posted by 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-24-2006, 04:07 PM
Sonic Sonic is offline
Junior Member
 
Join Date: Apr 2006
Posts: 7
Sonic is on a distinguished road
Quote:
Originally Posted by 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-24-2006, 04:22 PM
cardio cardio is offline
Senior Member
 
Join Date: Sep 2005
Location: St Louis, MO, USA
Posts: 176
cardio is an unknown quantity at this point
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.

Code:
/////////////////////////////////////////////

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-28-2006, 05:01 AM
Nicholishen's Avatar
Nicholishen Nicholishen is offline
Senior Member
 
Join Date: Dec 2005
Posts: 531
Nicholishen is on a distinguished road
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

PHP Code:
//+------------------------------------------------------------------+
//|                                              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 proMathAbs(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.1drop=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.1lotMM lots;
   if (
lotMM 1.0lotMM MathCeil(lotMM);
   if  (
lotMM 100lotMM 100;
   return (
lotMM);

__________________
"Anyone who has never made a mistake has never tried anything new." -Albert Einstein

Last edited by Nicholishen : 04-28-2006 at 05:13 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-28-2006, 05:21 PM
cardio cardio is offline
Senior Member
 
Join Date: Sep 2005
Location: St Louis, MO, USA
Posts: 176
cardio is an unknown quantity at this point
Thanks

That looks very good - I will test it out.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
To trade or not to trade Indicators? yaniv_av Indicators - Metatrader 4 1 03-27-2007 01:01 PM


All times are GMT. The time now is 10:58 AM.