• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 2:28am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 2:28am
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 trades, Based on Magic Number Modification 14 replies

Close pending order on profit taken by magic number 11 replies

Close all pending orders and open orders once they reach profit target 3 replies

Close all orders at a certain time ea 2 replies

how to close a trade with the magic number? 1 reply

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 1
Attachments: Close all orders with magic number at certain profit
Exit Attachments
Tags: Close all orders with magic number at certain profit
Cancel

Close all orders with magic number at certain profit

  • Post #1
  • Quote
  • First Post: Apr 5, 2009 9:46pm Apr 5, 2009 9:46pm
  •  weekapaugh
  • | Joined Aug 2007 | Status: Member | 41 Posts
I have an incomplete script which will close all orders with a certain magic number at a certain profit target. I can not get the script to work and seems to be missing some basic formatting/function calls. Can anyone take a look, I'm sure it just needs a few things added (int init() etc etc).

Thanks


...
extern bool AutoClosePositions = true;
...
double MaxTargetProfit;
MaxTargetProfit = TotalOrders()*10; //This is up to your profit target or formula!
...
...
if(AutoClosePositions && TotalOrders()!= 0) {
if(Profit()>=MaxTargetProfit){
int total = OrdersTotal();
for(int i=total-1;i>=0;i--){
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;

if(OrderMagicNumber()==MAGIC){
switch(type){

case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);
break;

case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5, Blue);
} //switch

if(result == false){
Print("Order ", OrderTicket(), " failed to close. Error:", GetLastError() );
}//if(result == false)
}//if(OrderMagicNumber()==MAGIC){
}//for(int i= ...
}//if(AccountProfit()>=MaxTargetProfit)
}//if(AutoClose...

int TotalOrders() {
int cnt=0;
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderMagicNumber()==MAGIC && (OrderType()==OP_BUY || OrderType()==OP_SELL)) {
cnt++;
}
}
}
return(cnt);
}

double Profit(){
double Prof=0;
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderMagicNumber()==MAGIC) {
Prof = Prof + OrderProfit();
}
}
}
return(Prof);
}
  • Post #2
  • Quote
  • Apr 5, 2009 10:32pm Apr 5, 2009 10:32pm
  •  magnumfreak
  • Joined Nov 2007 | Status: Trying manual mode again | 2,210 Posts
based on this section of code it will close based on just magic number:

if(OrderMagicNumber()==MAGIC){
switch(type){

case OP_BUY : result = OrderClose(OrderTicket(),OrderLots( ),MarketInfo(OrderSymbol(),MODE_BID ),5,Red);
break;

case OP_SELL : result = OrderClose(OrderTicket(),OrderLots( ),MarketInfo(OrderSymbol(),MODE_ASK ),5, Blue);
} //switch


take all of that switch statement out, it is not needed. when you close an order you only need ordercloseprice(). so it would be OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Blue);

so are you looking for something to close all orders with the correct magic number if the total profit reaches x amount? Do you need to close any pending orders as well??
 
 
  • Post #3
  • Quote
  • Apr 5, 2009 10:42pm Apr 5, 2009 10:42pm
  •  weekapaugh
  • | Joined Aug 2007 | Status: Member | 41 Posts
Yes, just looking to close all orders with the correct magic number if the total profit reaches x amount for just the orders with a certain magic number. So it would total the orders with a certain magic number and close just those if the total profit target is met.

Isn't the code I posted missing something? When I put that exactly in a new blank script file it complies with a million errors.
 
 
  • Post #4
  • Quote
  • Apr 5, 2009 10:47pm Apr 5, 2009 10:47pm
  •  weekapaugh
  • | Joined Aug 2007 | Status: Member | 41 Posts
Actually I guess this should be an EA and not a script???
 
 
  • Post #5
  • Quote
  • Apr 7, 2009 1:31pm Apr 7, 2009 1:31pm
  •  FOREXflash
  • | Commercial Member | Joined Sep 2008 | 957 Posts
Try this EA.........
Attached File(s)
File Type: mq4 Profit protection.mq4   5 KB | 1,574 downloads
forexflash
 
 
  • Post #6
  • Quote
  • Apr 8, 2009 10:07am Apr 8, 2009 10:07am
  •  weekapaugh
  • | Joined Aug 2007 | Status: Member | 41 Posts
Thanks for posting....this looks good.
 
 
  • Post #7
  • Quote
  • Apr 8, 2009 10:12am Apr 8, 2009 10:12am
  •  zam_c61
  • | Joined Dec 2007 | Status: Member | 15 Posts
Quoting FOREXflash
Disliked
Try this EA.........
Ignored
can u modify my ea to close all entry pos when account equity/account balance=1.003 . figure can be adjust. email me at [email protected] .
 
 
  • Post #8
  • Quote
  • Edited 11:32pm Aug 15, 2009 6:56pm | Edited 11:32pm
  •  altoronto
  • Joined Jul 2009 | Status: Member | 211 Posts
Thank you for profit ea, can you explain how it works? I mean I have one EA on different currencies and I want when let's say when is +100pips/-100pips profit/lose to close orders and no more trades for that day.

extern int StartLockPips=200;
extern int MagicNumber=1803;
extern bool CloseAtPipsProfitReached=false;
extern int ScalpProfitPips=30;
extern bool AllowTradeClosure=true;

Thanks.

Quoting FOREXflash
Disliked
Try this EA.........
Ignored
 
 
  • Post #9
  • Quote
  • Last Post: Oct 5, 2009 10:35am Oct 5, 2009 10:35am
  •  forexsaint
  • Joined Jun 2009 | Status: &lt;-That's how u gonna b, in the END! | 1,509 Posts
> HERE < is Solution for same Requirement.

Hope that helps
100 Fold Challenge->Interested? ->https://www.forexfactory.com/thread/32152
 
 
  • Platform Tech
  • /
  • Close all orders with magic number at certain profit
  • 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