How do I open a trade position

 

Hi, my name is Enrico. I've been asked to develop an MQL5 EA and it has been hard so far as I've never been involved in this field.


I'd like to ask one simple question: I'd like to know how to actually open a trade.

I need to open the first trade on a random position between Long or Short. So I could randomize the number and test it's working but I can't get the order function to work.


Would you have any suggestion?

The code I wrote so far is what follows:

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

void OnInit()
   {
      MathSrand( GetTickCount() );
   }

int r = MathRand();

void OnTick()
   {
   double Ask = NormalizeDouble ( SymbolInfoDouble (_Symbol, SYMBOL_ASK), _Digits );
      if (r <= 16383)
            {
               trade.Buy (0.01, NULL, Ask, 0, ( Ask + 100 * _Point ), NULL);
               Print(r, " bitches in my Lex");
            }
            else
            {
               trade.Sell (0.01, NULL, Ask, 0, ( Ask + 100 * _Point ), NULL);
               Print(r, " of my beats in yo stereo");
            }


Thank you very much for your time, I hope you guys can help me out.

E.

 

This line must be inside OnTick().

int r = MathRand();
 
ef91:

Hi, my name is Enrico. I've been asked to develop an MQL5 EA and it has been hard so far as I've never been involved in this field.


I'd like to ask one simple question: I'd like to know how to actually open a trade.

I need to open the first trade on a random position between Long or Short. So I could randomize the number and test it's working but I can't get the order function to work.


Would you have any suggestion?

The code I wrote so far is what follows:


Thank you very much for your time, I hope you guys can help me out.

E.


Please insert the code correctly: Правильно вставляем код на форуме (I, for the first time, corrected your message).
 

Hi Enrico

for the Sell there is a bug with tp value

trade.Sell (0.01, NULL, Ask, 0, ( Ask + 100 * _Point ), NULL)


You must modify in 

trade.Sell (0.01, NULL, Ask, 0, ( Ask - 100 * _Point ), NULL)
 
Alain Verleyen:

This line must be inside OnTick().


Thank you Alain for your reply.


The reason why that line is outside OnTick() is because I only need to generate a random value for the position only the first time that I launch the EA.

After that is all about checking every 60 sec whether that trade reached TP or SL, and, if so, close it and open a second trade in the opposite direction of the first one. And so on.


Please let me know if the modify you suggested still applies to my case.


Thank you

E.

 
Vladimir Karputov:

Please insert the code correctly: Правильно вставляем код на форуме (I, for the first time, corrected your message).


Thank you for your help Vladimir.


Is there a help page on the forum where it shows how to do it in english?

Sorry for asking but I don't understand the language in which the article you linked is written.


Thank you again

Enrico

 
eastore1:

Hi Enrico

for the Sell there is a bug with tp value


You must modify in 


Hi EAS,

can you explain to me why is it a bug?

Being that I don't have that much knowledge about the language, it'd be very helpful if you could also explain to me why, so that when I'm using that function again I can use it in the most efficient way.


Thank you very much for your help.

E.

 
ef91: The reason why that line is outside OnTick() is because I only need to generate a random value for the position only the first time that I launch the EA.
That won't work. First you generate r (global initialization,) then you SRand. Assign the value in OnInit.


ef91: can you explain to me why is it a bug?
How can the TP of a sell order be higher then the open price? That would be take negative profit.
 
whroeder1:
That won't work. First you generate r (global initialization,) then you SRand. Assign the value in OnInit.


How can the TP of a sell order be higher then the open price? That would be take negative profit.


- You mean that first I declare r in the global variables as a random int with MathRand() and then I set MathSrand(GetTickCount); in void OnStart() ?


Thank you for your help whroeder

E.


 
ef91:


Hi EAS,

can you explain to me why is it a bug?

Being that I don't have that much knowledge about the language, it'd be very helpful if you could also explain to me why, so that when I'm using that function again I can use it in the most efficient way.


Thank you very much for your help.

E.

Hi Enrico

The Tp of a Buy position must be set on an higher value than the openprice

Instead the Tp of a Sell position must be set on a lower value

BUG: in your code you try to set, for sell position, a TP (ASK + 100 point) that is bigger than open price (ASK)

Probably is a copy and paste error

Cordially

A.F.

 
eastore1:

Hi Enrico

The Tp of a Buy position must be set on an higher value than the openprice

Instead the Tp of a Sell position must be set on a lower value

BUG: in your code you try to set, for sell position, a TP (ASK + 100 point) that is bigger than open price (ASK)

Probably is a copy and paste error

Cordially

A.F.


Thank you A.F.

Is just that I didn't know what exactly those values in the trade.Buy functions were so I didn't know how to intervene on it.

I was just copying it without thinking about it (and getting it wrong obviously).


I seem to have solved the problem of performing the first random trade (if it can be of any interest) only once at the beginning of the EA:

//+------------------------------------------------------------------+
//|                                               cheduecoglioni.mq5 |
//|                                                   Gianni Esperti |
//|                                  https://www.gianniesperti.come? |
//+------------------------------------------------------------------+
#property copyright "Gianni Esperti"
#property link      "https://www.gianniesperti.come?"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

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

uint s;
bool bos;
int r;


int OnInit()
   {
   
      MathSrand( GetTickCount() );
      
   s = GetTickCount();
   r = MathRand();

   double Ask = NormalizeDouble ( SymbolInfoDouble (_Symbol, SYMBOL_ASK), _Digits );
   
      if (r <= 16383)
            {

               Print(r, " bitches in my Lex");
               trade.Buy (0.01, NULL, Ask, 0, ( Ask + 100 * _Point ), NULL);
               bos = true;

            }
            else
            {
            
               Print(r, " of my beats in yo stereo");
               trade.Sell (0.01, NULL, Ask, 0, ( Ask - 100 * _Point ), NULL);
               bos = false;
            
            }
   
   
    
   return (INIT_SUCCEEDED);
   }
Reason: