dynamic lotsize not working. help! - page 2

 
Ryan L Johnson #:
TBH, I actually prefer to use standard code elements too. 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. Maybe someone else can chime in here and give us the hard facts.

it was my "learnings"; albeit over 20 years ago now -- that lower machine languages, ie languages that use class(es) -- make instructions to hardware "directly", which would under most circumstances use the faster cpu caches or to hardware directly; while mql4/5/python/pine script -- DO NOT.

if anything i said is incorrect, then, please correct me. 

but, of course, some of the std library codes may not be 100%, correct syntax required to work with mt5, but if it is, then the std libary code will be faster. But again, if this is wrong info, then, please correct me.
 
Michael Charles Schefe #:

it was my "learnings"; albeit over 20 years ago now -- that lower machine languages, ie languages that use class(es) -- make instructions to hardware "directly", which would under most circumstances use the faster cpu caches or to hardware directly; while mql4/5/python/pine script -- DO NOT.

if anything i said is incorrect, then, please correct me. 

but, of course, some of the std library codes may not be 100%, correct syntax required to work with mt5, but if it is, then the std libary code will be faster. But again, if this is wrong info, then, please correct me.

I don't want to speculate like a know-it-all without factual testing. That's why I assume that include/library files must pass data through MT5 which then, in turn, communicates with an mq5 file─sort of like a terminal-wide GlobalVariable. In contrast, I imagine that standard code elements only communicate with MT5.

Having said that, a machine system file like a DLL is probably faster than any of that─which would be aligned with your learnings. Regarding mqh files, this seems to be an apples versus oranges comparison. I think that we can all agree that an mqh file is not a machine system file.

Obviously, I don't have any "inside" knowledge of the inner workings of MT5. Presumably, we can't because that's proprietary info. I do, however, occasionally Print() test code blocks from start of execution to finish for speed tracking purposes. The only issue there is that there are a multitude of ways to code the same logic, so the test might be somewhat subjective.

If no one chimes in here with more definite info, I'll do such a test and post back with the results measured in milliseconds... when the markets reopen.

 
Ryan L Johnson #:

If no one chimes in here with more definite info, I'll do such a test and post back with the results measured in milliseconds... when the markets reopen.

Thanks. I already do some print commands, but after this issue, my plan is to add a lot more of these print lines; but am busy much of this week with other things.
 
Ryan L Johnson #:


my dybamic lotsize is working for buys, BUT NOT FOR SELLS.

so it would seem that my issue was unrelated to my abusing the class. something else.

i have only posted the OnTick and the function that sends the trade. if want more, then i have no issue posting the full files, only posted the excerpt for readability. just ask for full files and they will be posted either via dm or here.

I want possible of 3 lotsizes. initial lot 0.35, others 0.14 and 3rd possible but rare to happen 0.5.

I want initial lot 0.35, and then a gap of 160 points, but trades can still happen in this gap, but those trades in this 160 gap above and below the most recent trade of 0.35 -- will be 0.14.

The first trade that opens outside this 320 gap will be 0.5 -- BUT. only the first trade on losing side of the closest 0.35 trade.

If all 0.35 trades close, then, the next trade that opens will be 0.35.

NOTE this is all working 100% with buy side, but not working at all on sell side. i have 14 trades open in 500 point range, all sell trades, and all same size of 0.35. 

And the biggest confusion of all this is that it is working on my backtests, but not on live demo account.

 
Michael Charles Schefe #:

my dybamic lotsize is working for buys, BUT NOT FOR SELLS.

so it would seem that my issue was unrelated to my abusing the class. something else.

i have only posted the OnTick and the function that sends the trade. if want more, then i have no issue posting the full files, only posted the excerpt for readability. just ask for full files and they will be posted either via dm or here.

I want possible of 3 lotsizes. initial lot 0.35, others 0.14 and 3rd possible but rare to happen 0.5.

I want initial lot 0.35, and then a gap of 160 points, but trades can still happen in this gap, but those trades in this 160 gap above and below the most recent trade of 0.35 -- will be 0.14.

The first trade that opens outside this 320 gap will be 0.5 -- BUT. only the first trade on losing side of the closest 0.35 trade.

If all 0.35 trades close, then, the next trade that opens will be 0.35.

NOTE this is all working 100% with buy side, but not working at all on sell side. i have 14 trades open in 500 point range, all sell trades, and all same size of 0.35. 

And the biggest confusion of all this is that it is working on my backtests, but not on live demo account.

I'm not going to lie. I can't make heads or tails of your code.

You can achieve 3 irregular lot sizes by simply referencing position volumes, position open prices, current prices, and using some if-then statements. If you're interested in a total rewrite, I'm sure I have an example that I can dig up. Just let me know.

 
Ryan L Johnson #:

I'm not going to lie. I can't make heads or tails of your code.

You can achieve 3 irregular lot sizes by simply referencing position volumes, position open prices, current prices, and using some if-then statements. If you're interested in a total rewrite, I'm sure I have an example that I can dig up. Just let me know.

that is what i have done in the loop in OnTick. but if you are willing to do the "rewrite", which i think you mean a rewrite of the open trades function, then i am willing to give anything a go at this point. no pun intended.
 
Michael Charles Schefe #i have only posted the OnTick and the function that sends the trade. if want more, then i have no issue posting the full files, only posted the excerpt for readability. just ask for full files and they will be posted either via dm or here.
Please post compilable code that can reproduce the problem in the Tester.
 
Michael Charles Schefe #:
that is what i have done in the loop in OnTick. but if you are willing to do the "rewrite", which i think you mean a rewrite of the open trades function, then i am willing to give anything a go at this point. no pun intended.
Ok. Will do...
 
Ryan L Johnson #:
TBH, I actually prefer to use standard code elements too. 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. Maybe someone else can chime in here and give us the hard facts.
I would never go back to flat use of the functions! Long live the OOP
 
//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.