Help Please

 

I've been reading searching trying and failing for 3 days on this EA and I cant figure out some things. here is what I have right now.

static int prevtime = 0; //Variables
int start() //Program start.
{
int counted_bars = IndicatorCounted();

if (Time[0] == prevtime) prevtime = Time[0]; // Only trade once per bar.

if(High[0]-Low[0]>20*Point && Close[0]<Open[0]) //Buy conditions.
OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0); //Buy define and execution.


if(High[0]-Low[0]>20*Point && Close[0]>Open[0]) //Sell Conditions.
OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0); //Sell define and execution.


Alert (GetLastError()); //Check for errors and display.



return(0); //Exit Start().

I can get it to make trades and send me an error every once in a while but...

Problems are: It trades on every tick. I know a fix is simple but I don't know anything about MQL besides its jibberish

Problem 2: When I input my SL and TP in the OrderSend Function I get error 130 constantly. I know what this is but I've tried 20 things and searched and searched and im stumped. They are set to 0 right now.

Objective: Open Sell after 20 point upmove with a 40 point TP and 30 SL. Same with Buy, but after a downmove. This CAN be closed out on the same bar but probably not.

Any input is much appreciated. Thank you!

 
static datetime prevtime=0; //It better be of datetime type.
int start() //Program start.
{
int counted_bars = IndicatorCounted(); //What is this line doing here? ;)

if (Time[0] > prevtime) //If time has passed...
 {
 if(High[0]-Low[0]>20*Point && Close[0]<Open[0]) //Buy conditions. 
  {
  OrderSend(Symbol(),OP_BUY,0.1,Ask,3,0,0); //Buy define and execution.
  prevtime=Time[0]; //Set new time.
  }

if(High[0]-Low[0]>20*Point && Close[0]>Open[0]) //Sell Conditions.
  {
  OrderSend(Symbol(),OP_SELL,0.1,Bid,3,0,0); //Sell define and execution.
  prevtime=Time[0]; //Set new time.
  }
 } //"If time has passed" loop ends here.

Alert (GetLastError()); //Check for errors and display.

return(0); //Exit Start().

Time[0] is the starting time of the current bar, so it would trade once per bar regardless of timeframe.

This should fix Problem 1, for a solution to Problem 2 look at the EA samples supplied with MetaTrader terminal.

 

  1. if(High[0]-Low[0]>20*Point
    This fails on 5 digit brokers. Adjust pips (and TP, SL) and points (slippage)
    //++++ These are adjusted for 5 digit brokers.
    double  pips2points,    // slippage  3 pips    3=points    30=points
            pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    
    When I input my SL and TP in the OrderSend Function I get error 130 constantly
    The above. Also on ECN brokers you must open the order and then set the TP, SL.

  2. It trades on every tick
    static datetime lastTrade0;
    if (condition && lastTrade0 != Time[0]){ lastTrade0 = Time[0];
       OrderSend(...)
    x
 

Ok. So I got it to only make 1 trade per bar. But I'm still stumped as to why I can't use a SL or TP in my OrderSend function. I understand how misunderstanding of digits could affect the program. I am testing this on USDJPY, there are 5 characters but only 3 digits. Digits defined in the mql4 book being AFTER the decimal. is this somehow causing a problem? *I have my variables at the top but I still want to know how this OrderSend() funcion works before I plug them in. Here is what I have Now.

 //+------------------------------------------------------------------+
//|                                                         Blah.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "https://www.metaquotes.net//"
extern int PriceMove=40;
extern int SL=40;
extern int TP=30;
extern int Lots=1;
extern int Slippage=3;
double  pips2points,    // slippage  3 pips    3=points    30=points
        pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int     init(){
    if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
    } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
  
 
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
static datetime prevtime=0; 
int start()                                    
{
if (Time[0] > prevtime)                                    //Reset bar time
 {
 if(High[0]-Low[0]>1*Point && Close[0]<Open[0])            //Buy Cnditions
  {
  OrderSend(Symbol(),OP_BUY,.01,Ask,3,0,0);                //Buy define and execution.
  Alert (GetLastError());                                  //Check for errors and display
  prevtime=Time[0];                                        //Reset bar time
  }
if(High[0]-Low[0]>1*Point && Close[0]>Open[0])             //Sell Conditions.
  {
  OrderSend(Symbol(),OP_SELL,.01,Bid,3,0,0);               //Sell define and execution.
  Alert (GetLastError());                                  //Check for errors and display
  prevtime=Time[0];                                        //Reset bar time
  }
 }            
 
return(0);                                                 //Exit Start
  
  }
//+------------------------------------------------------------------+

Reason: