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(Risk100){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);}

 

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?

Reason: