Jump to content

Ask Req - Distance between orders in Ea. I need help


jdonut

Recommended Posts

Request.

Please help me learn (what I am doing wrong) so I can help someone else in the future. I am asking for help,... not someone to do it for me. Learning what I am doing is everything. I have the Mq4 Tutorial but it is still hard to understand. I am more of a hands on learner.

 

Mainly I need some help with order spacing - The distance between orders in pips when using multiple orders and getting the equity stop to also work with sells and the take profit. If you test it you will see what I mean. It compiles alright.

 

 

So far I changed some < to > and ==... sort of things. I also cut and pasted an equity stop into it which works with buys but not with sells and it doesn't allow for a take profit input. This Ea, I think has promise. It back tests pretty good with 1 position. I was wondering if anyone could help me clean up the code and help me make it work? Here is the code; with most recent changes I have made.

 

-------------------------------------------------------------

#define OrSt "Simple moving average EA"

extern string IS="---------------- Indicator Settings";
extern double MA_Period=20;
extern double MODE_MA=0; //0=simple, 1=exponential, etc...
extern string LM="---------------- Lot Management";
extern double Lots=0.01;
extern bool MM=false; //money management
extern double Risk=10; //risk in percentage
extern string TSTB="---------------- TP SL TS BE";
extern int Pips=50;             // **Distance between Orders
bool RealSL_Enabled=false;
int RealSL=5; //stop loss under 15 pîps
bool RealTP_Enabled=false;
int RealTP=10; //take profit under 10 pîps
extern int SL=0; //stop loss
extern int TP=0; //take profit
extern int TS=25; //trailing stop
extern int TS_Step=25; //trailing stop step
extern double Incremental_Eq_target=1;
extern int BE=10; //breakeven
extern string EXT="---------------- Extras";
extern bool Reverse=false;
extern bool Add_Positions=false; //positions cumulated
extern int MaxOrders=3; //maximum number of orders - if I can get the pips to increment- could be more???
extern int Magic=0;
int i;

int Slip=3;static int TL=0;double MML=0;

// Expert start function
int start(){int j=0,limit=1;double BV=0,SV=0;BV=0;SV=0;double MA1,MA2;
 if(CntO(OP_BUY,Magic)>0) TL=1;if(CntO(OP_SELL,Magic)>0) TL=-1;for(int i=1;i<=limit;i++){
 
 MA1=iMA(Symbol(),0,MA_Period,0,MODE_MA,PRICE_CLOSE,i+1);
 MA2=iMA(Symbol(),0,MA_Period,0,MODE_MA,PRICE_CLOSE,i);

 if(MA2>MA1){if(Reverse) SV=1; else BV=1; break;}
 if(MA2<MA1){if(Reverse) BV=1; else SV=1; break;}}

// Expert money management
if(MM){if(Risk<0.1 || Risk>100) {Comment("Invalid Risk Value."); return(0);}
  else {MML=MathFloor((AccountFreeMargin() *AccountLeverage()*Risk*Point*100)/(Ask*MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo(Symbol(),MODE_MINLOT )))*MarketInfo(Symbol(),MODE_MINLOT );}}
if(MM==false){MML=Lots;}

// Expert init positions
int cnt=0,OP=0,OS=0,OB=0,CS=0,CB=0;OP=0;for(cnt=0; cnt<OrdersTotal();cnt++) {OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if((OrderType()==OP_SELL || OrderType()==OP_BUY) && OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic) || Magic==0)) OP=OP+1;}
if(OP>=1){OS=0; OB=0;}OB=0;OS=0;CB=0;CS=0;


// Expert conditions to open position
if(SV>0){OS=1;OB=0;}if(BV>0){OB=1;OS=0;}

//***Added *Point + Pips in it
// Expert conditions to close position
if((SV>0) || (RealSL_Enabled && (OrderOpenPrice()-Bid*Point+Pips*Point)/Point>=RealSL)||(RealTP_Enabled && (Ask-OrderOpenPrice())/Point>=RealTP)){CB=0;}
if((BV>0) || (RealSL_Enabled && (Ask-OrderOpenPrice()*Point+Pips*Point)/Point>=RealSL)||(RealTP_Enabled && (OrderOpenPrice()-Bid)/Point>=RealTP)){CS=0;}
for(cnt=0;cnt<OrdersTotal();cnt++){OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic) || Magic==0)){if (CB==1){OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Red); return(0);}}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic) || Magic==0)){
if(CS==1){OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);return(0);}}}double SLI=0,TPI=0;int TK=0; 

//***Added *Point + Pips in it
// Expert open position value
if((AddP()+Pips && Add_Positions +Pips*Point && OP<=MaxOrders) || (OP==0 && !Add_Positions)) {
if(OS==1){if (TP==0) TPI=0; else TPI=Bid+TP*Point+Pips*Point;if (SL==0) SLI=0; else SLI=Bid+SL*Point+Pips*Point;TK=OrderSend(Symbol(),OP_SELL,MML,Bid,Slip,SLI,TPI,OrSt,Magic,0,Red);OS=0;return(0);}	
if(OB==1){if(TP==0) TPI=0; else TPI=Ask-TP*Point+Pips*Point;if(SL==0) SLI=0; else SLI=Ask-SL*Point+Pips*Point;TK=OrderSend(Symbol(),OP_BUY,MML,Ask,Slip,SLI,TPI,OrSt,Magic,0,Lime);OB=0; return(0);}}
for(j=0;j<OrdersTotal();j++){if(OrderSelect(j,SELECT_BY_POS, MODE_TRADES)){if (OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic) || Magic==0)) {TrP();}}}return(0);}

// Expert number of orders
int CntO(int Type,int Magic){int _CntO;_CntO=0;
for(int j=0;j<OrdersTotal();j++){OrderSelect(j, SELECT_BY_POS, MODE_TRADES);if(OrderSymbol()==Symbol()) {if((OrderType()==Type && (OrderMagicNumber()==Magic) || Magic==0)) _CntO++;}}return(_CntO);}

// Expert trailing stop
void TrP(){double pb,pa,pp;pp=MarketInfo(OrderSymbol(),MODE_POINT);if (OrderType()==OP_BUY){pb=MarketInfo(OrderSymbol(),MODE_BID);

// *** Added Expert incremental equity stop - isn't working right yet...Help!
if (AccountProfit()>=Incremental_Eq_target)
{
   for(i=OrdersTotal()-1;i>=0;i--)
      {
      OrderSelect(i, SELECT_BY_POS);
      int type   = OrderType();
              
      bool result = false;
             
      switch(type)
         {
         //Close opened long positions
         case OP_BUY  : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slip,Pink);
                        
              
         //Close opened short positions
         case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slip,Pink);
                         
         }
         
      if(result == false)
         {
           Sleep(300000000);
         }  
      }
     Print ("Account Balance Profit Reached. All Open Trades Have Been Closed");
     return(0);
  }  
  
  Comment("Balance: ",AccountBalance(),", Account Equity: ",AccountEquity(),", Account Profit: ",AccountProfit());
  
 return(0);

//Expert breakeven
   if(BE>0){if((pb-OrderOpenPrice())>BE*pp){if((OrderStopLoss()-OrderOpenPrice())<0){ModSL(OrderOpenPrice()+0*pp);}}}
   if(TS>0){if((pb-OrderOpenPrice())>TS*pp){if(OrderStopLoss()<pb-(TS+TS_Step-1)*pp){ModSL(pb-TS*pp);return;}}}}
 if(OrderType()==OP_SELL){pa=MarketInfo(OrderSymbol(),MODE_ASK);if(BE>0){if((OrderOpenPrice()-pa)>BE*pp){if((OrderOpenPrice()-OrderStopLoss())<0){ModSL(OrderOpenPrice()-0*pp);}}}
 if (TS>0){if (OrderOpenPrice()-pa>TS*pp){if (OrderStopLoss()>pa+(TS+TS_Step-1)*pp || OrderStopLoss()==0){ModSL(pa+TS*pp);return;}}}}}

//Expert stoploss
void ModSL(double ldSL){bool fm;fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldSL,OrderTakeProfit(),0,CLR_NONE);}


//***Added *Point + Pips in it figuring it mainly needed it when adding positions.   
//Expert add positions function
bool AddP(){int _num=0; int _ot=0;Pips=0;
for (int j=0;j>OrdersTotal();j++){if(OrderSelect (j,SELECT_BY_POS,Pips*Point)==true && OrderSymbol()==Symbol() && OrderType()<3 && ((OrderMagicNumber()==Magic) || Magic==0)) {	
 _num++;if(OrderOpenTime()>_ot) _ot=OrderOpenTime();}}
if(_num==0) return(true);if(_num>0 && ((Time[0]-_ot))>0) return(true);else return(false);

}

----------------------------------------------------------------
Here are back test results.  It also works very good on M1, M5, M15
-------------------------------------------------------------------
Symbol EURUSD (Euro vs. United States Dollar)
Period 30 Minutes (M30) 2009.01.05 01:00 - 2009.05.29 22:30 (2009.01.05 - 2009.05.31)
Settings:  Model Every tick (the most precise method based on all available least timeframes)
Parameters IS="---------------- Indicator Settings"; MA_Period=20; MODE_MA=0; LM="---------------- Lot Management"; Lots=0.01; MM=true; Risk=5; TSTB="---------------- TP SL TS BE"; SL=0; TP=0; TS=25; TS_Step=25; Incremental_Eq_target=1; BE=10; EXT="---------------- Extras"; Reverse=false; Add_Positions=true; MaxOrders=2; Magic=0;

-------------------------------------------------------------------

Bars in test 5990 Ticks modelled 4472597 Modelling quality 90.00%

Mismatched charts errors 2

Initial deposit 500.00

Total net profit 1984.64 Gross profit 2016.25 Gross loss -31.61

Profit factor 63.79 Expected payoff 1.49

Absolute drawdown 177.64 Maximal drawdown 1877.30 (85.35%) Relative drawdown 85.35% (1877.30)

Total trades 1334 Short positions (won %) 177 (97.74%) Long positions (won %) 1157 (98.44%)

Profit trades (% of total) 1312 (98.35%) Loss trades (% of total) 22 (1.65%)

Largest profit trade 97.20 loss trade -8.24

Average profit trade 1.54 loss trade -1.44

Maximum consecutive wins (profit in money) 502 (331.30) consecutive losses (loss in money) 3 (-23.12)

Maximal consecutive profit (count of wins) 389.47 (90) consecutive loss (count of losses) -23.12 (3)

Average consecutive wins 73 consecutive losses

 

----------------------------------------------------------------------------

I know there should be a *point Pips=0 in there somewhere...my only question is where are they?? I have tried everything it seems. It's very frustrating. :-|

----------------------------------------------------------------------------

 

Kudos to anyone that can help me.

Curt

Link to comment
Share on other sites

  • 2 weeks later...

Re: Ask Req - Distance between orders in Ea. I need help

 

Didn't read the code, since you said you would like to try it yourself. if you need to separate order 1 from order 2, 3,4, etc...

 

- if you want to separate them by distance of price points, then you need to capture the last price. Put it into variable and compare it using "if" statement. If not fulfilled, then don't send new order.

 

- if you want to separate them by distance of time, then you can take "new candle on chart". If no new chart, don't send new order. For the time calculation, just put it on the selected timeframe.

 

Hope that helps.

 

Regards,

Ore no Shinka Hikari yo Hayai. Zen Uchi o Nani no Mono Ore no Shinka Chuito Kore Nai.

Ten no Michi yo Iki. Subete o Sukosadoru Otoko.

Link to comment
Share on other sites

Re: Ask Req - Distance between orders in Ea. I need help

 

William, Kudos for the info. I will have to look at it this weekend when I have more time. I will have to figure out what you are saying but I better have the tutorial in front of me when I try it. :-) Kudos JD

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...