How to start with MQL5 - page 17

 
Ahmad861 :

The code you provided does not check if an order is already open or set stoploss and takeprofit

Ok, here's your more complex code: 

Simple script: it opens a BUY position. In this case, you can set Stop loss and Take profit.

How to start with MQL5
How to start with MQL5
  • 2019.08.18
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
I've coded the EA and it works fine most of the time, stoploss and takeprofit are set perfectly, i noticed that sometimes the EA glitches and it keeps running on every tick. For example, i had a trade on CADJPY and i had stoploss set at 100 pips and take profit at 250 pips, i also had a print statement coded into the EA, the EA would open the trade and set stoploss a tick above and takeprofit a tick below on a sell trade or vice versa on a buy trade and would hit stoploss every time, in under 30 seconds i blew half the funds on my demo account and the console would print every on every single tick, its not a problem with JPY pairs and the same problem occured with other pairs as well, any fix ?
Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
Before you proceed to study the trade functions of the platform, you must have a clear understanding of the basic terms: order, deal and position...
 
Ahmad861 :
I've coded the EA and it works fine most of the time, stoploss and takeprofit are set perfectly, i noticed that sometimes the EA glitches and it keeps running on every tick. For example, i had a trade on CADJPY and i had stoploss set at 100 pips and take profit at 250 pips, i also had a print stated coded into the EA, the EA would open the trade and set stoploss a tick above and takeprofit a tick below on a sell trade or vice versa on a buy trade and the console would print every on every single tick, its not a problem with JPY pairs and the same problem occured with other pairs as well, any fix ?

It is necessary to strictly control the chain: signal - trade order - execution of a trade order.

I gave an example in

Forum on trading, automated trading systems and testing trading strategies

How to start with MQL5

Vladimir Karputov, 2020.10.22 20:31

We catch the transaction

Code: We catch the transaction.mq5

We catch the transaction


This is a blank - an example of how to control a trading signal. This code avoids the situation: opening multiple positions. How it works: after a trade order is generated, we increase the 'SPosition' structure. In OnTick, if 'SPosition' is greater than zero, go to the block for opening positions. In OnTradeTransaction, we monitor the situation when a position appears exactly on our signal.


 
Ahmad861 :

Can i send you my code through a personal message and could you tell me whats wrong with it ?

Not.

 
Ahmad861 :

This is my code excluding the logic, i know its very shabby but i need help figuring out what is wrong with it as i am still new to programming

Your code won't compile. Your code contains a huge bunch of errors.


And one more thing - you are committing the BIGGEST MISTAKE: you CREATE an indicator handle AT EVERY TICK! Remember: in MQL5, the indicator handle MUST BE CREATED ONCE !!! And you need to do this in OnInit () !!!

 
Vladimir Karputov:

Your code won't compile. Your code contains a huge bunch of errors.

That's the thing, it compiles fine, the only errors i get are to check OrderSend, that's why i dont understand what im doing wrong, if you could guide me as to how i can fix it, thank you

 
Ahmad861:

That's the thing, it compiles fine, the only errors i get are to check OrderSend, that's why i dont understand what im doing wrong, if you could guide me as to how i can fix it, thank you

Forum on trading, automated trading systems and testing trading strategies

How to start with MQL5

Vladimir Karputov, 2021.03.05 05:24

Your code won't compile. Your code contains a huge bunch of errors.


And one more thing - you are committing the BIGGEST MISTAKE: you CREATE an indicator handle AT EVERY TICK! Remember: in MQL5, the indicator handle MUST BE CREATED ONCE !!! And you need to do this in OnInit () !!!


 
How to start with MQL5
How to start with MQL5
  • 2020.09.17
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 

Thanks a ton, is everything else in the code fine ?

 
Vladimir Karputov:

im using multiple symbols and timeframes through loops, declaring handles in OnInit will not work without looping

Reason: