cm_BuySellStopLimit

Author: Copyright © 2018, Vladimir Hlystov
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open orders
0 Views
0 Downloads
0 Favorites
cm_BuySellStopLimit
ÿþ//+------------------------------------------------------------------+

//|                                                _BuyStopLimit.mq4 |

//|                                                cmillion@narod.ru |

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

#property copyright "Copyright © 2018, Vladimir Hlystov"

#property link      "cmillion@narod.ru"

#property version   "1.00"

#property strict

#property script_show_inputs

MqlTradeRequest request;  // ?0@0<5B@K B>@3>2>3> 70?@>A0

MqlTradeResult result;    // @57C;LB0B B>@3>2>3> 70?@>A0

MqlTradeCheckResult check;

ENUM_ORDER_TYPE_FILLING FillingMode=ORDER_FILLING_RETURN;

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

input int     LevelStop         = 100,    //@0AAB>O=85 4> stop >@45@0

               LevelLimit        = 500,    //@0AAB>O=85 4> limit >@45@0

               Stoploss          = 0,     

               Takeprofit        = 0;     

input double  Lot               = 0.01;   

input int      Magic             = 0;

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

void OnStart()

{

   int FILLING_MODE=(int)SymbolInfoInteger(_Symbol, SYMBOL_FILLING_MODE);

   if ((SYMBOL_FILLING_IOC & FILLING_MODE)==SYMBOL_FILLING_IOC) FillingMode=ORDER_FILLING_IOC;// A5 8;8 G0AB8G=>

   if ((SYMBOL_FILLING_FOK & FILLING_MODE)==SYMBOL_FILLING_FOK) FillingMode=ORDER_FILLING_FOK;//<>65B 1KBL 8A?>;=5= 8A:;NG8B5;L=> 2 C:070==>< >1J5<5

   double Price = NormalizeDouble(ChartPriceOnDropped(),_Digits);

   string txt = "";

   double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);

   double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);

   if (Bid<Price)

   {

      ObjectsDeleteAll(0,"SellStop");

      ObjectsDeleteAll(0,"BuyLimit");

      ObjectDelete(0,"SSBL");

      if (LevelStop!=0) 

      {

         txt = StrCon(txt,"  Sellstop ",DoubleToString(Price-LevelStop*_Point,_Digits));

         drawline("SellStop",clrPink,Price-LevelStop*_Point);

         drawtext("SellStop ",StrCon("SellStop ",DoubleToString(Lot,2)),iTime(NULL,0,10),Price-LevelStop*_Point);



      }

      if (LevelLimit!=0) 

      {

         txt = StrCon(txt,"  BuyLimit ",DoubleToString(Price-LevelLimit*_Point,_Digits));

         drawline("BuyLimit",clrPink,Price-LevelLimit*_Point);

         drawtext("BuyLimit ",StrCon("BuyLimit ",DoubleToString(Lot,2)),iTime(NULL,0,10),Price-LevelLimit*_Point);

      }

      drawline("SSBL",clrPink,Price);drawtext("SSBL ",txt,iTime(NULL,0,10),Price);

   }

   if (Bid>Price)

   {

      ObjectsDeleteAll(0,"BuyStop");

      ObjectsDeleteAll(0,"SellLimit");

      ObjectDelete(0,"BSSL");

      if (LevelStop!=0) 

      {

         txt = StrCon(txt,"  BuyStop ",DoubleToString(Price+LevelStop*_Point,_Digits));

         drawline("BuyStop",clrPaleTurquoise,Price+LevelStop*_Point);

         drawtext("BuyStop ",StrCon("BuyStop ",DoubleToString(Lot,2)),iTime(NULL,0,10),Price+LevelStop*_Point);

      }

      if (LevelLimit!=0) 

      {

         txt = StrCon(txt,"  SellLimit ",DoubleToString(Price+LevelLimit*_Point,_Digits));

         drawline("SellLimit",clrPaleTurquoise,Price+LevelLimit*_Point);

         drawtext("SellLimit ",StrCon("SellLimit ",DoubleToString(Lot,2)),iTime(NULL,0,10),Price+LevelLimit*_Point);

      }

      drawline("BSSL",clrPaleTurquoise,Price);drawtext("BSSL ",txt,iTime(NULL,0,10),Price);

   }

   double SL,TP;Comment("");

   while(!IsStopped())

   {

      Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);

      Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);

      int bar=(int)ChartGetInteger(0,CHART_FIRST_VISIBLE_BAR);

      double SSBL=ObjectGetDouble(0,"SSBL",OBJPROP_PRICE);

      double BuyStop=NormalizeDouble(ObjectGetDouble(0,"BuyStop",OBJPROP_PRICE),_Digits);

      double BuyLimit=NormalizeDouble(ObjectGetDouble(0,"BuyLimit",OBJPROP_PRICE),_Digits);

      double BSSL=ObjectGetDouble(0,"BSSL",OBJPROP_PRICE);

      double SellStop=NormalizeDouble(ObjectGetDouble(0,"SellStop",OBJPROP_PRICE),_Digits);

      double SellLimit=NormalizeDouble(ObjectGetDouble(0,"SellLimit",OBJPROP_PRICE),_Digits);

      Comment(TimeToString(TimeCurrent(),TIME_SECONDS)," !:@8?B >B:@KB8O\n",SellStop==0?"":(StrCon("SellStop ",DoubleToString(SellStop,_Digits)))," ",BuyLimit==0? "":(StrCon("BuyLimit ",DoubleToString(BuyLimit,_Digits))),

                             SSBL!=0?StrCon("   ?@8 4>AB865=88 ",DoubleToString(SSBL,_Digits),"\n"):"\n",

                             BuyStop==0? "":(StrCon("BuyStop ",DoubleToString(BuyStop,_Digits)))," ",SellLimit==0?"":(StrCon("SellLimit ",DoubleToString(SellLimit,_Digits))),

                             BSSL!=0?StrCon("   ?@8 4>AB865=88 ",DoubleToString(BSSL,_Digits)):"  ");

      

      if (SellStop!=0) {ObjectSetDouble(0,"SellStop ",OBJPROP_PRICE, SellStop);ObjectSetInteger(0,"SellStop ",OBJPROP_TIME,iTime(NULL,0,bar-1));}

      else ObjectDelete(0,"SellStop ");

      if (BuyLimit!=0) {ObjectSetDouble(0,"BuyLimit ",OBJPROP_PRICE, BuyLimit);ObjectSetInteger(0,"BuyLimit ",OBJPROP_TIME,iTime(NULL,0,bar-1));}

      else ObjectDelete(0,"BuyLimit ");

      if (SSBL!=0) 

      {

         ObjectSetDouble(0,"SSBL ",OBJPROP_PRICE, SSBL);

         txt=NULL;

         if (LevelStop!=0)  txt = StrCon("  Sellstop ",DoubleToString(SellStop,_Digits));

         if (LevelLimit!=0) txt = StrCon(txt,"  BuyLimit ",DoubleToString(BuyLimit,_Digits));

         ObjectSetString(0,"SSBL ",OBJPROP_TEXT, StrCon("Bid=",DoubleToString(SSBL,_Digits),txt));

         ObjectSetInteger(0,"SSBL ",OBJPROP_TIME,iTime(NULL,0,bar));

        if (Bid>=SSBL)

         {

            Comment(TimeToString(TimeCurrent(),TIME_SECONDS)," F5=0 4>AB83;0 ",DoubleToString(SSBL,_Digits)," >B:@K205<",SellStop!=0?StrCon(" SellStop ",DoubleToString(SellStop,_Digits)):"",BuyLimit!=0?StrCon(" BuyLimit ",DoubleToString(BuyLimit,_Digits)):"");

            if (SellStop!=0)

            {

               if (Takeprofit!=0) TP = NormalizeDouble(SellStop - Takeprofit * _Point,_Digits); else TP=0;

               if (Stoploss!=0)   SL = NormalizeDouble(SellStop + Stoploss   * _Point,_Digits); else SL=0;

               SendOrder(ORDER_TYPE_SELL_STOP,SellStop,Lot,SL,TP);

               //if (OrderSend(Symbol(),OP_SELLSTOP,Lot,SellStop,0,SL,TP,"SellStop",0,0,CLR_NONE)==-1) 

                //  Print("Error ",GetLastError()," SellStop ",DoubleToString(SellStop,_Digits)," SL ",DoubleToString(SL,_Digits)," TP ",DoubleToString(TP,_Digits));

            }

            if (BuyLimit!=0)

            {

               if (Takeprofit!=0) TP = NormalizeDouble(BuyLimit + Takeprofit * _Point,_Digits); else TP=0;

               if (Stoploss!=0)   SL = NormalizeDouble(BuyLimit - Stoploss   * _Point,_Digits); else SL=0;

               SendOrder(ORDER_TYPE_BUY_LIMIT,BuyLimit,Lot,SL,TP);

               //if (OrderSend(Symbol(),ORDER_TYPE_BUY_LIMIT,Lot,BuyLimit,0,SL,TP,"BuyLimit",0,0,CLR_NONE)==-1) 

               //   Print("Error ",GetLastError()," BuyLimit ",DoubleToString(BuyLimit,_Digits)," SL ",DoubleToString(SL,_Digits)," TP ",DoubleToString(TP,_Digits));

            }

            ObjectDelete(0,"SSBL");

         }

      }

      else {ObjectDelete(0,"SellStop");ObjectDelete(0,"BuyLimit");ObjectDelete(0,"SSBL ");}

      

      

      if (BuyStop!=0) {ObjectSetDouble(0,"BuyStop ",OBJPROP_PRICE, BuyStop); ObjectSetInteger(0,"BuyStop ",OBJPROP_TIME,iTime(NULL,0,bar-1));}

      else ObjectDelete(0,"BuyStop ");

      if (SellLimit!=0) {ObjectSetDouble(0,"SellLimit ",OBJPROP_PRICE, SellLimit);ObjectSetInteger(0,"SellLimit ",OBJPROP_TIME,iTime(NULL,0,bar-1));}

      else ObjectDelete(0,"SellLimit ");

      if (BSSL!=0) 

      {

         ObjectSetDouble(0,"BSSL ",OBJPROP_PRICE, BSSL);

         txt=NULL;

         if (LevelStop!=0)  txt = StrCon(txt,"  BuyStop ",DoubleToString(BuyStop,_Digits));

         if (LevelLimit!=0) txt = StrCon(txt,"  SellLimit ",DoubleToString(SellLimit,_Digits));

         ObjectSetString(0,"BSSL ",OBJPROP_TEXT, StrCon("Bid=",DoubleToString(BSSL,_Digits),txt));

         ObjectSetInteger(0,"BSSL ",OBJPROP_TIME,iTime(NULL,0,bar-1));

         if (Bid<=BSSL)

         {

            Comment(TimeToString(TimeCurrent(),TIME_SECONDS)," F5=0 4>AB83;0 ",DoubleToString(BSSL,_Digits)," >B:@K205<",BuyStop!=0?StrCon(" BuyStop ",DoubleToString(BuyStop,_Digits)):"",SellLimit!=0?StrCon(" SellLimit ",DoubleToString(SellLimit,_Digits)):"");

            if (BuyStop!=0)

            {

               if (Takeprofit!=0) TP = NormalizeDouble(BuyStop + Takeprofit * _Point,_Digits); else TP=0;

               if (Stoploss!=0)   SL = NormalizeDouble(BuyStop - Stoploss   * _Point,_Digits); else SL=0;

               SendOrder(ORDER_TYPE_BUY_STOP,BuyStop,Lot,SL,TP);

               //if (OrderSend(Symbol(),OP_BUYSTOP,Lot,BuyStop,0,SL,TP,"BuyStop",0,0,CLR_NONE)==-1) 

               //   Print("Error ",GetLastError()," BuyStop ",DoubleToString(BuyStop,_Digits)," SL ",DoubleToString(SL,_Digits)," TP ",DoubleToString(TP,_Digits));

            }

            if (SellLimit!=0)

            {

               if (Takeprofit!=0) TP = NormalizeDouble(SellLimit - Takeprofit * _Point,_Digits); else TP=0;

               if (Stoploss!=0)   SL = NormalizeDouble(SellLimit + Stoploss   * _Point,_Digits); else SL=0;

               SendOrder(ORDER_TYPE_SELL_LIMIT,SellLimit,Lot,SL,TP);

               //if (OrderSend(Symbol(),OP_SELLLIMIT,Lot,SellLimit,0,SL,TP,"SellLimit",0,0,CLR_NONE)==-1) 

                //  Print("Error ",GetLastError()," SellLimit ",DoubleToString(SellLimit,_Digits)," SL ",DoubleToString(SL,_Digits)," TP ",DoubleToString(TP,_Digits));

            }

            ObjectDelete(0,"BSSL");

         }

      }

      else {ObjectDelete(0,"BuyStop");ObjectDelete(0,"SellLimit");ObjectDelete(0,"BSSL ");}

      if (ObjectFind(0,"BuyStop ")==-1 && ObjectFind(0,"BuyLimit ")==-1&& ObjectFind(0,"SellStop ")==-1 && ObjectFind(0,"SellLimit ")==-1)

      {

         break;

      }

      Sleep(500);

      //RefreshRates();

   }

   //Comment("!:@8?B 70:>=G8; A2>N @01>BC ",TimeToString(TimeCurrent()));

}

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

int drawline(string NameL, color col, double Y1)

{

   ObjectCreate(0,NameL, OBJ_HLINE, 0,0,Y1,0,Y1);

   ObjectSetInteger(0,NameL, OBJPROP_COLOR, col);

   ObjectSetInteger(0,NameL, OBJPROP_STYLE, STYLE_DASH);

   ObjectSetInteger(0,NameL, OBJPROP_WIDTH, 1);

   ObjectSetInteger(0,NameL, OBJPROP_BACK,  true);

   ObjectSetInteger(0,NameL, OBJPROP_SELECTED,  true);

   ObjectSetInteger(0,NameL,OBJPROP_SELECTABLE,true); 

   ObjectSetInteger(0,NameL,OBJPROP_SELECTED,true); 

   return(0);

}

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

bool drawtext(const string            name="Text",              // 8<O >1J5:B0 

                const string            text="Text",              // A0< B5:AB 

                datetime                time=0,                   // 2@5<O B>G:8 ?@82O7:8 

                double                  price=0,                  // F5=0 B>G:8 ?@82O7:8 

                const color             clr=clrRed,               // F25B 

                const int               sub_window=0,             // =><5@ ?>4>:=0 

                const string            font="Arial",             // H@8DB 

                const int               font_size=8,             // @07<5@ H@8DB0 

                const double            angle=0.0,                // =0:;>= B5:AB0 

                const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_LOWER,          // 2K45;8BL 4;O ?5@5<5I5=89 

                const bool              back=false,               // =0 704=5< ?;0=5 

                const bool              selection=false,          // 2K45;8BL 4;O ?5@5<5I5=89 

                const bool              hidden=true,              // A:@KB 2 A?8A:5 >1J5:B>2 

                const long              z_order=0)                // ?@8>@8B5B =0 =060B85 <KHLN 

  {

   long              chart_ID=0;

   if (ObjectFind(0,name)==-1)

   {

      if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price)) 

        { 

         return(false); 

        } 

   }

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text); 

   ObjectSetString(chart_ID,name,OBJPROP_FONT,font); 

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size); 

   ObjectSetInteger(chart_ID,name,OBJPROP_TIME,time); 

   ObjectSetDouble(chart_ID,name,OBJPROP_PRICE,price); 

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle); 

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor); 

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); 

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); 

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); 

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); 

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); 

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); 

   return(true); 

  } 

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

string StrCon(string t1=NULL,string t2=NULL,string t3=NULL,string t4=NULL,string t5=NULL,string t6=NULL,string t7=NULL,string t8=NULL,string t9=NULL,string t10=NULL,

              string t11=NULL,string t12=NULL,string t13=NULL,string t14=NULL,string t15=NULL,string t16=NULL,string t17=NULL,string t18=NULL,string t19=NULL,string t20=NULL,

              string t21=NULL,string t22=NULL)

{

   string str;

   #ifdef __MQL4__ str=StringConcatenate(#endif  

   #ifdef __MQL5__ StringConcatenate(str,#endif t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22);

   return(str);

}

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

uint SendOrder(ENUM_ORDER_TYPE Tip, double Price, double lt, double sl, double tp)

{

   ZeroMemory(request);

   request.type_filling = FillingMode;

   request.action = TRADE_ACTION_PENDING;

   request.magic  = Magic;

   request.symbol = _Symbol;

   request.volume = lt;

   request.sl = sl;

   request.tp = tp;

   request.type=Tip;

   request.comment=NULL;

   request.price=Price;

   request.type_time=ORDER_TIME_GTC;

   request.expiration=0;



   if(OrderCheck(request,check))

   {

      if(!OrderSend(request,result))

         PrintFormat("Order Send error %d",GetLastError());

      Print(__FUNCTION__,":",result.comment," SL ",DoubleToString(sl,_Digits)," TP ",DoubleToString(tp,_Digits)); 

      if(result.retcode==10016) Print(result.bid,result.ask,result.price); 

   }

   return result.retcode;

}

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

bool IsNewOrderAllowed()

{

   int max_allowed_orders=(int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);

   if(max_allowed_orders==0) return(true);

   if(OrdersTotal()<max_allowed_orders) return(true);

   return(false);

}

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

double CheckVolumeValue(double volume)

{

   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(volume<min_volume) return(min_volume);

   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(volume>max_volume) return(max_volume);

   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);//--- 3@040F8O >1J5<0

   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001) return(ratio*volume_step);

   return(volume);

}

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

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