Help me to understand simple code: open position (buy or sell) and trailing stop

 

 Hi everyone,

i started to study MQL5 and robo trading in the last 2 weeks, then i am totally new to this coding topic.

I am setting up the basic of the code.

This is my idea:

  • - based on conditions ("the strategy", but this will come later) i setup buy o sell position
  • - setup a maxRisk, ie maximum loss for the order
  • - setup a minProfit, so i this % is reached, then i can trail the SL e TP to maximize the profit
  • - setup closing conditions (eg conditions to close the positions, but this is still not there).

I have a simple code attached, but I have some issue:

  • - trailing stops is not working
  • - TP for sell orders is not working (if the price is lower of TP, the position is not closed).

Can you please help to understand how to correct the code and improve these basic functions?

Sorry if it is too trivial.

Thanks all for the help and support!

Petruss

Files:
PPLib.mqh  9 kb
PP-EA-04b.mq5  19 kb
 
petruss #:

sorry, noted I uploaded a not-working file. Here a working version:

Here is some support materials 
https://www.mql5.com/en/forum/189685
https://www.mql5.com/en/forum/296230

NOTE-1: Traders and coders are working for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members on this forum.

Freelance section of the forum should be used in most of the cases.


NOTE-2: this is English-speaking forum. Italian comments in your code adding an extra layer of complexity.
Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2025.02.08
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

Oleksandr, Fernando,

many thanks for your comments!

I will have a look at the links :)