EA_Stop_Order

Author: Copyright © 2018, Хлыстов Владимир
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
EA_Stop_Order
ÿþ#property copyright "Copyright © 2018, %;KAB>2 ;048<8@"

#property link      "cmillion@narod.ru"

#property strict

#property description "!>25B=8: @0A:@K205B A5BL 87 AB>? >@45@>2"

#property description "?@8 =01>@5 7040==>3> C@>2=O ?@81K;8 70:@K205B 2A5 >@45@0."

//--------------------------------------------------------------------

extern double  ProfitClose          = 1;        //?@81K;L 2 20;NB5 45?>78B0 ?@8 :>B>@>9 ?@>8AE>48B 70:@KB85 >@45@>2

extern bool    SELL                 = true;     //>B:@KBL >@45@ SELLSTOP

extern bool    BUY                  = true;     //>B:@KBL >@45@ BUYSTOP

extern int     MaxOrders            = 20;       //:>;-2> >@45@>2 A5B:8

extern int     stoploss             = 0;        //C@>25=L 2KAB02;5=8O SL, 5A;8 0, B> SL =5 2KAB02;O5BAO

extern int     takeprofit           = 0;        //C@>25=L 2KAB02;5=8O TP, 5A;8 0, B> TP =5 2KAB02;O5BAO

extern int     delta                = 100;      //@0AAB>O=85 >B B5:CI59 F5=K

extern double  Lot                  = 0.1;      //>1J5< >@45@0

extern double  K_Lot                = 1.5;      //C25;8G5=85 ;>B0 A H03><

extern int     attempts             = 10;       //:>;-2> ?>?KB>: 70:@KB8O

extern bool    CloseLoc             = true;     //70:@K20BL >@45@0 ?@8 ?>;=>< ;>:5

extern int     slippage             = 30;       //?@>A:0;L7K20=85

extern int     Magic                = 123456;   //C=8:0;L=K9 =><5@ >@45@0

//--------------------------------------------------------------------

string txt;

int n=0,STOPLEVEL;

//--------------------------------------------------------------------

int OnInit()

{

   Comment("0?CA: A>25B=8:0 EA Stop Order ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));

   if (Digits==3 || Digits==5) slippage=30;

   return(INIT_SUCCEEDED);

}

//--------------------------------------------------------------------

void OnTick()

{

   double Profit=0;

   int i,r=0,tip;

   n=0;

   for (i=0; i<OrdersTotal(); i++)

   {    

      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

      { 

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

         { 

            tip = OrderType(); 

            if (tip<2) {r++;Profit+=OrderProfit()+OrderSwap()+OrderCommission();}

            else n++;

         }

      }

   } 

   //---

   Comment("Balance  ",AccountBalance(),"\nEquity  ",AccountEquity(),"\nOrders  ",n,"(",r,")  Profit  ",Profit);

   if (Profit>ProfitClose) CLOSEORDER();

   if (CloseLoc && r==MaxOrders*2) CLOSEORDER();

   //---

   if (n>0 || Profit!=0) return;

   //---

   STOPLEVEL=(int)MarketInfo(Symbol(),MODE_STOPLEVEL);

   double Price;

   double LOT=Lot;

   for (i=1; i<=MaxOrders; i++)

   {

      if (BUY)

      {

         Price = NormalizeDouble(Ask+delta*i*Point,Digits);

         OPENORDER (1,Price,LOT,i);

      }

      if (SELL)

      {  

         Price = NormalizeDouble(Bid-delta*i*Point,Digits);

         OPENORDER (-1,Price,LOT,i);

      }

      LOT=LOT*K_Lot;

   }

}

//--------------------------------------------------------------------

void OnDeinit(const int reason)

{

   Comment("!>25B=8: 70:>=G8; A2>N @01>BC ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));

}

//--------------------------------------------------------------------

void OPENORDER(int ord,double Price,double LOT,int i)

{

   int error,err=0;

   double SL=0,TP=0;

   txt=NULL;

   while (!IsStopped())

   {  error=true;

      if (ord==1 ) 

      {

         if (takeprofit!=0) TP  = NormalizeDouble(Price + takeprofit*Point,Digits); else TP=0;

         if (stoploss!=0)   SL  = NormalizeDouble(Price - stoploss*Point,Digits); else SL=0;     

         error=OrderSend(Symbol(),OP_BUYSTOP, LOT,Price,slippage,SL,TP,"http://cmillion.ru",Magic,0,Blue);

      }

      if (ord==-1) 

      {

         if (takeprofit!=0) TP = NormalizeDouble(Price - takeprofit*Point,Digits); else TP=0;

         if (stoploss!=0)   SL = NormalizeDouble(Price + stoploss*Point,Digits);  else SL=0;              

         error=OrderSend(Symbol(),OP_SELLSTOP,LOT,Price,slippage,SL,TP,"http://cmillion.ru",Magic,0,Red);

      }

      if (error==-1)

      {  

         txt=StringConcatenate(txt,"\nError ",GetLastError());

         if (ord== 1) txt = StringConcatenate(txt,"  OPENORDER BUYSTOP ",i,"   Ask =",DoubleToStr(Ask,Digits),"   Price =",DoubleToStr(Price,Digits)," (",NormalizeDouble((Price-Ask)/Point,0),")  SL =",DoubleToStr(SL,Digits)," (",NormalizeDouble((Price-SL)/Point,0),")  TP=",DoubleToStr(TP,Digits)," (",NormalizeDouble((TP-Price)/Point,0),")  STOPLEVEL=",STOPLEVEL);

         if (ord==-1) txt = StringConcatenate(txt,"  OPENORDER SELLSTOP ",i,"   Bid =",DoubleToStr(Bid,Digits),"   Price =",DoubleToStr(Price,Digits)," (",NormalizeDouble((Bid-Price)/Point,0),")  SL =",DoubleToStr(SL,Digits)," (",NormalizeDouble((SL-Price)/Point,0),")  TP=",DoubleToStr(TP,Digits)," (",NormalizeDouble((Price-TP)/Point,0),")  STOPLEVEL=",STOPLEVEL);

         Print(txt);

         Comment(txt,"  ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));

         err++;Sleep(1000);RefreshRates();

      }

      else 

      {

         Comment("@45@ ",error," CA?5H=> 2KAB02;5= ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));

         return;

      }

      n++;

      if (err >attempts) return;

   }

return;

}                  

//--------------------------------------------------------------------

bool CLOSEORDER()

{

   bool error=true;

   int j,err,nn=0,OT;

   while (true)

   {

      for (j = OrdersTotal()-1; j >= 0; j--)

      {

         if (OrderSelect(j, SELECT_BY_POS))

         {

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

            {

               OT = OrderType();

               if (OT>1) error=OrderDelete(OrderTicket());

               if (OT==OP_BUY) 

               {

                  error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),slippage,Blue);

                  if (error) Comment("0:@KB >@45@ N ",OrderTicket(),"  ?@81K;L ",OrderProfit(),

                                     "     ",TimeToStr(TimeCurrent(),TIME_SECONDS));

               }

               if (OT==OP_SELL) 

               {

                  error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),slippage,Red);

                  if (error) Comment("0:@KB >@45@ N ",OrderTicket(),"  ?@81K;L ",OrderProfit(),

                                     "     ",TimeToStr(TimeCurrent(),TIME_SECONDS));

               }

               if (!error) 

               {

                  err = GetLastError();

                  if (err<2) continue;

                  if (err==129) 

                  {  Comment("5?@028;L=0O F5=0 ",TimeToStr(TimeCurrent(),TIME_SECONDS));

                     RefreshRates();

                     continue;

                  }

                  if (err==146) 

                  {

                     if (IsTradeContextBusy()) Sleep(2000);

                     continue;

                  }

                  Comment("H81:0 ",err," 70:@KB8O >@45@0 N ",OrderTicket(),

                          "     ",TimeToStr(TimeCurrent(),TIME_SECONDS));

               }

            }

         }

      }

      n=0;

      for (j = 0; j < OrdersTotal(); j++)

      {

         if (OrderSelect(j, SELECT_BY_POS))

         {

            if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) n++;

         }  

      }

      if (n==0) return(1);

      nn++;

      if (nn>10) {Alert(Symbol()," 5 C40;>AL 70:@KBL 2A5 A45;:8, >AB0;>AL 5I5 ",n);return(0);}

      Sleep(1000);

      RefreshRates();

   }

   return(1);

}

//------------------------------------------------------------------



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