script_cm_open_2_stoporders

Author: Copyright 2016, cmillion@narod.ru
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open orders
0 Views
0 Downloads
0 Favorites
script_cm_open_2_stoporders
//+------------------------------------------------------------------+
//|                                  script_cm_open_2_stoporders.mq4 |
//|                                Copyright 2016, cmillion@narod.ru |
//|                                               http://cmillion.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, cmillion@narod.ru"
#property link      "http://cmillion.ru"
#property version   "1.00"
#property strict
#property show_inputs
#property description "Ñêðèïò îòêðûòèÿ ïðîòèâîïîëîæíûõ ñòîï îðäåðîâ â óêàçàííîå âðåìÿ"
#property description "Ïîñëå ñðàáàòûâàíèÿ îäíîãî, âòîðîé óäàëÿåòñÿ"
/*--------------------------------------------------------------------
 îïðåäåëåííîå âðåìÿ(çàäà¸òñÿ â ïàðàìåòðàõ) âûñòàâëÿþòñÿ 
äâà îòëîæåííûõ îðäåðà áàéñòîï è ñåëñòîï íà ðàññòîÿíèè â ïóíêòàõ (çàäàåòñÿ â 
ïàðàìåòðàõ) îò òåêóùåé öåíû, ñ TP è SL â ïóíêòàõ (çàäàåòñÿ â ïàðàìåòðàõ). 
Ïîñëå ñðàáàòûâàíèÿ îäíîãî èç îðäåðîâ, ïðîòèâîïîëîæíûé óäàëÿåòñÿ.
Äàëåå ñêðèïò çàêàí÷èâàåò ñâîþ ðàáîòó.
//--------------------------------------------------------------------*/
extern int      level          = 10,       //ðàññòîÿíèå îò òåêóùåé öåíû äî îðäåðîâ
stoploss       = 50,       //óðîâåíü âûñòàâëåíèÿ SL, åñëè 0, òî SL íå âûñòàâëÿåòñÿ
takeprofit     = 50,       //óðîâåíü âûñòàâëåíèÿ TP, åñëè 0, òî TP íå âûñòàâëÿåòñÿ
Magic          = 0;        //óíèêàëüíûé íîìåð îðäåðà
extern double   Lot            = 0.1;      //îáúåì îðäåðîâ
extern datetime TimeSet=D'2016.01.06 01:08'; //Âðåìÿ âûñòàâëåíèÿ îðäåðîâ, åñëè òåêóùåå âðåìÿ áîëüøå óñòàíîâëåííîãî, òî âûñòàâëÿþòñÿ ñðàçó
int slippage=3;
double STOPLEVEL;
//--------------------------------------------------------------------
void OnStart()
  {
   if(Digits==3 || Digits==5) {level*=10;stoploss*=10;takeprofit*=10;slippage*=10;}
   while(TimeCurrent()<TimeSet)
     {
      if(IsStopped()) {Comment("ïðèíóäèòåëüíîå çàêðûòèå ñêðèïòà ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));return;}
      Sleep(1000);
      Comment(TimeToStr(TimeCurrent(),TIME_SECONDS),"\nÑêðèïò îòêðûòèÿ ïðîòèâîïîëîæíûõ ñòîï îðäåðîâ â ",
              TimeToStr(TimeSet,TIME_DATE|TIME_SECONDS),"\n",WindowExpertName(),"\n",
              "Äî âûñòàâëåíèÿ ïîçèöèé îñòàëîñü ",TimeToStr(TimeSet-TimeCurrent(),TIME_SECONDS));
     }
//---
   string txt=NULL;
   RefreshRates();
   STOPLEVEL=MarketInfo(Symbol(),MODE_STOPLEVEL);
   if(level<STOPLEVEL) level=(int)STOPLEVEL;
   double SL,TP,Price=NormalizeDouble(Ask+level*Point,Digits);
   if(takeprofit!=0)
     {
      if(takeprofit<STOPLEVEL) TP=NormalizeDouble(Price+STOPLEVEL*Point,Digits);
      else  TP=NormalizeDouble(Price+takeprofit*Point,Digits);
     }
   else TP=0;
   if(stoploss!=0)
     {
      if(stoploss<STOPLEVEL) SL=NormalizeDouble(Price-STOPLEVEL*Point,Digits);
      else  SL=NormalizeDouble(Price-stoploss*Point,Digits);
     }
   else SL=0;
   if(OrderSend(Symbol(),OP_BUYSTOP,Lot,Price,slippage,SL,TP,NULL,Magic,0,clrBlue)!=-1)
      txt=StringConcatenate(txt,"Îðäåð BUYSTOP óñïåøíî âûñòàâëåí ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS),"\n");
   else txt=StringConcatenate(txt,"\nError ",GetLastError(),"  OPENORDER BUYSTOP     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,"\n");

   Price=NormalizeDouble(Bid-level*Point,Digits);
   if(takeprofit!=0)
     {
      if(takeprofit<STOPLEVEL) TP=NormalizeDouble(Price-STOPLEVEL*Point,Digits);
      else  TP=NormalizeDouble(Price-takeprofit*Point,Digits);
     }
   else TP=0;
   if(stoploss!=0)
     {
      if(stoploss<STOPLEVEL) SL=NormalizeDouble(Price+STOPLEVEL*Point,Digits);
      else  SL=NormalizeDouble(Price+stoploss*Point,Digits);
     }
   else SL=0;
   if(OrderSend(Symbol(),OP_SELLSTOP,Lot,Price,slippage,SL,TP,NULL,Magic,0,clrRed)!=-1)
      txt=StringConcatenate(txt,"Îðäåð SELLSTOP óñïåøíî âûñòàâëåí ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS),"\n");
   else txt=StringConcatenate(txt,"\nError ",GetLastError(),"  OPENORDER SELLSTOP     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,"\n");
   Comment(txt);

//---
   while(true)
     {
      if(IsStopped()) {Comment("ïðèíóäèòåëüíîå çàêðûòèå ñêðèïòà ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));return;}
      int TicketB=0,TicketS=0,OT,j;
      for(j=OrdersTotal(); j>=0; j--)
        {
         if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES))
           {
            OT=OrderType();
            if(OrderSymbol()==Symbol() && OT>1 && OrderMagicNumber()==Magic)
              {
               if(OT==OP_BUYSTOP) TicketB=OrderTicket();
               if(OT==OP_SELLSTOP) TicketS=OrderTicket();
              }
           }
        }
      if(TicketB>0 && TicketS==0) if(OrderDelete(TicketB)) break;
      if(TicketS>0 && TicketB==0) if(OrderDelete(TicketS)) break;
      if(TicketS==0 && TicketB==0) break;
      Comment("Ñêðèïò îòêðûòèÿ ïðîòèâîïîëîæíûõ ñòîï îðäåðîâ\n",WindowExpertName(),"\n",txt,
              "Æäåì îòêðûòèÿ îäíîãî èç îðäåðîâ ",TimeToStr(TimeCurrent(),TIME_SECONDS));
      Sleep(1000);
     }
   Comment("Ñêðèïò \"",WindowExpertName(),"\" çàêîí÷èë ñâîþ ðàáîòó ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
  }
//--------------------------------------------------------------------

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