problem with clossing the positions

 

hey!!!i'm new at this forum and I have the problem:

int Crossed (double line1, double line2)


{
static int last_direction = 0;
static int current_direction = 0;
//Don't work in the first load, wait for the first cross!
static bool first_time = true;
if(first_time == true)
{
first_time = false;
return (0);
}
//----
if(line1 > line2)
current_direction = 1; //up
if(line1 < line2)
current_direction = 2; //down
//----
if(current_direction != last_direction) //changed != rozny
{
last_direction = current_direction;
return(last_direction);
}
else
{
return (0); //not changed
}
}






//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+

int start()
{
double ticket, total,s1,s2,m1,m2;


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

.

.

.

.


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

total = OrdersTotal();


if(total < 1)
{
if(isCrossed ==1 || m1==1)//jezeli jeden to daje sygnal buy i zajmuje dluga pozycje
{
ticket = OrderSend(Symbol(),0, Lots,NormalizeDouble(Ask,Digits),3,Ask-500*Point,Ask+500*Point,
"", 12345, 0, Green);
}
if(isCrossed == 2 || s2==2)//jezeli dwa to daje sygnal sell i zajmuje krotka pozycje
{
ticket = OrderSend(Symbol(),1, Lots, NormalizeDouble(Bid,Digits),3,Bid+500*Point,Ask-500*Point,
"", 12345, 0, Red);
}
return(0);
}
//--------------------------------------------------------------


if(OrderSelect(0,SELECT_BY_POS))
{
if(isCrossed == 1 )//zamykanie pozycji
OrderClose(OrderTicket(),Lots,NormalizeDouble(Ask,Digits),3,Red);




else
if(isCrossed == 2 )//zamykanie pozycji
OrderClose(OrderTicket(),Lots,NormalizeDouble(Bid,Digits),3,Red);

}

retutn(0);

generali the MA open position then the moving averege are crossing and they are closing at the next cross(with profit or loos), the problem is that the positions don't whona close every time only sometimes, I don't know what to do?

please help mi and corect my code :)

 

You need to select an order using OrderSelect() before attempting to close it.


CB

 
but there is OrderSelect() you do't see it?
 

Must've mis-read your code. Or you've been amending it since you posted it? Hmmm.

Anyways - you should start by replacing retutn(0); with return(0); That would not have compiled.

And there is no need to normalize Ask or Bid.

And you should use OrderLots().

And you should check whether the order is a buy or sell so you know whether to close at the Bid or Ask.

And you ought to add some error checking/handling.

Apart from that ... perfect.


CB

 

"And there is no need to normalize Ask or Bid."

yes I need because generete mi the error

 
pietra997:

"And there is no need to normalize Ask or Bid."

yes I need because generete mi the error

you need to check the order type to decide use bid or ask price when closing it

 
pietra997:

"And there is no need to normalize Ask or Bid."

yes I need because generete mi the error

you need to check the order type to decide use bid or ask price when closing it, if not, sometime error invalid price will generated

 
pietra997:

"And there is no need to normalize Ask or Bid."

yes I need because generete mi the error

I can assure you that you do NOT need to normalize Ask or Bid as these are normalized by definition.

You have mis-interpreted the error.

See my post above for what you need to do to get this code working.


But hey - what do I know?


CB