Expert Advisor

 

Hello Forum Members,

I am totally new to programming and trying to learn it to test indicators with expert advisor. I read the article step by step guide to writing an expert advisor in mql 5 for beginners and mostly copied the code from it. Below code doesn't do what I intend to do. I think it has something to do with arrays or I might be completely wrong. Could you possibly point me in the right direction to solve my problem?

<Deleted>


 
swarthykhan:

Hello Forum Members,

I am totally new to programming and trying to learn it to test indicators with expert advisor. I read the article step by step guide to writing an expert advisor in mql 5 for beginners and mostly copied the code from it. Below code doesn't do what I intend to do. I think it has something to do with arrays or I might be completely wrong. Could you possibly point me in the right direction to solve my problem?

<Deleted>

1st lesson:

When you post your code make sure you use the function 'Code' Alt+S and insert your code there

code_attach

 
On a positive note, if you're new to programming your first EA should probably be only a few lines long...
 

"Below code doesn't do what I intend to do."

This is a typical situation for using the debugger from the Editor:

Before using it place some breakpoints with the mouse or with F9 then navigate to Options => Tab and check the settings.

Then press F5 or open the debug menu from the edit line..

Now you can control the variables and the if and you'll see why the " code doesn't do what I intend to do.".

Happy New Year and an successful 2020.. :)

 
Kenneth Parling:

1st lesson:

When you post your code make sure you use the function 'Code' Alt+S and insert your code there


//+------------------------------------------------------------------+
//|                                                        metro.mq5 |
//|                                           Copyright 2019, Swarthy|
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Swarthy"
#property link      "https://www.mql5.com"
#property version   "1.00"


input uint PeriodWPR=18;
input int StepSizeFast=34;
input int StepSizeSlow=40;
input int Shift=0;
input int AtrPeriod=14;
input double Lot=0.1;

int EA_Magic=437;
int wpr_handle; //handle for wpr metro
int atr_handle; //handle for atr
double wpr_fast[]; //array for storing wpr fast values
double wpr_slow[]; //array for storing wpr slow values
double atr_value[]; //array for storing atr values
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   wpr_handle=iCustom(Symbol(),0,"Examples\\metro_wpr", PeriodWPR,StepSizeFast,StepSizeSlow,Shift);
   atr_handle=iATR(Symbol(),0,AtrPeriod);
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   IndicatorRelease(wpr_handle);
   IndicatorRelease(atr_handle);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   MqlTick latest_price;      // To be used for getting recent/latest price quotes
   MqlTradeRequest mrequest;  // To be used for sending our trade requests
   MqlTradeResult mresult;    // To be used to get our trade results
   MqlRates mrate[];          // To be used to store the prices, volumes and spread of each bar
   ZeroMemory(mrequest);      // Initialization of mrequest structure
//Array as series
   ArraySetAsSeries(wpr_fast,true);
   ArraySetAsSeries(wpr_slow,true);
   ArraySetAsSeries(atr_value,true);

//--- Copy last 2 values of my indicators to (arrays)
   CopyBuffer(wpr_handle,1,0,2,wpr_fast);
   CopyBuffer(wpr_handle,2,0,2,wpr_slow);
   CopyBuffer(atr_handle,0,0,1,atr_value);

//--- Get the last price quote using the MQL5 MqlTick Structure
   if(!SymbolInfoTick(_Symbol,latest_price))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }
//--- Buying order
   if(wpr_fast[0]>wpr_slow[0] && wpr_fast[1]<=wpr_slow[1]) // metro wpr gives a buy signal
     {
         ZeroMemory(mrequest);
         mrequest.action = TRADE_ACTION_DEAL;                                   // immediate order execution
         mrequest.price = latest_price.ask;                                     // latest ask price
         mrequest.sl = (latest_price.ask - atr_value[0]);    // Stop Loss
         mrequest.tp = (latest_price.ask + atr_value[0]);        // Take Profit
         mrequest.symbol = _Symbol;                                             // currency pair
         mrequest.volume = Lot;                                                 // number of lots to trade
         mrequest.magic = EA_Magic;                                             // Order Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                        // Buy Order
         mrequest.type_filling = ORDER_FILLING_FOK;                             // Order execution type
         mrequest.deviation=100;                                                // Deviation from current price
      OrderSend(mrequest,mresult);
     }

   return;
  }
//+------------------------------------------------------------------+

Thank you Kenneth for the first lesson and your reply. Here it is.

What I am trying to achieve seems simple. A custom indicator's two line crossover is a signal. I try to get the last two candle information to find if there is a signal. Since I am new to this I do not know what to look for.

Thanks.

Reason: