How to code? - page 63

 

HOW to Open an order at the beginning of a bar only ??

Hi,

I'm implementing an EA in which I want to open an order only if a cross of some level by an indicator has occured between the second previous bar and the previous one.

For example, on the daily chart, If there is a cross between the 18th of January and the 19th of January, I want to open an order on the 20th of January.

I did something like this:

When I open an order, I record the order open time.

Then I do the following test: Is (CurrentTime() - LastOrderOpenTime) < Period() ??

If yes, it means that the current bar is not finished, yet, and I shouldn't open a new order. And also, when this condition becomes false, I should be on the next bar of the chart, and the cross happened before the last bar and no order should be opened until the next signal.

The problem is that when attaching it to a chart, it keeps on creating new orders until the bar is finished. Anybody can tell me what I did wrong?

 
dvarrin:
Hi,

I'm implementing an EA in which I want to open an order only if a cross of some level by an indicator has occured between the second previous bar and the previous one.

For example, on the daily chart, If there is a cross between the 18th of January and the 19th of January, I want to open an order on the 20th of January.

I did something like this:

When I open an order, I record the order open time.

Then I do the following test: Is (CurrentTime() - LastOrderOpenTime) < Period() ??

If yes, it means that the current bar is not finished, yet, and I shouldn't open a new order. And also, when this condition becomes false, I should be on the next bar of the chart, and the cross happened before the last bar and no order should be opened until the next signal.

The problem is that when attaching it to a chart, it keeps on creating new orders until the bar is finished. Anybody can tell me what I did wrong?

Not sure but here is what somone jotted done for me sometime ago. I'd have to google it to give proper props to the original writer.

It helps determine whether or not your on the opening of a new bar.

int newbar()

{

double g;

int m,s,k;

m=Time[0]+Period()*60-TimeCurrent();

g=m/60.0;

s=m%60;

m=(m-m%60)/60;

return(m);

}[/CODE]

And I use it like this:

[CODE]if (newbar()==Period())

Hope that helps.

 
dvarrin:
Hi,

I'm implementing an EA in which I want to open an order only if a cross of some level by an indicator has occured between the second previous bar and the previous one.

For example, on the daily chart, If there is a cross between the 18th of January and the 19th of January, I want to open an order on the 20th of January.

I did something like this:

When I open an order, I record the order open time.

Then I do the following test: Is (CurrentTime() - LastOrderOpenTime) < Period() ??

If yes, it means that the current bar is not finished, yet, and I shouldn't open a new order. And also, when this condition becomes false, I should be on the next bar of the chart, and the cross happened before the last bar and no order should be opened until the next signal.

The problem is that when attaching it to a chart, it keeps on creating new orders until the bar is finished. Anybody can tell me what I did wrong?

Then I do the following test: Is (CurrentTime() - LastOrderOpenTime) < Period() ??

you can add // return (0);

OR

this might help.. add the code..

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

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;

if(OrderSymbol()== Symbol () && OrderMagicNumber()== MN)

{

if( OrderOpenTime() >= iTime(0, PERIOD_D1, 0) samebar++;

}

}

you can choose from the 2..

if ( samebar > 0 ) return (0);

OR

if (samebar < 1) {your order codes;}

i am not good in coding so might anyone try check if i place the right codes..

 
dvarrin:
Hi,

I'm implementing an EA in which I want to open an order only if a cross of some level by an indicator has occured between the second previous bar and the previous one.

For example, on the daily chart, If there is a cross between the 18th of January and the 19th of January, I want to open an order on the 20th of January.

I did something like this:

When I open an order, I record the order open time.

Then I do the following test: Is (CurrentTime() - LastOrderOpenTime) < Period() ??

If yes, it means that the current bar is not finished, yet, and I shouldn't open a new order. And also, when this condition becomes false, I should be on the next bar of the chart, and the cross happened before the last bar and no order should be opened until the next signal.

The problem is that when attaching it to a chart, it keeps on creating new orders until the bar is finished. Anybody can tell me what I did wrong?

Ummm...

if(iOpen(Symbol(),0,0)==iClose(Symbol(),0,0)&&iLow(Symbol(),0,0)==iHigh(Symbol(),0,0))

{

//A new bar has happend.

}
 

How do I know the profit of the previous trade? And the lots, and the side?

 
Dan7974:
How do I know the profit of the previous trade? And the lots, and the side?

Use OrdersHistoryTotal() instead of OrdersTotal(). Then use OrderProfit(), OrderLots() etc. You must make sure that historical quotes for the timeperiod you are interested in are loaded though, and I think you can only do this manually - right-click on the 'Orders History' tab and select 'All History'.

Should have added that you need to use 'MODE_HISTORY' with OrderSelect()...

 
omelette:
Use OrdersHistoryTotal() instead of OrdersTotal(). Then use OrderProfit(), OrderLots() etc. You must make sure that historical quotes for the timeperiod you are interested in are loaded though, and I think you can only do this manually - right-click on the 'Orders History' tab and select 'All History'. Should have added that you need to use 'MODE_HISTORY' with OrderSelect()...

Hi Omelette,

Do you know that BT have a problem looking in the history: it looks on the real history, not the one of the BT. I asked Metaquote few months ago about this bug but they didn't have any answer.... Maybe now it's fixed...

 

Hi

How do u code an interest rate tightening cycle on one currency ,and a interest rate reduction cycle on another currency?

Can EA look at swaps current and swaps historical rates?

OILFXPRO

 
Files:
Reason: