How to code? - page 248

 
 

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

// 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(),Bid,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,Slip,SLI,TPI,OrSt,Magic,0,Blue);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 breakeven

void TrP(){double pb,pa,pp;pp=MarketInfo(OrderSymbol(),MODE_POINT);if(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(),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(OrderSelect(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);}

 

/*

// 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;

*/

only add /*at the beguining of the expert conditions to close position

and */ at the end

msn aboukalil ( at) hotmail.fr

 

Thanks aboukalil for the quick answer, but I don't quite understand, if I edit that out, won't I have no close command, it will leave my existing trades, but my EA will never close it's own trade to open in the opposite direction?

Can you help a little on this

 

Howdy to all.....

Curious as to whether or not anyone knows of or possess a generic indicator that allows one to call any line based single buffer indicator in the iCustoms of generic one so that when the generic indicator is attached to a chart, it will display as dual colored histogram bar indicator whose color change is based off the slope of the iCustomed indy?

Any and all assistance is appreciated.

 

Ladies and gentelmen,

How can I turn an indicator into csv file?Thanks.

 
biddick:
Ladies and gentelmen, How can I turn an indicator into csv file?Thanks.

Do you mean how to save to CSV file?

Check this:

Tools - .csv reader

 
 

ple help modify this little script

Hi,

Could somebody pls help to modify this little order entry script

so that the lotsize traded is a function of a % risked of the account size

i know this line of code should be inserted somewhere but i have no clue how to program it

double Risk = RiskPercent / 100;

if (UseMoneyMgmt)

Lots=NormalizeDouble(AccountBalance()*Risk/stoploss/(MarketInfo(Symbol(),MODE_TICKVALUE)),2);

many thanks in advance

Files:
 

pending order

hi!

how do i manage pending orders? (with mt4 api)

when my program was received a TT_ORDER_PENDING_OPEN (trade type)

and it was received a PUMP_UPDATE_ACTIVATION in pumping notification