Help - how to modify an EA to leave existing trades open

 
I need a pretty simple solution if someone can help

I have an EA that is now profitable but it opens a trade and closes a trade at the same time, so that only 1 trade is active at any one time.

I want to now run it on an existing account that has a few existing trades already open, I want to leave those trades running, yet have the EA continue opening and closing its own trade, as a side issue, when any of the existing trades become profitable, it then closes those.

This side request of closing the existing trades at profit can be ignored if that will keep it simple, I can close those manually if need be.

Here is what my EA uses at the moment
if(val1 > val2) {trade_text = " BUY trade open";trend_text = "UP";BV=1;break;}
if(val1 < val2) {trade_text = " SELL trade open";trend_text = "DOWN";SV=1;break;}}

// expert money management
if(MM){if(Risk<0.1||Risk>100){Comment("Invalid Risk Value.");return(0);}
else{MML=MathFloor((AccountFreeMargin()*AccountLev erage()*Risk*Point*100)/(Ask*MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo( Symbol(),MODE_MINLOT)))*MarketInfo(Symbol(),MODE_M INLOT);}}
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)&&Or derSymbol()==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;}

// expert conditions to close position
if((SV>0)||(RealSL_Enabled&&(OrderOpenPrice()-Bid)/Point>=RealSL)||(RealTP_Enabled&&(Ask-OrderOpenPrice())/Point>=RealTP)){CB=1;}
if((BV>0)||(RealSL_Enabled&&(Ask-OrderOpenPrice())/Point>=RealSL)||(RealTP_Enabled&&(OrderOpenPrice()-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){OrderClose(OrderTicket(),OrderLots(),Bi d,Slip,Aqua);temp = Bid;return(0);}}
if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&& ((OrderMagicNumber()==Magic)||Magic==0)){
if(CS==1){OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Gold);temp = Bid;return(0);}}}double SLI=0,TPI=0;int TK=0;

// expert open position value
if((AddP()&&Add_Positions&&OP<=MaxOrders)||(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;OS=0;return(0);}
if(OB==1){if(TP==0)TPI=0;else TPI=Ask+TP*Point;if(SL==0)SLI=0;else SLI=Ask-SL*Point;{TK=OrderSend(Symbol(),OP_BUY,MML,Ask,Sli p,SLI,TPI,OrSt,Magic,0,Blue);OB=0;return(0);}}
for(j=0;j<OrdersTotal();j++){if(OrderSelect(j,SELE CT_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_P OS,MODE_TRADES);if(OrderSymbol()==Symbol()){if((Or derType()==Type&&(OrderMagicNumber()==Magic)||Magi c==0))_CntO++;}}return(_CntO);}

//expert breakeven
void TrP(){double pb,pa,pp;pp=MarketInfo(OrderSymbol(),MODE_POINT);i f(OrderType()==OP_BUY){pb=MarketInfo(OrderSymbol(),MODE_BID);
if(BE>0){if((pb-OrderOpenPrice())>BE*pp){if((OrderStopLoss()-OrderOpenPrice())<0){ModSL(OrderOpenPrice()+0*pp); }}}

// expert trailing stop
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(),l dSL,OrderTakeProfit(),0,CLR_NONE);}

//expert add positions function
bool AddP(){int _num=0; int _ot=0;
for (int j=0;j<OrdersTotal();j++){if(OrderSelect(j,SELECT_B Y_POS)==true && OrderSymbol()==Symbol()&&OrderType()<3&&((OrderMag icNumber()==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);} 
 

 

It's more of a concept than specific code (although I do have some of that too), but I use MyMagicNumber * 100 as my base, and look for trades in range MyMagicNumber * 100 to MyMagicNumber * 100 + 99 for open trades

Range 00-49 is buy, 50-99 is sell.

I have 5 SL methods

0x or 5x = SL method 0 = trailing SL

1X or 6X = SL method 1 = SL of Hi/Lo last N bars

2X or 7X = SL method 2 = SL of last Peak/Trough etc.

This allows for up to 10 trades of any particular SL method.

The concept can be modified to allow say 100 trades for each of 100 SL methods.

In your case, I would write a 'GetNextAvailableMagicNumberForBuyWithStopLossType(int pStopLossMethod)" that scanned open orders.

Then again, there's nothing stopping you from having multiple orders with SAME MagicNumber - so long as you do not treat it like a unique key.

*WARNING* I understand that in the US you cannot have hedged trades (some long, some short) open at the same time, but I am not affected by that ;-)

*IDEA* If you want to be long & short at the same time, use a combination symbol (Long USDJPY, Short USDJPY via EURJPY & EURUSD). okay, you get 2 spreads, but it's allowed (I think)

I am also toying with the idea of using OrderComment to store extra information rather than MagicNumber, but am afraid that it might get clobbered my MT4 ("Last part of the comment may be changed by server.")

 
brewmanz:

Oh dear, thanks for your help but I don't have a chance of coding that, any way you could provide the coding please?

 
beyond:


Maybe it would be simpler if the EA just ignores any existing open trades, i.e just leave them open, and then opens and closes its own new single trade

At the moment it opens and closes its own trade, but it also closes any additional existing open trades


How could I code this?

 
extern int Magic = 12345;

//rest of your code

// by closing the orders

if(OrderType()==OP_BUY && OrderSymbol()==Symbol()&& OrderMagicNumber()== Magic)
   {
   OrderClose(....);
   }

if(OrderType()==OP_SELL && OrderSymbol()==Symbol()&& OrderMagicNumber()== Magic)
   {
   OrderClose(....);
   }
Reason: