Welcome to Blog No 7 in this miniseries of 10 blogs in which I will exploit MT4GUIs functionalities.
I hope you find it both interesting as well as valuable!
Sincerely
Thommy Tikka
Risk- and money management are important “PRE-trade” components to handle to achieve success in trading.
Fixed lot size or risk based lot size?
Risk based on Account balance or on Equity balance?
The choice is yours when you use following piece of code!
// mt4gui-PRETradeManagement.mq4 // Created by Thommy Tikka (thommy_tikka@hotmail.com) // Version 0.1, 20130412 // Include library file with common functions for mt4gui #include <mt4gui.mqh> // Declare global variables int hwnd = 0; int enablePRETradeBtn, moneyMgmtBtn, visualSLLineBtn; int gUIXPos; double actualSLPrice, actualSLPips; double usePoint, lotSize=0; string sLStatus="Default", lSStatus="Fixed"; //Set default PRE-trade variables, possible to change in the EA Properties extern string MONEYMANAGEMENTSETTINGS = "----- Money/Risk Management -----"; extern bool MoneyManagementRiskInPercent = false, // Money Management, if true uses Risk Percentage, if false uses Lot Size RiskBasedOnAccountBalance = true; // Risk base on AccountBalance or AccountEquity extern double RiskPercent = 10, // Risk Percentage, percent amount of account to risk when Money Management set to true, overrides Lot Size FixedLotSize = 0.1, DefaultStopLoss = 10; extern string GUIPOSITION = "----- GUI Position -----"; // Y-Position the guiButtons extern int GUIYPos = 50; // MT4 function called during the module initialization //+-------------------------------------------------------------------------------------------+ int init() { hwnd = WindowHandle(Symbol(),Period()); guiRemoveAll(hwnd); // Automatic X-Postion to the right gUIXPos = guiGetChartWidth(hwnd)-200; // Delete visual lines from chart DeleteVisualSLLine(); // Add screenshot button to chart function call CreateButtons(); // Disable all gui Buttons ChangePRETradeState(); // Set PointValue usePoint = PipPoint(Symbol()); // Set default actualSL based on EA properties actualSLPips = DefaultStopLoss; return(0); } // MT4 function called during deinitialization of the module //+-------------------------------------------------------------------------------------------+ int deinit() { // Very important to cleanup and remove all gui items from chart if (hwnd>0) guiRemoveAll(hwnd); guiCleanup(hwnd); // Delete visual lines from chart DeleteVisualSLLine(); // Remove eventual Comment Comment (""); return(0); } // MT4 function called on every MT4 tick //+-------------------------------------------------------------------------------------------+ int start() { // Call function ManageEvents on every MT4 tick ManageEvents(); // Call function Move SL based on line if (!ObjectFind("INT.SLLine")==-1) ChangeSL(); // Call function to calculate lotSize CalculateLotSize(); // Show SL and lotSize¨ if (actualSLPrice!=0) sLStatus="Actual"; if (MoneyManagementRiskInPercent && RiskBasedOnAccountBalance) lSStatus="AccountBalance riskbased LotSize: "; if (MoneyManagementRiskInPercent && !RiskBasedOnAccountBalance) lSStatus="EquityBalance riskbased LotSize: "; if (!MoneyManagementRiskInPercent) lSStatus="Fixed LotSize: "; Comment (sLStatus+" SL Pips: "+DoubleToStr(actualSLPips,1)+"\n"+lSStatus+DoubleToStr(lotSize,5)); } // MT4GUI functions to create Button //+-------------------------------------------------------------------------------------------+ void CreateButtons() { // Position and size of the button ,x ,y ,a ,b (x, y = position from top left corner. a, b = size of button) enablePRETradeBtn = guiAdd(hwnd, "checkbox", gUIXPos, GUIYPos, 140, 30, "Enable PRETrade"); guiSetBgColor(hwnd, enablePRETradeBtn, LightGray);guiSetTextColor(hwnd, enablePRETradeBtn, Black); // Add Risk/Fixed LotSize Btn if (MoneyManagementRiskInPercent) moneyMgmtBtn = guiAdd(hwnd, "button", gUIXPos, GUIYPos+30, 140, 20, "Risk Lots"); else moneyMgmtBtn = guiAdd(hwnd, "button", gUIXPos, GUIYPos+30, 140, 20, "Fixed Lots"); // Add visual SL Line Btn visualSLLineBtn = guiAdd(hwnd, "button", gUIXPos, GUIYPos+50, 140, 30, "SL-line"); } // MT4GUI functions to capture Button Events //+-------------------------------------------------------------------------------------------+ void ManageEvents() { // If PRE-Trade Btn is checked, enable PRE-Trade actions if (guiIsChecked(hwnd, enablePRETradeBtn)) {guiSetBgColor(hwnd, enablePRETradeBtn, Blue);} else {guiSetBgColor(hwnd, enablePRETradeBtn, Aqua);} ChangePRETradeState(); // If visualSLLineBtn is clicked call function to draw lines if (guiIsClicked(hwnd, visualSLLineBtn)) { PlaySound("click.wav"); if (ObjectFind("INT.SLLine")==-1) DrawVisualSLLine(); else { DeleteVisualSLLine();} } // If moneyMgmtBtn is clicked if (guiIsClicked(hwnd, moneyMgmtBtn)) { PlaySound("click.wav"); if (MoneyManagementRiskInPercent) { MoneyManagementRiskInPercent=false; guiSetText(hwnd, moneyMgmtBtn, "Fixed lots", 16, "Arial Rounded MT Bold"); } else { MoneyManagementRiskInPercent=true; guiSetText(hwnd, moneyMgmtBtn, "Risk lots", 16, "Arial Rounded MT Bold"); } } } // MT4 function draw SL visual line //+-------------------------------------------------------------------------------------------+ void DrawVisualSLLine() { if (actualSLPrice==0) { ObjectCreate("INT.SLLine", OBJ_HLINE, 0, 0, Bid-DefaultStopLoss*usePoint); actualSLPips = Bid -(Bid-DefaultStopLoss*usePoint); } else ObjectCreate("INT.SLLine", OBJ_HLINE, 0, 0, actualSLPrice); ObjectSet("INT.SLLine", OBJPROP_STYLE, 1); ObjectSet("INT.SLLine", OBJPROP_WIDTH, 1); ObjectSet("INT.SLLine", OBJPROP_COLOR, Red); ObjectCreate("INT.SLLineLabel", OBJ_TEXT, 0, Time[5], ObjectGet("INT.SLLine", OBJPROP_PRICE1) ); ObjectSetText("INT.SLLineLabel", "SL", 8, "Arial", Red); } // MT4 function delete SL visual line //+-------------------------------------------------------------------------------------------+ void DeleteVisualSLLine() { for(int i = ObjectsTotal(); i >= 0; i--) if(StringSubstr(ObjectName(i), 0, 4) == "INT.") ObjectDelete(ObjectName(i)); } // MT4GUI function, change button states //+-------------------------------------------------------------------------------------------+ void ChangePRETradeState() { if (guiIsChecked(hwnd,enablePRETradeBtn)) { guiEnable(hwnd, visualSLLineBtn, 1); guiSetBgColor(hwnd, visualSLLineBtn, DeepSkyBlue); guiEnable(hwnd, moneyMgmtBtn, 1); guiSetBgColor(hwnd, moneyMgmtBtn, RoyalBlue); } else { guiEnable(hwnd, visualSLLineBtn, 0); guiSetBgColor(hwnd, visualSLLineBtn, LightGray); guiEnable(hwnd, moneyMgmtBtn, 0); guiSetBgColor(hwnd, moneyMgmtBtn, LightGray); DeleteVisualSLLine(); } } // MT4 function, change SL based on visual line move //+-------------------------------------------------------------------------------------------+ void ChangeSL() { actualSLPrice = ObjectGet("INT.SLLine", OBJPROP_PRICE1); ObjectMove("INT.SLLineLabel", 0, Time[5], ObjectGet("INT.SLLine", OBJPROP_PRICE1)); actualSLPips = (Bid-actualSLPrice)/usePoint; } // MT4 function, Calculate LotSize //+-------------------------------------------------------------------------------------------+ void CalculateLotSize() { if (MoneyManagementRiskInPercent) { if (RiskBasedOnAccountBalance) double RiskAmount = AccountBalance() * RiskPercent / 100; else RiskAmount = AccountEquity() * RiskPercent / 100; if (!ObjectFind("INT.SLLine")==-1) { double CalcLots = RiskAmount / (actualSLPips/usePoint);} else CalcLots = RiskAmount / DefaultStopLoss*usePoint; lotSize = CalcLots; } else lotSize = FixedLotSize; } // MT4 function, Calculate symbols PipPoint //+-------------------------------------------------------------------------------------------+ double PipPoint(string Currency) { int CalcDigits = MarketInfo(Currency,MODE_DIGITS); if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01; else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001; return(CalcPoint); }
In next Blog I will show you how you can use MT4GUI functionality in more dynamically changing environment!
Thats all folks!
You must be logged in to post a comment.