Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategy
0 Views
0 Downloads
0 Favorites
NOC-EA
extern double dBuyLevel = 0.54;
extern double dSellLevel= 0.74;
extern double dStopLoss = 200;
extern double dTrailingStop = 174;

// ------

double dTakeProfit = 0;

datetime timePrev = 0;
int nBars;
int nSlip = 5;

extern double risk = 0.05;

int nMagic = 0;


// ------

int init ()
{
    nBars = Bars;

    
    return(0);
}
// ------
int deinit()
{
    return(0);
}

// ------

int start()
{
    if(Bars < 200)
        return(0);
    
    if(!IsBarEnd())
        return(0);
    
    // ------
    
    double dNoc = iCustom(NULL, 0, "_NOC", 0, 3);
    double dNocPrev = iCustom(NULL, 0, "_NOC", 0, 4);
        
    for(int nCnt = OrdersTotal() - 1; nCnt >= 0; nCnt--)
    {
        OrderSelect(nCnt, SELECT_BY_POS, MODE_TRADES);
        if(OrderMagicNumber() == nMagic)
        {
            if(OrderType() == OP_BUY)
            {         
                if(dNocPrev >= dSellLevel && dNoc <= dSellLevel)
                {
                    OrderClose(OrderTicket(), 
                        OrderLots(), Bid, nSlip, Aqua);
                    break;
                }
            }
            else if(OrderType() == OP_SELL)
            {
                if(dNocPrev <= dBuyLevel && dNoc >= dBuyLevel)
                {
                    OrderClose(OrderTicket(), 
                        OrderLots(), Ask, nSlip, OrangeRed);
                    break;
                }
            }
        }
    }

    int nNumOfOpenedOrders = 0;
    for(nCnt = OrdersTotal() - 1; nCnt >= 0; nCnt--)
    {
        OrderSelect(nCnt, SELECT_BY_POS, MODE_TRADES);
        if(OrderMagicNumber() == nMagic)
            nNumOfOpenedOrders++;
    }

    if(nNumOfOpenedOrders == 0)
    {
        if(dNocPrev <= dBuyLevel && dNoc >= dBuyLevel) 
        {
            OrderSend(Symbol(), OP_BUY,  GetRisk(risk), Ask, 
                nSlip, Ask - dStopLoss*Point, 0, "", 
                nMagic, 0, Aqua);
        }
        else if(dNocPrev >= dSellLevel && dNoc <= dSellLevel) 
        {
            OrderSend(Symbol(), OP_SELL, GetRisk(risk), Bid, 
                nSlip, Bid + dStopLoss*Point, 0, "", 
                nMagic, 0, OrangeRed);
        }
    }
        
    // ------

    ModifyOrders();
    
    // ------
    
    return(0);
}


// ------

void ModifyOrders()
{
    for(int nCnt = 0; nCnt < OrdersTotal(); nCnt++)
    {
        OrderSelect(nCnt, SELECT_BY_POS, MODE_TRADES);
        if(OrderMagicNumber() == nMagic)
        {
            if(OrderType() == OP_BUY)
            {
                if(OrderStopLoss() < Bid - dTrailingStop*Point - 5 * Point)
                {
                    OrderModify(OrderTicket(), OrderOpenPrice(), 
                        Bid - dTrailingStop*Point, OrderTakeProfit(), 0, Aqua);
                    break;
                }
            }
            
            if(OrderType() == OP_SELL)
            {
                if(OrderStopLoss() > Ask + dTrailingStop*Point + 5 * Point)
                {
                    OrderModify(OrderTicket(), OrderOpenPrice(), 
                        Ask + dTrailingStop*Point, OrderTakeProfit(), 
                        0, OrangeRed);
                    break;
                }
            }
        }
    }
}

// ------

bool IsBarEnd()
{
    bool bIsBarEnd = false;
    if(nBars != Bars)
    {
        bIsBarEnd = true;
        nBars = Bars;
    }
    
    return(bIsBarEnd);
}

double GetRisk( double dRisk)
{
   
   double   dMinLot = MarketInfo (Symbol (),MODE_MINLOT);
   double   dMaxLot = MarketInfo (Symbol (),MODE_MAXLOT);
   double   dLotStep = MarketInfo (Symbol (),MODE_LOTSTEP);
   double   dLotSize = MarketInfo (Symbol (),MODE_LOTSIZE);
   double   dLots;

   if (dMinLot < 0 || dMaxLot <= 0.0 || dLotStep <= 0.0)
   {
      Print ("CalculateVolume: invalid MarketInfo() results [",dMinLot,",",dMaxLot,",",dLotStep,"]");
      return (0);
   }

   if (AccountLeverage () <= 0)
   {
      Print ("CalculateVolume: invalid AccountLeverage() [",AccountLeverage (),"]");
      return (0);
   }

   dLots = NormalizeDouble (AccountBalance () * dRisk * AccountLeverage () / dLotSize,2);
   dLots = NormalizeDouble (dLots / dLotStep,0) * dLotStep;
   if (dLots < dMinLot) dLots = dMinLot;
   if (dLots > dMaxLot) dLots = dMaxLot;
   return (dLots);
}


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