dynamic lotsize not working. help! - page 3

 
Gerard William G J B M Dinh Sy #:
I would never go back to flat use of the functions! Long live the OOP
Are there reasons, aside from convenience, for loving OOP in MQL5?
 
Vinicius Pereira De Oliveira #:
Please post compilable code that can reproduce the problem in the Tester.

here.

Ryan L Johnson #:
Are there reasons, aside from convenience, for loving OOP in MQL5?

will check out your code in 8 hours from now. whatever happens, thanks for being so generous with your time on this!

Files:
 
Ryan L Johnson #:
I haven't tested the comparative efficiencies of standard versus OOP, but I've always assumed that standard code is faster than accessing external include/library files.

Given your branching statements, OOP overhead is the last thing you should worry about:

Forum on trading, automated trading systems and testing trading strategies

dynamic lotsize not working. help!

Ryan L Johnson, 2026.04.28 15:54

//globals

ulong PositionTicket, positionTime;

ENUM_POSITION_PROPERTY_DOUBLE openprice;
ENUM_POSITION_PROPERTY_INTEGER opentime;

string positionType;

double intitialOpenPrice;

bool midLowerBought, midUpperBought, midLowerSold, midUpperSold;

//OnTick()

GetPositions();

if(PositionsTotal() == 0
   && your initial buy entry conditions here)
     {
      trade.Buy(0.35, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_ASK), 0, 0, NULL);
      GetPositions();
      intialOpenPrice = openPrice;
     }

if(PositionsTotal() == 1
   && positionType == "buyTrade"
   && SymbolInfoDouble(_Symbol, SYMBOL_ASK) <= initialOpenPrice - 160 * _Point))
     {
      trade.Buy(0.14, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_ASK), 0, 0, NULL);
      midLowerBought = true;
     } 

if(PositionsTotal() == 1
   && positionType == "buyTrade"
   && SymbolInfoDouble(_Symbol, SYMBOL_ASK) >= initialOpenPrice + 160 * _Point))
     {
      trade.Buy(0.14, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_ASK), 0, 0, NULL);
      midUpperBought = true;
     }

if(PositionsTotal() == 2
   && positionType == "buyTrade"
   && midLowerBought == true
   && SymbolInfoDouble(_Symbol, SYMBOL_ASK) <= initialOpenPrice - 320 * _Point))
     {
      trade.Buy(0.05, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_ASK), 0, 0, NULL);
     }

if(PositionsTotal() == 3
   && positionType == "buyTrade"
   && midLowerBought == true
   && midUpperBought == true
   && SymbolInfoDouble(_Symbol, SYMBOL_ASK) <= initialOpenPrice - 320 * _Point))
     {
      trade.Buy(0.05, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_ASK), 0, 0, NULL);
     }

if(PositionsTotal() == 0
   && your initial sell entry conditions here)
     {
      trade.Sell(0.35, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_BID), 0, 0, NULL);
      GetPositions();
      intialOpenPrice = openPrice;
     }

if(PositionsTotal() == 1
   && positionType == "sellTrade"
   && SymbolInfoDouble(_Symbol, SYMBOL_BID) >= initialOpenPrice + 160 * _Point))
     {
      trade.Sell(0.14, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_BID), 0, 0, NULL);
      midUpperSold = true;
     } 

if(PositionsTotal() == 1
   && positionType == "sellTrade"
   && SymbolInfoDouble(_Symbol, SYMBOL_BID) <= initialOpenPrice - 160 * _Point))
     {
      trade.Sell(0.14, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_BID), 0, 0, NULL);
      midLowerSold = true;
     }

if(PositionsTotal() == 2
   && positionType == "sellTrade"
   && midUpperSold == true
   && SymbolInfoDouble(_Symbol, SYMBOL_BID) >= initialOpenPrice + 320 * _Point))
     {
      trade.Sell(0.05, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_BID), 0, 0, NULL);
     }

if(PositionsTotal() == 3
   && positionType == "sellTrade"
   && midUpperSold == true
   && midLowerSold == true
   && SymbolInfoDouble(_Symbol, SYMBOL_BID) <= initialOpenPrice + 320 * _Point))
     {
      trade.Sell(0.05, _Symbol, SymbolInfoDouble(_Symbol, SYMBOL_BID), 0, 0, NULL);
     }

//custom function

void GetPositions()
  {
   if(PositionsTotal() >= 1)
     {
      for(int i = PositionsTotal() - 1; i >= 0; i--) // count all currency pair positions
        {
         // get the ticket number
         PositionTicket = PositionGetTicket(i);
         ENUM_POSITION_TYPE positiontype = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
         openprice = (ENUM_POSITION_PROPERTY_DOUBLE)PositionGetDouble(POSITION_PRICE_OPEN);
         opentime = (ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_TIME);

         if(positiontype == POSITION_TYPE_BUY)
           {
            positionType = "buyTrade";
            positionTime = POSITION_TIME;
           }

         if(positiontype == POSITION_TYPE_SELL)
           {
            positionType = "sellTrade";
            positionTime = POSITION_TIME;
           }
        }
     }
  }
Notes: positionTime is superfluous but I left it in place in case you have a use for it. The code can be sped up by replacing strings with int's─strings are merely more illustrative here. You can also get the count of positions from the custom function instead of PositionsTotal() if you wish. Order return results are omitted. The Bought/Sold variables also have to be conditionally reset to false at some point in the code─presumably when PositionsTotal() == 0. I wrote the OnTick() code directly into the Forum code module, so it is uncompiled and untested.
 
Vladislav Boyko #:

Given your branching statements, OOP overhead is the last thing you should worry about:

Try wrapping similar code in functions and stop copying and pasting it. Then you'll see opportunities to optimize your branching statements. As you continue to refactor and optimize your code, you will most likely end up using OOP.
 
Vladislav Boyko #:
Try wrapping similar code in functions and stop copying and pasting it. Then you'll see opportunities to optimize your branching statements. As you continue to refactor and optimize your code, you will most likely end up using OOP.

I like OOP and I use it a lot, but it's by choice not by facts. You will hardly find factual arguments to convince someone OOP is better than procedural coding.

 
Alain Verleyen #:

I like OOP and I use it a lot, but it's by choice not by facts. You will hardly find factual arguments to convince someone OOP is better than procedural coding.

I love breaking code into small functions. I don't know how to continue breaking code into small functions as my codebase grows without encapsulation. I'm sure there are non-OOP approaches that solve this problem, but I'm not familiar with them, and I can't do without OOP.

Regarding other approaches, I've long been considering looking at the Linux kernel source code—I'm curious how they organized that huge C codebase, which, as far as I know, doesn't have OOP. But I keep putting it off because I love OOP.

 
Vinicius Pereira De Oliveira #:
Please post compilable code that can reproduce the problem in the Tester.
not sure if the code i posted will show the issue in backtest, but the code is 100% same code that i have used last week and this on demo account, live charts. my dynamic lotsize is working for buy trades, but all my sell trades are the same, bigger lotsize, when most of em should be smaller 0.14. see description of my planned dynamic lotsize in a previous comment. (#14)
 
Vladislav Boyko #:
Try wrapping similar code in functions and stop copying and pasting it.

As a caveat to that, too many custom functions in an mq5 file will jam up the Tester as previously confirmed by Fernando.

@Michael Charles Schefe, you might even be confirming this based on the plethora of custom functions in your code and its Tester incompatibility. That has happened to me as well in the past.

Having said that, there's nothing preventing anyone from putting my proposed OnTick() code into custom functions.

 
Ryan L Johnson #:
As a caveat to that, too many custom functions in an mq5 file will jam up the Tester as previously confirmed by Fernando.

I will never believe this until someone provides me with a way to reproduce it myself.

Ryan L Johnson #:
Having said that, there's nothing preventing anyone from putting my proposed OnTick() code into custom functions.

Yes, sure! You claim that OOP can be "expensive" in terms of performance, and then you publish procedural code that can be optimized. That's why I wrote a couple of comments. But I'm already drifting off-topic, sorry, I won't continue.

 
Vladislav Boyko #:

Yes, sure! You claim that OOP can be "expensive" in terms of performance, and then you publish procedural code that can be optimized. That's why I wrote a couple of comments. But I'm already drifting off-topic, sorry, I won't continue.

even tho i would add an int variable=PositionsTotal() and reused this int for all those lines in OnTick; i do not see anymore optimising opportunities there. i woul also do same for ask and bid prices, however, this would only be out of habit, i do not think it would make the code run faster since these lines are not used at every tick -- correct?

Ryan L Johnson #:


all my trades use same signal conditions, ie not a grid system, just fyi, but thats ok i am sure i will still use your code to find a fix.