Author: sanyooooook
Orders Execution
It automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategy
0 Views
0 Downloads
0 Favorites
OrderSend
//+------------------------------------------------------------------+
//|                                                    OrderSend.mq4 |
//|                                                      sanyooooook |
//|                                                 http://sn-bot.ru |
//+------------------------------------------------------------------+
#property copyright "sanyooooook"
#property link      "http://sn-bot.ru"
#property version   "1.00"
#property strict

extern double L=0.1;//Lots
int slippage=3;
extern double StopLoss=50;//Stop Loss(in pips)
extern double TakeProfit=50;//Take Profit (in pips)
extern int Magik=0;//Magic number
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   OpenOrders(_Symbol,OP_BUY,Ask,L,slippage,StopLoss,TakeProfit,Magik,"","",false);
  }
//+------------------------------------------------------------------+
int OpenOrders(string Symb,//Symbol
               int cmd,//Type
               double OpenPrice,
               double lot,
               int slip,//Slippage
               double SL,//StopLoss
               double TP,//TakeProfit
               int MG,//Magik
               string PP,//Comment Log
               string coment,//CommentOrder
               bool OpenStops=true)//ECN - false
  {
   Print("Send: ","Symb=",Symb,", cmd=",cmd,", OpenPrice=",OpenPrice,", lot=",lot,", slip=",slip,", SL=",SL,", TP=",TP,", MG=",MG,", PP=",PP,", coment=",coment,", OpenStops=",OpenStops);
   double PriceSL=0;
   double PriceTP=0;
   int ticket=-1;
   lot=NL(Symb,lot);//NormalizeLot
   double Point_=MarketInfo(Symb,MODE_POINT);
   if(lot>MarketInfo(Symb,MODE_MAXLOT))
     {
      Print("lot > MAXLOT!");return(-1);
     }
   if(lot<MarketInfo(Symb,MODE_MINLOT))
     {
      Print("lot < MINLOT!");return(-1);
     }
   if(FreeEquity(Symb,lot)==(-1))return(-1);//check the availability of the free margin for opening an order
   int err=0;
   RefreshRates();
   int StopLevel=int (MarketInfo(Symb,MODE_STOPLEVEL));
   if(SL<StopLevel&&SL!=0)SL=StopLevel+1;
   if(TP<StopLevel&&TP!=0)TP=StopLevel+1;
   double Ask_=n(MarketInfo(Symb,MODE_ASK),Symb),Bid_=n(MarketInfo(Symb,MODE_BID),Symb);
   Print("Ask=",Ask_,", Bid=",Bid_,", STOPLEVEL=",StopLevel);
   switch(cmd)
     {
      case OP_SELL:
        {
         TP=TP*(-1);
         if(SL>0||SL<0)PriceSL=n(Bid_+SL*Point_,Symb);else PriceSL=0;
         if(TP>0||TP<0)PriceTP=n(Bid_+TP*Point_,Symb);else PriceTP=0;
         if(OpenStops)
           {
            ticket=OrderSend(Symb,cmd,lot,Bid_,slip,PriceSL,PriceTP,coment,MG);err=GetLastError();
            if(ticket>=0)
              {
               Print("Open SELL "+string(ticket));return(ticket);
              }
              } else {ticket=OrderSend(Symb,cmd,lot,Bid_,slip,0,0,coment,MG);err=GetLastError();if(ticket>=0)Print("Open SELL "+string(ticket));
           }
         break;
        }
      case OP_BUY:
        {
         SL=SL*(-1);
         if(SL>0||SL<0)PriceSL=n(Ask_+SL*Point_,Symb); else PriceSL=0;
         if(TP>0||TP<0)PriceTP=n(Ask_+TP*Point_,Symb); else PriceTP=0;
         if(OpenStops)
           {
            ticket=OrderSend(Symb,cmd,lot,Ask_,slip,PriceSL,PriceTP,coment,MG);err=GetLastError();
            if(ticket>=0)
              {
               Print("Open BUY "+string(ticket));return(ticket);
              }
              } else {ticket=OrderSend(Symb,cmd,lot,Ask_,slip,0,0,coment,MG);err=GetLastError();if(ticket>=0)Print("Open BUY "+string(ticket));
           }
         break;
        }
      case OP_BUYLIMIT:
        {
         SL=SL*(-1);
         if(SL>0||SL<0)PriceSL=n(OpenPrice+SL*Point_,Symb); else PriceSL=0;
         if(TP>0||TP<0)PriceTP=n(OpenPrice+TP*Point_,Symb); else PriceTP=0;
         if(OpenStops)
           {
            ticket=OrderSend(Symb,cmd,lot,OpenPrice,slip,PriceSL,PriceTP,coment,MG);err=GetLastError();
            if(ticket>=0)
              {
               Print("Open BUYLIMIT "+string(ticket));return(ticket);
              }
              } else {ticket=OrderSend(Symb,cmd,lot,OpenPrice,slip,0,0,coment,MG);err=GetLastError();if(ticket>=0)Print("Open BUYLIMIT "+string(ticket));
           }
         break;
        }
      case OP_BUYSTOP:
        {
         SL=SL*(-1);
         if(SL>0||SL<0)PriceSL=n(OpenPrice+SL*Point_,Symb); else PriceSL=0;
         if(TP>0||TP<0)PriceTP=n(OpenPrice+TP*Point_,Symb); else PriceTP=0;
         if(OpenStops)
           {
            ticket=OrderSend(Symb,cmd,lot,OpenPrice,slip,PriceSL,PriceTP,coment,MG);err=GetLastError();
            if(ticket>=0)
              {
               Print("Open BUYSTOP "+string(ticket));return(ticket);
              }
              } else {ticket=OrderSend(Symb,cmd,lot,OpenPrice,slip,0,0,coment,MG);err=GetLastError();if(ticket>=0)Print("Open BUYSTOP "+string(ticket));
           }
         break;
        }
      case OP_SELLLIMIT:
        {
         TP=TP*(-1);
         if(SL>0||SL<0)PriceSL=n(OpenPrice+SL*Point_,Symb); else PriceSL=0;
         if(TP>0||TP<0)PriceTP=n(OpenPrice+TP*Point_,Symb); else PriceTP=0;
         if(OpenStops)
           {
            ticket=OrderSend(Symb,cmd,lot,OpenPrice,slip,PriceSL,PriceTP,coment,MG);err=GetLastError();
            if(ticket>=0)
              {
               Print("Open SELLLIMIT "+string(ticket));return(ticket);
              }
              } else {ticket=OrderSend(Symb,cmd,lot,OpenPrice,slip,0,0,coment,MG);err=GetLastError();if(ticket>=0)Print("Open SELLLIMIT "+string(ticket));
           }
         break;
        }
      case OP_SELLSTOP:
        {
         TP=TP*(-1);
         if(SL>0||SL<0)PriceSL=n(OpenPrice+SL*Point_,Symb); else PriceSL=0;
         if(TP>0||TP<0)PriceTP=n(OpenPrice+TP*Point_,Symb); else PriceTP=0;
         if(OpenStops)
           {
            ticket=OrderSend(Symb,cmd,lot,OpenPrice,slip,PriceSL,PriceTP,coment,MG);err=GetLastError();
            if(ticket>=0)
              {
               Print("Open SELLSTOP "+string(ticket));return(ticket);
              }
              } else {ticket=OrderSend(Symb,cmd,lot,OpenPrice,slip,0,0,coment,MG);err=GetLastError();if(ticket>=0)Print("Open SELLSTOP "+string(ticket));
           }
         break;
        }
     }
   if(ticket<=0)
     {
      Print("error open #",err,PP);
     }
   else
     {
      if(TP==0&&SL==0)return(ticket);
      if(OrderSelect(ticket,SELECT_BY_TICKET))
        {
         RefreshRates();
         if(PriceTP==OrderTakeProfit()&&PriceSL==OrderStopLoss()) return(ticket);
         else
           {
            if(OrderModify(OrderTicket(),OrderOpenPrice(),PriceSL,PriceTP,0)==false)
              {
               Print("error modify#",GetLastError(),PP);
              }
            else Print("Order #",OrderTicket(),", successfully modified.");
           }
        }
     }
   return(ticket);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int FreeEquity(string Symb,double lot)
  {
   int res=0;
   if(MarketInfo(Symb,MODE_MARGINREQUIRED)*lot<AccountFreeMargin())
     {
      Print(MarketInfo(Symb,MODE_MARGINREQUIRED)*lot," ",AccountEquity());
     }
   else
     {
      Print("Insufficient funds for the opening position of the volume");
      res=(-1);
     }
   return(res);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double NL(string Sym,double lots)
  {
   double step=MarketInfo(Sym,MODE_LOTSTEP);
   return (MathRound(lots/step)*step);
  }
//+------------------------------------------------------------------+
double n(double v,string S)
  {return(NormalizeDouble(v,int(MarketInfo(S,MODE_DIGITS))));}
//+------------------------------------------------------------------+

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