Adding unknown values in mql5

 

Good day everyone.

Firstly, I appreciate everyone who have contributed on this forum. Thanks a lot

I need help with a particular arithmetic.

I want to add some values together In mql5 , the value might be currently unknown, 

But would be known during the execution of the code

For example, If want to add all the lotsize of all the trades made during a bot trading or to add the profit or loss of closed trades

How can I do the addition?

Thanks in advance 

 
Mary Kehinde Olaniyi:

Good day everyone.

Firstly, I appreciate everyone who have contributed on this forum. Thanks a lot

I need help with a particular arithmetic.

I want to add some values together In mql5 , the value might be currently unknown, 

But would be known during the execution of the code

For example, If want to add all the lotsize of all the trades made during a bot trading or to add the profit or loss of closed trades

How can I do the addition?

Thanks in advance 

Moved your thread to section "Experts and Automated Trading Systems"

If you can show some code, we might be able to give you some advice. - But at the moment it is unclear what you are able to code.

 
Dominik Egert #:

Moved your thread to section "Experts and Automated Trading Systems"

If you can show some code, we might be able to give you some advice. - But at the moment it is unclear what you are able to code.

Thanks for the reply

I'm just trying to get aquatinted with the buttons here, but here's what I'm trying to do


If ( I opened 5 trades )

{

If( the positions are closed ) 

{

I want to add the lotsize of all the trades together and store it in a variable

}

}


I've tried using the "+=" to add the values to a variable, but it only stores the value of the recently closed trade,

But Instead I want to add the lotsize of all the 5 trades 


Thanks in advance 

 
Mary Kehinde Olaniyi #:

Thanks for the reply

I'm just trying to get aquatinted with the buttons here, but here's what I'm trying to do


If ( I opened 5 trades )

{

If( the positions are closed ) 

{

I want to add the lotsize of all the trades together and store it in a variable

}

}


I've tried using the "+=" to add the values to a variable, but it only stores the value of the recently closed trade,

But Instead I want to add the lotsize of all the 5 trades 


Thanks in advance 

I suggest you start with the book.

https://www.mql5.com/en/book

Else, I can suggest to do a search on the forum, "How to get started"

 

Does your EA close all the 5 position?

if so you could search deal history for the last 5 deal. Find the deal volume and increment it through all 5 deal.

You could see the code here as a guide: https://www.mql5.com/en/docs/trading/historyselect

Documentation on MQL5: Trade Functions / HistorySelect
Documentation on MQL5: Trade Functions / HistorySelect
  • www.mql5.com
HistorySelect - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Luandre Ezra #:

Does your EA close all the 5 position?

if so you could search deal history for the last 5 deal. Find the deal volume and increment it through all 5 deal.

You could see the code here as a guide: https://www.mql5.com/en/docs/trading/historyselect

Thanks for the response. Yes the 5 positions are closed, how can I increment it, because if I use the "++" operator, it would only add 1 to the volume

And besides, the 5 trades is just an illustration, it could be 10 or more, lets just say throughout the trading session

 
Dominik Egert #:

I suggest you start with the book.

https://www.mql5.com/en/book

Else, I can suggest to do a search on the forum, "How to get started"

This is going to be time consuming, but I'll go through it

Thanks for your contribution 

 
You increment it using +=.

Currently I'm away from my computer, will give you example if I can.
Documentation on MQL5: Language Basics / Operations and Expressions / Assignment Operations
Documentation on MQL5: Language Basics / Operations and Expressions / Assignment Operations
  • www.mql5.com
Assignment Operations - Operations and Expressions - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Luandre Ezra #:
You increment it using +=.

Currently I'm away from my computer, will give you example if I can.

Thanks for your assistance

I also tried implementing an idea I had earlier today, I used something like this

total_loss = 0;

If ( last_profit != total_loss) 

total_loss = total_loss + last_profit;


The explanation for the above illustration is;

If the last closed deal was not equals to 0

In case the last closed deal was a loss, i want to store the value as total loss and then 

If the next trades closed in profit, I want to subtract it from the total loss 


The main idea of this is to be able to track how much I've lost and how quick i can recover from it


The only challenge I faced while implementing this is that the total loss variable changes as the last closed trade profit/loss changes

Making it very hard to make calculations, as I can't add a value that is constantly changing


Maybe if there was a way to store the last closed deal profit/loss value without it changing once a new one emerges

Sorry if my explanation is very lengthy

You can drop your ideas here even if it isn't in a code format, I'll try to implement them


And regarding the mql5 docs, I can't find anything that answers my question there

 

I want to say a big thank you to everyone who contributed

There was an error in my code, I was trying to keep the value of a function instead of keeping the value of the profit only

I've fixed this and I'm all good here

Once again, thanks y'all 🙏😁

Reason: