• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 3:20am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 3:20am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Close all open trades if current total open lotsize exceeds"X" 0 replies

close open order with script while EA is in backtest 1 reply

ALL Close open order script for latest mt4 build 0 replies

script to "close open orders" and "open opposite orders" 3 replies

Close Current Pair Script - Need some help 2 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 1
Attachments: Script to close current open order
Exit Attachments
Tags: Script to close current open order
Cancel

Script to close current open order

  • Post #1
  • Quote
  • First Post: Aug 6, 2008 12:08pm Aug 6, 2008 12:08pm
  •  Haidan Inua
  • | Joined Jun 2008 | Status: Member | 29 Posts
Hi , I need some help with some coding. I want to write script to close current open order.. below is my script when I open a trade. What I want to do once the trend has changed I want to close current open trade. My script only allow open 1 trade at one time.
Please, can anyone help..
thanks

See below
>>>

if (cur_trend != pre_trend)//Trend changed
{
if (cur_val-0.01 > cur_val2 || cur_val+0.01 < cur_val1) //Setting 1
{


total =OrdersTotal();

//This where I want to close current open order if any..

if (total<1) //If no open order, proceed to buy or sell
{
if (pre_trend < cur_trend )
{
Print ("Trend has changed to UP ");

ticket=OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-250*Point,Ask+300*Point,"My EA",0,0,Green);
}
if (pre_trend > cur_trend )
{

Print ("Trend has changed to DOWN ");
ticket=OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Bid+250*Point,Bid-300*Point,"My EA",0,0,Green);

}
Print ("Total Order ", OrdersTotal());
Print ("Orders Type ", OrderType ());
Print ("Orders Open ", OrderOpenPrice());
Print ("Order Ticket ", OrderTicket());
}
}

}
  • Post #2
  • Quote
  • Aug 6, 2008 12:15pm Aug 6, 2008 12:15pm
  •  Ronald Raygun
  • Joined Jul 2007 | Status: 32 y/o Investor/Trader/Programmer | 5,016 Posts
Do you want to close only one trade? Or all trades?

The code looks like it's within an EA. You need to have the EA sort through all open trades to try and determine which trade it wants to manage.

PHP Code
  
int Total 
= OrdersTotal();
 
   for (
int i = 0; i < Total; i ++) {
      
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(
OrderType() <= OP_SELL &&  OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
 
// your closing code here
 
} 
 
 
  • Post #3
  • Quote
  • Aug 6, 2008 12:37pm Aug 6, 2008 12:37pm
  •  Haidan Inua
  • | Joined Jun 2008 | Status: Member | 29 Posts
Hi Ronald Raygun,
My EA only allow to 1 open trade at a time.
I want to close all open trade. In my case here total open trade only one.
I am not using magic number, could you please guide me how to use this in order to close the trade.

Thanks
 
 
  • Post #4
  • Quote
  • Aug 6, 2008 1:13pm Aug 6, 2008 1:13pm
  •  Ronald Raygun
  • Joined Jul 2007 | Status: 32 y/o Investor/Trader/Programmer | 5,016 Posts
There you go. See if you can study the code and copy what you need into teh expert advisor to get it working.
Attached File(s)
File Type: mq4 Close All Positions.mq4   1 KB | 397 downloads
 
 
  • Post #5
  • Quote
  • Last Post: Aug 6, 2008 10:35pm Aug 6, 2008 10:35pm
  •  Haidan Inua
  • | Joined Jun 2008 | Status: Member | 29 Posts
Hi Ronald Raygun,
Thanks.. the coding works.. but i think there is a problem with my EA..
What I want to do is.

1. Open trade when trend change.
2. From here I take a value from previous bar [2], and bar [1].Compare the changes.
3. If the trend changed take a trade at current bar[0].

4. Wait for a next bar open, and again compare the trend.
5. If there is a trend change, and I have and opened trade from previous trend change. I want to close that trade.
6. Then I open another trade based on the recent trend change.

My EA will look at every thick. I think this is where the problem is.
I saw you had written a reply to some one regarding disabling every thick..
I still studying it but not quite sure I understand it..

Thanks..
 
 
  • Platform Tech
  • /
  • Script to close current open order
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023