Coding help - page 161

 

2 orders at a time

Hello, I am trying to code an EA, and I have a problem, dont know how to solve it :?

so the problem is that I want to open 2 orders (OPENSTOP and SELLSTOP) at the current time, but on this EA it opens maby of them about 100 or more, so how to solve it to open only 2 :??

extern double Lots=1;

extern double h_beg=10;

extern double h_end=23;

extern double TakeProfit=20;

extern double StopLoss=90;

//+------------------------------------------------------------------+

int start()

{

double prevbar_H;

double prevbar_L;

double bar_H;

double bar_L;

double sell;

double buy;

bool trade_time;

bool sell_opened;

bool buy_opened;

//+------------------------------------------------------------------+

if (Hour()==h_beg)

{

trade_time = true;

for(int h=1; h<=11; h++)

{

bar_H = iHigh(NULL,0,h);

if (prevbar_H==0 || prevbar_H<bar_H)

{

prevbar_H = bar_H;

}

}

for(int l=1; l<=11; l++)

{

bar_L = iLow(NULL,0,l);

if (prevbar_L==0 || prevbar_L>bar_L)

{

prevbar_L = bar_L;

}

}

}

if (Hour()==h_end)

{

trade_time = false;

prevbar_H = 0;

prevbar_L = 0;

DeletePending();

DeleteBUY();

sell=0;

buy=0;

}

//+--------------------------------------------------------buy or sell----------+

if(Hour()==h_beg)

{

if(sell==0)

{

OrderSend(Symbol(),OP_SELLSTOP,Lots,prevbar_L,3,prevbar_L+200*Point,prevbar_L-200*Point,"MA sample",16384,0,Green);

sell=1;

}

if(buy==0)

{

OrderSend(Symbol(),OP_BUYSTOP,Lots,prevbar_H,3,prevbar_H-200*Point,prevbar_H+200*Point,"MA sample",16384,0,Green);

buy=1;

}

}

//+------------------------------------------------------------------+

return(0);

}

//+------------------------------------------------------------------+

void DeletePending()

{

for(int i=0; i<OrdersTotal(); i++)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

{

OrderDelete(OrderTicket());

}

}

}

void DeleteBUY()

{

for(int i=0; i<OrdersTotal(); i++)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

{

if (OrderType() == OP_BUY)

{

OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

}

}

}

}
 
carbonmimetic:
HI Mladen,

if I want count bars between 2 lows,how can avoid bar of sunday 23.00 p.m to 00.00?

because on 4h chart there is that bar and lasts only one hour.....and it appears also on daily chart.

thank's in advance

carbonmimetic

Add a day of week check similar to this :

if (TimeDayOfWeek(Time[yourCounter])!=0)

0 is Sunday and in that case you have to skip that bar

 

because the system that i use is like martingale trading system , once i place the order is about 1 working order and 4 pending order , when lose the 2nd pending order will become working order , so i need to move the prev's TP equal to current order, therefore i hope can have EA to automatic to this so i no need keep watching it , cause sometime need to go out for lunch or dinner . i try to code myself but don't know how to select new order and modify the prev order .

 
mladen:
carbonmimetic

Add a day of week check similar to this :

if (TimeDayOfWeek(Time[yourCounter])!=0)
0 is Sunday and in that case you have to skip that bar

ok....thanks a lot!

 

hi mladen,

could you please help look into the code, i see in live chart, it can repaint past signal line , when i press the refresh of mt4 menu, the line can shift position completely, i mean past signal line.

could you fix this problem, not to repaint past or cannot autorefresh problems. is it something like counting bars having problem or ways of internal loop? I am just superficial beginner of code. Many thanks for help.

Files:
 
kenwa:
hi mladen,

could you please help look into the code, i see in live chart, it can repaint past signal line , when i press the refresh of mt4 menu, the line can shift position completely, i mean past signal line.

could you fix this problem, not to repaint past or cannot autorefresh problems. is it something like counting bars having problem or ways of internal loop? I am just superficial beginner of code. Many thanks for help.

kenwa

Replace init and start with these :

double CCI_idx[];

double CCI_idt[];

int init()

{

IndicatorBuffers(2);

SetIndexStyle(0,DRAW_LINE);

SetIndexBuffer(0,CCI_idx);

SetIndexBuffer(1,CCI_idt);

return(0);

}

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

int start()

{

int i,counted_bars=IndicatorCounted();

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

int limit = MathMin(Bars-counted_bars,Bars-1);

for(i=limit;i>=0;i--) CCI_idt = iCCI(abc,0,CCI_Period,applied_price,i);

for(i=limit;i>=0;i--) CCI_idx = iMAOnArray(CCI_idt,Bars,MA_Period,0,MA_Method,i);

return(0);

}

You can not use the same buffer for cci values colecting and then to store averages of those cci - it was causing the repainting. Also, removed the time frame refernce from CCI call. If you wish it to calculate in multi time frame mode too, it has to be written differently

__________________________

PS: that same advice could be aplied to using different symbol on a current chart. The number of changed bars of a current symbol does not have to be the same at all as the number of bars of some other target symbol so you can get a repainting efect again, but left that as is

 

Hi mladen,

refer to #1606 above, since i am very superficial beginner or indeed know very little, i cannot understand your meaning, could you attached a workable indicator here? can i use one buffer to calculate CCI value and then done the average, instead of using two buffers? you mention different symbols, could it possible to write a simple version to accomodate two symbol on same chart? if it is mtf, how to write instead, many thanks for help.

 
kenwa:
Hi mladen, refer to #1606 above, since i am very superficial beginner or indeed know very little, i cannot understand your meaning, could you attached a workable indicator here? can i use one buffer to calculate CCI value and then done the average, instead of using two buffers? you mention different symbols, could it possible to write a simple version to accomodate two symbol on same chart? if it is mtf, how to write instead, many thanks for help.

kenwa

You can not use the same buffer for calculate buffer and then to store the results of the average of that CCI in the same buffer - average will change the past values

As of multi symbol indicator (the one that can access other symbols from a current chart) : if I post the solution for that I am afraid that it will not be simple. There is no simple solution for that and you first have to clear up what happens with two different symbols in the same time

Attaching the version that works correctly on a current symbol and in current time frame (that is the only way how you can calculate an average of a cci if you want to use iMAOnArray() function)

Files:
 

kenwa

This would be the simplest possible way to make it work in multi time frame and to be able to chose any symbol. You will see that it is not as simple as it sounds at the first glance, but this one does all needed to collect correct data and to avoid repainting when it does that

 

hi mladen,

thanks so much,refer to above, how if your mtf if write in non-mtf version be? i cannot modified myself due to my kindergarten coding level. by the way, because you seems used up 2 buffers to calculate, but mt4 only allow 8 buffers maximum is it?if i want to like the attached reference indicator (which is in no way a good indicator (has bugs inside i think, not so workable in live)), just for your reference, it can show eight symbols at the same time, not necessary currency or any symbols i want input myself externally, (i think superimpose the same indicator in sw is not work as the vertical scale is not fix there) how you can help me create a cci mtf one like the one of reference indicator (not necessary so complex, but has similar functions?) Many thanks again for kind help.

Files:
reference.mq4  17 kb
Reason: