Unable to run script

 

In a recent post, see "ea runs on my broker but not on another", I posted my code for a program that automatically sets the stoploss given the lots opened on a trade and a predetermined risk level inputted to an extern variable. It would not run, so it seemed, on two different brokers. It turns out this was not the problem at all. The program runs beautifully for its purpose. A discussion was started that it might have to do with the decimal point used by the broker. It had nothing to do with that. The problem occurs because in the original code I tried to automatically generate the ticket for the existing trade. All of the problems are solved if I manually enter the ticket number at the beginning to an extern variable. Here is the code:

//+------------------------------------------------------------------+
//|                                           auto buy stop loss.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"


extern int ticket = 0;   //THIS IS THE SIMPLE FIX! 
extern double risk_factor = 0.02;
extern double reward_risk_ratio = 2.0;
 
 
 
int auto_stop_loss = 1;
int determine_pip_value = 1;
int symbol_detect = 0;
int set_modify = 1;
int error;

double stop_loss_points = 0.0; 
double spread = 0.0;
double equity = 0.0;
double a = 0.0;
double b = 0.0;  
double lots = 0.0;
double value_per_pip = 10.00;
double pip_value = 0.0;
 
bool modify_result;
 
int init()
  {
 
   return(0);
  }
 
int deinit()
  {
 
   return(0);
  }
 
int start()
  {
 
 
   
   
  if(OrderSelect(ticket,SELECT_BY_TICKET))
     {
       lots = OrderLots();
      }   
   
   
   if(auto_stop_loss == 1)
 { 
  value_per_pip = MarketInfo(Symbol(),MODE_TICKVALUE) * 10.0;
  equity = AccountEquity(); 
  a = equity * risk_factor;
  b = value_per_pip * lots;
  stop_loss_points =a/b - spread; 
  auto_stop_loss = 0;
 } 
 
 
 symbol_detect = StringFind(Symbol(),"JPY",0);
 
  if(determine_pip_value  == 1)
  {
   if(symbol_detect > 1)
    {
     pip_value = 0.01;
     determine_pip_value = 0;
    }
  else
   {
    pip_value = 0.0001;
    determine_pip_value = 0;
   }
  }
 
  
  
  if(set_modify == 1)
   {
    if(OrderSelect(ticket,SELECT_BY_TICKET))
     {
      modify_result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() - (stop_loss_points * pip_value),OrderOpenPrice() + (stop_loss_points * reward_risk_ratio * pip_value),0,0);
       
       if(modify_result !=True)
          {
            error = GetLastError();
            Print("LastError = ",error);
            
          } 
           else error = 0;
            
       
      set_modify = 0;
     } 
   }
 
 
 
   return(0);
  }
 

However, as suggested by others, I have tried to convert this to a script. I have never used a script before. I created a script template in the editor and copied the code above as follows:

//+------------------------------------------------------------------+
//|                                     auto buy stoploss script.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"


extern int ticket = 0;    
extern double risk_factor = 0.02;
extern double reward_risk_ratio = 2.0;
 
 
int start()
  {
 
 
int auto_stop_loss = 1;
int determine_pip_value = 1;
int symbol_detect = 0;
int set_modify = 1;
int error;

double stop_loss_points = 0.0; 
double spread = 0.0;
double equity = 0.0;
double a = 0.0;
double b = 0.0;  
double lots = 0.0;
double value_per_pip = 10.00;
double pip_value = 0.0;
 
bool modify_result;
 
  
   
  if(OrderSelect(ticket,SELECT_BY_TICKET))
     {
       lots = OrderLots();
      }   
   
   
   if(auto_stop_loss == 1)
 { 
  value_per_pip = MarketInfo(Symbol(),MODE_TICKVALUE) * 10.0;
  equity = AccountEquity(); 
  a = equity * risk_factor;
  b = value_per_pip * lots;
  stop_loss_points =a/b - spread; 
  auto_stop_loss = 0;
 } 
 
 
 symbol_detect = StringFind(Symbol(),"JPY",0);
 
  if(determine_pip_value  == 1)
  {
   if(symbol_detect > 1)
    {
     pip_value = 0.01;
     determine_pip_value = 0;
    }
  else
   {
    pip_value = 0.0001;
    determine_pip_value = 0;
   }
  }
 
  
  
  if(set_modify == 1)
   {
    if(OrderSelect(ticket,SELECT_BY_TICKET))
     {
      modify_result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() - (stop_loss_points * pip_value),OrderOpenPrice() + (stop_loss_points * reward_risk_ratio * pip_value),0,0);
       
       if(modify_result !=True)
          {
            error = GetLastError();
            Print("LastError = ",error);
            
          } 
           else error = 0;
            
       
      set_modify = 0;
     } 
   }
 
 
  
 
   return(0);
  }
 

But this script does not run when I invoke it. What an I doing wrong?

 
forestmyopia:

In a recent post, see "ea runs on my broker but not on another", I posted my code for a program that automatically sets the stoploss given the lots opened on a trade and a predetermined risk level inputted to an extern variable. It would not run, so it seemed, on two different brokers. It turns out this was not the problem at all. The program runs beautifully for its purpose. A discussion was started that it might have to do with the decimal point used by the broker. It had nothing to do with that. The problem occurs because in the original code I tried to automatically generate the ticket for the existing trade. All of the problems are solved if I manually enter the ticket number at the beginning to an extern variable. Here is the code:

However, as suggested by others, I have tried to convert this to a script. I have never used a script before. I created a script template in the editor and copied the code above as follows:

But this script does not run when I invoke it. What an I doing wrong?

You haven't fixed your code, a ticket number of zero isn't valid even in the Strategy Tester, you had better learn how to test your code . . .