Keep a $var alive? And how to increase lot size?

 

 Im try to make a system when the proft is more then X pips, increase the lot size by X.

 

But how can i store a variable like  "increase_lot" .. Because now it will stays "10" and not keeping the change that is made later on in the script after "ontick"..

(it reloads the EA every tick i see, but how can i keep a variable alive? )

 

And is it possible to add more lot size when the trade is made?

I couldn't find a function in the documentation yet..

 

My example code:  

// start increase by profit more then 
input double Lot_i_start = 10;
// increase lot size by 
input double Lot_i_lot = 0.1;

// starting var   
double increase_lot = Lot_i_start;

void OnTick() {
        if(PositionSelect(symbol[i])) {
            if ((get_profit(symbol[i])- increase_lot) > Lot_i_start) {
                double increase_lot = get_profit(symbol[i]);
            }
        }
    }

 
Kima:

 Im try to make a system when the proft is more then X pips, increase the lot size by X.

But how can i store a variable like  "increase_lot" .. Because now it will stays "10" and not keeping the change that is made later on in the script after "ontick"..

(it reloads the EA every tick i see, but how can i keep a variable alive? )

And is it possible to add more lot size when the trade is made?

I couldn't find a function in the documentation yet..

My example code:  

1. Make the variable declared as globally declared variable, and then assign a value to it in OnInit().

2. I read somewhere but I forget where :). You can add, lot to existing opened position but your open price will be adjusted.  

Documentation on MQL5: Standard Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Standard Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Standard Constants, Enumerations and Structures / Indicator Constants / Price Constants - Documentation on MQL5
 

1. After some reading it does work now, thx.

// start increase by profit more then 
input double Lot_i_start = 10;
// increase lot size by 
input double Lot_i_lot = 0.1;

// starting var   
string increase_lot;

int OnInit()
  { 
  // get global var
      if(GlobalVariableCheck(increase_lot) == false) { 
GlobalVariableSet("increase_lot",Lot_i_start);
   }
}

void OnTick() {
        if(PositionSelect(symbol[i])) {
            if ((get_profit(symbol[i])- GlobalVariableGet("increase_lot")) > Lot_i_start) {
// add more lots size and edit "increase_lot" to avoid a loop
                 GlobalVariableSet("increase_lot", get_profit(symbol[i]));
            }
        }
    }

 

2. Ok, so it is possible.

Ive just red that Dimitar Manov also did something with the adding more value to the lots. 

And its no problem if the  open price will be adjusted, then i can also leave the line after adding more lots to my trade.

  

 
Kima:

1. After some reading it does work now, thx.

 

2. Ok, so it is possible.

Ive just red that Dimitar Manov also did something with the adding more value to the lots. 

And its no problem if the  open price will be adjusted, then i can also leave the line after adding more lots to my trade.

  

1. I was not suggesting of using MT Global Variable, I was suggesting of using ...


1. Make the variable declared as globally declared variable, and then assign a value to it in OnInit().

 Here's the example, based on your first code

// start increase by profit more then 
input double Lot_i_start = 10;
// increase lot size by 
input double Lot_i_lot = 0.1;

// starting var   
double increase_lot ; //--- this increase_lot variableis declared globally

//+---------------------------------------------+
int OnTick()
   {
   increase_lot = Lot_i_start; //--- then assign the value

   return (0);
   }

//+---------------------------------------------+
void OnTick() 
  {
        if(PositionSelect(symbol[i])) {
            if ((get_profit(symbol[i])- increase_lot) > Lot_i_start) {
                double increase_lot = get_profit(symbol[i]);
            }
        }
   }

 

 
Kima:

 Im try to make a system when the proft is more then X pips, increase the lot size by X.

 

But how can i store a variable like  "increase_lot" .. Because now it will stays "10" and not keeping the change that is made later on in the script after "ontick"..

(it reloads the EA every tick i see, but how can i keep a variable alive? )

 

And is it possible to add more lot size when the trade is made?

I couldn't find a function in the documentation yet..

 

My example code:  

 

1- Global 'Scope' Variable is the variables you can access from anywhere in your code.

2- They have to be declared outside the function that uses them. Best place is the top of the program.

3- They are by default 'static' variables which mean they save their value during the life cycle of the program.

4- Don't confuse 'Global Scope Variables' with 'MT Global Variables'.

5- If you use static keyword inside the function you will make your local variables save their value the life cycle of the program too.

 

 

To add more lot to the opened trade open new trade of the same symbol with the desired lot.
 

1. Ah okj, ive changed the code. It is more simple then i thought. 

2. okj, ill try

Reason: