//+------------------------------------------------------------------+
//|                                                     stop bot.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                             http://www.ox.opsss  |
//+------------------------------------------------------------------+

#property copyright "Bartholomew Roberts"
#property link      "Antigua & Barbuda"

#include <stdlib.mqh>
#include <WinUser32.mqh>

// exported variables
extern int TrailingStop2 = 12;
extern int NewTakeProfit2 = 30;
extern int TrailingGap2 = 7;


// local variables
double PipValue=1;    
bool Terminated = false;
string LF = "\n";  
int NDigits = 4;   
int ObjCount = 0;  
int current = 0;



int init()
{
    NDigits = Digits;
    
    if (false) ObjectsDeleteAll();      
    
    
    Comment("");    
}

// Expert start
int start()
{
    if (Bars < 10)
    {
        Comment("Not enough bars");
        return (0);
    }
    if (Terminated == true)
    {
        Comment("EA Terminated.");
        return (0);
    }
    
    OnEveryTick1();
    
}

void OnEveryTick1()
{
    if (true == false && false) PipValue = 10;
    if (true && (NDigits == 3 || NDigits == 5)) PipValue = 10;
    
    TrailingStop2();
    
}

void TrailingStop2()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
        {
            double takeprofit = OrderTakeProfit();
            
            if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TrailingStop2*PipValue*Point)
            {
                if (OrderStopLoss() < Ask-(TrailingStop2+TrailingGap2)*PipValue*Point)
                {
                    if (NewTakeProfit2 != 0) takeprofit = Ask+(NewTakeProfit2 + TrailingStop2)*PipValue*Point;
                    bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), Ask-TrailingStop2*PipValue*Point, takeprofit, OrderExpiration(), White);
                    if (ret1 == false)
                    Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                }
            }
            if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TrailingStop2*PipValue*Point)
            {
                if (OrderStopLoss() > Bid+(TrailingStop2+TrailingGap2)*PipValue*Point)
                {
                    if (NewTakeProfit2 != 0) takeprofit = Bid-(NewTakeProfit2 + TrailingStop2)*PipValue*Point;
                    bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid+TrailingStop2*PipValue*Point, takeprofit, OrderExpiration(), White);
                    if (ret2 == false)
                    Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                }
            }
        }
    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}



int deinit()
{
    if (false) ObjectsDeleteAll();
    
    
}

