Pending orders UP Single SL and TP

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Pending orders UP Single SL and TP
ÿþ//+------------------------------------------------------------------+

//|                           Pending orders UP Single SL and TP.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.000"

#property description "The script sets the pending orders up from the price"

#property description "Single SL and TP and Deviation ..."

#property description "... in Points (1.00045-1.00055=10 points)"

#property script_show_inputs

//---

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>

CTrade         m_trade;                      // trading object

CSymbolInfo    m_symbol;                     // symbol info object

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

//| Enum pending orders UP                                           |

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

enum ENUM_PENDING_ORDERS_UP

  {

   buy_stop=1,    // Buy Stop

   sell_limit=2   // Sell Limit

  };

//--- input parameters

input ushort                     InpUpGep          = 100;            // Gap for pending orders UP from the current price

input ushort                     InpUpStep         = 50;             // Step between orders UP (in pips)

input ENUM_PENDING_ORDERS_UP     InpUpOrders       = buy_stop;       // Type of pending orders UP

input uchar                      InpUpQuantity     = 5;              // UP quantity

input double                     InpLots           = 0.1;            // Lots

input ushort                     InpStopLoss       = 50;             // Stop Loss

input ushort                     InpTakeProfit     = 50;             // Take Profit

input ulong                      InpDeviation      = 30;             // Deviation

input ulong                      InpMagic          = 200;            // Magic number

//---

double   ExtUpGep=0.0;

double   ExtUpStep=0.0;

double   ExtStopLoss=0.0;

double   ExtTakeProfit=0.0;

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

//| Script program start function                                    |

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

void OnStart()

  {

//---

   if(InpLots<=0.0)

     {

      Print("The \"Lots\" can't be smaller or equal to zero");

      return;

     }

//---

   if(!m_symbol.Name(Symbol())) // sets symbol name

      return;

   if(!RefreshRates())

      return;

   string err_text="";

   if(!CheckVolumeValue(InpLots,err_text))

     {

      Print(err_text);

      return;

     }

//---

   m_trade.SetExpertMagicNumber(InpMagic);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(m_symbol.Name());

   m_trade.SetDeviationInPoints(InpDeviation);

   m_trade.SetAsyncMode(true);

//---

   ExtUpGep       = InpUpGep     * m_symbol.Point();

   ExtUpStep      = InpUpStep    * m_symbol.Point();

   ExtStopLoss    = InpStopLoss  * m_symbol.Point();

   ExtTakeProfit  = InpTakeProfit* m_symbol.Point();

//--- start work

   double start_price_ask=m_symbol.Ask()+ExtUpGep;

   double start_price_bid=m_symbol.Bid()+ExtUpGep;

//--- pending orders UP

   double single_sl  = 0.0;

   double single_tp  = 0.0;

   if(InpUpOrders==buy_stop)

     {

      single_sl = (ExtStopLoss==0.0)   ? 0.0 : start_price_ask - ExtStopLoss;

      single_tp = (ExtTakeProfit==0.0) ? 0.0 : start_price_ask + ExtUpStep*((double)(InpUpQuantity-1)) + ExtTakeProfit;

     }

   else

     {

      single_sl = (ExtStopLoss==0.0)   ? 0.0 : start_price_bid + ExtUpStep*((double)(InpUpQuantity-1)) + ExtTakeProfit;

      single_tp = (ExtTakeProfit==0.0) ? 0.0 : start_price_bid - ExtStopLoss;

     }

   for(int i=0; i<InpUpQuantity; i++)

     {

      double price_ask     = start_price_ask+i*ExtUpStep;

      double price_bid     = start_price_bid+i*ExtUpStep;

      if(InpUpOrders==buy_stop)

        {

         m_trade.BuyStop(InpLots,m_symbol.NormalizePrice(price_ask),m_symbol.Name(),

                         m_symbol.NormalizePrice(single_sl),

                         m_symbol.NormalizePrice(single_tp));

        }

      else

        {

         m_trade.SellLimit(InpLots,m_symbol.NormalizePrice(price_bid),m_symbol.Name(),

                           m_symbol.NormalizePrice(single_sl),

                           m_symbol.NormalizePrice(single_tp));

        }

     }

  }

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

//| Refreshes the symbol quotes data                                 |

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

bool RefreshRates()

  {

//--- refresh rates

   if(!m_symbol.RefreshRates())

     {

      Print("RefreshRates error");

      return(false);

     }

//--- protection against the return value of "zero"

   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)

      return(false);

//---

   return(true);

  }

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

//| Check the correctness of the order volume                        |

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

bool CheckVolumeValue(double volume,string &error_description)

  {

//--- minimal allowed volume for trade operations

   double min_volume=m_symbol.LotsMin();

   if(volume<min_volume)

     {

      error_description=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);

      return(false);

     }

//--- maximal allowed volume of trade operations

   double max_volume=m_symbol.LotsMax();

   if(volume>max_volume)

     {

      error_description=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);

      return(false);

     }

//--- get minimal step of volume changing

   double volume_step=m_symbol.LotsStep();

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

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

     {

      error_description=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",

                                     volume_step,ratio*volume_step);

      return(false);

     }

   error_description="Correct volume value";

   return(true);

  }

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

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