VirtualOpenSell

Author: TheXpert
Miscellaneous
It opens Message Boxes to the user
0 Views
0 Downloads
0 Favorites
VirtualOpenSell
//+------------------------------------------------------------------+
//|                                              VirtualOpenSell.mq4 |
//|                         https://login.mql5.com/ru/users/TheXpert |
//+------------------------------------------------------------------+
#property copyright "TheXpert"
#property link      "https://login.mql5.com/ru/users/TheXpert"

#property show_inputs
extern color         SelectionColor    = Red;

extern double Lots = 0.5;

#define IDOK 1
#define IDCANCEL 2
 
bool Confirmed(string action)
{
   int result = MessageBox("Do you really want to\n" + action + "?", "Please confirm", IDOK);
   return (result == IDOK);
}

void SetHLine(string name, double price, color clr = Red, int style = 0, int width = 2)
{
   ObjectCreate(name, OBJ_HLINE, 0, 0, price); 
   
   ObjectSet(name, OBJPROP_PRICE1, price);
   ObjectSet(name, OBJPROP_STYLE, style);
   ObjectSet(name, OBJPROP_COLOR, clr);
   ObjectSet(name, OBJPROP_WIDTH, width);
}
 
///=========================================================================
/// Misc
///=========================================================================
 
string ID()
{
   static string result;
   if (StringLen(result) == 0) result = "ZZCEA_sell";
   result = StringSetChar(result, 0, 255);
   
   return (result);
}
 
///=========================================================================
/// DND Implementation
///=========================================================================
 
bool CheckDrag(string name)
{
   double x = ObjectGet(name, OBJPROP_PRICE1);
   if (GetLastError() != 0) return (false);
   
   if (ObjectSet(name, OBJPROP_PRICE1, x*1.1))
   {
      ObjectSet(name, OBJPROP_PRICE1, x);
      return (false);
   }
   
   return (true);
}
 
bool CheckDrop(string name)
{
   double x = ObjectGet(name, OBJPROP_PRICE1);
   if (GetLastError() != 0) return (true);
   
   if (ObjectSet(name, OBJPROP_PRICE1, x*1.1))
   {
      ObjectSet(name, OBJPROP_PRICE1, x);
      return (true);
   }
   
   return (false);
}
 
 
bool IsDragging = false;
string DragName = "";

bool CanFinish = false;
 
void OnDropped(string name)
{
   IsDragging = false;

   if (name != ID() + "price") return;
   
   double p = ObjectGet(name, OBJPROP_PRICE1);
   
   if (p >= Bid)
   {
      if (Confirmed(" open " + DoubleToStr(Lots, 2) + " sell at " + DoubleToStr(p, Digits)))
      {
         CanFinish = true;
         GlobalVariableSet("_VIRTUAL_C_open_1" + DoubleToStr(Lots, 2), NormalizeDouble(p, Digits));
      }
   }
}
 
///=========================================================================
/// Implementation
///=========================================================================

int init()
{
   IsDragging = false;
   DragName = "";
}
 
int start()
{
   while (!IsStopped() && !CanFinish)
   {
      if (IsDragging)
      {
         if (CheckDrop(DragName))
         {
            OnDropped(DragName);
         }
      }
      else
      {
         double p = (WindowPriceMax() + WindowPriceMin())/2.0;
         SetHLine(ID() + "price", p, SelectionColor);
         
         if (CheckDrag(ID() + "price")) 
         {
            DragName = ID() + "price";
            IsDragging = true;
         }
      }
   
      WindowRedraw();
   
      Sleep(50);
   }
   
   ObjectDelete(ID() + "price");

   return(0);
}

Comments