Other News

How To Assign an MQL Variable Based on Logic


Q:

What is the best way to set and update a variable automatically? i.e. If I wanted to change a variable from a default false value to true following some logic evaluation, which function would I use to do this?

 

A:

This does not require a function, you can simply enter the desired value on the assignment tab of the Variable configuration window.

After you drag, drop and save a variable, the variable is added to the toolbox. From there, multiple copies of the variable can be added to the drawing. Each copy can have its own unique assigned value.

 

This image shows where the variable “MyTestVar” is saved in the ToolBox:


 

 

Multiple copies of MyTestVar can be dragged and dropped onto the drawing pad. In the drawing below, they are attached to the different outputs of a logic element:


 

Each copy of the variable can assign a unique value. In the drawing above, a value of 10 is assigned to MyTestVar if the logic evaluates to true, otherwise a value of 20 is assigned to MyTestVar.

 


 

 

 

 

 

 



To get notified about the latest questions and answers, follow us!



RAQ

Close Trade After 24 Hours

Q: 

How can I tell my EA to close a trade if a profit is not hit after 12 candles. I am trading on the 1Hour timeframe and I only want the trade to be open for 24 hours.  If it is not in profit, close trade.

A:
The function fnGetOpenOrderInfo can be used to get the information you need:
(1) How long the trade has been opened
(2) If the trade is profitable
The fnGetOpenOrderInfo function is found in the Trade menu of the Function’s Toolbox:
To get the time the trade has been opened, select INFO_OPEN_MINUTES for the InfoType parameter, and give it a nice name like “TradeOpenMinutes”:
To get the prfot of the trade, select  INFO_PROFIT for the InfoType parameter, ang give it a name like “TradeProfit”:
Now these two values can be used in a Logic Element. If the logic evaluates to True, the Trade is closed using the fnCloseOrder function:
The final drawing looks something like this:

 VTS EA Package: Download and load this ZIP file into VTS

 To get notified about the latest questions and answers, follow us!
RAQ