Download MetaTrader 5

Mysql Signals Debug help please

To add comments, please log in or register
Louis Stoltz
636
Louis Stoltz  

I coded this mysql mql4 from scripts I found of these forums and I got it to work right.

I put this on AUDNZD Chart and started trading only the one pair but I have also other pairs that I want to trade and I don't want to open multiple charts and only use one because there too many pairs too open. So I want to trade multiple currencies on one chart so to save me time. I started debugging this and I cannot seem to get Print() to show in Journal and its not printing the two pairs in journal. So I don't know why its not opening the two pairs.

I load the same script on USDCHF and it works but why do I need to open two different charts? I also get no ordersend error or any indication whats going on.


#import "libmysql.dll"

int mysql_init(int db);
int mysql_errno(int TMYSQL);
int mysql_real_connect(int TMYSQL, string host, string user, string password, string DB,int port,int socket,int clientflag);
int mysql_real_query(int TMSQL, string query, int length);
void mysql_close(int TMSQL);
 
int mysql_store_result(int TMSQL);
string mysql_fetch_row(int result);
int mysql_num_rows(int result);
void mysql_free_result(int result);
 
int mysql;

int mNumber;
string mSymbol, mType;
double mLots, mOpen, mClose, mStopLoss, mTakeProfit; 
#define DELIM ";" 
/**

  Global Variables

**/  

double glbOrderProfit;
double glbOrderOpen;
double glbOrderStop;
double glbOrderType;
double glbOrderTicket;

double Lots = 5;

int init()
  {
   mysql = mysql_init(mysql);
   if(mysql != 0) 
       Print("allocated");
   string host = "localhost";
   string user = "";
   string password = "";
   string DB = "mysignals";
   int clientflag = 0;
   int port = 3306;
   string socket = "";
   int res = mysql_real_connect(mysql,host,user,password,DB,port,socket,clientflag);
   int err = GetLastError();
   if(res == mysql) 
       Print("connected");
   else 
       Print("error=", mysql, " ", mysql_errno(mysql), " ");
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   mysql_close(mysql);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {


   string query = "";
   string row,row2;   
   int length = 0;
   double op=0,cp=0;
   
   string date = TimeToStr(TimeCurrent(),TIME_DATE);   
   date = stringReplace(date, ".", "-");

   query = StringConcatenate("SELECT concat(';',concat_ws(';', id, symbol, trade, lots, op, cp, st, tp)) FROM signals WHERE closedate IS NULL AND opendate LIKE '%", date, "%' ;");

   length = StringLen(query);
   mysql_real_query(mysql, query, length);
   int result = mysql_store_result(mysql);
   int numOfRows = mysql_num_rows(result);
   for (int i=0;i<numOfRows;i++) {
      row = mysql_fetch_row(result);

      decodeTrade(row); 

      int debug = 0;
      if(mType == "Sell" && OrderFind(mNumber) == false)
      {   
        OrderSend(mSymbol, OP_SELL, Lots, Bid, 3, mStopLoss, mTakeProfit, "Sell Order", mNumber, 0, Blue);
        debug=1;  
      }
      if(mType == "Buy" && OrderFind(mNumber) == false)
      {
        OrderSend(mSymbol, OP_BUY, Lots, Ask, 3, mStopLoss, mTakeProfit, "Buy Order", mNumber, 0, Blue);
        debug=1;
      }
      
      Print("Over here", mType);

      Comment("Magic=", mNumber, ",Symbol=", mSymbol, ",Type=", mType, ",Lots=", mLots, ",Open=", mOpen, ",Close=", mClose, ",SL=", mStopLoss, ",TakeProfit=", mTakeProfit);   
                        
   }
   mysql_free_result(result);
  

  int total = OrdersTotal();

   for(int cnt = 0 ; cnt < total ; cnt++)
   {

      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      
      query = StringConcatenate("SELECT concat(';',concat_ws(';', id, symbol, trade, lots, op, cp, st, tp)) FROM signals WHERE ", OrderMagicNumber(), " ;");      
      length = StringLen(query);
      mysql_real_query(mysql, query, length);
      result = mysql_store_result(mysql);
      numOfRows = mysql_num_rows(result);
      
      for (i=0;i<numOfRows;i++) {
         row = mysql_fetch_row(result);
         
         decodeTrade(row);  
         
         if (mClose != 0) 
         {
           if(mType == "Buy" && OrderFind(mNumber) == true && Bid >= mClose)CloseOrder(glbOrderTicket);
           if(mType == "Sell" && OrderFind(mNumber) == true && Ask <= mClose)CloseOrder(glbOrderTicket);
         }
         
      }
   
   mysql_free_result(result); 
     
   }  
     
return(0);     
}


/**

  Close Order Function

**/  

void CloseOrder( int Ticket )

{

  OrderSelect(Ticket, SELECT_BY_TICKET);

  if (OrderType() == OP_BUY) {
    OrderClose(Ticket, OrderLots(), Bid, 0);     
  }  

  else {
    OrderClose(Ticket, OrderLots(), Ask, 0);      
  }  

 return;  

}  



/**

  Order Find Function

**/  

bool OrderFind(int Magic) 

{

   glbOrderType = -1;
   glbOrderTicket = -1;
   glbOrderProfit = 0;
   glbOrderOpen = -1;
   glbOrderStop = -1;

   int total = OrdersTotal();

   bool res = false;

   for(int cnt = 0 ; cnt < total ; cnt++)

     {

       OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

       if(OrderMagicNumber() == Magic)

         {

           glbOrderType = OrderType();
           glbOrderTicket = OrderTicket();
           glbOrderProfit = OrderProfit();
           glbOrderOpen = OrderOpenPrice();
           glbOrderStop = OrderStopLoss();

           res = true;

         }

     }

 return(res);

}

string stringReplace(string haystack, string needle, string replace=""){
   string left, right;
   int start=0;
   int rlen = StringLen(replace);
   int nlen = StringLen(needle);
   while (start > -1){
      start = StringFind(haystack, needle, start);
      if (start > -1){
         if(start > 0){
            left = StringSubstr(haystack, 0, start);
         }else{
            left="";
         }
         right = StringSubstr(haystack, start + nlen);
         haystack = left + replace + right;
         start = start + rlen;
      }
   }
   return (haystack);  
}

void decodeTrade(string trade) {
   int begin = StringFind(trade, DELIM)+1;
   int end = StringFind(trade, DELIM, begin);
   mNumber = StrToInteger(StringSubstr(trade, begin, end-begin));
   begin = end+1;
   end = StringFind(trade, DELIM, begin);
   mSymbol = StringSubstr(trade, begin, end-begin);
   begin = end+1;
   end = StringFind(trade, DELIM, begin);
   mType = StringSubstr(trade, begin, end-begin);
   begin = end+1;
   end = StringFind(trade, DELIM, begin);
   mLots = StrToDouble(StringSubstr(trade, begin, end-begin));
   begin = end+1;
   end = StringFind(trade, DELIM, begin);
   mOpen = StrToDouble(StringSubstr(trade, begin, end-begin));
   begin = end+1;
   end = StringFind(trade, DELIM, begin);
   mClose = StrToDouble(StringSubstr(trade, begin, end-begin));
   begin = end+1;
   end = StringFind(trade, DELIM, begin);
   mStopLoss = StrToDouble(StringSubstr(trade, begin, end-begin));
   begin = end+1;
   end = StringLen(trade);
   mTakeProfit = StrToDouble(StringSubstr(trade, begin, end-begin));
}  
whroeder1
15901
whroeder1  
bonechair: but why do I need to open two different charts? I also get no ordersend error or any indication whats going on.
  1. Because you use SYMBOL specific values
    OrderSend(mSymbol, OP_SELL, Lots, Bid, 3, mStopLoss, mTakeProfit, "Sell Order", mNumber, 0, Blue);
    if(mType == "Buy" && OrderFind(mNumber) == true && Bid >= mClose)CloseOrder(glbOrderTicket);
    OrderClose(Ticket, OrderLots(), Bid, 0);
    
  2. You don't filter by magic number
  3. OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
    
    You don't test your return codes What are Function return values ? How do I use them ? - MQL4 forum so you don't know what's going on.
Louis Stoltz
636
Louis Stoltz  

Oh no I missed that thanks for spotting that.

Louis Stoltz
636
Louis Stoltz  
Any Idea why my journal is spitting out no errors and Print is not working? Journal would have said something like Ordersend error 130
Simon Gniadkowski
Moderator
18025
Simon Gniadkowski  
bonechair:
Any Idea why my journal is spitting out no errors and Print is not working? Journal would have said something like Ordersend error 130

Why aren't you testing the return value and reporting the error ?

What are Function return values ? How do I use them ?

Louis Stoltz
636
Louis Stoltz  
Ok I can do that, it works without it in strategy tester.
Simon Gniadkowski
Moderator
18025
Simon Gniadkowski  
bonechair:
Ok I can do that, it works without it in strategy tester.
Does it also give you all the values of the variables you need to determine the cause of the error ? like Bd, Ask, Open price, type, position size, Lot Step, Min Lot, etc, etc ? Print what you might need, then when you need it you will have it. . . .
Louis Stoltz
636
Louis Stoltz  

Sorry for replying so late, I dont get alerts on these new messages.

Uhm print does not debug for me, I had to use Comment. Not sure why this code is not printing, must be dll or something, Im not sure.

Simon Gniadkowski
Moderator
18025
Simon Gniadkowski  
bonechair:

Sorry for replying so late, I dont get alerts on these new messages.

Uhm print does not debug for me, I had to use Comment. Not sure why this code is not printing, must be dll or something, Im not sure.

Print() prints to the Experts tab for live/demo and to the Strategy Tester journal tab for Strategy Tester.
Louis Stoltz
636
Louis Stoltz  
No problem in Strategy Tester but when I go live I dont see Print in Journal. Maybe the live journal restricted on mine.
Louis Stoltz
636
Louis Stoltz  

2014.02.02 12:06:39 2013.12.16 00:27 mysql_strategy GBPJPY,M1: ðŠ; *‹; ;548;GBPUSD;Buy Stop;0.21;1.63650;1.62989;1.62752;1.64548

I have a problem all of a sudden with mysql that I cant get the rows no because I get garbled characters all of a sudden with this dll. ðŠ; *‹; ;548;GBPUSD;

Why would this happen all of a sudden, Ive read about this on other threads too. Not sure what todo now.

12
To add comments, please log in or register