Download MetaTrader 5

My first EA, Order Send not working

To add comments, please log in or register
Lal Adidasan
194
Lal Adidasan  

Dear Friends,

I'm writing my first EA. Order send is not firing but i am getting  next line alert at every tick. Could you please clarify why this is happening?

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

//|                                                 Lal-First EA.mq4 |

//|                        Copyright 2016, MetaQuotes Software Corp. |

//|                                           https://book.mql4.com/ |

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

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

#property link      "https://book.mql4.com/"

#property version   "1.00"

#property strict

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

//| Expert initialization function                                   |

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

int OnInit()

  {

   Alert("EA Initiated");

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   long Stochasticvalue;

   int LotSize;

   long MagicNumber;

  

   LotSize = 1;

    Stochasticvalue = iStochastic(NULL, 1, 100, 50, 1, MODE_SMA, 0, MODE_MAIN, 0);



   if (Stochasticvalue < 20 )

      {

      OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,0,0,"Buy Order",MagicNumber,0,Green);

      Alert ("Buy Placed;" "  Stochastic = ",Stochasticvalue);

      }



   if (Stochasticvalue > 80)

      {

      OrderSend(Symbol(),OP_SELL,LotSize,Bid,0,0,0,"Sell Order",MagicNumber,0,Red);

      Alert ("Sell Placed;" "  Stochastic = ",Stochasticvalue);

      }

  }

//+------------------------------------------------------------------+
MQL4 Tutorial
MQL4 Tutorial
  • book.mql4.com
MQL4 Tutorial
Files:
whroeder1
15073
whroeder1  
adidasanlal: order send is not firing but i am getting  next line alert at every tick

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,0,0,"Buy Order",MagicNumber,0,Green);
    Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. if (Stochasticvalue < 20 )
    You are checking a level, an alert per tick is expected. Check for a change in signal.
    static double Stochasticvalue=50;
    double prevStoch = Stochasticvalue;
    Stochasticvalue = iStochastic(NULL, 1, 100, 50, 1, MODE_SMA, 0, MODE_MAIN, 0);
    if (Stochasticvalue < 20 && prevStoch >= 20)
    Function returns a double not a long. iStochastic - Technical Indicators - MQL4 Reference
Sergey Golubev
Moderator
95207
Sergey Golubev  
adidasanlal:

Dear Friends,

I'm writing my first EA. Order send is not firing but i am getting  next line alert at every tick. Could you please clarify why this is happening?   

...

Lal Adidasan
194
Lal Adidasan  
whroeder1:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. OrderSend(Symbol(),OP_BUY,LotSize,Ask,0,0,0,"Buy Order",MagicNumber,0,Green);
    Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  3. if (Stochasticvalue < 20 )
    You are checking a level, an alert per tick is expected. Check for a change in signal.
    static double Stochasticvalue=50;
    double prevStoch = Stochasticvalue;
    Stochasticvalue = iStochastic(NULL, 1, 100, 50, 1, MODE_SMA, 0, MODE_MAIN, 0);
    if (Stochasticvalue < 20 && prevStoch >= 20)
    Function returns a double not a long. iStochastic - Technical Indicators - MQL4 Reference
Thank you so much for your valuable time. It is really helpful.
To add comments, please log in or register