How I assemble my advisor by trial and error - page 34

 

figured out how to correctly prescribe the risk

#define  Magic_Number 0
//+------------------------------------------------------------------+
//| Enum Lor or Risk                                                 |
//+------------------------------------------------------------------+
enum ENUM_LOT_OR_RISK
  {
   lots=0,     // Constant lot
   risk=1,     // Risk in percent
  };
//+------------------------------------------------------------------+


input double   MaximumRisk             = 0.01;          // Maximum Risk in percentage
input double   DecreaseFactor          = 3;             // Descrease factor
input ENUM_LOT_OR_RISK InpLotOrRisk    = lots;          // Money management: Lot OR Risk
   //+------------------------------------------------------------------+
   //| Calculate optimal lot size                                       |
   //+------------------------------------------------------------------+
   double            TradeSizeOptimized(void)
     {
      double price=0.0;
      double margin=0.0;
      //--- select lot size
      if(!SymbolInfoDouble(_Symbol,SYMBOL_ASK,price))
         return(0.0);
      if(!OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,1.0,price,margin))
         return(0.0);
      if(margin<=0.0)
         return(0.0);
      double lot=0;
      //---
      switch(InpLotOrRisk)
        {
         case lots:
            lot=MaximumRisk;
            break;
         case risk:
            lot=NormalizeDouble(AccountInfoDouble(ACCOUNT_MARGIN_FREE)*MaximumRisk/margin,2);
         default:
            break;
        }
      //--- calculate number of losses orders without a break
      if(DecreaseFactor>0)
        {
         //--- select history for access
         HistorySelect(0,TimeCurrent());
         //---
         int    orders=HistoryDealsTotal();  // total history deals
         int    losses=0;                    // number of losses orders without a break

         for(int i=orders-1; i>=0; i--)
           {
            ulong ticket=HistoryDealGetTicket(i);
            if(ticket==0)
              {
               Print("HistoryDealGetTicket failed, no trade history");
               break;
              }
            //--- check symbol
            if(HistoryDealGetString(ticket,DEAL_SYMBOL)!=_Symbol)
               continue;
            //--- check Expert Magic number
            if(HistoryDealGetInteger(ticket,DEAL_MAGIC)!=Magic_Number)
               continue;
            //--- check profit
            double profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
            if(profit>0.0)
               break;
            if(profit<0.0)
               losses++;
           }
         //---
         if(losses>1)
            lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
        }
      //--- normalize and check limits
      double stepvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
      lot=stepvol*NormalizeDouble(lot/stepvol,0);

      double minvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
      if(lot<minvol)
         lot=minvol;

      double maxvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);
      if(lot>maxvol)
         lot=maxvol;
      //--- return trading volume
      return(lot);

     }
   //+------------------------------------------------------------------+

where lot - insertTradeSizeOptimized()

I need to correct it in Expert Advisor - otherwise it is not correct there

 

renamed - expert completed. all errors corrected. Title < Horse move ><Horse move

Horse move

Files:
Horse_move.mq5  190 kb
 


Aleksandr Klapatyuk:

Added to the previous Expert, two methods for the Horizontal line.

1 possibility : line 1 will open line 4 at a given distance, line 2 will open line 3 at a given distance.

2nd possibility: line 7 will open a line 10 at a given distance, which moves behind the price and when the price touches it, a command will trigger. line 8 will open a line 9 at a given distance, - the same action as for 7 and 10.

#property version "1.01"

There is another option -line 1 will open 4 and 9.line 2 will open 3 and 10.

line 7 will open 10 and 3 .line 8 will open 9 and4

input string   t3="------ Obj:Name 1-2-3-4 ------";     // Имя Объекта
input string   InpObjUpNameZ           = "TOP 1";       // Obj: TOP (Name Obj) ВВЕРХУ 1
input string   InpObjDownNameZ         = "LOWER 2";     // Obj: LOWER (Name Obj) ВНИЗУ 2
input int      Step                    = 5;             // Obj: Шаг сетки, пунктов(0 = false)
input string   InpObjDownName0         = "TOP 3";       // Obj: TOP (Name Obj) ВВЕРХУ 3
input ENUM_TRADE_COMMAND InpTradeCommand=open_sell;     // Obj:  command:
input string   InpObjUpName0           = "LOWER 4";     // Obj: LOWER (Name Obj) ВНИЗУ 4
input ENUM_TRADE_COMMAND InpTradeCommand0=open_buy;     // Obj:  command:
input string   t5="- 2_Obj:Trailing Line 7-8-9-10 --- ";// Trailing Obj:Line
input string   InpObjUpNameZx          = "TOP 7";       // Obj: TOP (Name Obj) ВВЕРХУ 7
input string   InpObjDownNameZx        = "LOWER 8";     // Obj: LOWER (Name Obj) ВНИЗУ 8
input int      StepZx                  = 5;             // Obj: Шаг сетки, пунктов(0 = false)
input string   InpObjUpNameX           = "TOP 9";       // Obj: TOP (Horizontal Line) ВВЕРХУ 9
input ENUM_TRADE_COMMAND InpTradeCommandX=open_buy;     // Obj:  command:
input string   InpObjDownNameX         = "LOWER 10";    // Obj: LOWER (Horizontal Line) ВНИЗУ 10
input ENUM_TRADE_COMMAND InpTradeCommand0X=open_sell;   // Obj:  command:
input ushort   InpObjTrailingStopX     = 5;             // Obj: Trailing Stop (distance from price to object, in pips)
input ushort   InpObjTrailingStepX     = 5;             // Obj: Trailing Step, in pips (1.00045-1.00055=1 pips)

to open the positions on the reverse and do not touch the settingsObj: command:

there is a reversal -

input string   t6="------ Obj: Revers Buy and Sell --"; // Obj: Revers Buy and Sell
input bool     ObjRevers               = false;         // Obj: Revers
Files:
Horse_move.mq5  190 kb
2.mq5  17 kb
 
Aleksandr Klapatyuk:

#property version "1.01"

It turned out to be another possibility -line 1 will open 4 and 9. line 2 will open 3 and 10.

line 7 will open 10 and 3 . line 8 will open 9 and 4

to open the positions in reverse and do not touch the settingsObj: command:

there is a reversal -

i have now started the test - i will not do anything till the end of the month, i wonder what will happen. - lot, i have set it to risk - it will somehow calculate

test

 

Not bad! Slowly - but it's getting better

test1

 

Yes! It's going straight to the top.

Alpari MT5

 
Aleksandr Klapatyuk:

renamed - expert completed. all errors corrected. Title < Horse move ><Horse move

Interesting Expert Advisor.

 

setinput double TargetProfit = 30000.00;// Target profit

probably not enough - should have been more. now we need to wait again, entry points and set the TargetProfit = 35000.00;// Target Profit

//+------------------------------------------------------------------+
input string   t0="------ Parameters --------";         // Настройка Эксперта
input string   Template                = "ADX";         // Имя шаблона(without '.tpl')
input datetime HoursFrom               = D'1970.01.01'; // Время старта Эксперта
input datetime HoursTo                 = D'2030.12.31'; // Время закрытия всех позиций
input double   TargetProfit            = 900000.00;     // Целевая прибыль
input uint     maxLimits               = 1;             // Кол-во Позиции Открыть в одну сторону
input double   MaximumRisk             = 0.01;          // Maximum Risk in percentage
input double   DecreaseFactor          = 3;             // Descrease factor
input ENUM_LOT_OR_RISK InpLotOrRisk    = lots;          // Money management: Lot OR Risk

opens positions from the indicator pivot lines timezone.mq5 from the line R2 - down S2 - up

Snapshot1

the indicator may not be deleted - its lines are updated the next day

Alpari MT5

the indicator is below - now I will search the website for the link to the indicatorhttps://www.mql5.com/ru/code/1114

Files:
 
Aleksandr Klapatyuk:

setinput double TargetProfit = 30000.00; // Target profit

probably not enough - should have been more. now we need to wait again, entry points and set the TargetProfit = 35000.00; // Target Profit

opens positions from the indicator pivot lines timezone.mq5 from the line R2 - down S2 - up

the indicator may not be deleted - its lines are updated the next day

the indicator below - now searching on the website for the link to the indicatorhttps://www.mql5.com/ru/code/1114

this is one of the options. but there are a million options

I forgot to say about the lot - set to risk input ENUM_LOT_OR_RISK InpLotOrRisk = lots;// Money management: Lot OR Risk

(expert information in .log) which warnings are issued https://www.mql5.com/ru/docs/event_handlers/ondeinit
Документация по MQL5: Обработка событий / OnDeinit
Документация по MQL5: Обработка событий / OnDeinit
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Возвращает текстовое описания причины деинициализации            |
Files:
20191107.log  272 kb
 

#property version "1.02"

found another way for the buttons

//+------------------------------------------------------------------+
//| Enum ENUM_BUTTON                                                 |
//+------------------------------------------------------------------+
enum ENUM_BUTTON
  {
   Button0=0,  // ВЫКЛ
   Button1=1,  // ВКЛ
   Button2=2,  // ВКЛ: AVGiS
  };
//+------------------------------------------------------------------+

here we select

input string   t9="------ Button: AVGiS -----";         // AVGiS (Или обычный режим Buy/Sell)
input ENUM_BUTTON Buttons              = Button0;       // Вкл: Копки Buy/Sell
input bool     ObjectLineX             = false;         // Button: Horizontal Line(true) || Buy/Sell(false)
input bool     TickRevers              = false;         // Button: Revers
input int      TrailingStop_STOP_LEVEL = 350;           // Trailing Stop LEVEL
input ENUM_TIMEFRAMES _period          = PERIOD_CURRENT;// period
input int      limit_total_symbol      = 190;           // limit_total_symbol
input int      limit_total             = 190;           // limit_total
//---

on position opening - stop loss is set immediately (yellow horizontal line)

adjusted here -input double InpStopLoss = 55;// Obj: Stop Loss, in pips (1.00045-1.00055=1 pips)

deactivate set 0

input string   t2="------ Obj:Trailing Line     --- ";  // Trailing Obj:Line
input double   InpStopLoss             = 55;            // Obj: Stop Loss, in pips (1.00045-1.00055=1 pips)
input ushort   InpObjTrailingStop      = 27;            // Obj: Trailing Stop (distance from price to object, in pips)
input ushort   InpObjTrailingStep      = 9;             // Obj: Trailing Step, in pips (1.00045-1.00055=1 pips)
Files:
Horse_move.mq5  198 kb
2.mq5  17 kb