negative_spread

Author: 1987pavlov
Orders Execution
It automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
negative_spread
//+------------------------------------------------------------------+
//|                                                      Session.mq5 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "1987pavlov"
#property link      "https://www.mql5.com"
#property version   "1.0"
//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <trade/trade.mqh>
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
input double Lots= 0.01;   //Ëîò
input int TpPips = 5000;   //Take Profit
input int SlPips = 5000;   //Stop Loss
input ENUM_ORDER_TYPE_FILLING Filling=ORDER_FILLING_RETURN;  //Ðåæèì çàïîëíåíèÿ îðäåðà
bool tradeResult=false,tradeOpened=false;;
//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
double gTickSize;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void OnInit()
  {
   gTickSize=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double bid   =SymbolInfoDouble(_Symbol,SYMBOL_BID); // Çàïðîñ çíà÷åíèÿ Bid
   double ask   =SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Çàïðîñ çíà÷åíèÿ Ask
   if(ask-bid<0 && !tradeResult) //OpenPosition
     {
      tradeResult=false;
      tradeOpened=false;
      MqlTradeRequest request={0};
      MqlTradeResult result={0};
      tradeOpened=true;
      bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
      request.action=TRADE_ACTION_DEAL;         // setting a pending order
      request.magic=68975;                      // ORDER_MAGIC
      request.symbol=_Symbol;                   // symbol
      request.volume=Lots;                      // volume in 0.1 lots
      request.sl=NormalizeDouble(bid + SlPips * gTickSize, _Digits);        // Stop Loss is not specified
      request.tp=NormalizeDouble(bid - TpPips * gTickSize, _Digits);        // Take Profit is not specified     
      request.type=ORDER_TYPE_SELL;             // order type
      request.price=bid;                        // open price
      request.type_filling=Filling;
      //--- send a trade request
      int i=PositionsTotal();//Wait openedPosition
      tradeResult=OrderSend(request,result);
      if(tradeResult) while(i==PositionsTotal())//Wait openedPosition
        {
        }
     }
//---
   if((tradeResult) && (tradeOpened)) //ClosePosition
     {
      CTrade trade;
      trade.SetTypeFilling(Filling);
      int i=PositionsTotal()-1;
      while(i>=0)
        {
         if(trade.PositionClose(_Symbol)) i--;
        }
      tradeResult=false;
     }
  }
//+------------------------------------------------------------------+

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