cm_BuySellStopLimit

Orders Execution
It automatically opens orders when conditions are reached
1 Views
0 Downloads
0 Favorites
cm_BuySellStopLimit
ÿþ//+------------------------------------------------------------------+

//|                                                _BuyStopLimit.mq4 |

//|                                                cmillion@narod.ru |

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

#property copyright "Copyright © 2018, Vladimir Hlystov http://cmillion.ru/"

#property link      "cmillion@narod.ru"

#property version   "1.00"

#property strict

#property script_show_inputs

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

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

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

               Stoploss          = 0,     

               Takeprofit        = 0;     

extern double  Lot               = 0.01;   

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

void OnStart()

{

   double Price = NormalizeDouble(WindowPriceOnDropped(),Digits);

   string txt = "";

   if (Bid<Price)

   {

      ObjectsDeleteAll(0,"SellStop");

      ObjectsDeleteAll(0,"BuyLimit");

      ObjectDelete(0,"SSBL");

      if (LevelStop!=0) 

      {

         txt = StringConcatenate(txt,"  Sellstop ",DoubleToStr(Price-LevelStop*Point,Digits));

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

         drawtext("SellStop ",StringConcatenate("SellStop ",DoubleToStr(Lot,2)),Time[10],Price-LevelStop*Point);

      }

      if (LevelLimit!=0) 

      {

         txt = StringConcatenate(txt,"  BuyLimit ",DoubleToStr(Price-LevelLimit*Point,Digits));

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

         drawtext("BuyLimit ",StringConcatenate("BuyLimit ",DoubleToStr(Lot,2)),Time[10],Price-LevelLimit*Point);

      }

      drawline("SSBL",clrPink,Price);drawtext("SSBL ",txt,Time[10],Price);

   }

   if (Bid>Price)

   {

      ObjectsDeleteAll(0,"BuyStop");

      ObjectsDeleteAll(0,"SellLimit");

      ObjectDelete(0,"BSSL");

      if (LevelStop!=0) 

      {

         txt = StringConcatenate(txt,"  BuyStop ",DoubleToStr(Price+LevelStop*Point,Digits));

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

         drawtext("BuyStop ",StringConcatenate("BuyStop ",DoubleToStr(Lot,2)),Time[10],Price+LevelStop*Point);

      }

      if (LevelLimit!=0) 

      {

         txt = StringConcatenate(txt,"  SellLimit ",DoubleToStr(Price+LevelLimit*Point,Digits));

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

         drawtext("SellLimit ",StringConcatenate("SellLimit ",DoubleToStr(Lot,2)),Time[10],Price+LevelLimit*Point);

      }

      drawline("BSSL",clrPaleTurquoise,Price);drawtext("BSSL ",txt,Time[10],Price);

   }

   double SL,TP;

   while(!IsStopped())

   {

      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("!:@8?B >B:@KB8O ",BuyStop==0? "":(StringConcatenate("BuyStop ",DoubleToStr(BuyStop,Digits)))  ," ",BuyLimit==0? "":(StringConcatenate("BuyLimit ",DoubleToStr(BuyLimit,Digits))),

                             " ",SellStop==0?"":(StringConcatenate("SellStop ",DoubleToStr(SellStop,Digits)))," ",SellLimit==0?"":(StringConcatenate("SellLimit ",DoubleToStr(SellLimit,Digits))),

                             "   ?@8 4>AB865=88 ",SSBL==0?DoubleToStr(BSSL,Digits):DoubleToStr(SSBL,Digits),"  ",TimeToStr(TimeCurrent(),TIME_SECONDS));

      

      if (SellStop!=0) ObjectSetDouble(0,"SellStop ",OBJPROP_PRICE, SellStop);

      else ObjectDelete(0,"SellStop ");

      if (BuyLimit!=0) ObjectSetDouble(0,"BuyLimit ",OBJPROP_PRICE, BuyLimit);

      else ObjectDelete(0,"BuyLimit ");

      if (SSBL!=0) 

      {

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

         ObjectSetString(0,"SSBL ",OBJPROP_TEXT, StringConcatenate("Bid=",DoubleToStr(SSBL,Digits),txt));

        if (Bid>=SSBL)

         {

            Comment("F5=0 4>AB83;0 ",DoubleToStr(SSBL,Digits)," >B:@K205<",SellStop!=0?" SellStop":"",BuyLimit!=0?" BuyLimit":"");

            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;

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

                  Print("Error ",GetLastError()," SellStop ",DoubleToStr(SellStop,Digits)," SL ",DoubleToStr(SL,Digits)," TP ",DoubleToStr(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;

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

                  Print("Error ",GetLastError()," BuyLimit ",DoubleToStr(BuyLimit,Digits)," SL ",DoubleToStr(SL,Digits)," TP ",DoubleToStr(TP,Digits));

            }

            ObjectDelete(0,"SSBL");

         }

      }

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

      

      

      if (BuyStop!=0) ObjectSetDouble(0,"BuyStop ",OBJPROP_PRICE, BuyStop);

      else ObjectDelete(0,"BuyStop ");

      if (SellLimit!=0) ObjectSetDouble(0,"SellLimit ",OBJPROP_PRICE, SellLimit);

      else ObjectDelete(0,"SellLimit ");

      if (BSSL!=0) 

      {

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

         ObjectSetString(0,"BSSL ",OBJPROP_TEXT, StringConcatenate("Bid=",DoubleToStr(BSSL,Digits),txt));

         if (Bid<=BSSL)

         {

            Comment("F5=0 4>AB83;0 ",DoubleToStr(BSSL,Digits)," >B:@K205<",BuyStop!=0?" BuyStop":"",SellLimit!=0?" SellLimit":"");

            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;

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

                  Print("Error ",GetLastError()," BuyStop ",DoubleToStr(BuyStop,Digits)," SL ",DoubleToStr(SL,Digits)," TP ",DoubleToStr(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;

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

                  Print("Error ",GetLastError()," SellLimit ",DoubleToStr(SellLimit,Digits)," SL ",DoubleToStr(SL,Digits)," TP ",DoubleToStr(TP,Digits));

            }

            ObjectDelete(0,"BSSL");

         }

      }

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

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

      {

         break;

      }

      Sleep(500);

      RefreshRates();

   }

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

}

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

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

{

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

   ObjectSet   (NameL, OBJPROP_COLOR, col);

   ObjectSet   (NameL, OBJPROP_STYLE, STYLE_DASH);

   ObjectSet   (NameL, OBJPROP_WIDTH, 1);

   ObjectSet   (NameL, OBJPROP_BACK,  true);

   ObjectSet   (NameL, OBJPROP_SELECTED,  true);

   return(0);

}

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

int drawtext(string NameL, string info, datetime X1, double Y1,color clr=clrGray)

{

   if (info=="") 

   {

      info=StringConcatenate(StringSubstr(NameL,6,0),"  lot ",DoubleToStr(Lot,2));

      if (Stoploss!=0) info=StringConcatenate(info,"  SL " ,DoubleToStr(Stoploss ,0));

      if (Takeprofit!=0) info=StringConcatenate(info,"  TP " ,DoubleToStr(Takeprofit ,0));

   }

   ObjectDelete (NameL);

   ObjectCreate (NameL, OBJ_TEXT,0,X1,Y1,0,0,0,0);

   ObjectSetText(NameL, info ,8,"Arial");

   ObjectSet    (NameL, OBJPROP_ANCHOR, ANCHOR_LOWER);

   ObjectSet    (NameL, OBJPROP_COLOR, clr);

   return(0);

}

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



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