Can some1 code an EA to open opposite positions (opened by another EA). I have seen many Expert advisor which on back testing give a regular downward graph of PnL...
i think opening opposite position of those EA can give a regular upward graph in backtesting..
Can Some code an EA to open opposite positions (opened by another EA based on its magic number).
here is EA which gives regular downward Graph while backtesting it... kindly check attached file.
+++++++++++++++++++++++++++++++++++ +++++
//+------------------------------------------------------------------+
//| Heiken Ashi Expert Advisor.mq4 |
//| Copyright © 2008, TradingSytemForex |
//| http://www.tradingsystemforex.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, TradingSytemForex"
#property link "http://www.tradingsystemforex.com"
#define OrSt "Heiken Ashi Expert Advisor"
extern string IS="---------------- Indicator Settings";
extern int MaMetod=2;
extern int MaPeriod=6;
extern int MaMetod2=3;
extern int MaPeriod2=2;
extern string LM="---------------- Lot Management";
extern double Lots=0.1;
extern bool MM=false; //money management
extern double Risk=10; //risk in percentage
extern string TSTB="---------------- TP SL TS BE";
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=0; //trailing stop
extern int TS_Step=1; //trailing stop step
extern int BE=0; //breakeven
extern string EXT="---------------- Extras";
extern bool Reverse=false;
extern bool Add_Positions=false; //positions cumulated
extern int MaxOrders=100; //maximum number of orders
extern int Magic=0;
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 HA1,HA2;
if(CntO(OP_BUY,Magic)>0)TL=1;if(Cnt O(OP_SELL,Magic)>0)TL=-1;for(int i=1;i<=limit;i++){
HA1=iCustom(Symbol(),0,"Heiken_Ashi _Smoothed",MaMetod,MaPeriod,MaMetod 2,MaPeriod2,2,i);
HA2=iCustom(Symbol(),0,"Heiken_Ashi _Smoothed",MaMetod,MaPeriod,MaMetod 2,MaPeriod2,3,i);
if(HA2>HA1){if(Reverse)SV=1;else BV=1;break;}
if(HA1>HA2){if(Reverse)BV=1;else SV=1;break;}}
// expert money management
if(MM){if(Risk<0.1||Risk>100){Comme nt("Invalid Risk Value.");return(0);}
else{MML=MathFloor((AccountFreeMarg in()*AccountLeverage()*Risk*Point*1 00)/(Ask*MarketInfo(Symbol(),MODE_LOTSI ZE)*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)||Ma gic==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;O S=0;}
// expert conditions to close position
if((SV>0)||(RealSL_Enabled&&(OrderO penPrice()-Bid)/Point>=RealSL)||(RealTP_Enabled&&(A sk-OrderOpenPrice())/Point>=RealTP)){CB=1;}
if((BV>0)||(RealSL_Enabled&&(Ask-OrderOpenPrice())/Point>=RealSL)||(RealTP_Enabled&&(O rderOpenPrice()-Bid)/Point>=RealTP)){CS=1;}
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){Order Close(OrderTicket(),OrderLots(),Bid ,Slip,Red);return(0);}}
if(OrderType()==OP_SELL&&OrderSymbo l()==Symbol()&&((OrderMagicNumber() ==Magic)||Magic==0)){
if(CS==1){OrderClose(OrderTicket(), OrderLots(),Ask,Slip,Red);return(0) ;}}}double SLI=0,TPI=0;int TK=0;
// expert open position value
if((AddP()&&Add_Positions&&OP<=MaxO rders)||(OP==0&&!Add_Positions)){
if(OS==1){if(TP==0)TPI=0;else TPI=Bid-TP*Point;if(SL==0)SLI=0;else SLI=Bid+SL*Point;TK=OrderSend(Symbo l(),OP_SELL,MML,Bid,Slip,SLI,TPI,Or St,Magic,0,Red);OS=0;return(0);}
if(OB==1){if(TP==0)TPI=0;else TPI=Ask+TP*Point;if(SL==0)SLI=0;els e SLI=Ask-SL*Point;TK=OrderSend(Symbol(),OP_B UY,MML,Ask,Slip,SLI,TPI,OrSt,Magic, 0,Lime);OB=0; return(0);}}
for(j=0;j<OrdersTotal();j++){if(Ord erSelect(j,SELECT_BY_POS,MODE_TRADE S)){if(OrderSymbol()==Symbol()&&((O rderMagicNumber()==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++){OrderSelec t(j,SELECT_BY_POS,MODE_TRADES);if(O rderSymbol()==Symbol()){if((OrderTy pe()==Type&&(OrderMagicNumber()==Ma gic)||Magic==0))_CntO++;}}return(_C ntO);}
//expert breakeven
void TrP(){double pb,pa,pp;pp=MarketInfo(OrderSymbol( ),MODE_POINT);if(OrderType()==OP_BU Y){pb=MarketInfo(OrderSymbol(),MODE _BID);
if(BE>0){if((pb-OrderOpenPrice())>BE*pp){if((OrderS topLoss()-OrderOpenPrice())<0){ModSL(OrderOpe nPrice()+0*pp);}}}
// expert trailing stop
if(TS>0){if((pb-OrderOpenPrice())>TS*pp){if(OrderSt opLoss()<pb-(TS+TS_Step-1)*pp){ModSL(pb-TS*pp);return;}}}}
if(OrderType()==OP_SELL){pa=MarketI nfo(OrderSymbol(),MODE_ASK);if(BE>0 ){if((OrderOpenPrice()-pa)>BE*pp){if((OrderOpenPrice()-OrderStopLoss())<0){ModSL(OrderOpen Price()-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(),Ord erOpenPrice(),ldSL,OrderTakeProfit( ),0,CLR_NONE);}
//expert add positions function
bool AddP(){int _num=0; int _ot=0;
for (int j=0;j<OrdersTotal();j++){if(OrderSe lect(j,SELECT_BY_POS)==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);}
+++++++++++++++++++++
Help Needed, EA to open opposite position of another EA.
-
mohdnaved
- Posts: 3
- Joined: Wed Dec 21, 2011 2:36 pm
Help Needed, EA to open opposite position of another EA.
You do not have the required permissions to view the files attached to this post.
-
mohdnaved
- Posts: 3
- Joined: Wed Dec 21, 2011 2:36 pm
Re: Help Needed, EA to open opposite position of another EA.
Khalid, can u plz go through the code and change few lines to make desired changes,,
or can u plz share or code some EA to open opposite position of any EA based on its magic number?
thanx a lot in advance.
or can u plz share or code some EA to open opposite position of any EA based on its magic number?
thanx a lot in advance.
-
mohdnaved
- Posts: 3
- Joined: Wed Dec 21, 2011 2:36 pm
Re: Help Needed, EA to open opposite position of another EA.
Do kindly check if this EA is doing some manipulation in Back testing results.. as when i clicked "Visualization" in backtesting, i noticed that it was closing orders on a price on which the actual price never moved.. in fact most of the time it was closing trades at price higher than the high of the candle.. how is that possible???
- Durante
- Trader
- Posts: 26
- Joined: Wed Dec 28, 2011 11:59 am
- Location: Brisbane, Australia
Re: Help Needed, EA to open opposite position of another EA.
MT4i have a pretty good free trade copier that can make mirror trades. They note however that they've never come across a losing ea that can be turned into a winner by doing this.
Good luck in any case
Good luck in any case
-
garyfritz
Re: Help Needed, EA to open opposite position of another EA.
Exactly. Writing a losing EA is **EASY**. In fact it's hard NOT to! If you could reverse trades and turn a loser into a winner, we'd all be retired on the beach.Durante wrote:MT4i have a pretty good free trade copier that can make mirror trades. They note however that they've never come across a losing ea that can be turned into a winner by doing this.
Consider an example: a system buys at price X, and has TP at X+50 and SL at X-25. Price goes up 30 pips, returns to X, goes down 30 pips, and returns to X. That trade is a loser in both directions.