expert advisor

 

Hello, I'm new to mql5 and generally not a good programmer. I am playing around with an ea that utilises price differentials between last and previous prices as a volatility factor and places a trade based on this. My code compiled fine but upon stategy-testing, no trades are placed, hence no meaningful data. Also, the graph of balance/equity are both horizontal straight lines. I will appreciate any help here. Thanks!

#include<Trade\Trade.mqh>
CTrade trade;

MqlTick tick_array[];
int OnInit()
  {

   ArraySetAsSeries(tick_array,true);

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   
  }

void OnTick()
  {
   if(CopyTicks(_Symbol,tick_array,COPY_TICKS_INFO,0,2)<2)
   return;

   // Difference between the last two ask prices, as a volatility determinant
   double en=tick_array[0].ask-tick_array[1].ask;

   if(en>=10*_Point)
   // Enter a Sell position if volatility reaches a positive threshold
   {trade.Sell(0.02,NULL,tick_array[0].ask,(tick_array[0].ask+5*_Point),(tick_array[0].ask-5*_Point),NULL);
   }
   else if(en<=-10*_Point)
   // Enter a Buy position if volatility reaches a negative threshold
   {trade.Buy(0.02,NULL,tick_array[0].bid,(tick_array[0].bid-5*_Point),(tick_array[0].bid+5*_Point),NULL);
   }
  }
 
amabeliever: Hello, I'm new to mql5 and generally not a good programmer. I am playing around with an ea that utilises price differentials between last and previous prices as a volatility factor and places a trade based on this. My code compiled fine but upon stategy-testing, no trades are placed, hence no meaningful data. Also, the graph of balance/equity are both horizontal straight lines. I will appreciate any help here. Thanks!

You are not verifying the return values from either the method functions or the trade results, so how will you know if it succeeded or not and why?

There should also be log output in Strategy Tester Journal. Did you analyse it?

 

You are in fact missing many verification steps. Read the following for how to improve your EA.

It may be for Market products but you should apply it to your own EA as well if you want it to work properly in a real live trading environment.

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.

 
Besides the above.
   {trade.Sell(0.02,NULL,tick_array[0].ask,(tick_array[0].ask+5*_Point),(tick_array[0].ask-5*_Point),NULL);

   {trade.Buy(0.02,NULL,tick_array[0].bid,(tick_array[0].bid-5*_Point),(tick_array[0].bid+5*_Point),NULL);
  1. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  2. Your stops are at 5 points. That doesn't even cover the spread.

 
Fernando Carreiro #:

You are in fact missing many verification steps. Read the following for how to improve your EA.

It may be for Market products but you should apply it to your own EA as well if you want it to work properly in a real live trading environment.

Thanks for the link, I'll go through it and try and verify the needed steps. The journal too!
 
William Roeder #:
Besides the above.
  1. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  2. Your stops are at 5 points. That doesn't even cover the spread.

I see @point 1. Trades are not placed because I'm buying/selling at the wrong market price. Plus my sl/tp may be less than the spread. I will adjust the code and revert here. Thanks a lot!
 
William Roeder #:
Besides the above.
  1. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 PIP spreads) in EURCHF? - General - MQL5 programming forum (2022)

  2. Your stops are at 5 points. That doesn't even cover the spread.

The code finally ran after the adjustments you suggested. Thanks a lot!
I only noticed very few instances that satisfy the if condition were traded during the backtest. I'm not sure why.
 
amabeliever #: I only noticed very few instances that satisfy the if condition were traded during the backtest. I'm not sure why.

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.

 
William Roeder #:

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.

Sorry for the ambiguity.
Let me try again, I need help knowing why every instance of the If condition was not traded during the backtest. Thanks
 
amabeliever #: I need help knowing why …
  1. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. What information have you provided? No logs, no debugging code.

  2. Do you really expect price to move ten (10) times the tick size in one tick?

  3. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
William Roeder #:
  1. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. What information have you provided? No logs, no debugging code.

  2. Do you really expect price to move ten (10) times the tick size in one tick?

  3. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

I guess I should get to work then. Thank you for the Articles.
Reason: