Hello. Would appreciate some assistance please. I am not an expert on Mql4. I just piece together snippets of codes here and there and come up with something that works most times. The code below is a buy script and calculates the lots based on account size. It seems to work for all other currency pairs, but fails to work for JPY currency pairs - the lots size calculated is wrong (I have checked this with other position size calculators). I have checked and checked the code and cannot not figure out what is causing the incorrect calculation. Hope somebody can help please. Thank you
Inserted Code
#property show_inputs #define NL "\n" extern double RiskPercent = 0.1; extern int Slippage = 3; extern int BEtop = 10; extern double atrMultiple = 1; extern int magic = 0; extern string TradeComment="BUY"; //+------------------------------------------------------------------+ //| script "Open a new Buy Order" | //+------------------------------------------------------------------+ double getATRvalue() { int pipMult = 10000; if (StringFind(Symbol(),"JPY",0) != -1) { pipMult = 100; } //fetch current 14-period ATR value double atr = iATR(NULL,0,20,0); atr = atr * pipMult; return (atr); } //+-----------------------------------------------------------------+// int start() { double Price = WindowPriceOnDropped(); bool result; int cmd,total,error,slippage; //---- int NrOfDigits = MarketInfo(Symbol(),MODE_DIGITS); // Nr. of decimals used by Symbol int PipAdjust; // Pips multiplier for value adjustment if(NrOfDigits == 5 || NrOfDigits == 3) // If decimals = 5 or 3 PipAdjust = 10; // Multiply pips by 10 else if(NrOfDigits == 4 || NrOfDigits == 2) // If digits = 4 or 3 (normal) PipAdjust = 1; //---- double nTickValue=MarketInfo(Symbol(),MODE_TICKVALUE); //value of a tick if(Digits==3 || Digits==5){ //If the digits are 3 or 5 we normalize multiplying by 10 nTickValue=nTickValue*10; } double atr = MathRound(getATRvalue()); double stopAtr = atr * atrMultiple; double BEplus = stopAtr + BEtop; double stop_loss = Price - stopAtr * Point * PipAdjust; double take_profit = Price + BEplus * Point * PipAdjust; double Lots = (AccountBalance()*RiskPercent/100)/(stop_loss*nTickValue); Lots = (MathRound(Lots/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP)) / 2; //to round up the lots to prevent errors with minimum lot size by broker etc. Comment("ATR(20): ", atr+ NL,"SLpips: ", stopAtr+ NL, "TPpips: ", BEplus+ NL, "Lots ", Lots); slippage = Slippage * PipAdjust; if(Ask > Price) { result = OrderSend(Symbol(),OP_BUYLIMIT,Lots,Price,slippage,stop_loss,take_profit,TradeComment,magic,0,CLR_NONE); result = OrderSend(Symbol(),OP_BUYLIMIT,Lots,Price,slippage,stop_loss,0,TradeComment,magic,0,CLR_NONE); } else { result = OrderSend(Symbol(),OP_BUYSTOP,Lots,Price,slippage,stop_loss,take_profit,TradeComment,magic,0,CLR_NONE); result = OrderSend(Symbol(),OP_BUYSTOP,Lots,Price,slippage,stop_loss,0,TradeComment,magic,0,CLR_NONE); } //---- return(0);