Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites

Profitability Reports

GBP/USD Oct 2024 - Jan 2025
20.00 %
Total Trades 3151
Won Trades 1532
Lost trades 1619
Win Rate 0.49 %
Expected payoff -1.53
Gross Profit 1172.60
Gross Loss -5990.30
Total Net Profit -4817.70
-100%
-50%
0%
50%
100%
TEST
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

#define MAGICMA  2

extern double Lots               = 0.1;
extern double TakeProfit = 8;
extern double TrailingStop = 6;
extern double Stoploss = 37;
extern double risk = 0.1;


//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }

  
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   int    res;
   double sema,lema,sema1,lema1;
   sema=iMA(Symbol(),0,2,0,MODE_EMA,PRICE_CLOSE,0);
   lema=iMA(Symbol(),0,40,0,MODE_EMA,PRICE_CLOSE,0);
   sema1=iMA(Symbol(),0,2,0,MODE_EMA,PRICE_CLOSE,1);
   lema1=iMA(Symbol(),0,40,0,MODE_EMA,PRICE_CLOSE,1);
//---- sell conditions
   if (sema>lema && sema1<lema1 && sema>sema1 && lema>lema1)  
     {
      res=OrderSend(Symbol(),OP_SELL,(MathCeil(AccountBalance() * risk / 12000)/10),Bid,3,Bid+Stoploss*Point,Bid-TakeProfit*Point,"",MAGICMA,0,Red);
      return;
     }

//---- buy conditions
   if (sema<lema && sema1>lema1 && sema<sema1 && lema<lema1) 
   
     {
      res=OrderSend(Symbol(),OP_BUY,(MathCeil(AccountBalance() * risk / 12000)/10),Ask,3,Ask-Stoploss*Point,Ask+TakeProfit*Point,"",MAGICMA,0,Blue);
      return;
     }  
//----
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // check for trailing stop
            if(TrailingStop>0)  
              {                 
               if(Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(OrderStopLoss()<Bid-Point*TrailingStop)
                    {
		                RefreshRates();
                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     //return(0);
                    }
                 }
              }
           }
         else // go to short position
           {
            if(TrailingStop>0)  
              {                 
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
                 {
                  if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
                    {
		                RefreshRates();
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
                     //return(0);
                    }
                 }
              }
           }
        }
     }
   return(0);
  }
// the end.
//----

//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }
//+--------------------------------------------------------------




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 ---