//+------------------------------------------------------------------+ //| Fixed Ratio.mq4 | //| Copyright © 2006, Renato P. dos Santos | //| http://www.reniza.com/forex/sureshot/ | //| based on Ryan Jones' Fixed Ratio MM | //+------------------------------------------------------------------+ #property copyright "Copyright © 2006, Renato P. dos Santos" #property link "http://www.reniza.com/forex/sureshot/" //---- Name for DataWindow label string short_name="yourexpertname"; //---- Input parameters extern int StopLoss=50; extern int TakeProfit=100; extern double RiskLevel=0.03; extern double InitialBalance=0.0; extern bool UseSound=False; extern bool WriteLog=True; //---- Internal variables color clOpenBuy=DodgerBlue; color clModiBuy=Cyan; color clCloseBuy=Cyan; color clOpenSell=Red; color clModiSell=Yellow; color clCloseSell=Yellow; color clDelete=White; string NameFileSound="expert.wav"; double LotPrecision; //---- Static variables static int MagicNumber; static int handle; static double LastLot; static double LastBalance; static int Delta; static double MyMinLot; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- MagicNumber=MagicfromSymbol(); if ( WriteLog ) handle=FileOpen(LogFileName(),FILE_CSV|FILE_WRITE); InitializeLot(); //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- if ( WriteLog ) FileClose(handle); //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- OpenBuy(Ask); OpenSell(Bid); //---- return(0); } //+------------------------------------------------------------------+ // Auxiliary functions go here //+------------------------------------------------------------------+ int MagicfromSymbol() { int MagicNumber=0; for (int i=0; i<5; i++) { MagicNumber=MagicNumber*3+StringGetChar(Symbol(),i); } MagicNumber=MagicNumber*3+Period(); return(MagicNumber); } void InitializeLot() { // based on Ryan Jones' Fixed Ratio MM // to be called once on init() section if ( InitialBalance==0 ) { int dathandle=FileOpen(DatFileName(),FILE_CSV|FILE_READ,";"); if (dathandle>0) { InitialBalance=FileReadNumber(dathandle); if ( WriteLog ) WritetoLog("InitialBalance(stored)=$"+DoubleToStr(InitialBalance,2)); FileClose(dathandle); } else { InitialBalance=AccountFreeMargin(); if ( WriteLog ) WritetoLog("InitialBalance=$"+DoubleToStr(InitialBalance,2)); dathandle=FileOpen(DatFileName(),FILE_CSV|FILE_WRITE,";"); FileWrite(dathandle,InitialBalance); FileClose(dathandle); } } else { if ( WriteLog ) WritetoLog("InitialBalance(configured)=$"+DoubleToStr(InitialBalance,2)); dathandle=FileOpen(DatFileName(),FILE_CSV|FILE_WRITE,";"); FileWrite(dathandle,InitialBalance); FileClose(dathandle); } LastBalance=InitialBalance; Delta=MathRound(InitialBalance*RiskLevel); if ( WriteLog ) WritetoLog("Delta=$"+DoubleToStr(Delta,0)); double DeltaPrecision=MathRound(MathLog(Delta)/MathLog(10)); if ( WriteLog ) WritetoLog("DeltaPrecision="+DoubleToStr(DeltaPrecision,0)); double DeltaPower=MathPow(10,DeltaPrecision-1); if ( WriteLog ) WritetoLog("DeltaPower="+DoubleToStr(DeltaPower,0)); Delta=MathRound(Delta/DeltaPower)*DeltaPower; if ( WriteLog ) WritetoLog("Delta(normalized)=$"+DoubleToStr(Delta,0)); double LotSize=MarketInfo(Symbol(),MODE_LOTSIZE)/100; if ( WriteLog ) WritetoLog("LotSize=$"+DoubleToStr(LotSize,0)); MyMinLot=Delta/LotSize; if ( WriteLog ) WritetoLog("MyMinLot="+DoubleToStr(MyMinLot,2)); double LotStep=MarketInfo(Symbol(),MODE_LOTSTEP); LotPrecision=MathRound(-MathLog(LotStep)/MathLog(10)); if ( WriteLog ) WritetoLog("LotStep="+DoubleToStr(LotStep,LotPrecision)); if ( WriteLog ) WritetoLog("Precision="+DoubleToStr(LotPrecision,0)); MyMinLot=NormalizeDouble(MyMinLot,LotPrecision); if ( MyMinLot