//--------------------------------------------------------------------------------
//                 This EA Programed And Developed By Akhmad FX
//         Contact Me To akhmad.sna@gmail.com For Your Custom EA Request
//                    Powered By Forex EA Generator Software
//          Etasoft Inc. Forex EA and Script Generator version 4.5   EA
//                        http://www.forexgenerator.com
//--------------------------------------------------------------------------------
// Keywords: MT4, Forex EA builder, create EA, expert advisor developer

#property copyright "Copyright © 2013, Akhmad FX"
#property link      "http://www.Eamarket.blogspot.com/"

#include <stdlib.mqh>
#include <WinUser32.mqh>

// exported variables
extern int SellStoploss18 = 5;
extern int SellTakeprofit18 = 100;
extern int PriceOffset18 = 0;
extern double BalanceRiskPercent18 = 5;
extern int SellStoploss21 = 5;
extern int SellTakeprofit21 = 100;
extern int PriceOffset21 = 2;
extern double BalanceRiskPercent21 = 5;
extern int BuyStoploss19 = 5;
extern int BuyTakeprofit19 = 100;
extern int PriceOffset19 = 0;
extern double BalanceRiskPercent19 = 5;
extern int BuyStoploss17 = 5;
extern int BuyTakeprofit17 = 100;
extern int PriceOffset17 = 2;
extern double BalanceRiskPercent17 = 5;
extern int TrailingStop20 = 10;
extern int NewTakeProfit20 = 0;
extern int TrailingGap20 = 1;
extern int TrailingStop14 = 10;
extern int NewTakeProfit14 = 0;
extern int TrailingGap14 = 1;
extern int TrailingStop15 = 10;
extern int NewTakeProfit15 = 0;
extern int TrailingGap15 = 1;
extern int TrailingStop16 = 10;
extern int NewTakeProfit16 = 0;
extern int TrailingGap16 = 1;


// local variables
double PipValue=1;    // this variable is here to support 5-digit brokers
bool Terminated = false;
string LF = "\n";  // use this in custom or utility blocks where you need line feeds
int NDigits = 4;   // used mostly for NormalizeDouble in Flex type blocks
int ObjCount = 0;  // count of all objects created on the chart, allows creation of objects with unique names
int current = 0;

datetime BarTime7 = 0;
int Count8 = 0;


int init()
{
    NDigits = Digits;
    
    if (false) ObjectsDeleteAll();      // clear the chart
    
    
    Comment("");    // clear the chart
    return(0);
}

// Expert start
int start()
{
    if (Bars < 10)
    {
        Comment("Not enough bars");
        return (0);
    }
    if (Terminated == true)
    {
        Comment("EA Terminated.");
        return (0);
    }
    
    OnEveryTick6();
    return(0);
}

void OnEveryTick6()
{
    if (true == false && true) PipValue = 10;
    if (true && (NDigits == 3 || NDigits == 5)) PipValue = 10;
    
    OncePerBar7();
    TrailingStop20();
    TrailingStop14();
    TrailingStop15();
    TrailingStop16();
    PrintInfoToChart_rev48();
    
}

void OncePerBar7()
{
    
    if (BarTime7 < Time[0])
    {
        // we have a new bar opened
        BarTime7 = Time[0]; // keep the new bar open time
        SellPendingRiskFixed18();
        SellPendingRiskFixed21();
        BuyPendingRiskFixed19();
        BuyPendingRiskFixed17();
        
    }
}

void SellPendingRiskFixed18()
{
    double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE) / AccountLeverage();
    double pipsize = 0.1 * 10;
    double maxlots = AccountFreeMargin() / 100 * BalanceRiskPercent18 / lotsize * pipsize;
    if (SellStoploss18 == 0) Print("OrderSend() error - stoploss can not be zero");
    double lots = maxlots / SellStoploss18 * 10;
    
    // calculate lot size based on current risk
    double lotvalue = 0.001;
    double minilot = MarketInfo(Symbol(), MODE_MINLOT);
    int powerscount = 0;
    while (minilot < 1)
    {
        minilot = minilot * MathPow(10, powerscount);
        powerscount++;
    }
    lotvalue = NormalizeDouble(lots, powerscount - 1);
    
    if (lotvalue < MarketInfo(Symbol(), MODE_MINLOT))    // make sure lot is not smaller than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MINLOT);
    }
    if (lotvalue > MarketInfo(Symbol(), MODE_MAXLOT))    // make sure lot is not greater than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MAXLOT);
    }
    
    int expire = TimeCurrent() + 60 * 478;
    double price = NormalizeDouble(High[1], NDigits) - PriceOffset18*PipValue*Point;
    double SL = price + SellStoploss18*PipValue*Point;
    if (SellStoploss18 == 0) SL = 0;
    double TP = price - SellTakeprofit18*PipValue*Point;
    if (SellTakeprofit18 == 0) TP = 0;
    if (478 == 0) expire = 0;
    int ticket = OrderSend(Symbol(), OP_SELLLIMIT, lotvalue, price, 3, SL, TP, "HLH4", 12, expire, Red);
    if (ticket == -1)
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
    
}

void SellPendingRiskFixed21()
{
    double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE) / AccountLeverage();
    double pipsize = 0.1 * 10;
    double maxlots = AccountFreeMargin() / 100 * BalanceRiskPercent21 / lotsize * pipsize;
    if (SellStoploss21 == 0) Print("OrderSend() error - stoploss can not be zero");
    double lots = maxlots / SellStoploss21 * 10;
    
    // calculate lot size based on current risk
    double lotvalue = 0.001;
    double minilot = MarketInfo(Symbol(), MODE_MINLOT);
    int powerscount = 0;
    while (minilot < 1)
    {
        minilot = minilot * MathPow(10, powerscount);
        powerscount++;
    }
    lotvalue = NormalizeDouble(lots, powerscount - 1);
    
    if (lotvalue < MarketInfo(Symbol(), MODE_MINLOT))    // make sure lot is not smaller than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MINLOT);
    }
    if (lotvalue > MarketInfo(Symbol(), MODE_MAXLOT))    // make sure lot is not greater than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MAXLOT);
    }
    
    int expire = TimeCurrent() + 60 * 478;
    double price = NormalizeDouble(Low[1], NDigits) - PriceOffset21*PipValue*Point;
    double SL = price + SellStoploss21*PipValue*Point;
    if (SellStoploss21 == 0) SL = 0;
    double TP = price - SellTakeprofit21*PipValue*Point;
    if (SellTakeprofit21 == 0) TP = 0;
    if (478 == 0) expire = 0;
    int ticket = OrderSend(Symbol(), OP_SELLSTOP, lotvalue, price, 3, SL, TP, "HLH4", 14, expire, Red);
    if (ticket == -1)
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
    
}

void BuyPendingRiskFixed19()
{
    double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE) / AccountLeverage();
    double pipsize = 0.1 * 10;
    double maxlots = AccountFreeMargin() / 100 * BalanceRiskPercent19 / lotsize * pipsize;
    if (BuyStoploss19 == 0) Print("OrderSend() error - stoploss can not be zero");
    double lots = maxlots / BuyStoploss19 * 10;
    
    // calculate lot size based on current risk
    double lotvalue = 0.001;
    double minilot = MarketInfo(Symbol(), MODE_MINLOT);
    int powerscount = 0;
    while (minilot < 1)
    {
        minilot = minilot * MathPow(10, powerscount);
        powerscount++;
    }
    lotvalue = NormalizeDouble(lots, powerscount - 1);
    
    if (lotvalue < MarketInfo(Symbol(), MODE_MINLOT))    // make sure lot is not smaller than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MINLOT);
    }
    if (lotvalue > MarketInfo(Symbol(), MODE_MAXLOT))    // make sure lot is not greater than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MAXLOT);
    }
    
    int expire = TimeCurrent() + 60 * 478;
    double price = NormalizeDouble(Low[1], NDigits) + PriceOffset19*PipValue*Point;
    double SL = price - BuyStoploss19*PipValue*Point;
    if (BuyStoploss19 == 0) SL = 0;
    double TP = price + BuyTakeprofit19*PipValue*Point;
    if (BuyTakeprofit19 == 0) TP = 0;
    if (478 == 0) expire = 0;
    int ticket = OrderSend(Symbol(), OP_BUYLIMIT, lotvalue, price, 3, SL, TP, "HLH4", 13, expire, Blue);
    if (ticket == -1)
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
    
}

void BuyPendingRiskFixed17()
{
    double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE) / AccountLeverage();
    double pipsize = 0.1 * 10;
    double maxlots = AccountFreeMargin() / 100 * BalanceRiskPercent17 / lotsize * pipsize;
    if (BuyStoploss17 == 0) Print("OrderSend() error - stoploss can not be zero");
    double lots = maxlots / BuyStoploss17 * 10;
    
    // calculate lot size based on current risk
    double lotvalue = 0.001;
    double minilot = MarketInfo(Symbol(), MODE_MINLOT);
    int powerscount = 0;
    while (minilot < 1)
    {
        minilot = minilot * MathPow(10, powerscount);
        powerscount++;
    }
    lotvalue = NormalizeDouble(lots, powerscount - 1);
    
    if (lotvalue < MarketInfo(Symbol(), MODE_MINLOT))    // make sure lot is not smaller than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MINLOT);
    }
    if (lotvalue > MarketInfo(Symbol(), MODE_MAXLOT))    // make sure lot is not greater than allowed value
    {
        lotvalue = MarketInfo(Symbol(), MODE_MAXLOT);
    }
    
    int expire = TimeCurrent() + 60 * 478;
    double price = NormalizeDouble(High[1], NDigits) + PriceOffset17*PipValue*Point;
    double SL = price - BuyStoploss17*PipValue*Point;
    if (BuyStoploss17 == 0) SL = 0;
    double TP = price + BuyTakeprofit17*PipValue*Point;
    if (BuyTakeprofit17 == 0) TP = 0;
    if (478 == 0) expire = 0;
    int ticket = OrderSend(Symbol(), OP_BUYSTOP, lotvalue, price, 3, SL, TP, "HLH4", 11, expire, Blue);
    if (ticket == -1)
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
    
}

void TrailingStop20()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == 11)
        {
            double takeprofit = OrderTakeProfit();
            
            if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TrailingStop20*PipValue*Point)
            {
                if (OrderStopLoss() < Ask-(TrailingStop20+TrailingGap20)*PipValue*Point)
                {
                    if (NewTakeProfit20 != 0) takeprofit = Ask+(NewTakeProfit20 + TrailingStop20)*PipValue*Point;
                    bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), Ask-TrailingStop20*PipValue*Point, takeprofit, OrderExpiration(), White);
                    if (ret1 == false)
                    Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                }
            }
            if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TrailingStop20*PipValue*Point)
            {
                if (OrderStopLoss() > Bid+(TrailingStop20+TrailingGap20)*PipValue*Point)
                {
                    if (NewTakeProfit20 != 0) takeprofit = Bid-(NewTakeProfit20 + TrailingStop20)*PipValue*Point;
                    bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid+TrailingStop20*PipValue*Point, takeprofit, OrderExpiration(), White);
                    if (ret2 == false)
                    Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                }
            }
        }
    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}

void TrailingStop14()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == 12)
        {
            double takeprofit = OrderTakeProfit();
            
            if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TrailingStop14*PipValue*Point)
            {
                if (OrderStopLoss() < Ask-(TrailingStop14+TrailingGap14)*PipValue*Point)
                {
                    if (NewTakeProfit14 != 0) takeprofit = Ask+(NewTakeProfit14 + TrailingStop14)*PipValue*Point;
                    bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), Ask-TrailingStop14*PipValue*Point, takeprofit, OrderExpiration(), White);
                    if (ret1 == false)
                    Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                }
            }
            if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TrailingStop14*PipValue*Point)
            {
                if (OrderStopLoss() > Bid+(TrailingStop14+TrailingGap14)*PipValue*Point)
                {
                    if (NewTakeProfit14 != 0) takeprofit = Bid-(NewTakeProfit14 + TrailingStop14)*PipValue*Point;
                    bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid+TrailingStop14*PipValue*Point, takeprofit, OrderExpiration(), White);
                    if (ret2 == false)
                    Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                }
            }
        }
    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}

void TrailingStop15()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == 13)
        {
            double takeprofit = OrderTakeProfit();
            
            if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TrailingStop15*PipValue*Point)
            {
                if (OrderStopLoss() < Ask-(TrailingStop15+TrailingGap15)*PipValue*Point)
                {
                    if (NewTakeProfit15 != 0) takeprofit = Ask+(NewTakeProfit15 + TrailingStop15)*PipValue*Point;
                    bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), Ask-TrailingStop15*PipValue*Point, takeprofit, OrderExpiration(), White);
                    if (ret1 == false)
                    Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                }
            }
            if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TrailingStop15*PipValue*Point)
            {
                if (OrderStopLoss() > Bid+(TrailingStop15+TrailingGap15)*PipValue*Point)
                {
                    if (NewTakeProfit15 != 0) takeprofit = Bid-(NewTakeProfit15 + TrailingStop15)*PipValue*Point;
                    bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid+TrailingStop15*PipValue*Point, takeprofit, OrderExpiration(), White);
                    if (ret2 == false)
                    Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                }
            }
        }
    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}

void TrailingStop16()
{
    for (int i=OrdersTotal()-1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
        if (OrderSymbol() == Symbol() && OrderMagicNumber() == 14)
        {
            double takeprofit = OrderTakeProfit();
            
            if (OrderType() == OP_BUY && Ask - OrderOpenPrice() > TrailingStop16*PipValue*Point)
            {
                if (OrderStopLoss() < Ask-(TrailingStop16+TrailingGap16)*PipValue*Point)
                {
                    if (NewTakeProfit16 != 0) takeprofit = Ask+(NewTakeProfit16 + TrailingStop16)*PipValue*Point;
                    bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), Ask-TrailingStop16*PipValue*Point, takeprofit, OrderExpiration(), White);
                    if (ret1 == false)
                    Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                }
            }
            if (OrderType() == OP_SELL && OrderOpenPrice() - Bid > TrailingStop16*PipValue*Point)
            {
                if (OrderStopLoss() > Bid+(TrailingStop16+TrailingGap16)*PipValue*Point)
                {
                    if (NewTakeProfit16 != 0) takeprofit = Bid-(NewTakeProfit16 + TrailingStop16)*PipValue*Point;
                    bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid+TrailingStop16*PipValue*Point, takeprofit, OrderExpiration(), White);
                    if (ret2 == false)
                    Print("OrderModify() error - ", ErrorDescription(GetLastError()));
                }
            }
        }
    }
    else
    Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
    
}

void PrintInfoToChart_rev48()
{
    string temp = "HLH4_Tick_TrailingStop\nExecuted : " + Count8 + "\n"
    + "Spread: " + DoubleToStr(MarketInfo(Symbol(), MODE_SPREAD)/PipValue, 2)+ "\n"
    + "................................................\n"
    + "ACCOUNT INFORMATION:\n"
    + "\n"
    + "Account Name   :     " + AccountName()+ "\n"
    + "Broker Name    :     " + AccountCompany()+ "\n"
    + "Account Number :     " + DoubleToStr(AccountNumber(), 0)+ "\n"
    + "Account Profit :     " + DoubleToStr(AccountProfit(), 2)+ "\n"
    + "\n"
    + "Account Leverage :     " + DoubleToStr(AccountLeverage(), 0)+ "\n"
    + "Account Balance  :     " + DoubleToStr(AccountBalance(), 2)+ "\n"
    + "Account Equity   :     " + DoubleToStr(AccountEquity(), 2)+ "\n"
    + "Free Margin      :     " + DoubleToStr(AccountFreeMargin(), 2)+ "\n"
    + "Used Margin      :     " + DoubleToStr(AccountMargin(), 2)+ "\n"
    + "Stop Out Level   :     " + DoubleToStr(AccountStopoutLevel(), 0)+ " %\n"
    + "\n"
    + "Date                :     " + TimeToStr(TimeCurrent(),TIME_DATE)+ "\n"
    + "Broker Server Time  :     " + TimeToStr(TimeCurrent(),TIME_SECONDS)+ "\n"
    + "Computer Local Time :     " + TimeToStr(TimeLocal(),TIME_SECONDS)+ "\n"
    + "................................................\n"
    + "\n"
    + "---------------------------------------------------------------\n"
    + "HAVE  A  NICE  TRADE  &  PROFIT  EVERYDAY  ! ! ! !\n"
    + "EA Provide By Akhmad FX , akhmad.sna@gmail.com\n"
    + "EA Programer And Custom EA Developer \n"
    + "Powered By Etasoft Inc. Forex EA and Script Generator Software \n"
    + "http://www.forexgenerator.com \n"
    + "---------------------------------------------------------------\n";
    
    
    Comment(temp);
    Count8++;
    
    
    
    
    
    
}



int deinit()
{
    if (false) ObjectsDeleteAll();
    
   return(0);    
}

