What is wrong with the following code? :(

 

//CODE BLOCK “A” – no issues here.

//Main logic for entry for long or short. Simple MA Cross.

//It works fine and it enters the orders correctly if there is NO Code Block B.

if (MAONE > MATWO && MAONEP < MATWOP)

{

EnterLong = True;

LongDir = True;

}

if (MAONE < MATWO && MAONEP > MATWOP)

{

EnterShort = True;

ShortDir = True;

}

//Code BLOCK “B”

//When I add this following block of code to add additional shorts/longs while the MA

// Cross is still in effect, the EA just makes only Long Orders only and weirdly it makes // // 50 trades and closes 50 trades in sequence. (like open 1, open 2, …. Open 50 and then // close 50, close 49, …. Close 1.) (the no of trades vary depend on the days I use for //testing). It opens for each candle/bar one trade...and after N no of trades open, it closes each one from the latest one to the first one opened. Weird?:(

// Not sure what is wrong with this block of code. Any one have come across this weird //error?:)

if (CountLongs(MagicNumber) > 1)

{

if (RSIVAL > 50 && (LongDir) )

{if (Ask > storedbuyorder) {AddXtraL=true;} }

}

if (CountShorts(MagicNumber) > 1)

{

if (RSIVAL <50 && (ShortDir))

{if (Bid < storedsellorder) {AddXtraS=true;} }

}

// Code block C

// Rest of the code for usual, order entry, trailing,etc. no issues there.

//

Thanks in advance for your time/help.

 

Sounds like the classical case of not limiting the number of orders per bar. When you send an order, increment a global variable called BarTraded. At every new bar, reset it to 0. When checking to enter, make sure BarTraded is less than the maximum number of orders you want to place.

Jon

 
Archael wrote >>

Sounds like the classical case of not limiting the number of orders per bar. When you send an order, increment a global variable called BarTraded. At every new bar, reset it to 0. When checking to enter, make sure BarTraded is less than the maximum number of orders you want to place.

Jon

Thanks a lot. Ideally, based on the above logic, there may not be mutilpe orders on a given bar, but very remote chance of that happening is there. But still I thought some error msg would pop up. Thanks again, will look up on that global var.

 
Kent wrote >>

Thanks a lot. Ideally, based on the above logic, there may not be mutilpe orders on a given bar, but very remote chance of that happening is there. But still I thought some error msg would pop up. Thanks again, will look up on that global var.

I did the following changes and I am still getting the error.

//NewBar initialization
if (NewBar())
{
BarTraded = 0;
barcount = barcount +1 ;
}

//-----------

//-----------
bool NewBar()
{
static datetime lastbar = 0;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
BarTraded = 0;
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}

//And in the order send code...

if(EnterShort == true && CountShorts(MagicNumber)== 0)
{
//CLOSE OPEN ORDER
CloseLongs(MagicNumber);
// PLACE THE ORDER
OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,StopShort(Ask,StopLoss),TakeShort(Bid,ProfitTarget),ExpertName,MagicNumber,EXPIRATION,EnterShortColor);
BarTraded=1;
}

 

At first glance, it seems like you did everything right except 1 thing.. you forgot to actually use the BarTraded variable ;)

if(EnterShort == true && CountShorts(MagicNumber)== 0)

needs to become

if(EnterShort == true && CountShorts(MagicNumber)== 0 && BarTraded == 0)

Jon

 
Archael wrote >>

At first glance, it seems like you did everything right except 1 thing.. you forgot to actually use the BarTraded variable ;)

if(EnterShort == true && CountShorts(MagicNumber)== 0)

needs to become

if(EnterShort == true && CountShorts(MagicNumber)== 0 && BarTraded == 0)

Jon

Well, I had added that code too and still it is not giving up:(

 

Is it actually an error? Cuz what I got from your first post is that you were getting 50 trades placed at the same time. This BarTraded thing will rpevent that. If you're getting an actual error with an error number though it's gonna be a different solution.

Jon

 
Archael wrote >>

Is it actually an error? Cuz what I got from your first post is that you were getting 50 trades placed at the same time. This BarTraded thing will rpevent that. If you're getting an actual error with an error number though it's gonna be a different solution.

Jon

I am not getting any compiling errors. It compiles with 0 errors. If I run the Code Block "A" which only enters long or short based on simple Movinve Average Crossovers. (MA1 Crossing MA2). But When I add some other conditions in Code Block "B" where I add the additional condition while the main trade is still SHORT and look for additional Shorts. When I run the EA for N of days, for some reason after the start main MA cross over, the EA makes only Buys (while it is supposed to make shorts (since the Main MA Xover is short it should make additionals shorts only) SHORT trades for every bar/candle for 180 bars/candles. The trades are happening like this. It opens 180 buys for all the 180 bars/candles and the close (latest bar/candle) it closes all the trades after it hit some no. I tried with different days and It opens around 170 trades and this time also only buys for so many bars/candles.

So I am not sure whether it is something to do with BarTraded or my logic in additional short/long. The main code Block A works without any issues.

Thanks again for your time/efforts in helping me out.

Again there are NO multiple trades in one Single BAR. I get one trade per bar which is ridiculous. Out of 180 bars it tests, there may be less than 10 bars which might match the conditions on Code Block "B". Hence the trades on every bar/candle (single trades per bar/candle) doesnot make any sense at all.

Will keep looking....

 

Ah ok then the suggestion I made with BarTraded was no good. Although it still has its uses, it won't fix your problem. Without seeing the code, I won't be able to tell what is wrong with your logic though. You'd need to make a variable that hold the type of trade you want for further orders and use that for all of them. It seems like there might also be something wrong with your "further orders" condition which makes it always true and opens trades for some reason. If you want to post more, I'll look it over.

Jon

 
Archael wrote >>

Ah ok then the suggestion I made with BarTraded was no good. Although it still has its uses, it won't fix your problem. Without seeing the code, I won't be able to tell what is wrong with your logic though. You'd need to make a variable that hold the type of trade you want for further orders and use that for all of them. It seems like there might also be something wrong with your "further orders" condition which makes it always true and opens trades for some reason. If you want to post more, I'll look it over.

Jon

Thanks. I do agree that BarTraded is not applicable in my case since I donot have any issue with multiple trades on a single bar. Mine is specific to wrong trades in ineligbile bars/candles(as per my logic). The code I have posted is the same I am testing it. I removed all the unncessary small routines since the main logic is on Block A and Block B (which is causing the logical error). I will dry run again with printf and see I can find out the problem. Sincere thanks to you for taking time/efforts to help me.

 

The blocks you posted above don't have the lines where you actually give values to your variables and stuff like that. This is like a stick in my bicycle wheels for debugging since there's no way to track down the problem. If you post the mq4 or copy-pasted code, I can probably tell you what's wrong in a couple of minutes.

Jon

Reason: