Reset variables after enabling autotrade

 

Hello, is there a way to reset variables automatically after disabling AutoTrading (or after enabling it)?

Example: variable = 0 and then it's equal to 1 because of the code.

I decide to disable AutoTrading and after one hour I decide to enable AutoTrading. I want the variable to be equal to 0 again. Is there a way to do this step (reset variables) automatically?

Thanks,

Marko

 
Marko Tuta:

Hello, is there a way to reset variables automatically after disabling AutoTrading (or after enabling it)?

Example: variable = 0 and then it's equal to 1 because of the code.

I decide to disable AutoTrading and after one hour I decide to enable AutoTrading. I want the variable to be equal to 0 again. Is there a way to do this step (reset variables) automatically?

Thanks,

Marko

Do it on OnInit, OnInit is triggered when enabling AutoTrading again. 
 
Marko Tuta: is there a way to reset variables automatically after disabling AutoTrading (or after enabling it)?

I'm assuming MT4/MQL4, given that you refer to AutoTrading and not AlgoTrading. So, your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893

 
Zaky Hamdoun #: OnInit is triggered when enabling AutoTrading again. 

Since when is the OnInit triggered when the Auto/Algo Trading button is enabled or disabled?

I have never seen that happen.

 
Marko Tuta: Is there a way to reset variables automatically after disabling AutoTrading (or after enabling it)?

Example: variable = 0 and then it's equal to 1 because of the code.

I decide to disable AutoTrading and after one hour I decide to enable AutoTrading. I want the variable to be equal to 0 again. Is there a way to do this step (reset variables) automatically?

Please clear up if this is a MQL4 or MQL5 question.

For both cases, the approach is similar. You would periodically read the status information of the Auto/Algo Trading and adjust your code logic or change variables accordingly.

 
Fernando Carreiro #:

Since when is the OnInit triggered when the Auto/Algo Trading button is enabled or disabled?

I have never seen that happen.

My bad, I thought it did but it was when changing the inputs, mixed up both. 

Then yeah, I guess solution would be to constantly store the auto trading status and check when it switches from off to on. 
 

Thank you for responses. I thought that i can solve using a code like this:

if (isTradeAllowed() == false) //autotrading disabled
{
        variable = 0; //reset variable
}

but I don't think this can solve my problem. If EA is disabled it will not "read" my code, right? Or am I wrong?

Improperly formatted code edited by moderator.
 
Marko Tuta #: Thank you for responses. I thought that i can solve using a code like this: but I don't think this can solve my problem. If EA is disabled it will not "read" my code, right? Or am I wrong?

Your code is without context so it is difficult to answer, but your approach is too simplified.

The OnTick() event handler is called on every tick. Are you going to reset the variable on every tick?

Instead detect when the state changes, either from enabled to disabled, or from disabled to enabled, and set your variables accordingly.

Also, please use the CODE button (Alt-S) when inserting code.

Code button in editor

 

Thanks. Inside the onTick() section I have the main code and my variable can takes value 0 or 1. 

At the beggining it has value 0. When an order opens, variable = 1. I defined my conditions when a trend begins and I want to open only one order per trend. Until the trend continuous I will not close the order.

Then the trend changes (always following my definitions and conditions) and the variable is again set to 0. I use this variable because I don't want to open more than 1 orders inside a same trend. If variable = 0, EA can open an order if there are conditions, if variable = 1, no new opens.

All of this happens inside the onTick() event.

Anyway, I want to reset this variable to 0 everytime I manually disable AutoTrading. In this way, when I will enable AutoTrading again, the variable will have the value 0 and it will open an order if there will be conditions.

Do you think that it's ok to put this code inside onTick()?

if (isTradeAllowed() == false) //autotrading disabled
{
        variable = 0; //reset variable
}
 
Marko Tuta #: If EA is disabled it will not “read” my code, right? Or am I wrong?
You are wrong. There is no such thing as “EA disabled.” Either it is running (with a smile face) or it is not. Trading disabled is irrelevant to EA running.
 
Hmm, ok. I supposed so. Do you know a way to solve this?