[ARCHIVE]Any rookie question, so as not to clutter up the forum. Professionals, don't pass it by. Can't go anywhere without you - 5. - page 432

 
Can you tell me if the Sleep command doesn't seem to work at all, or should it be replaced by something else?
 
lottamer:
Can you tell me if the Sleep command does not seem to work at all, or should it be replaced by something else?
It doesn't.
 
This topic is now closed and archived. All posts after that will be deleted. Go to the new topic: https://www.mql5.com/ru/forum/145455
 
Comments not related to this topic have been moved to "Questions from MQL4 MT4 MetaTrader 4 beginners".
 

HI Great people, Please, i would like to ask direction on writing this final snippet to complete my bot, these are my achievements so far :

1. i have included a helper function that calculates the exact lot that risk a specific amount (in this case its 1% per trade), for a $10,000 account that should be $100 per trade 

 double CalculateUnitSize(string pMarket, double pMoneyCapital, double pRiskPercentage, int pStoplossPoints, double pAllowedMaxUnitSize) 
   {
      //---Calculate LotSize based on Equity, Risk in decimal and StopLoss in points
      double maxLots, minLots, oneTickValue, moneyRisk, lotsByRisk, lotSize;
      int totalTickCount;

      maxLots = MaxUnitSizeAllowedForMargin(pMarket, pMoneyCapital, pAllowedMaxUnitSize);
      minLots = SymbolInfoDouble(pMarket, SYMBOL_VOLUME_MIN);
      oneTickValue = SymbolInfoDouble(pMarket, SYMBOL_TRADE_TICK_VALUE); // Tick value of the asset

      moneyRisk = (pRiskPercentage/100) * pMoneyCapital;
      totalTickCount = ToTicksCount(pMarket, pStoplossPoints);

      //---Calculate the Lot size according to Risk.
      lotsByRisk = moneyRisk / (totalTickCount * oneTickValue);
      lotSize = MathMax(MathMin(lotsByRisk, maxLots), minLots);      
      lotSize = NormalizeLots(pMarket, lotSize);
      return (lotSize);
   }

   double MaxUnitSizeAllowedForMargin(string pMarket, double pMoneyCapital, double pAllowedMaxUnitSize) 
   {
      // Calculate Lot size according to Equity.
      double marginForOneLot, lotsPossible;
      if(OrderCalcMargin(ORDER_TYPE_BUY, pMarket, 1, SymbolInfoDouble(pMarket, SYMBOL_ASK), marginForOneLot)) { // Calculate margin required for 1 lot
         lotsPossible = pMoneyCapital * 0.98 / marginForOneLot;
         lotsPossible = MathMin(lotsPossible, MathMin(pAllowedMaxUnitSize, SymbolInfoDouble(pMarket, SYMBOL_VOLUME_MAX)));
         lotsPossible = NormalizeLots(pMarket, lotsPossible);
      } else {
         lotsPossible = SymbolInfoDouble(pMarket, SYMBOL_VOLUME_MAX);
      }   
      return (lotsPossible);
   }

   int ToTicksCount(string pMarket, uint pPointsCount) 
   {
      double uticksize = SymbolInfoDouble(pMarket, SYMBOL_TRADE_TICK_SIZE);
      int utickscount = uticksize > 0 ? (int)((pPointsCount / uticksize) * uticksize) : 0; //-- fix prices by ticksize
      return utickscount;
   }

   double NormalizeLots(string pMarket, double pLots) {       
      double lotstep   = SymbolInfoDouble(pMarket, SYMBOL_VOLUME_STEP);
      int lotdigits    = (int) - MathLog10(lotstep);
      return NormalizeDouble(pLots, lotdigits);   
   } 

2. i place a trade using a breakout strategy and obviously only risk 1% each time thanks to the above function , but there is one small dilemma;

sometimes the price breaks through but can't quite hit the TP which is set by me arbitrarily, I was wondering if there is a way to close as much of the running trade as possible,

so for example;

since we are only risking $100 per trade,

if we have a BUY trade with a TP set at 100pts,

and my function above accurately calculated we need to place 1lots to make $100 at 100pts,

and SL set at the same 100pts so 1:1 RR,

how can one close out 10$ partially after every 10 point increase in price towards my TP,

figuring out exactly how much out of the 1lot to partially close out each time is extremely tricky because remember the distance from the point the trade was taken increases as price gets closer to the TP in this example

e.g

at 10 points from the BUY price we need to close out x amount of lots ,

at 20 points from the BUY price we need to close out y amount of lots ,

at 30 points from the BUY price we need to close out z amount of lots ,

...and so on until we reach 90points

at which point if we add all the lot we closed it should still equal 1lot which is what we placed the trade with


MANY THANKS!