How to code? - page 84

 

Buy and sell on the 100's

Zamanib,

I understand what you wrote, but I don't see how you close at a profit.

This looks to me like a Hedge type EA.

There are several of those in forex-tsd,

and you don't have to Martingale.

Big Be

 
european:
Anybody can share ideas/code on how to Close an Order at TakeProfit value.

For some reason 'TakeProfit' in OrderSend() function doesn't always work on short/sell orders in MT4, though it works well in long/buy orders.

I wrote some code comparing order's TakeProfit value with current price, but it doesnt work correctly. Any ideas?

euro

longs ar closed at "bid" price, while shorts are closed at "ask" price.

 
Big Be:
Zamanib,

I understand what you wrote, but I don't see how you close at a profit.

This looks to me like a Hedge type EA.

There are several of those in forex-tsd,

and you don't have to Martingale.

Big Be

I see the market move in waves up and down some week you get 2 cycles up/down i have a different stratey when it trend. i trade manualy

please direct me to the hedge ea . I see lot of it . but not what excatly what i want.

 

code or platform problem?

ralph.ronnquist:
longs ar closed at "bid" price, while shorts are closed at "ask" price.

Thanks for replying.

For some reason my code doesnt work:

if (OrderSelect(orderNo, SELECT_BY_TICKET)==true) {

if (OrderType() == OP_BUY && Close[0] >= OrderTakeProfit()) CloseOrder(orderNo);

if (OrderType() == OP_SELL && Close[0] <= OrderTakeProfit()) CloseOrder(orderNo); }

I would be grateful for advice.

euro

 
european:
Thanks for replying.

For some reason my code doesnt work:

if (OrderSelect(orderNo, SELECT_BY_TICKET)==true) {

if (OrderType() == OP_BUY && Close[0] >= OrderTakeProfit()) CloseOrder(orderNo);

if (OrderType() == OP_SELL && Close[0] <= OrderTakeProfit()) CloseOrder(orderNo); }

[/PHP]

I would be grateful for advice.

euro

Right; "Close[0]" is the "bid" price, which is not the price an OP_SELL is closed against. An OP_SELL is closed against the "ask" price, which is some few pips (aka spread) above the "bid" price.

When your EA runs, the "bid" price is also available as the variable named "Bid" and the "ask" price as the variable named "Ask". So the code snippet would be better looking like:

[PHP]if (OrderSelect(orderNo, SELECT_BY_TICKET)==true) {

if (OrderType() == OP_BUY && Bid >= OrderTakeProfit()) CloseOrder(orderNo);

if (OrderType() == OP_SELL && Ask <= OrderTakeProfit()) CloseOrder(orderNo); }

 

Please help in code, thanks...

Hi,

I have new in writing EA, I was trying the following but got an error of "Invalid ticket for OrderModify function", Can someone please kindly have a look and help? Thanking in advance.

void ModifyHedgeSL(int SL) {

if (SL<1) return;

bool bres;

double sl,openadj;

if (lastopenbuy==1 && lastopensell==0) {openadj=Ask-Bid;} else {openadj=0;}

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

OrderSelect (i, SELECT_BY_POS,MODE_TRADES);

if ( OrderSymbol() == Symbol() || OrderMagicNumber() == expertId && OrderType() == OP_BUY )

{

sl = GetLastLongOpenPrice()-openadj-SL*Point;

bres = OrderModify (OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0, White);

if (bres) Print("Modifing Hedge BUY completed");

if (!bres) Print("Error Modifying Hedge BUY order : ",ErrorDescription(GetLastError()));

}

if ( OrderSymbol() == Symbol() || OrderMagicNumber() == expertId && OrderType() == OP_SELL )

{

sl = GetLastShortOpenPrice()-openadj+SL*Point;

bres = OrderModify (OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0, Gold);

if (bres) Print("Modifing Hedge SELL completed");

if (!bres) Print("Error Modifying Hedge SELL order : ",ErrorDescription(GetLastError()));

}

}

return;

}

double GetLastLongOpenPrice() {

int ord;

double LastLongOpenPrice=0;

//----

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

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if (OrderSymbol() == Symbol() && OrderMagicNumber()==expertId && OrderType() == OP_BUY)

{

LastLongOpenPrice=OrderOpenPrice();

}

}

if (LastLongOpenPrice>0) {return(LastLongOpenPrice);}

else {return(-1);}

}

 

How to code buy/sell orders when SL is hit

For example, if I buy 1 lot of EUR/JPY at 164.30 and Sl at 164.00.

if SL is hit, then I'll place another buy order at the same price, 164.30, with the same SL at 164, and with more lots, say 1.5. Then if SL is hit again, another buy order will be placed to buy 2 lots at the same price with the same SL.

Is there any code for this?? Thanks!

 
boostrade:
For example, if I buy 1 lot of EUR/JPY at 164.30 and Sl at 164.00.

if SL is hit, then I'll place another buy order at the same price, 164.30, with the same SL at 164, and with more lots, say 1.5. Then if SL is hit again, another buy order will be placed to buy 2 lots at the same price with the same SL.

Is there any code for this?? Thanks!

Look at this thread about Frank EA https://www.mql5.com/en/forum

I think it is what you need or you can use it as an example.

 

ok

newdigital:
Look at this thread about Frank EA https://www.mql5.com/en/forum I think it is what you need or you can use it as an example.

tks a lot newdigital, any questions i'll place here. tks again.

 

TP issue

if (OrderSelect(orderNo, SELECT_BY_TICKET)==true) {

if (OrderType() == OP_BUY && Bid >= OrderTakeProfit()) CloseOrder(orderNo);

if (OrderType() == OP_SELL && Ask <= OrderTakeProfit()) CloseOrder(orderNo); }

Ralph,

as you can see I call a separate function CloseOrder() where i had used 'Ask' and 'Bid' as you recommend but the problem still exist.

// Function CloseOrder **********************************************************

bool CloseOrder(int orderT) {

double sA;

if (OrderSelect(orderT, SELECT_BY_TICKET)==true) {

if (OrderType() == OP_BUY) sA = Bid;

else sA = Ask;

bool bClosed = OrderClose(orderT,OrderLots(),sA,0,CLR_NONE);

if (bClosed == 1) {

pTrades = 0; return(1); }

else return(0); }}

**********************************************************

I was hoping it will quarantee that the trade will be closed once TP is hit, but that is not the case, see chart attached, where the short order was opened at 104.03 with TP at 103.79. Althouth the price hit it (TP) the order wasn't closed.

Files:
tp_failed.jpg  32 kb
Reason: