Code for Hedging MT4

 
<Deleted>
//-----------------------------------
// Open Order pending by other pending orders
//-----------------------------------


int start ()

  {

   ngày giờ tĩnh t;

   if (t == Thời gian [0]) return (0);

      double MyPoint = Point;

      if (Digits == 3 || Digits == 5) MyPoint = Point * 10;

  double Gia1, Gia2, Gia3;

  datetime Last1, Last2, Close1, Close2, Close3, Close4, Close5, Close6, Close7;

  double LoiNhuan1, LoiNhuan2, LoiNhuan3, LoiNhuan4, LoiNhuan5, LoiNhuan6, LoiNhuan7;

  int Type1, Type2, Type3, Type4, Type5, Type6, Type7;

  int Order1, Order2, Order3, Order4, Order5, Order6, Order7;

  int1, vé2, vé3, vé4, vé5, vé6, vé7;

  tổng số int; 

if (điều kiện == true)

 {

         TheStopLoss = 0;

         TheTakeProfit = 0;

         

     

         if (TP> 0) TheTakeProfit = Ask + TP * MyPoint;

         if (SL> 0) TheStopLoss = Ask-SL * MyPoint;          

         OrderSend (Symbol (), OP_BUY, Lot, Ask, Slip, NormalizeDouble (TheStopLoss, Digits), NormalizeDouble (TheTakeProfit, Digits), "vuthg0106@gmail.com", MagicNumber, 0, clrAliceBlue); 

      

         TheStopLoss = 0;

         TheTakeProfit = 0;

         if (TP> 0) TheTakeProfit = Ask- (SL-3) * MyPoint;

         if (SL> 0) TheStopLoss = Ask + TP * MyPoint; 

         OrderSend (Symbol (), OP_SELLSTOP, 3 * Lot, (Ask- (SL / 2) * MyPoint), Slip, NormalizeDouble (TheStopLoss, Digits), NormalizeDouble (TheTakeProfit, Digits), "vuthg0106@gmail.com", MagicNumber, 0, clrAliceBlue);

         t = Thời gian [0];

       

         

 } 

 // Bảo hiểm rủi ro   

 for (int cnt = OrderTotal (); cnt> = 0; cnt--)

    { 

   OrderSelect (cnt-2, SELECT_BY_POS, MODE_TRADES);

    

      Gia1 = OrderOpenPrice (); 

      Last1 = OrderOpenTime ();

      TPF1 = OrderTakeProfit ();

      LoiNhuan1 = OrderProfit ();

      Type1 = OrderType ();

      Order1 = OrderTicket ();

      Close1 = OrderCloseTime ();

      Lots1 = OrderLots ();

      

     if (Type1 == OP_BUY && Lots1 == Lot)

     { 

     OrderSelect (cnt-1, SELECT_BY_POS, MODE_TRADES);

      Type2 = OrderType ();

      Last2 = OrderOpenTime ();

      tổng = OrderTotal ();

     Gia2 = OrderOpenPrice ();

     Lots2 = OrderLots ();    

     if (Type2> 1) continue;

     

      {

    

         tổng = OrderTotal (); 

         if (OrderTotal ()> total + 1) t = Time [0]; 

         khác

        {

         TheStopLoss = 0;

         TheTakeProfit = 0;

         if (TP> 0) TheTakeProfit = Gia1 + TP * MyPoint;

         if (SL> 0) TheStopLoss = Gia1-SL * MyPoint; 

         ticket3 = OrderSend (Symbol (), OP_BUYSTOP, 2 * Lots2, (Gia1), Slip, NormalizeDouble (TheStopLoss, Digits), NormalizeDouble (TheTakeProfit, Digits), "vuthg0106@gmail.com", MagicNumber, 0, clrAliceBlue); / / đồng ý  

          nếu (vé3> 0)

          t = Thời gian [0];

         

          }     

         }

    

    }

    

 

   } 

   

 // xóa đơn hàng đang chờ xử lý      

   for (int i = OrderTotal () - 1; i> = 0; i--)

    {            

OrderSelect (i, SELECT_BY_POS, MODE_TRADES); Type2 = OrderType (); Order2 = OrderTicket ();

if (OrderSymbol () == Symbol ())

{ 

OrderSelect (i-1, SELECT_BY_POS, MODE_TRADES);

      Gia1 = OrderOpenPrice (); 

      Last1 = OrderOpenTime ();

      TPF1 = OrderTakeProfit ();

      LoiNhuan1 = OrderProfit ();

      Type1 = OrderType ();

      Order1 = OrderTicket ();

      Close1 = OrderCloseTime ();

      Lots1 = OrderLots ();

      if ((Type2 == OP_SELLSTOP && LoiNhuan1> = TPF1 && Close1 <TimeCurrent ()) || (Type2 == OP_BUYSTOP && LoiNhuan1> = TPF1 && Close1 <TimeCurrent ()))  

      {

            OrderDelete (Order2);

               

         }

      

      }

   }  

     

 

return (0);   

  }
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
For equity securities, the Depth of Market window is available, where you can see the current Buy and Sell orders. Desired direction of a trade operation, required amount and requested price are specified for each order. To obtain information...
Files:
 
Please edit your post and
use the code button (Alt+S) when pasting code

I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.

I will move your topic to the MQL4 and Metatrader 4 section.

You would do better to take the time to learn coding instead of using an EA Builder.

Part of the terms of using an EA builder are to not remove the heading, but you have removed it.

 
Keith Watford
Thank you very much. Glad to have your help
 
  1. vuthg0106:

    I'm a non-programmer, but I'm trying to code hedging as shown.

    I am having a problem like this:

    I first open a buy order and a pending order at the same time.

    When the price increases and touches the TP of the buy order, it deletes the pending order (when the pending order does not turn into an order field).

    When the price goes down to the point of a pending sell order , the pending sell order changes to a sell market order and immediately opens another pending buy order (the pending order changes to a field order while opening new pending orders. command and number). Command OrderSelec is Connecting next order)

    As in the code below I have compiled, however it does not run properly.

    Hope someone help me

    Thank you very much

    Please post only in English on this forum. Use the automatic translation tool if needed. Use simple language structure when using mechanical translation.

  2. Hedging, grid trading, same as Martingale.

    Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum 2015.02.11

    Why it won't work: Calculate Loss from Lot Pips - MQL5 programming forum 2017.07.11

  3.    ngày giờ tĩnh t;
    
       if (t == Thời gian [ 0 ]) return ( 0 );
    
       int1 , vé 2 , vé 3 , vé 4 , vé 5 , vé 6 , vé 7 ;
    
      tổng số int ; 
    
    if (điều kiện == true )
    Those lines are not valid code. Variable names and type names can not have spaces. Do not post code that will not even compile; don't waste everyone's time.

  4. EA builder, EA Builder Pro, EATree, Etasoft forex generator, Forex Strategy Builder, FOREXEADVISOR STRATEGY BUILDER, FX EA Builder, fxDreema, FxPro, Molanis, Online Forex Expert Advisor Generator, Strategy Builder FX, Strategy Quant, Visual Trader Studio, MQL5 Wizard, etc., are all the same. You will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.
    1. Since you haven't learned MQL4/5, therefor there is no common language for us to communicate.
      If we tell you what you need, you can't code it.
      If we give you the code, you don't know how to integrate it into yours.
      We are willing to HELP you when you post your attempt (using SRC) and state the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.

    2. EA builder makes bad code counting up while closing multiple orders.
      EA builder makes bad code Bars is unreliable (Max bars in chart), volume is unreliable (miss ticks.) Always use time.
      EA builder makes bad code, not adjusting for 4/5 digit brokers, TP/SL and slippage.
      EA builder makes bad code, not adjusting for ECN brokers. pre-Build 500)
      EA builder makes bad code, not checking return codes.

    3. EATree uses objects on chart to save values — not persistent storage (files or GV+Flush.) No recovery (crash/power failure.)

    4. FX EA Builder makes bad code, not checking return codes.
      FX EA Builder makes bad code, loosing open tickets on terminal restart. No recovery (crash/power failure.)
      FX EA Builder makes bad code, not adjusting stops for the spread.
      FX EA Builder makes bad code, using OrdersTotal directly.

    5. FOREXEADVISOR STRATEGY BUILDER makes bad code, non-updateing global variables.
      FOREXEADVISOR STRATEGY BUILDER makes bad code, compilation errors.
      FOREXEADVISOR STRATEGY BUILDER makes bad code, not checking return codes.

    Learn to code it, or pay someone (Freelance) someone to code it.
              Hiring to write script - General - MQL5 programming forum #1 2019.08.21

  5.    for (int i = OrderTotal () - 1; i> = 0; i--){
         OrderSelect (i, SELECT_BY_POS, MODE_TRADES); Type2 = OrderType (); Order2 = OrderTicket ();
         if (OrderSymbol () == Symbol ()){
            OrderSelect (i-1, SELECT_BY_POS, MODE_TRADES)
    When i becomes zero your second OrderSelect fails.

    Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (strict), it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum 2012.05.20
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles 25 March 2014

 
vuthg0106:
<Deleted>

This is an English language forum.

I have deleted whatever you had written in your first post. I am sure that it was originally English.

Post only in English please and that includes when quoting other people's posts. Do not quote them when they have been translated to your language.

 
position in different direction(hedging) at the same time lead to change position size
Reason: