CloseBasketProfit

Author: Copyright � 2006, Robert Hill.
CloseBasketProfit
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
CloseBasketProfit
//+------------------------------------------------------------------+
//|                                            CloseBasketProfit.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property link      "http://www.metaquotes.net"
#property copyright "Copyright © 2006, Robert Hill."
#property show_inputs

//---- Include
#include <stderror.mqh>
#include <stdlib.mqh>

//---- 
extern int  ProfitTarget = 10; // Profit target in dollars

//---- 
double      TotalProfit = 0.0;
int         total, i;
bool        result = false;

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
   {
//---- Calculate total profit on all trades
      total = OrdersTotal();
      for(i=total-1;i>=0;i--)
         {
            OrderSelect(i, SELECT_BY_POS);
            TotalProfit += OrderProfit();
         }
      if (TotalProfit >= ProfitTarget)
         {
//---- First close losing trades
            //total = OrdersTotal();
            for(i=total-1;i>=0;i--)
               {
                  OrderSelect(i, SELECT_BY_POS);
                  result = false;
                  switch(OrderType())
                     {
//---- Close opened long positions
                        case OP_BUY : 
                           if ( OrderProfit() < 0) 
                              result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                           break;
//---- Close opened short positions
                        case OP_SELL   : 
                           if ( OrderProfit() < 0) 
                              result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                     }
                  if(result == false)
                     {
                        Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
                        Sleep(3000);
                     }  
               }
//---- Now close remaining trades
            //total = OrdersTotal();
            for(i=total-1;i>=0;i--)
               {
                  OrderSelect(i, SELECT_BY_POS);
                  result = false;
                  switch(OrderType())
                     {
//---- Close opened long positions
                        case OP_BUY : 
                           result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                           break;
//---- Close opened short positions
                        case OP_SELL   : 
                           result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
                            
                     }
                  if(result == false)
                     {
                        Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
                        Sleep(3000);
                     }  
               }
         }
//----
      return(0);
   }
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---