Скачать MetaTrader 5
Авторизуйтесь или зарегистрируйтесь, чтобы добавить комментарий
Создавай интересные темы на форуме по автоматическому трейдингу!
Bremsberg
6
Bremsberg 2013.04.24 07:26 

Добавте  трайлиг стоп для советника 4H GPB/JPY

трайлинг стоп для buy    для sell   10 ПУНКТОВ


//-------------------------------------------------------------
//  Etasoft Inc. Forex EA and Script Generator version 4.1   EA
//-------------------------------------------------------------
// Keywords: MT4, Forex EA builder, create EA, expert advisor developer

#property copyright "Copyright © 2011, Etasoft Inc. Forex EA Generator v4.1"
#property link      "http://www.forexgenerator.com/"

#include <stdlib.mqh>
#include <WinUser32.mqh>

// exported variables
extern double MaximumLoss18 = 100;
extern int SellStoploss41 = 100;
extern int SellTakeprofit41 = 300;
extern double BalanceRiskPercent41 = 5;
extern int BuyStoploss37 = 100;
extern int BuyTakeprofit37 = 300;
extern double BalanceRiskPercent37 = 5;


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

double InitialBalance18 = 0;
extern int ORDCOUNT=1;
int Count20 = 0;


int init()
{
    NDigits = Digits;
   
    if (false) ObjectsDeleteAll();      // clear the chart
   
    InitialBalance18 = AccountBalance();
   
   
    Comment("");    // clear the chart
}

// Expert start
int start()
{
    if (Bars < 10)
    {
        Comment("Not enough bars");
        return (0);
    }
    if (Terminated == true)
    {
        Comment("EA Terminated.");
        return (0);
    }
   
    OnEveryTick19();
   
}

void OnEveryTick19()
{
    if (true == false && true) PipValue = 10;
    if (true && (NDigits == 3 || NDigits == 5)) PipValue = 10;
   
    CheckAccountLosses18();
    PrintInfoToChart20();
   
}

void CheckAccountLosses18()
{
    if (InitialBalance18 - AccountBalance() < MaximumLoss18)
    {
        CustomIf30();
       
    }
}

void CustomIf30()
{
    if (OrdersTotal() <= ORDCOUNT)
    {
        TechnicalAnalysis2x31();
        TechnicalAnalysis2x36();
       
    }
}

void TechnicalAnalysis2x31()
{
    if ((Open[2] < iMA(NULL, PERIOD_H4,21,0,MODE_SMA,PRICE_LOW,2)) && (Close[2] < iMA(NULL, PERIOD_M1,21,0,MODE_SMA,PRICE_LOW,2)))
    {
        TechnicalAnalysis2x23();
       
    }
}

void TechnicalAnalysis2x23()
{
    if ((Open[1] < iMA(NULL, PERIOD_H4,21,0,MODE_SMA,PRICE_LOW,1)) && (Close[1] < iMA(NULL, PERIOD_H4,21,0,MODE_SMA,PRICE_LOW,1)))
    {
        TechnicalAnalysis2x24();
       
    }
}

void TechnicalAnalysis2x24()
{
    if ((iMA(NULL, PERIOD_H4,21,0,MODE_SMA,PRICE_LOW,1) > iMA(NULL, PERIOD_H4,8,0,MODE_EMA,PRICE_OPEN,1)) && (iADX(NULL, PERIOD_H4,14,PRICE_CLOSE,MODE_MAIN,2) < iADX(NULL, PERIOD_H4,14,PRICE_CLOSE,MODE_MAIN,1)))
    {
        TechnicalAnalysis2x25();
       
    }
}

void TechnicalAnalysis2x25()
{
    if ((iADX(NULL, PERIOD_H4,14,PRICE_CLOSE,MODE_PLUSDI,1) < 15) && (iADX(NULL, PERIOD_H4,14,PRICE_CLOSE,MODE_MINUSDI,1) > 15))
    {
        TechnicalAnalysis2x26();
       
    }
}

void TechnicalAnalysis2x26()
{
    if ((iMA(NULL, PERIOD_H4,21,0,MODE_SMA,PRICE_LOW,3) < iMA(NULL, PERIOD_H4,8,0,MODE_EMA,PRICE_OPEN,3)) && (1 == 1))
    {
        SellOrderRiskFixed41();
       
    }
}

void SellOrderRiskFixed41()
{
    double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE) / AccountLeverage();
    double pipsize = 1 * 10;
    double maxlots = AccountBalance() / 100 * BalanceRiskPercent41 / lotsize * pipsize;
    if (SellStoploss41 == 0) Print("OrderSend() error - stoploss can not be zero");
    double lots = maxlots / SellStoploss41 * 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);
    }
    double SL = Bid + SellStoploss41*PipValue*Point;
    if (SellStoploss41 == 0) SL = 0;
    double TP = Bid - SellTakeprofit41*PipValue*Point;
    if (SellTakeprofit41 == 0) TP = 0;
   
    int ticket = -1;
    if (true)
    ticket = OrderSend(Symbol(), OP_SELL, lotvalue, Bid, 4, 0, 0, "My Expert", 1, 0, Red);
    else
    ticket = OrderSend(Symbol(), OP_SELL, lotvalue, Bid, 4, SL, TP, "My Expert", 1, 0, Red);
    if (ticket > -1)
    {
        if (true)
        {
            OrderSelect(ticket, SELECT_BY_TICKET);
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Red);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
           
    }
    else
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
}

void TechnicalAnalysis2x36()
{
    if ((Open[2] > iMA(NULL, PERIOD_H4,21,0,MODE_SMA,PRICE_HIGH,2)) && (Close[2] > iMA(NULL, PERIOD_M1,21,0,MODE_SMA,PRICE_HIGH,2)))
    {
        TechnicalAnalysis2x22();
       
    }
}

void TechnicalAnalysis2x22()
{
    if ((Open[1] > iMA(NULL, PERIOD_H4,21,0,MODE_SMA,PRICE_HIGH,1)) && (Close[1] > iMA(NULL, PERIOD_H4,21,0,MODE_SMA,PRICE_HIGH,1)))
    {
        TechnicalAnalysis2x21();
       
    }
}

void TechnicalAnalysis2x21()
{
    if ((iMA(NULL, PERIOD_H4,21,0,MODE_SMA,PRICE_HIGH,1) < iMA(NULL, PERIOD_H4,8,0,MODE_EMA,PRICE_OPEN,1)) && (iADX(NULL, PERIOD_H4,14,PRICE_CLOSE,MODE_MAIN,2) < iADX(NULL, PERIOD_H4,14,PRICE_CLOSE,MODE_MAIN,1)))
    {
        TechnicalAnalysis2x34();
       
    }
}

void TechnicalAnalysis2x34()
{
    if ((iADX(NULL, PERIOD_H4,14,PRICE_CLOSE,MODE_PLUSDI,1) > 15) && (iADX(NULL, PERIOD_H4,14,PRICE_CLOSE,MODE_MINUSDI,1) < 15))
    {
        TechnicalAnalysis2x32();
       
    }
}

void TechnicalAnalysis2x32()
{
    if ((iMA(NULL, PERIOD_H4,21,0,MODE_SMA,PRICE_HIGH,3) > iMA(NULL, PERIOD_H4,8,0,MODE_EMA,PRICE_OPEN,3)) && (1 == 1))
    {
        BuyOrderRiskFixed37();
       
    }
}

void BuyOrderRiskFixed37()
{
    double lotsize = MarketInfo(Symbol(),MODE_LOTSIZE) / AccountLeverage();
    double pipsize = 1 * 10;
    double maxlots = AccountBalance() / 100 * BalanceRiskPercent37 / lotsize * pipsize;
    if (BuyStoploss37 == 0) Print("OrderSend() error - stoploss can not be zero");
    double lots = maxlots / BuyStoploss37 * 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);
    }
    double SL = Ask - BuyStoploss37*PipValue*Point;
    if (BuyStoploss37 == 0) SL = 0;
    double TP = Ask + BuyTakeprofit37*PipValue*Point;
    if (BuyTakeprofit37 == 0) TP = 0;
   
    int ticket = -1;
    if (true)
    ticket = OrderSend(Symbol(), OP_BUY, lotvalue, Ask, 4, 0, 0, "My Expert", 2, 0, Blue);
    else
    ticket = OrderSend(Symbol(), OP_BUY, lotvalue, Ask, 4, SL, TP, "My Expert", 2, 0, Blue);
    if (ticket > -1)
    {
        if (true)
        {
            OrderSelect(ticket, SELECT_BY_TICKET);
            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, Blue);
            if (ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
           
    }
    else
    {
        Print("OrderSend() error - ", ErrorDescription(GetLastError()));
    }
}

void PrintInfoToChart20()
{
    string temp = "4H \nExecuted : " + Count20 + "\n"
    + "Spread: " + DoubleToStr(MarketInfo(Symbol(), MODE_SPREAD)/PipValue, 2)+ "\n"
    + "------------------------------------------------\n"
    + "ACCOUNT INFORMATION:\n"
    + "\n"
    + "Account Name:     " + AccountName()+ "\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"
    + "------------------------------------------------\n";
    Comment(temp);
    Count20++;
   
}



int deinit()
{
    if (false) ObjectsDeleteAll();
   
   
}


/
Авторизуйтесь или зарегистрируйтесь, чтобы добавить комментарий