Author: cnhhhh
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
BatchHH
ÿþ//+------------------------------------------------------------------+

//|                           Copyright 2023, cnhhhh Software Corp. |

//|                                          http://www.cnhhhh.com/ |

//+------------------------------------------------------------------+

// My first mq4 code, please contact me if you have some problem.

// Good Luck!!

#property strict 

#property copyright "cnhhhh"

#property link      "http://www.cnhhhh.com"



//----

extern int TakeProfit=800;

extern int TakeStop=400;

extern double Lots=0.2;

//extern double MaximumRisk=10;

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

int start()

  {

   int cnt, ticket;

   if(Bars<100)

     {

      Print("bars less than 100");

      return(0);

     }

   if(TakeProfit<10)

     {

      Print("TakeProfit less than 10");

      return(0);

     }

   string TradeSymbol=Symbol();



   double PriceH=High[1];

   double PriceL=Low[1];

   double TakeStopChange = TakeStop*Point/2;



   double BuyPrice     = Ask;

   double BuyStopLoss  = Ask - TakeStop*Point;

   double BuyTakeProfit= Ask + TakeProfit*Point;



   double SellPrice    = Bid;

   double SellStopLoss = Bid + TakeStop*Point;

   double SellTakeProfit= Bid - TakeProfit*Point;



   int total=OrdersTotal();

//----

   bool need_long =true;

   bool need_short=true;

   

   if(TradeSymbol!="XAUUSD")

   {

      return(0);

   }

   

// First update existing orders.

   if(NewBar())

     {

      for(cnt=0; cnt<total; cnt++)

        {

         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==16384)

           {

            if(OrderType()==OP_BUYSTOP)

              {

               need_long=false;

               if(BuyStopLoss-OrderStopLoss()>TakeStopChange)

                 {

                  //Print(BuyStopLoss, " ",OrderStopLoss());

                  OrderModify(OrderTicket(),BuyPrice,BuyStopLoss,BuyTakeProfit,0,Green);

                 }

              }

            if(OrderType()==OP_SELLSTOP)

              {

               need_short=false;

               if(OrderStopLoss()-SellStopLoss>TakeStopChange)

                 {

                  //Print(SellStopLoss, " ",OrderStopLoss());

                  OrderModify(OrderTicket(),SellPrice,SellStopLoss,SellTakeProfit,0,Red);

                 }

              }

            if(OrderType()==OP_BUY)

              {

               need_long=false;

               if(BuyStopLoss-OrderStopLoss()>TakeStopChange)

                 {

                  //Print(BuyStopLoss, " ",OrderStopLoss());

                  OrderModify(OrderTicket(),OrderOpenPrice(),BuyStopLoss,BuyTakeProfit,0,Green);

                  //OrderDelete(OrderTicket());

                 }

              }

            if(OrderType()==OP_SELL)

              {

               need_short=false;

               if(OrderStopLoss()-SellStopLoss>TakeStopChange)

                 {

                  //Print(SellStopLoss, " ",OrderStopLoss());

                  OrderModify(OrderTicket(),OrderOpenPrice(),SellStopLoss,SellTakeProfit,0,Red);

                  //OrderDelete(OrderTicket());

                 }



              }

           }



        }



      //if(AccountFreeMargin()<(5*lot))//NUS\Pbk$R­e

      if(OrdersTotal()>1)//NUS\Pbk$R­e

        {

         Print("We have too many lots ", AccountFreeMargin());

         return(0);

        }



      if(Bid>=PriceH)

        {

         ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,BuyPrice,0,BuyStopLoss,BuyTakeProfit,"BatchHH",16384,0,Green);

         Sleep(180000);

        }



      if(Ask<=PriceL)

        {

         ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,SellPrice,0,SellStopLoss,SellTakeProfit,"BatchHH",16384,0,Red);

         Sleep(180000);

        }

     }

     return(0);

  }



//+------------------------------------------------------------------+

//| NewBar function                                                                 |

//+------------------------------------------------------------------+

bool NewBar()

  {

   static datetime newtime = 0;

   if(newtime != Time[0])

     {

      newtime = Time[0];

      return(true);

     }

   return(false);

  }

//+------------------------------------------------------------------+

//int  OrderSend(

//string   symbol,              // ¤NfÁTÍy

//int      cmd,                 // pNUS0VSUS{|‹W

//double   volume,              // ¢‹USKbpe

//double   price,               // _ÓN÷N<h

//int      slippage,            // ¹pî]

//double   stoploss,            // bk_c

//double   takeprofit,          // bkÈv

//string   comment=NULL,        // ¢‹USèlʑ

//int      magic=0,             // ¢‹UST›{^x

//datetime expiration=0,        // öeP–

//color    arrow_color=clrNONE  // _ÓNMOn>f:y­{4Yœ˜r‚

//)



//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---