MQL4 EA lot size

 

Hello, traders/programmers,

Currently I'm developing couple of EAs'

After setting conditions to open a trade,

For example, lets say I'm using EMA crossover:


For the first position I'm using 0.01 lot(s), for the second position I want to use different lot size,

How can I set a different lot size for the second position?




 

Many ways to go about it.

One way is to use a static variable.

static int trades = 1;
double customLots = 0.2;

double lots = trades == 2 ? customLots : 0.1;

int ticket = OrderSend(_Symbol, OP_BUY, lots, ...);

if (ticket >- 1) {
  trades++;
}
 
u110725:

Can you demonstrate more on your comment?

Please don't ask for help via PM. It's more beneficial for you to respond here as you may receive more insight from other programmers.

static int trades = 1;
double customLots = 0.2;

double lots = trades == 2 ? customLots : 0.1;

int ticket = OrderSend(_Symbol, OP_BUY, lots, ...);

if (ticket >- 1) {
  trades++;
}

Any variable declaration that is prefixed with the static keyword is initialized one time only and will always retain its value in memory, even if it loses scope, until the program is uninitialized.

The Ternary operator is a shorthand form of writing an if-else statement.

double lots = trades == 2 ? customLots : 0.1;

// is the same as

double lots;

if (trades == 2)
  lots = customLots;
else
  lots = 0.1;

When you call OrderSend, it will return the ticket # if the order was placed successfully or -1 if it failed. Therefore, if ticket > -1, we know the order was placed.

If the order was placed, we increment the trades counter by one using trades++. This is a shorthand form of writing trades = trades + 1 or trades += 1. You can learn more about these types of operations here.

Visibility Scope and Lifetime of Variables - Variables - Language Basics - MQL4 Reference
Visibility Scope and Lifetime of Variables - Variables - Language Basics - MQL4 Reference
  • docs.mql4.com
Visibility Scope and Lifetime of Variables - Variables - Language Basics - MQL4 Reference
 

First of all thank you for responding,

But I tried to add the code but it didn't work.


Can you create a simple EA and insert the code?