Script only works in debugger

To add comments, please log in or register
CamenM
97
CamenM  

I'm able to run my script just fine in the debugger, and I figured out how to change the time period and pair because the default was the EURUSD 1H.  Everything works fine in the debugger, but it won't execute in my demo account.

//+------------------------------------------------------------------+
//|                                            BUY_LIMIT_Fib_61.8%.mq4 |
//|                                             Tom Marazzo
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property script_show_inputs

extern double  TakeProfit = 1.618;
extern double  StopLoss = 0.20; //80%
extern int     Candle = 2;
extern double  LotSize = 25;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   double takeProfitLevel;
   double stopLossLevel;
   double price = Ask; //Need to use this in my OP_BUYLIMIT
 
 
   //Use these for Calculating Point C
   float PipDiff;
   float C;
  
   PipDiff = (High[Candle] - Low[Candle]);
   Alert("The pip range is " + string(PipDiff*10000));
  
   
   C = Low[Candle]+(PipDiff)*0.382;
   Alert("Point C is " + string(C));
  
   //Create Order
   takeProfitLevel = Low[Candle]+(PipDiff)*TakeProfit;
   stopLossLevel = Low[Candle]+(PipDiff)*StopLoss;
 
 //Order if price is still between High and Low of Specified Candle
       if(price > Low[Candle] && price < High[Candle])
       {
           int executeOrder;
               
            if (price >= C)
              {
                  executeOrder =  OrderSend(Symbol(),OP_BUYLIMIT,LotSize, C ,10, stopLossLevel,takeProfitLevel,"Pending BUY Order",0,2,clrBlue);
                  Alert("PENDING Buy Limit Placed At " + string(C));        
              }
           
        }     
        else
        {
           Alert("Order Outside of Range - Order Failed to Fill");
        }
 }
  
   }
   */
  
 
//+------------------------------------------------------------------+

Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
Choose a suitable trading strategy and subscribe to it with a few clicks. All Signals are provided with detailed statistics and informative charts. Become a trading signal provider and sell subscriptions to thousands of traders around the world. With the Signals service, your successful strategy can generate income with a small start-up budget...
CamenM
97
CamenM  

It is not recognizing me executeOrder line????  Cananyone make a suggestion?  I've been debugging for 4 hours and it seems to only work when it wants to???


//+------------------------------------------------------------------+

//|                                            BUY_LIMIT_Fib_61.8%.mq4 |

//|                                             Tom Marazzo

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2017, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

#property script_show_inputs


extern double  TakeProfit = 1.618;

extern double  StopLoss = 0.20; //80%

extern double  SetPercentRisk = 1;

extern double  Reward = 3;

extern int     Candle = 9;

//extern double  LotSize = 25;


//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart()

  {

   double takeProfitLevel;

   double stopLossLevel;

   double price = Ask; //Need to use this in my OP_BUYLIMIT

    

  

  

//+------------------------------------------------------------------+

//|  Use these for Calculating Point C                               |

//+------------------------------------------------------------------+

   float PipDiff;

   float C;

   

//+------------------------------------------------------------------+

//|  Determines number of PIPs per Candle                            |

//+------------------------------------------------------------------+   

   PipDiff = (High[Candle] - Low[Candle]);

   Alert("The pip range is " + string(PipDiff*10000));

   

    

   C = Low[Candle]+(PipDiff)*0.382;

   Alert("Point C is " + string(C));

   

//+------------------------------------------------------------------+

//|  Take Profit is 1.168 Fib                                        |

//|  Stop Loss is 80% Retracement                                    |

//+------------------------------------------------------------------+

   takeProfitLevel = Low[Candle]+(PipDiff)*TakeProfit;

   stopLossLevel = Low[Candle]+(PipDiff)*StopLoss;

   

   

//+------------------------------------------------------------------+

//|  Create 3:1 Risk-Reward Condition check                          |

//+------------------------------------------------------------------+

    double RiskPips = (C - stopLossLevel)*10000;

    Alert("Number of Pips Risked is  " + RiskPips);

    

    double RewardPips = (takeProfitLevel - C)*10000;

    Alert("Number of Pips Rewarded is  " + RewardPips);

    

    double RiskRewardCalc = RiskPips * Reward;

    Alert ("The risk pips x 3 is " + RiskRewardCalc);

      

  

//+------------------------------------------------------------------+

//|  Calculate the number of Lots                                    |

//+------------------------------------------------------------------+

   double Risk = (SetPercentRisk/100);

   

   double EquityRisk = (AccountFreeMargin()* Risk);

         Alert("Equity Risked is  " + EquityRisk);

   double Lots = (EquityRisk / RiskPips);

   

      //MathFloor(AccountFreeMargin()*EquityRisk/MarketInfo(Symbol(),MODE_MARGINREQUIRED)/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP);// Lots


         Alert ("Number of Lots to Trade is: ", Lots);

   int executeOrder;

   

//+------------------------------------------------------------------+

//|  If Candle is not greater that 25 PIPs, it's not worth the trade |

//+------------------------------------------------------------------+

   

   if(PipDiff >= 0.0025)

   {

//+------------------------------------------------------------------+

//|  Confirm there is enough Risk to Reward to trade                 |

//+------------------------------------------------------------------+

        if(RewardPips > RiskRewardCalc )

        {

            //Order if price is still between High and Low of Specified Candle

             if(price > Low[Candle] && price < High[Candle])

             {

                  if(Lots > 100)

                  {

                  Lots = 100;

                  Alert ("Number of Revised Lots to Trade is: ", Lots);

                  }

           

                       if (price >= C)

                          {

                              executeOrder =  OrderSend(Symbol(),OP_BUYLIMIT,Lots, C ,10, stopLossLevel,takeProfitLevel,"Pending BUY Order",0,1,clrBlue);

                              Alert("PENDING Buy Limit Placed At " + string(C));         

                          }

            

              }      

              else

              {

                 Alert("Order Outside of Range - Order Failed to Fill");

              }

          }

          else

          {

          Alert("Don't Trade = Not Enough Reward to take this trade");

          }

          

      }

      else

       {

       Alert("Don't Trade = Not Enough Risk to Reward to take this trade");

       }

         

 }

   /*Adjuest Pips for 4 or 5 digit Broker

   int NumOfDigits = MarketInfo(Symbol(), MODE_DIGITS);

   int PipAdjust;

   if(NumOfDigits == 5 || NumOfDigits == 3)

   {

   PipAdjust = 10;

   }

   else

   if(NumOfDigits == 4 || NumOfDigits == 2)

   {

   PipAdjust = 1;

   }

   */

   

  

//+------------------------------------------------------------------+


Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
Choose a suitable trading strategy and subscribe to it with a few clicks. All Signals are provided with detailed statistics and informative charts. Become a trading signal provider and sell subscriptions to thousands of traders around the world. With the Signals service, your successful strategy can generate income with a small start-up budget...
CamenM
97
CamenM  

I tested this out on the EURUSD Weekly chart just now and it worked fine.  It takes the specified candle (in this case 1 - just before the 0) and stets a Buy Limit Order at the 61.8% retracement, SL at 80% and TP at 1.618.  IF the Lots calculated goes beyond 100 Lots, it restricts it to stay at 100 lots.


I can't get it to work outside of the debugger OR at lower timeframes.  


Can anyone else have a look?

Thanks

Jack Thomas
1626
Jack Thomas  
CamenM:

I'm able to run my script just fine in the debugger, and I figured out how to change the time period and pair because the default was the EURUSD 1H.  Everything works fine in the debugger, but it won't execute in my demo account.


FIRST THING:


SECOND THING:

If running this as an EA, did you enable "Live Trading" when you loaded the EA on your chart?



THIRD THING:

If running this as a script, did you get any error messages?


FOURTH THING:

What does the metatrader Experts panel and Journal panel say? If there is an error, it might also be reported there.

William Roeder
20228
William Roeder  
executeOrder =  OrderSend(Symbol(), OP_BUYLIMIT, Lots, C, 10, stopLossLevel, takeProfitLevel, "Pending BUY Order", 0, 1, clrBlue);
Alert("PENDING Buy Limit Placed At " + string(C));  
FIFTH THING:
Check your return codes and find out why.
          What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
          Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
CamenM
97
CamenM  

//+------------------------------------------------------------------+
//|                                            BUY_LIMIT_Fib_61.8%.mq4 |
//|                                             Tom Marazzo
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property script_show_inputs

extern double  TakeProfit = 1.618;
extern double  StopLoss = 0.20; //80%
extern double  SetPercentRisk = 1;
extern double  Reward = 3;
extern int     Candle = 1;
//extern double  LotSize = 25;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   double takeProfitLevel;
   double stopLossLevel;
   double price = Ask; //Need to use this in my OP_BUYLIMIT
    
  
  
//+------------------------------------------------------------------+
//|  Use these for Calculating Point C                               |
//+------------------------------------------------------------------+
   float PipDiff;
   float C;
   
//+------------------------------------------------------------------+
//|  Determines number of PIPs per Candle                            |
//+------------------------------------------------------------------+   
   PipDiff = (High[Candle] - Low[Candle]);
   Alert("The pip range is " + string(PipDiff*10000));
   
    
   C = Low[Candle]+(PipDiff)*0.382;
   Alert("Point C is " + string(C));
   
//+------------------------------------------------------------------+
//|  Take Profit is 1.168 Fib                                        |
//|  Stop Loss is 80% Retracement                                    |
//+------------------------------------------------------------------+
   takeProfitLevel = Low[Candle]+(PipDiff)*TakeProfit;
   stopLossLevel = Low[Candle]+(PipDiff)*StopLoss;
   
   
//+------------------------------------------------------------------+
//|  Create 3:1 Risk-Reward Condition check                          |
//+------------------------------------------------------------------+
    double RiskPips = (C - stopLossLevel)*10000;
    Alert("Number of Pips Risked is  " + RiskPips);
    
    double RewardPips = (takeProfitLevel - C)*10000;
    Alert("Number of Pips Rewarded is  " + RewardPips);
    
    double RiskRewardCalc = RiskPips * Reward;
    Alert ("The risk pips x 3 is " + RiskRewardCalc);
      
  
//+------------------------------------------------------------------+
//|  Calculate the number of Lots                                    |
//+------------------------------------------------------------------+
   double Risk = (SetPercentRisk/100);
   
   double EquityRisk = (AccountFreeMargin()* Risk);
         Alert("Equity Risked is  " + EquityRisk);
   double Lots = (EquityRisk / RiskPips);
   
      //MathFloor(AccountFreeMargin()*EquityRisk/MarketInfo(Symbol(),MODE_MARGINREQUIRED)/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP);// Lots

         Alert ("Number of Lots to Trade is: ", Lots);
   int executeOrder;
   
//+------------------------------------------------------------------+
//|  If Candle is not greater that 25 PIPs, it's not worth the trade |
//+------------------------------------------------------------------+
   
   if(PipDiff >= 0.0025)
   {
//+------------------------------------------------------------------+
//|  Confirm there is enough Risk to Reward to trade                 |
//+------------------------------------------------------------------+
        if(RewardPips > RiskRewardCalc )
        {
            //Order if price is still between High and Low of Specified Candle
             if(price > Low[Candle] && price < High[Candle])
             {
                  if(Lots > 100)
                  {
                    Lots = 100;
                     Alert ("Number of Revised Lots to Trade is: ", Lots);
                  }
                   else
                  {
                     Lots;
                  }
                         if (price >= C)
                                {
                                    executeOrder =  OrderSend(Symbol(),OP_BUYLIMIT,Lots, C ,10, stopLossLevel,takeProfitLevel,"Pending BUY Order",0,1,clrBlue);
                                    Alert("PENDING Buy Limit Placed At " + string(C));         
                                }
              }                
              else
              {
              Alert("Order Outside of Range - Order Failed to Fill");
              }
            }          
          else
          {
          Alert("Don't Trade = Not Enough Reward to take this trade");
          }       
      }
      else
       {
       Alert("Don't Trade = Not Enough Risk to Reward to take this trade");
       }
         
 }
   /*Adjuest Pips for 4 or 5 digit Broker
   int NumOfDigits = MarketInfo(Symbol(), MODE_DIGITS);
   int PipAdjust;
   if(NumOfDigits == 5 || NumOfDigits == 3)
   {
   PipAdjust = 10;
   }
   else
   if(NumOfDigits == 4 || NumOfDigits == 2)
   {
   PipAdjust = 1;
   }
   */
   
  
//+------------------------------------------------------------------+

whroeder1,

thanks for this!  It's currently a script for now.  It seems to work when it wants to - which means my logic has errors somewhere.  It works, just not consistently I see.

I don't have any compile errors at all either.

I'm getting this message in the Journal where it loads then "removed" happens??  I suspect it's my logic that is stopping the order execution because sometimes it actually does execute??




William Roeder
20228
William Roeder  
CamenM: I'm getting this message in the Journal where it loads then "removed" happens??  I suspect it's my logic that is stopping the order execution

It's a script, it runs once and exits. It's not a EA that runs each tick.

CamenM
97
CamenM  
whroeder1:

It's a script, it runs once and exits. It's not a EA that runs each tick.


I know it's a script because I'm the rookie who wrote it LOL.  Bit, it wouldn't execute the script often.  Anyway, thanks for even looking.  I found the issues and have converted it into an EA.  Bit, this time for some stupid reason my Optimization and Backtesting have stopped producing any results after I was getting good execution.

Do I need to clear a buffer or something?

To add comments, please log in or register