VisualTrading





//+------------------------------------------------------------------+
//|                                                VisualTrading.mq4 |
//|                   Dmitry Yakovlev, Russia,Omsk, WM R865705290089 |
//|                                       dmitry_yakovlev@rambler.ru |
//+------------------------------------------------------------------+
#property copyright "Dmitry Yakovlev, Russia,Omsk, WM R865705290089"
#property link      "dmitry_yakovlev@rambler.ru"

#import  "shell32.dll"           //Connect a dll (provided with Windows)             
  int ShellExecuteA(int hwnd,string Operation,string File,string Parameters,string Directory,int ShowCmd); 
#import "user32.dll"
  int MessageBoxA(int hWnd ,string lpText,string lpCaption,int uType);
#import

extern color      OpenColor=Yellow;
extern color      TakeProfitColor=Lime;
extern color      StopLossColor=Red;
extern bool       lShowTargets=true;
extern string     _donate1="Äëÿ $ áëàãîäàðíîñòåé:";
extern string     _donate2="R865705290089";

int cnt=0;
int awh[];

void init()
{
   int i=0,j=0;
   string n;
   
   CheckDonate();
   
   DeleteHistory();
   for(i=0;i<OrdersTotal();i++)
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderSymbol()==Symbol()))
   {
         n=OrderSymbol()+"_"+DoubleToStr(OrderTicket(),0)+"_"+DoubleToStr(Period(),0);
         if(ObjectFind(n+"open")==-1)
            {ObjectCreate(n+"open",OBJ_HLINE,awh[j],Time[0],OrderOpenPrice()); ObjectSet(n+"open",OBJPROP_COLOR,OpenColor);}
         
         if(OrderStopLoss()!=0 && ObjectFind(n+"stop")==-1)
            {ObjectCreate(n+"stop",OBJ_HLINE,awh[j],Time[0],OrderStopLoss()); ObjectSet(n+"stop",OBJPROP_COLOR,StopLossColor);}
         
         if(OrderTakeProfit()!=0 && ObjectFind(n+"take")==-1)
            {ObjectCreate(n+"take",OBJ_HLINE,awh[j],Time[0],OrderTakeProfit()); ObjectSet(n+"take",OBJPROP_COLOR,TakeProfitColor);}
   }

   DeleteLabels();
   ShowLabels();
   cnt=OrdersTotal();
}

void deinit()
{
   string n;
   int i=0,j=0;
   
   for(i=0;i<OrdersTotal();i++)
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderSymbol()==Symbol()))
   {
         n=OrderSymbol()+"_"+DoubleToStr(OrderTicket(),0)+"_"+DoubleToStr(Period(),0);
         if(ObjectFind(n+"open")!=-1) ObjectDelete(n+"open");
         if(ObjectFind(n+"stop")!=-1) ObjectDelete(n+"stop");
         if(ObjectFind(n+"take")!=-1) ObjectDelete(n+"take");
   }
   DeleteHistory();
   for(i=0;i<30;i++)
   {
      ObjectDelete(Symbol()+DoubleToStr(Period(),0)+"topen"+i);
      ObjectDelete(Symbol()+DoubleToStr(Period(),0)+"ttake"+i);
      ObjectDelete(Symbol()+DoubleToStr(Period(),0)+"tstop"+i);
   }
   DeleteLabels();
}

void start()
{
   string n;
   int i=0,j=0;

   if(cnt!=OrdersTotal()) init();
   
   DeleteHistory();
   DeleteLabels();

   double popen, ptake, pstop;
   for(i=0;i<OrdersTotal();i++)
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderSymbol()==Symbol()))
   {
      n=OrderSymbol()+"_"+DoubleToStr(OrderTicket(),0)+"_"+DoubleToStr(Period(),0);

      if((ObjectFind(n+"stop")==-1) && (OrderStopLoss()!=0))
      //if(MessageBox("Óáðàòü ñòîï ëîññ?","Óáðàòü?",4)==6)
      //{
      //   OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderTakeProfit(),0,CLR_NONE);
      //   ObjectDelete(n+"stop");
      //   continue;
      //}
      //else
      {
         ObjectCreate(n+"stop",OBJ_HLINE,awh[j],Time[0],OrderStopLoss()); ObjectSet(n+"stop",OBJPROP_COLOR,StopLossColor);
      }
      
      if((ObjectFind(n+"take")==-1) && (OrderTakeProfit()!=0))
      //if(MessageBox("Óáðàòü òåéê ïðîôèò?","Óáðàòü?",4)==6)
      //{
      //   OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),0,0,CLR_NONE);
      //   ObjectDelete(n+"take");
      //   continue;
      //}
      //else
      {
         ObjectCreate(n+"take",OBJ_HLINE,awh[j],Time[0],OrderTakeProfit()); ObjectSet(n+"take",OBJPROP_COLOR,TakeProfitColor);
      }
   
      if(ObjectFind(n+"open")==-1)
      if(MessageBox("Çàêðûòü îðäåð "+OrderTicket()+"?","Çàêðûòü?",4)==6)
      {
         if(OrderType()==OP_BUY) popen=Bid;
         if(OrderType()==OP_SELL) popen=Ask;
         OrderClose(OrderTicket(),OrderLots(),popen,3,CLR_NONE);
         OrderDelete(OrderTicket());
         ObjectDelete(n+"take");
         ObjectDelete(n+"stop");
         continue;
      }
      else
      {
         ObjectCreate(n+"open",OBJ_HLINE,awh[j],Time[0],OrderOpenPrice()); ObjectSet(n+"open",OBJPROP_COLOR,OpenColor);
      }
   
      popen=NormalizeDouble(ObjectGet(n+"open",OBJPROP_PRICE1),Digits);
      ptake=NormalizeDouble(ObjectGet(n+"take",OBJPROP_PRICE1),Digits);
      pstop=NormalizeDouble(ObjectGet(n+"stop",OBJPROP_PRICE1),Digits);
      //Comment("");
      if((popen!=NormalizeDouble(OrderOpenPrice(),Digits)) || (pstop!=NormalizeDouble(OrderStopLoss(),Digits)) || (ptake!=NormalizeDouble(OrderTakeProfit(),Digits)))
      {
         if((OrderType()!=OP_BUY) && (OrderType()!=OP_SELL) && (NormalizeDouble(popen,Digits)!=NormalizeDouble(OrderOpenPrice(),Digits)))
         {
            OrderModify(OrderTicket(),popen,OrderStopLoss(),OrderTakeProfit(),0,CLR_NONE);//continue;
         }
         if(((OrderType()==OP_BUY) || (OrderType()==OP_SELL)) && (NormalizeDouble(popen,Digits)!=NormalizeDouble(OrderOpenPrice(),Digits)))
         {
            ObjectSet(n+"open",OBJPROP_PRICE1,OrderOpenPrice());//continue;
         }
         if((OrderType()==OP_BUY) && (NormalizeDouble(pstop,Digits)>Bid))
         {
            ObjectSet(n+"stop",OBJPROP_PRICE1,OrderStopLoss());//continue;
         }
         if((OrderType()==OP_SELL) && (NormalizeDouble(pstop,Digits)<Ask))
         {
            ObjectSet(n+"stop",OBJPROP_PRICE1,OrderStopLoss());//continue;
         }
         
         if((NormalizeDouble(ptake,Digits)!=NormalizeDouble(OrderTakeProfit(),Digits)) && (OrderTakeProfit()!=0) && (ptake!=0))
         {
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),ptake,0,CLR_NONE);//continue;
         }
         if((NormalizeDouble(pstop,Digits)!=NormalizeDouble(OrderStopLoss(),Digits)) && (OrderStopLoss()!=0) && (pstop!=0))
         {
            OrderModify(OrderTicket(),OrderOpenPrice(),pstop,OrderTakeProfit(),0,CLR_NONE);//continue;
         }
      }
   } 
   
   ShowLabels();
   if(lShowTargets) fShowTargets();

   WindowRedraw();
}

void DeleteHistory()
{
   string n;
   int i=0,j=0;
   
   for(i=0;i<OrdersHistoryTotal();i++)
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && (OrderSymbol()==Symbol()))
   {
      n=OrderSymbol()+"_"+DoubleToStr(OrderTicket(),0)+"_"+DoubleToStr(Period(),0);

      if(ObjectFind(n+"open")!=-1) ObjectDelete(n+"open");
      if(ObjectFind(n+"stop")!=-1) ObjectDelete(n+"stop");
      if(ObjectFind(n+"take")!=-1) ObjectDelete(n+"take");
   }
}

void fShowTargets()
{
   int      i=0, prof_pts=0, ticket=0; 
   double   dist_stop_pts, dist_stop;
   double   dist_take_pts, dist_take;
   double   pr=(Ask+Bid)/2, prof=0;
   datetime CT=Time[0]+Period()*60*(WindowBarsPerChart()/20+2); // time shift
   double   CP=5*Point; // price shift
   int      FS=10; // font size
   color    prof_clr=Blue;
   string   txt="";
   //-------------------------------------------------------------
   for(i=0;i<10;i++)
   {
      ObjectDelete(Symbol()+DoubleToStr(Period(),0)+"topen"+i);
      ObjectDelete(Symbol()+DoubleToStr(Period(),0)+"ttake"+i);
      ObjectDelete(Symbol()+DoubleToStr(Period(),0)+"tstop"+i);
   }
   for(i=0;i<OrdersTotal();i++)
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   {
      // <---ñ÷èòàåì
      RefreshRates();
      prof=OrderProfit();
      if(OrderType()==OP_BUY || OrderType()==3 || OrderType()==4) 
      {
         pr=Bid;
         prof_pts=pr/Point-OrderOpenPrice()/Point;
      }
      if(OrderType()==OP_SELL || OrderType()==2 || OrderType()==5)
      {
         pr=Ask;
         prof_pts=OrderOpenPrice()/Point-pr/Point;
      }
      prof_clr=Blue;
      if(prof_pts<0) prof_clr=Red;
      if(prof_pts>0) prof_clr=Green;
      ObjectCreate(Symbol()+DoubleToStr(Period(),0)+"topen"+i,OBJ_TEXT,0,CT,OrderOpenPrice()+CP);
      txt="";
      if(OrderType()==0 || OrderType()==1) txt=" ("+DoubleToStr(MathAbs(prof),2)+"$)";
      ObjectSetText(Symbol()+DoubleToStr(Period(),0)+"topen"+i,DoubleToStr(MathAbs(prof_pts),0)+txt,FS,"Arial",prof_clr);

      dist_take_pts=0;dist_take=0;
      if(OrderTakeProfit()!=0) 
      {
         dist_take_pts=MathAbs(pr/Point-OrderTakeProfit()/Point);
         txt=DoubleToStr(dist_take_pts,0);
         dist_take_pts=MathAbs(OrderOpenPrice()/Point-OrderTakeProfit()/Point);
         txt=txt+"("+DoubleToStr(dist_take_pts,0)+"/"+DoubleToStr(dist_take_pts*OrderLots()*10,0)+"$)";
         
         ObjectCreate(Symbol()+DoubleToStr(Period(),0)+"ttake"+i,OBJ_TEXT,0,CT,OrderTakeProfit()+CP);
         ObjectSetText(Symbol()+DoubleToStr(Period(),0)+"ttake"+i,txt,FS,"Arial",Green);
      }
      
      dist_stop_pts=0;dist_stop=0;
      if(OrderStopLoss()!=0)
      {
         dist_stop_pts=MathAbs(pr/Point-OrderStopLoss()/Point);
         txt=DoubleToStr(dist_stop_pts,0);
         dist_stop_pts=MathAbs(OrderOpenPrice()/Point-OrderStopLoss()/Point);
         txt=txt+"("+DoubleToStr(dist_stop_pts,0)+"/"+DoubleToStr(dist_stop_pts*OrderLots()*10,0)+"$)";

         ObjectCreate(Symbol()+DoubleToStr(Period(),0)+"tstop"+i,OBJ_TEXT,0,CT,OrderStopLoss()+CP);
         ObjectSetText(Symbol()+DoubleToStr(Period(),0)+"tstop"+i,txt,FS,"Arial",Red);
      }
      // ðèñóåì--->
   }
}

void DeleteLabels()
{
   int i=0;
   string n="";
   int t=Time[WindowFirstVisibleBar()];
   
   for(i=0;i<OrdersHistoryTotal();i++)
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) && OrderSymbol()==Symbol())
   {
      n=OrderSymbol()+Period()+DoubleToStr(OrderTicket(),0);
      ObjectDelete(n+"stoplbl");
      ObjectDelete(n+"takelbl");
   }
   
   for(i=0;i<OrdersTotal();i++)
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol())
   {
      n=OrderSymbol()+Period()+DoubleToStr(OrderTicket(),0);
      ObjectDelete(n+"stoplbl");
      ObjectDelete(n+"takelbl");
   }
}

void ShowLabels()
{
   int i=0;
   string n="";
   int t=Time[WindowFirstVisibleBar()-(WindowBarsPerChart()/20)];
   
   //Comment(WindowBarsPerChart());
   
   for(i=0;i<OrdersTotal();i++)
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol())
   {
      n=OrderSymbol()+Period()+DoubleToStr(OrderTicket(),0);

      ObjectDelete(n+"stoplbl");
      ObjectCreate(n+"stoplbl",OBJ_TEXT,0,t,OrderStopLoss()); 
      ObjectSetText(n+"stoplbl","#"+DoubleToStr(OrderTicket(),0)+"  stop",8,"Arial",StopLossColor);
   
      ObjectDelete(n+"takelbl");
      ObjectCreate(n+"takelbl",OBJ_TEXT,0,t,OrderTakeProfit()); 
      ObjectSetText(n+"takelbl","#"+DoubleToStr(OrderTicket(),0)+"  take",8,"Arial",TakeProfitColor);
   }
}

void CheckDonate()
{
   int fd=0; string pay="0"; datetime dt=0;
   string fn="VisualTrading.txt";
   fd=FileOpen(fn,FILE_READ|FILE_CSV,";");
   if(fd>=1)
   {
      pay=FileReadString(fd); if(pay!="0" && pay!="1") pay="0";
      dt=StrToTime(FileReadString(fd));
   }
   else
   {
      dt=TimeCurrent();
      fd=FileOpen(fn,FILE_WRITE|FILE_CSV,";");
      FileWrite(fd,"0",TimeToStr(dt,TIME_DATE));
   }
   FileClose(fd);
   
   if(pay=="0" && (TimeCurrent()-dt)>10*24*60*60) // 5 äíåé
   {
      if(MessageBoxA(0,"Åñëè Âàì ïîíðàâèëñÿ ñîâåòíèê VisualTrading,\n õîòèòå ïîìî÷ü àâòîðó ìàòåðèàëüíî?","Âîïðîñ",4)==6)
      {
         ShellExecuteA(0,"Open","iexplore.exe","wmk:payto?Purse=R865705290089&Amount=100&Desc=VisualTrading&BringToFront=Y&ExecEvenKeeperIsOffline=Y","",7);
         pay="1";
      }
      dt=TimeCurrent();
      
      fd=FileOpen(fn,FILE_WRITE|FILE_CSV,";");
      FileWrite(fd,pay,TimeToStr(dt,TIME_DATE));
      FileClose(fd);
   }
   FileClose(fd);
}









Sample





Analysis



Market Information Used:

Series array that contains open time of each bar


Indicator Curves created:


Indicators Used:



Custom Indicators Used:
shell32
user32

Order Management characteristics:
Checks for the total of open orders

It Closes Orders by itself
It can change open orders parameters, due to possible stepping strategy
Checks for the total of closed orders

Other Features:

It opens Message Boxes to the user
Uses files from the file system
It reads information from a file
It writes information to file