NegotiationPanel

Author: Rafael Floriani Pinto
Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
NegotiationPanel
//+------------------------------------------------------------------+
//|                                             NegotiationPanel.mq5 |
//|                                            Rafael Floriani Pinto |
//|                           https://www.mql5.com/pt/users/rafaelfp |
//+------------------------------------------------------------------+
#property copyright "Rafael Floriani Pinto"
#property link      "https://www.mql5.com/pt/users/rafaelfp"
#property version   "1.00"
#include<Trade/Trade.mqh>
#include<ClassControlPanel.mqh>
CTrade Operation;
CControlPainel ObjName(0,0.25,0.75,5,5,CORNER_LEFT_UPPER);

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   ObjName.CreatePanel();
   ObjName.CreateText("Ask: 0.0 Bid: 0.0",clrWhite,9);
   ObjName.CreateText("StopLoss Points",clrRed,10);
   ObjName.CreateText("0",clrRed,10,false);
   ObjName.CreateText("TakeProfit Points",clrGreen,10);
   ObjName.CreateText("0",clrGreen,10,false);
   ObjName.CreateText("Volume",clrWhite,10);
   ObjName.CreateText("0",clrWhite,10,false);
   ObjName.CreateButton("Buy at market",clrBlack,clrGreen);
   ObjName.CreateButton("Sell at market",clrBlack,clrRed);
   ObjName.CreateText("Poits to open order",clrWhite,10);
   ObjName.CreateText("0",clrWhite,10,false);
   ObjName.CreateButton("Buy limit",clrBlack,clrGreen);
   ObjName.CreateButton("Sell limit",clrBlack,clrRed);
   ObjName.CreateButton("Buy stop",clrBlack,clrGreen);
   ObjName.CreateButton("Sell stop",clrBlack,clrRed);
   ObjName.CreateButton("Close All",clrBlack,clrBlue);
   ObjName.CreateText("Position result: 0.0",clrWhite);

   return(INIT_SUCCEEDED);
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjName.DeletePanel();
  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   double Ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   double Bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
   string AskS=DoubleToString(Ask,_Digits);
   string BidS=DoubleToString(Bid,_Digits);
   ObjName.TextModifyString(1,TEXT_TEXTSHOW,"Ask: "+AskS+" Bid: "+BidS);


   double Result=GetPositionResult();
   string SResult=DoubleToString(Result,2);
   ObjName.TextModifyString(10,TEXT_TEXTSHOW,"Position result: "+SResult);
   if(Result>0)
     {
      ObjName.TextModifyInteger(10,TEXT_FONTCOLOR,clrGreen);
      return;
     }
   if(Result<0)
     {
      ObjName.TextModifyInteger(10,TEXT_FONTCOLOR,clrRed);
     }
   else
     {
      ObjName.TextModifyInteger(10,TEXT_FONTCOLOR,clrWhite);
     }


  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long& lparam,
                  const double& dparam,
                  const string& sparam
                 )
  {

   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      if(ObjName.ButtonGetState(1))
        {
         string TP=ObjName.TextGetString(3);
         string SL=ObjName.TextGetString(5);
         string Vol=ObjName.TextGetString(7);
         int PointsTP=(int)StringToInteger(TP);
         int PointsSL=(int)StringToInteger(SL);
         double Volume=(double)StringToDouble(Vol);
         BuyAtMarket(Volume,PointsTP,PointsSL);
         ObjName.ButtonSetState(1,false);
        }

      if(ObjName.ButtonGetState(2))
        {
         string TP=ObjName.TextGetString(3);
         string SL=ObjName.TextGetString(5);
         string Vol=ObjName.TextGetString(7);
         int PointsTP=(int)StringToInteger(TP);
         int PointsSL=(int)StringToInteger(SL);
         double Volume=(double)StringToDouble(Vol);
         SellAtMarket(Volume,PointsTP,PointsSL);
         ObjName.ButtonSetState(2,false);
        }
      if(ObjName.ButtonGetState(3))
        {
         string TP=ObjName.TextGetString(3);
         string SL=ObjName.TextGetString(5);
         string Vol=ObjName.TextGetString(7);
         string Alv=ObjName.TextGetString(9);
         int PointsTP=(int)StringToInteger(TP);
         int PointsSL=(int)StringToInteger(SL);
         int AlvI=(int)StringToInteger(Alv);
         double Volume=(double)StringToDouble(Vol);
         BuyLimit(AlvI,Volume,PointsTP,PointsSL);
         ObjName.ButtonSetState(3,false);
        }
      if(ObjName.ButtonGetState(4))
        {
         string TP=ObjName.TextGetString(3);
         string SL=ObjName.TextGetString(5);
         string Vol=ObjName.TextGetString(7);
         string Alv=ObjName.TextGetString(9);
         int PointsTP=(int)StringToInteger(TP);
         int PointsSL=(int)StringToInteger(SL);
         int AlvI=(int)StringToInteger(Alv);
         double Volume=(double)StringToDouble(Vol);
         SellLimit(AlvI,Volume,PointsTP,PointsSL);
         ObjName.ButtonSetState(4,false);
        }
      if(ObjName.ButtonGetState(5))
        {
         string TP=ObjName.TextGetString(3);
         string SL=ObjName.TextGetString(5);
         string Vol=ObjName.TextGetString(7);
         string Alv=ObjName.TextGetString(9);
         int PointsTP=(int)StringToInteger(TP);
         int PointsSL=(int)StringToInteger(SL);
         int AlvI=(int)StringToInteger(Alv);
         double Volume=(double)StringToDouble(Vol);
         BuyStop(AlvI,Volume,PointsTP,PointsSL);
         ObjName.ButtonSetState(5,false);
        }
      if(ObjName.ButtonGetState(6))
        {
         string TP=ObjName.TextGetString(3);
         string SL=ObjName.TextGetString(5);
         string Vol=ObjName.TextGetString(7);
         string Alv=ObjName.TextGetString(9);
         int PointsTP=(int)StringToInteger(TP);
         int PointsSL=(int)StringToInteger(SL);
         int AlvI=(int)StringToInteger(Alv);
         double Volume=(double)StringToDouble(Vol);
         SellStop(AlvI,Volume,PointsTP,PointsSL);
         ObjName.ButtonSetState(6,false);
        }

      if(ObjName.ButtonGetState(7))
        {
         CancelAllOrdersAndPositions();
         ObjName.ButtonSetState(7,false);
        }

     }

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyAtMarket(double Volume,int TPPoints,int SLPoints)
  {
   if(Volume<=0 || TPPoints<=0 || SLPoints<=0)
      return;
   double TP=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double SL=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double Price=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   TP=NormalizeDouble(Price+TP,_Digits);
   SL=NormalizeDouble(Price-SL,_Digits);
   Operation.Buy(Volume,_Symbol,Price,SL,TP);

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellAtMarket(double Volume,int TPPoints,int SLPoints)
  {
   if(Volume<=0 || TPPoints<=0 || SLPoints<=0)
      return;
   double TP=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double SL=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double Price=SymbolInfoDouble(_Symbol,SYMBOL_BID);
   TP=NormalizeDouble(Price-TP,_Digits);
   SL=NormalizeDouble(Price+SL,_Digits);
   Operation.Sell(Volume,_Symbol,Price,SL,TP);

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyLimit(int Into,double Volume,int TPPoints,int SLPoints)
  {
   if(Volume<=0 || TPPoints<=0 || SLPoints<=0 || Into<=0)
      return;
   double TP=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double SL=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double Alv=Into*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);;
   double Price=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   Price=NormalizeDouble(Price-Alv,_Digits);
   TP=NormalizeDouble(Price+TP,_Digits);
   SL=NormalizeDouble(Price-SL,_Digits);
   Operation.BuyLimit(Volume,Price,_Symbol,SL,TP);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellLimit(int Into,double Volume,int TPPoints,int SLPoints)
  {
   if(Volume<=0 || TPPoints<=0 || SLPoints<=0 || Into<=0)
      return;
   double TP=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double SL=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double Alv=Into*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);;
   double Price=SymbolInfoDouble(_Symbol,SYMBOL_BID);
   Price=NormalizeDouble(Price+Alv,_Digits);
   TP=NormalizeDouble(Price-TP,_Digits);
   SL=NormalizeDouble(Price+SL,_Digits);
   Operation.SellLimit(Volume,Price,_Symbol,SL,TP);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyStop(int Into,double Volume,int TPPoints,int SLPoints)
  {
   if(Volume<=0 || TPPoints<=0 || SLPoints<=0 || Into<=0)
      return;
   double TP=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double SL=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double Alv=Into*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);;
   double Price=SymbolInfoDouble(_Symbol,SYMBOL_BID);
   Price=NormalizeDouble(Price+Alv,_Digits);
   TP=NormalizeDouble(Price+TP,_Digits);
   SL=NormalizeDouble(Price-SL,_Digits);
   Operation.BuyStop(Volume,Price,_Symbol,SL,TP);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellStop(int Into,double Volume,int TPPoints,int SLPoints)
  {
   if(Volume<=0 || TPPoints<=0 || SLPoints<=0 || Into<=0)
      return;
   double TP=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double SL=TPPoints*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
   double Alv=Into*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);;
   double Price=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
   Price=NormalizeDouble(Price-Alv,_Digits);
   TP=NormalizeDouble(Price-TP,_Digits);
   SL=NormalizeDouble(Price+SL,_Digits);
   Operation.SellStop(Volume,Price,_Symbol,SL,TP);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CancelAllOrdersAndPositions()
  {
   int N=PositionsTotal();
   ulong Ticket;
   for(int i=N-1; i>=0; i--)
     {
      Ticket=PositionGetTicket(i);
      Operation.PositionClose(Ticket);
     }
   N=OrdersTotal();
   for(int i=N-1; i>=0; i--)
     {
      Ticket=OrderGetTicket(i);
      Operation.OrderDelete(Ticket);
     }

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double GetPositionResult()
  {
   double temp=0;
   int N=PositionsTotal();
   ulong Ticket;
   for(int i=N-1; i>=0; i--)
     {
      Ticket=PositionGetTicket(i);
      temp+=PositionGetDouble(POSITION_PROFIT);
     }
   return temp;

  }
//+------------------------------------------------------------------+

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