Forex
Google
New signals service!

Go Back   Forex Trading > Programming > Metatrader Programming


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack (1) Thread Tools Display Modes
  #831 (permalink)  
Old 04-19-2008, 08:16 PM
Member
 
Join Date: Oct 2006
Posts: 71
Big Be is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #832 (permalink)  
Old 04-20-2008, 02:16 AM
ralph.ronnquist's Avatar
Senior Member
 
Join Date: Oct 2006
Posts: 280
ralph.ronnquist is on a distinguished road
Quote:
Originally Posted by european View Post
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #833 (permalink)  
Old 04-20-2008, 07:45 AM
Member
 
Join Date: Sep 2007
Posts: 35
zamanib is on a distinguished road
Quote:
Originally Posted by Big Be View Post
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #834 (permalink)  
Old 04-21-2008, 02:46 AM
european's Avatar
Senior Member
 
Join Date: Apr 2006
Posts: 284
european is on a distinguished road
code or platform problem?

Quote:
Originally Posted by ralph.ronnquist View Post
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #835 (permalink)  
Old 04-21-2008, 03:38 AM
ralph.ronnquist's Avatar
Senior Member
 
Join Date: Oct 2006
Posts: 280
ralph.ronnquist is on a distinguished road
Quote:
Originally Posted by european View Post
Thanks for replying.
For some reason my code doesnt work:

PHP Code:
if (OrderSelect(orderNoSELECT_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
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 Code:
if (OrderSelect(orderNoSELECT_BY_TICKET)==true) {
if (
OrderType() == OP_BUY    && Bid >= OrderTakeProfit())  CloseOrder(orderNo);
if (
OrderType() == OP_SELL && Ask <= OrderTakeProfit())  CloseOrder(orderNo); } 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #836 (permalink)  
Old 04-21-2008, 09:54 AM
Junior Member
 
Join Date: Jun 2007
Posts: 6
leialex is on a distinguished road
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);}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #837 (permalink)  
Old 04-21-2008, 11:34 AM
boostrade's Avatar
Member
 
Join Date: Dec 2006
Posts: 43
boostrade is on a distinguished road
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!
__________________
Enjoy trading. Have a goal, make it happen.
http://boostrade.spaces.live.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #838 (permalink)  
Old 04-21-2008, 11:51 AM
Administrator
 
Join Date: Sep 2005
Posts: 16,817
Blog Entries: 145
newdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud of
Quote:
Originally Posted by boostrade View Post
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 Buy/sell EA
I think it is what you need or you can use it as an example.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #839 (permalink)  
Old 04-21-2008, 01:41 PM
boostrade's Avatar
Member
 
Join Date: Dec 2006
Posts: 43
boostrade is on a distinguished road
ok

Quote:
Originally Posted by newdigital View Post
Look at this thread about Frank EA Buy/sell EA
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.
__________________
Enjoy trading. Have a goal, make it happen.
http://boostrade.spaces.live.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #840 (permalink)  
Old 04-21-2008, 02:03 PM
european's Avatar
Senior Member
 
Join Date: Apr 2006
Posts: 284
european is on a distinguished road
TP issue

PHP Code:
if (OrderSelect(orderNoSELECT_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.
Attached Images
File Type: jpg TP failed.JPG (31.8 KB, 61 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Tags
candle time, CHinGsMAroonCLK, coders guru, expert advisor, forex, how to code, I_XO_A_H, mechanical trading, trading

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

LinkBacks (?)
LinkBack to this Thread: http://www.forex-tsd.com/metatrader-programming/554-how-code.html
Posted By For Type Date
Need an experienced programmer? - Page 2 Post #0 Refback 09-24-2008 07:24 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to code this? iscuba11 Metatrader 4 mql 4 - Development course 1 08-03-2007 05:22 PM


All times are GMT. The time now is 12:08 PM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.