volume-calc-public/volume-calc-public.mq5
bsdodavid fe6702f3e6
2026-01-29 12:50:47 +05:30

1336 satır
No EOL
86 KiB
MQL5

#property version "2.10"
#property description "🔹 This EA is developed by an independent developer."
#property description "❌ Unauthorized duplication, distribution, or resale is strictly prohibited and considered illegal."
#property description "📩 To purchase a license, please contact: bsdodavid@gmail.com"
#property description "🚫 If you bought this EA from anyone other than the official source (bsdodavid@gmail.com), please report it to the owner."
#include <Trade\Trade.mqh>
#include <Controls\Button.mqh>
#include <Controls\Edit.mqh>
#include <Controls\RadioGroup.mqh>
#include <Controls\ComboBox.mqh>
#include <Controls\Dialog.mqh>
#include <Controls\Label.mqh>
#include <daily-level.mqh>
#include <position-object.mqh>
#define BUY_LIMIT_BTN_NAME "Buy/Lmt/stp"
#define SELL_LIMIT_BTN_NAME "Sell/Lmt/stp"
#define RESET_BTN_NAME "Reset"
#define PARTIAL_BTN_NAME "Partial"
#define BE_BTN_NAME "BE"
#define LABEL_NAME_SL "Label SL"
#define LABEL_NAME_TP "Label TP"
#define LABEL_NAME_PRICE "Label Price"
#define LABEL_NAME_RISK "Risk"
#define EDIT_NAME "EDIT FIELD"
#define DAILY_LEVELS_BTN_NAME "Daily Levels"
#define HIDE_ZONE_BTN_NAME "Hide Zones"
#define HIDE_OTHER_TF_ZONE "Hide OTR TF zones"
#define LABEL_SL_TEXT "SL"
#define LABEL_TP_TEXT "TP"
#define LABEL_PRICE_TEXT "Price"
#define SL_RADIO_BTN_VALUE 1
#define TP_RADIO_BTN_VALUE 2
#define PRICE_RADIO_BTN_VALUE 3
#define BUTTON_WIDTH 80
#define SMALL_BUTTON_WIDTH 30
#define LARGE_BUTTON_WIDTH 100
#define BUTTON_HEIGHT 20
#define RAD_BTN_HEIGHT 70
#define EDIT_FIELD_WIDTH 80
#define LABEL_WIDTH 60
#define START_X 11
#define START_Y 11
#define H_SPACING 10 // Horizontal gap between elements
#define V_SPACING 5 // Vertical gap between rows
enum ENUM_OBJ_TYPE {
TYPE_BUTTONS,
TYPE_SMALL_BUTTONS,
TYPE_MEDIUM_BUTTONS,
TYPE_LARGE_BUTTONS,
TYPE_RAD_BUTTONS,
TYPE_EDIT_FIELDS,
TYPE_LABEL,
TYPE_SCROLL
};
input group "--General--"
input bool isLogTrades = true; //Log your trades in a text file
input group "--Money Management--"
input double riskPercent = 0.5; //(%)Percentage you want to risk
input double partial_percentage = 50; //Partial TP in Percentage(%)
input group "--Daily Levels--"
input color dOpen = clrGreen; //Daily Open
input color wOpen = clrMagenta; //Weekly Open
input color mOpen = clrOrange; //Monthly Open
input color yOpen = clrBlueViolet; //Yearly Open
input color prevDayHigh = clrDarkSlateBlue; //Previous day high
input color prevDayLow = clrDarkSlateBlue; //Previous day low
input color prevMonHigh = clrDeepPink; //Previous day high
input color prevMonLow = clrDeepPink; //Previous day low
input group "---Generate Zone Templates---"
input group "--Press button 'U' to access Template1--"
input string TEMPLATE_NAME1 = NULL; //Template Name
input bool TEMPLATE_FILL1 = true; //Template Fill
input color TEMPLATE_COLOR1 = clrNONE; //Template Color
input ENUM_LINE_STYLE TEMPLATE_STYLE1 = STYLE_SOLID; //Template Style
input group "--Press button 'I' to access Template2--"
input string TEMPLATE_NAME2 = NULL; //Template Name
input bool TEMPLATE_FILL2 = true; //Template Fill
input color TEMPLATE_COLOR2 = clrNONE; //Template Color
input ENUM_LINE_STYLE TEMPLATE_STYLE2 = STYLE_SOLID; //Template Style
input group "--Press button 'O' to access Template3--"
input string TEMPLATE_NAME3 = NULL; //Template Name
input bool TEMPLATE_FILL3 = true; //Template Fill
input color TEMPLATE_COLOR3 = clrNONE; //Template Color
input ENUM_LINE_STYLE TEMPLATE_STYLE3 = STYLE_SOLID; //Template Style
input group "--Press button 'P' to access Template4--"
input string TEMPLATE_NAME4 = NULL; //Template Name
input bool TEMPLATE_FILL4 = true; //Template Fill
input color TEMPLATE_COLOR4 = clrNONE; //Template Color
input ENUM_LINE_STYLE TEMPLATE_STYLE4 = STYLE_SOLID; //Template Style
input group "--Press button 'J' to access Template5--"
input string TEMPLATE_NAME5 = NULL; //Template Name
input bool TEMPLATE_FILL5 = true; //Template Fill
input color TEMPLATE_COLOR5 = clrNONE; //Template Color
input ENUM_LINE_STYLE TEMPLATE_STYLE5 = STYLE_SOLID; //Template Style
input group "--Press button 'K' to access Template6--"
input string TEMPLATE_NAME6 = NULL; //Template Name
input bool TEMPLATE_FILL6 = true; //Template Fill
input color TEMPLATE_COLOR6 = clrNONE; //Template Color
input ENUM_LINE_STYLE TEMPLATE_STYLE6 = STYLE_SOLID; //Template Style
input group "--Non-hotkey-template1--"
input string TEMPLATE_NAME7 = NULL; //Template Name
input bool TEMPLATE_FILL7 = true; //Template Fill
input color TEMPLATE_COLOR7 = clrNONE; //Template Color
input ENUM_LINE_STYLE TEMPLATE_STYLE7 = STYLE_SOLID; //Template Style
input group "--Non-hotkey-template2--"
input string TEMPLATE_NAME8 = NULL; //Template Name
input bool TEMPLATE_FILL8 = true; //Template Fill
input color TEMPLATE_COLOR8 = clrNONE; //Template Color
input ENUM_LINE_STYLE TEMPLATE_STYLE8 = STYLE_SOLID; //Template Style
input group "--Non-hotkey-template3--"
input string TEMPLATE_NAME9 = NULL; //Template Name
input bool TEMPLATE_FILL9 = true; //Template Fill
input color TEMPLATE_COLOR9 = clrNONE; //Template Color
input ENUM_LINE_STYLE TEMPLATE_STYLE9 = STYLE_SOLID; //Template Style
input group "---Generate Line Templates---"
input group "--Line Template 1--"
input string TEMPLATE_LINE_NAME1 = NULL;
input color TEMPLATE_LINE_COLOR1 = clrNONE;
input ENUM_LINE_STYLE TEMPLATE_LINE_STYLE1 = STYLE_SOLID;
input long TEMPLATE_LINE_WIDTH1 = 1;
input group "--Line Template 2--"
input string TEMPLATE_LINE_NAME2 = NULL;
input color TEMPLATE_LINE_COLOR2 = clrNONE;
input ENUM_LINE_STYLE TEMPLATE_LINE_STYLE2 = STYLE_SOLID;
input long TEMPLATE_LINE_WIDTH2 = 1;
input group "--Line Template 3--"
input string TEMPLATE_LINE_NAME3 = NULL;
input color TEMPLATE_LINE_COLOR3 = clrNONE;
input ENUM_LINE_STYLE TEMPLATE_LINE_STYLE3 = STYLE_SOLID;
input long TEMPLATE_LINE_WIDTH3 = 1;
input group "--Line Template 4--"
input string TEMPLATE_LINE_NAME4 = NULL;
input color TEMPLATE_LINE_COLOR4 = clrNONE;
input ENUM_LINE_STYLE TEMPLATE_LINE_STYLE4 = STYLE_SOLID;
input long TEMPLATE_LINE_WIDTH4 = 1;
input group "--Line Template 5--"
input string TEMPLATE_LINE_NAME5 = NULL;
input color TEMPLATE_LINE_COLOR5 = clrNONE;
input ENUM_LINE_STYLE TEMPLATE_LINE_STYLE5 = STYLE_SOLID;
input long TEMPLATE_LINE_WIDTH5 = 1;
input group "--Line Template 6--"
input string TEMPLATE_LINE_NAME6 = NULL;
input color TEMPLATE_LINE_COLOR6 = clrNONE;
input ENUM_LINE_STYLE TEMPLATE_LINE_STYLE6 = STYLE_SOLID;
input long TEMPLATE_LINE_WIDTH6 = 1;
string rad_btns[] = {"sl", "tp", "price"};
string line_templates[6] = {};
double selectedPrice = 0;
double slValue = 0;
double tpValue = 0;
double priceValue = 0;
bool isEnableEvent;
struct TRADE_DETAIL {
string symbol;
double price;
double sl;
double tp;
double volume;
datetime time;
};
//+------------------------------------------------------------------+
//| Class CControlsDialog |
//| Usage: main dialog of the Controls application |
//+------------------------------------------------------------------+
class CControlsDialog : public CAppDialog
{
private:
CRadioGroup m_radio_group;
CComboBox m_combo_box;
CButton m_buy_button;
CButton m_sell_button;
CButton m_reset_button;
CButton m_partial_button;
CButton m_be_button;
CButton m_daily_level_button;
CButton m_hidezone_button;
CButton m_hide_otr_tf_zone_button;
CLabel m_label_sl;
CLabel m_label_tp;
CLabel m_label_price;
CLabel m_label_risk;
CLabel m_label_sl_value;
CLabel m_label_tp_value;
CLabel m_label_price_value;
CLabel m_label_risk_value;
public:
CControlsDialog(void);
~CControlsDialog(void);
//--- create
virtual bool Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);
//--- chart event handler
virtual bool OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);
CEdit m_edit;
protected:
//--- create dependent controls
bool CreateRadioGroup(void);
bool CreateComboBox(void);
bool CreateBuyButton(void);
bool CreateSellButton(void);
bool CreateResetButton(void);
bool CreatePartialButton(void);
bool CreateBEbutton(void);
bool CreateDailyLevelButton(void);
bool CreateHideZonesButton(void);
bool CreateHideOtrTfZones(void);
bool CreateEditField(void);
bool CreateLabelSl(void);
bool CreateLabelTp(void);
bool CreateLabelPrice(void);
bool CreateLabelRisk(void);
bool CreateLabelSlValue(void);
bool CreateLabelTpValue(void);
bool CreateLabelPriceValue(void);
bool CreateLabelRiskValue(void);
//--- handlers of the dependent controls events
void OnChangeRadioGroup(void);
void OnChangeComboBox(void);
void OnClickSendBuyOrderButton(void);
void OnClickSendSellOrderButton(void);
void OnClickPartialButton(void);
void OnClickBEbutton(void);
void OnClickResetButton(void);
void OnClickDailyLevelButton(void);
void OnClickHideZoneButton(void);
void OnClickHideOtrTFZones(void);
void OnChangeEditField(void);
void OnEndEditField(void);
};
//+------------------------------------------------------------------+
//| Event Handling |
//+------------------------------------------------------------------+
EVENT_MAP_BEGIN(CControlsDialog)
ON_EVENT(ON_CHANGE, m_radio_group, OnChangeRadioGroup)
ON_EVENT(ON_CHANGE, m_combo_box, OnChangeComboBox)
ON_EVENT(ON_CLICK, m_buy_button, OnClickSendBuyOrderButton)
ON_EVENT(ON_CLICK, m_sell_button, OnClickSendSellOrderButton)
ON_EVENT(ON_CLICK, m_reset_button, OnClickResetButton)
ON_EVENT(ON_CLICK, m_partial_button, OnClickPartialButton)
ON_EVENT(ON_CLICK, m_be_button, OnClickBEbutton)
ON_EVENT(ON_CLICK, m_daily_level_button, OnClickDailyLevelButton)
ON_EVENT(ON_CLICK, m_hidezone_button, OnClickHideZoneButton)
ON_EVENT(ON_CLICK, m_hide_otr_tf_zone_button, OnClickHideOtrTFZones)
ON_EVENT(ON_END_EDIT, m_edit, OnEndEditField)
EVENT_MAP_END(CAppDialog)
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CControlsDialog::CControlsDialog(void)
{
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CControlsDialog::~CControlsDialog(void)
{
}
//+------------------------------------------------------------------+
//| Create |
//+------------------------------------------------------------------+
CPositionObject *g_pos;
bool CControlsDialog::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
{
if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2))
return(false);
if(!CreateRadioGroup())
return(false);
if(!CreateComboBox())
return(false);
if(!CreateBuyButton())
return(false);
if(!CreateSellButton())
return(false);
if(!CreateResetButton())
return(false);
if(!CreatePartialButton())
return(false);
if(!CreateBEbutton())
return(false);
if(!CreateDailyLevelButton())
return(false);
if(!CreateHideZonesButton())
return(false);
//if(!CreateHideOtrTfZones())
// return(false);
if(!CreateEditField())
return(false);
if(!CreateLabelSl())
return(false);
if(!CreateLabelTp())
return(false);
if(!CreateLabelPrice())
return(false);
if(!CreateLabelRisk())
return(false);
if(!CreateLabelSlValue())
return(false);
if(!CreateLabelTpValue())
return(false);
if(!CreateLabelPriceValue())
return(false);
if(!CreateLabelRiskValue())
return(false);
return(true);
}
//+------------------------------------------------------------------+
//| Create the "RadioGroup" element |
//+------------------------------------------------------------------+
bool CControlsDialog::CreateRadioGroup(void)
{
int x1, y1, x2, y2;
GetElementRect(0, 0, x1, y1, x2, y2, TYPE_RAD_BUTTONS);
if(!m_radio_group.Create(m_chart_id,m_name+"RadioGroup",m_subwin, x1, y1, x2, y2))
return(false);
if(!Add(m_radio_group))
return(false);
for(int i=0;i<ArraySize(rad_btns);i++)
if(!m_radio_group.AddItem(rad_btns[i], i+1))
return(false);
m_radio_group.Value(1<<2);
return(true);
}
bool CControlsDialog::CreateComboBox(void) {
int x1, y1, x2, y2;
GetElementRect(3, 0, x1, y1, x2, y2, TYPE_BUTTONS);
if(!m_combo_box.Create(0, m_name+"ComboBox", m_subwin, x1, y1, x2, y2))
return(false);
if(!Add(m_combo_box))
return(false);
if(ArraySize(line_templates) > 0){
for(int i=0; i<ArraySize(line_templates); i++) {
if(StringLen(line_templates[i]) > 0){
if(!m_combo_box.ItemAdd(line_templates[i], i+1))
return false;
}
}
}
return(true);
}
bool CControlsDialog::CreateBuyButton(void) {
int x1, y1, x2, y2;
GetElementRect(0, 1, x1, y1, x2, y2, TYPE_BUTTONS);
if(!m_buy_button.Create(0, BUY_LIMIT_BTN_NAME, 0, x1, y1, x2, y2))
return false;
if(!m_buy_button.Text(BUY_LIMIT_BTN_NAME))
return false;
if(!Add(m_buy_button))
return false;
return true;
}
bool CControlsDialog::CreateSellButton(void) {
int x1, y1, x2, y2;
GetElementRect(0, 2, x1, y1, x2, y2, TYPE_BUTTONS);
if(!m_sell_button.Create(0, m_name + SELL_LIMIT_BTN_NAME, 0, x1, y1, x2, y2))
return false;
if(!m_sell_button.Text(SELL_LIMIT_BTN_NAME))
return false;
if(!Add(m_sell_button))
return false;
return true;
}
bool CControlsDialog::CreateResetButton(void) {
int x1, y1, x2, y2;
GetElementRect(0, 3, x1, y1, x2, y2, TYPE_SMALL_BUTTONS);
if(!m_reset_button.Create(0, m_name + RESET_BTN_NAME, 0, x1, y1, x2, y2))
return false;
if(!m_reset_button.Text("R"))
return false;
if(!Add(m_reset_button))
return false;
return true;
}
bool CControlsDialog::CreateEditField(void) {
int x1, y1, x2, y2;
GetElementRect(1, 1, x1, y1, x2, y2, TYPE_EDIT_FIELDS);
if(!m_edit.Create(0, m_name + EDIT_NAME, 0, x1, y1, x2, y2))
return false;
if(!m_edit.Text(DoubleToString(selectedPrice)))
return(false);
if(!m_edit.ReadOnly(false))
return(false);
if(!Add(m_edit))
return false;
m_edit.ReadOnly(false);
return true;
}
bool CControlsDialog::CreatePartialButton(void) {
int x1, y1, x2, y2;
GetElementRect(1, 2, x1, y1, x2, y2, TYPE_BUTTONS);
if(!m_partial_button.Create(0, m_name+PARTIAL_BTN_NAME, 0, x1, y1, x2, y2))
return false;
if(!m_partial_button.Text(PARTIAL_BTN_NAME))
return false;
if(!Add(m_partial_button))
return false;
return true;
}
bool CControlsDialog::CreateBEbutton(void) {
int x1, y1, x2, y2;
GetElementRect(1, 3, x1, y1, x2, y2, TYPE_SMALL_BUTTONS);
if(!m_be_button.Create(0, m_name+BE_BTN_NAME, 0, x1, y1, x2, y2))
return false;
if(!m_be_button.Text(BE_BTN_NAME))
return false;
if(!Add(m_be_button))
return false;
return true;
}
bool CControlsDialog::CreateDailyLevelButton(void) {
int x1, y1, x2, y2;
GetElementRect(6, 1, x1, y1, x2, y2, TYPE_BUTTONS);
if(!m_daily_level_button.Create(0, m_name+DAILY_LEVELS_BTN_NAME, 0, x1, y1, x2, y2))
return false;
if(!m_daily_level_button.Text(DAILY_LEVELS_BTN_NAME))
return false;
if(!Add(m_daily_level_button))
return false;
return true;
}
bool CControlsDialog::CreateHideZonesButton(void) {
int x1, y1, x2, y2;
GetElementRect(6, 2, x1, y1, x2, y2, TYPE_BUTTONS);
if(!m_hidezone_button.Create(0, m_name+HIDE_ZONE_BTN_NAME, 0, x1, y1, x2, y2))
return false;
if(!m_hidezone_button.Text(HIDE_ZONE_BTN_NAME))
return false;
if(!Add(m_hidezone_button))
return false;
return true;
}
bool CControlsDialog::CreateHideOtrTfZones(void) {
int x1, y1, x2, y2;
GetElementRect(6, 2, x1, y1, x2, y2, TYPE_BUTTONS);
if(!m_hide_otr_tf_zone_button.Create(ChartID(), m_name+HIDE_OTHER_TF_ZONE, 0, x1, y1, x2, y2))
return false;
if(!m_hide_otr_tf_zone_button.Text(HIDE_OTHER_TF_ZONE))
return false;
if(!Add(m_hide_otr_tf_zone_button))
return false;
return true;
}
bool CControlsDialog::CreateLabelSl(void) {
int x1, y1, x2, y2;
GetElementRect(2, 1, x1, y1, x2, y2, TYPE_LABEL);
if(!m_label_sl.Create(0, m_name+LABEL_NAME_SL, 0, x1, y1, x2, y2))
return false;
if(!Add(m_label_sl))
return false;
if(!m_label_sl.Text(LABEL_SL_TEXT))
return false;
return true;
}
bool CControlsDialog::CreateLabelTp(void) {
int x1, y1, x2, y2;
GetElementRect(2, 2, x1, y1, x2, y2, TYPE_LABEL);
if(!m_label_tp.Create(0, m_name+LABEL_NAME_TP, 0, x1, y1, x2, y2))
return false;
if(!Add(m_label_tp))
return false;
if(!m_label_tp.Text(LABEL_TP_TEXT))
return false;
return true;
}
bool CControlsDialog::CreateLabelPrice(void) {
int x1, y1, x2, y2;
GetElementRect(2, 3, x1, y1, x2, y2, TYPE_LABEL);
if(!m_label_price.Create(0, m_name+LABEL_NAME_PRICE, 0, x1, y1, x2, y2))
return false;
if(!Add(m_label_price))
return false;
if(!m_label_price.Text(LABEL_PRICE_TEXT))
return false;
return true;
}
bool CControlsDialog::CreateLabelRisk(void) {
int x1, y1, x2, y2;
GetElementRect(4, 1, x1, y1, x2, y2, TYPE_LABEL);
if(!m_label_risk.Create(0, m_name+LABEL_NAME_RISK, 0, x1, y1, x2, y2))
return false;
if(!Add(m_label_risk))
return false;
if(!m_label_risk.Text(LABEL_NAME_RISK))
return false;
return true;
}
bool CControlsDialog::CreateLabelSlValue(void) {
//if(!m_label_sl_value.Create(0, m_name+"SL Value", 0, 110, 80, 170, 110))
int x1, y1, x2, y2;
GetElementRect(3, 1, x1, y1, x2, y2, TYPE_LABEL);
if(!m_label_sl_value.Create(0, m_name+"SL Value", 0, x1, y1, x2, y2))
return false;
if(!Add(m_label_sl_value))
return false;
if(!m_label_sl_value.Text(DoubleToString(0, _Digits)))
return false;
return true;
}
bool CControlsDialog::CreateLabelTpValue(void) {
int x1, y1, x2, y2;
GetElementRect(3, 2, x1, y1, x2, y2, TYPE_LABEL);
if(!m_label_tp_value.Create(0, m_name+"TP Value", 0, x1, y1, x2, y2))
return false;
if(!Add(m_label_tp_value))
return false;
if(!m_label_tp_value.Text(DoubleToString(0, _Digits)))
return false;
return true;
}
bool CControlsDialog::CreateLabelPriceValue(void) {
int x1, y1, x2, y2;
GetElementRect(3, 3, x1, y1, x2, y2, TYPE_LABEL);
if(!m_label_price_value.Create(0, m_name+"Price Value", 0, x1, y1, x2, y2))
return false;
if(!Add(m_label_price_value))
return false;
if(!m_label_price_value.Text(DoubleToString(0, _Digits)))
return false;
return true;
}
bool CControlsDialog::CreateLabelRiskValue(void) {
int x1, y1, x2, y2;
GetElementRect(5, 1, x1, y1, x2, y2, TYPE_LABEL);
if(!m_label_risk_value.Create(0, m_name+"Risk value", 0, x1, y1, x2, y2))
return false;
if(!Add(m_label_risk_value))
return false;
if(!m_label_risk_value.Text(DoubleToString(riskPercent, 2)))
return false;
return true;
}
//+------------------------------------------------------------------+
//| Event handler |
//+------------------------------------------------------------------+
void CControlsDialog::OnChangeRadioGroup(void)
{
Comment(__FUNCTION__+" : Value="+IntegerToString(m_radio_group.Value()));
//m_edit.Text(DoubleToString(selectedPrice, _Digits));
if(m_radio_group.Value() == SL_RADIO_BTN_VALUE) {
m_label_sl_value.Text(m_edit.Text());
}
if(m_radio_group.Value() == TP_RADIO_BTN_VALUE) {
m_label_tp_value.Text(m_edit.Text());
}
if(m_radio_group.Value() == PRICE_RADIO_BTN_VALUE) {
m_label_price_value.Text(m_edit.Text());
}
isEnableEvent = false;
}
void CControlsDialog::OnChangeComboBox(void) {
if(m_combo_box.Select() == TEMPLATE_LINE_NAME1)
modifyLines(TEMPLATE_LINE_COLOR1, TEMPLATE_LINE_STYLE1, TEMPLATE_LINE_WIDTH1, TEMPLATE_LINE_NAME1);
if(m_combo_box.Select() == TEMPLATE_LINE_NAME2)
modifyLines(TEMPLATE_LINE_COLOR2, TEMPLATE_LINE_STYLE2, TEMPLATE_LINE_WIDTH2, TEMPLATE_LINE_NAME2);
if(m_combo_box.Select() == TEMPLATE_LINE_NAME3)
modifyLines(TEMPLATE_LINE_COLOR3, TEMPLATE_LINE_STYLE3, TEMPLATE_LINE_WIDTH3, TEMPLATE_LINE_NAME3);
if(m_combo_box.Select() == TEMPLATE_LINE_NAME4)
modifyLines(TEMPLATE_LINE_COLOR4, TEMPLATE_LINE_STYLE4, TEMPLATE_LINE_WIDTH4, TEMPLATE_LINE_NAME4);
if(m_combo_box.Select() == TEMPLATE_LINE_NAME5)
modifyLines(TEMPLATE_LINE_COLOR5, TEMPLATE_LINE_STYLE5, TEMPLATE_LINE_WIDTH5, TEMPLATE_LINE_NAME5);
if(m_combo_box.Select() == TEMPLATE_LINE_NAME6)
modifyLines(TEMPLATE_LINE_COLOR6, TEMPLATE_LINE_STYLE6, TEMPLATE_LINE_WIDTH6, TEMPLATE_LINE_NAME6);
}
void CControlsDialog::OnClickSendBuyOrderButton(void) {
double sl = StringToDouble(m_label_sl_value.Text());
double tp = StringToDouble(m_label_tp_value.Text());
double price = StringToDouble(m_label_price_value.Text());
placeOrders(sl, tp, price, POSITION_TYPE_BUY);
isEnableEvent = false;
// datetime curr_time = iTime(_Symbol, PERIOD_CURRENT, 1);
// int period_multiplier = getPeriodMultiplier(Period());
//
// if(g_pos!=NULL)
// delete g_pos;
//
// g_pos = new CPositionObject(price,sl,tp,curr_time,curr_time+(period_multiplier*10),"buy");
}
void CControlsDialog::OnClickSendSellOrderButton(void) {
double sl = StringToDouble(m_label_sl_value.Text());
double tp = StringToDouble(m_label_tp_value.Text());
double price = StringToDouble(m_label_price_value.Text());
placeOrders(sl, tp, price, POSITION_TYPE_SELL);
isEnableEvent = false;
// datetime curr_time = iTime(_Symbol, PERIOD_CURRENT, 1);
// int period_multiplier = getPeriodMultiplier(Period());
//
// if(g_pos!=NULL)
// delete g_pos;
//
// g_pos = new CPositionObject(price,sl,tp,curr_time,curr_time+(period_multiplier*10),"sell");
}
void CControlsDialog::OnClickResetButton(void) {
selectedPrice = 0;
m_edit.Text("");
m_label_sl_value.Text("");
m_label_tp_value.Text("");
m_label_price_value.Text("");
m_radio_group.Value(NULL);
isEnableEvent = false;
}
void CControlsDialog::OnClickPartialButton(void) {
for(int i=PositionsTotal()-1; i>=0; i--) {
ulong pos_ticket = PositionGetTicket(i);
if(PositionSelectByTicket(pos_ticket)) {
if(Symbol() == positionInfo.Symbol()){
double volume_step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
double partial_volume = NormalizeDouble(positionInfo.Volume() * (partial_percentage / 100.0),(int)MathLog10(1.0 / volume_step));
trade.PositionClosePartial(pos_ticket, partial_volume);
}
}
}
}
void CControlsDialog::OnClickBEbutton(void) {
for(int i=PositionsTotal()-1; i>=0; i--) {
ulong pos_ticket = PositionGetTicket(i);
if(PositionSelectByTicket(pos_ticket)) {
if(Symbol() == positionInfo.Symbol()) {
if(positionInfo.PositionType() == POSITION_TYPE_BUY)
trade.PositionModify(pos_ticket, positionInfo.PriceOpen()+(6*_Point), positionInfo.TakeProfit());
else if(positionInfo.PositionType() == POSITION_TYPE_SELL)
trade.PositionModify(pos_ticket, positionInfo.PriceOpen()-(6*_Point), positionInfo.TakeProfit());
}
}
}
}
void CControlsDialog::OnClickDailyLevelButton(void) {
CKeyLevels dailyLevel(dOpen, wOpen, mOpen, yOpen, prevDayHigh, prevDayLow, prevMonHigh, prevMonLow);
ChartRedraw(0);
}
void CControlsDialog::OnClickHideZoneButton(void) {
HideZone();
}
void CControlsDialog::OnEndEditField(void) {
isEnableEvent = false;
}
void CControlsDialog::OnClickHideOtrTFZones(void) {
HideOtherTFZones();
}
//+------------------------------------------------------------------+
//| Global Variables |
//+------------------------------------------------------------------+
CControlsDialog ExtDialog;
CTrade trade;
CPositionInfo positionInfo;
int OnInit(){
line_templates[0] = TEMPLATE_LINE_NAME1;
line_templates[1] = TEMPLATE_LINE_NAME2;
line_templates[2] = TEMPLATE_LINE_NAME3;
line_templates[3] = TEMPLATE_LINE_NAME4;
line_templates[4] = TEMPLATE_LINE_NAME5;
line_templates[5] = TEMPLATE_LINE_NAME6;
if(!ExtDialog.Create(0,"Controls", 0, 40,40,400,260))
return(INIT_FAILED);
ExtDialog.Minimized(true);
ExtDialog.Run();
return(INIT_SUCCEEDED);
}
void OnTick() {
MqlTick tick[1]={};
if(!SymbolInfoTick(_Symbol, tick[0])) {
return;
}
double ask = tick[0].ask;
double bid = tick[0].bid;
double spread = ask-bid;
double spread_in_points = spread / _Point;
Comment("------------------------------------------Spread: ", NormalizeDouble(spread_in_points, _Digits));
}
void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam) {
if(id == CHARTEVENT_CLICK) {
if(!isEnableEvent) {
int x = (int)lparam;
int y = (int)dparam;
datetime time = 0;
int subWindow = 0;
if(ChartXYToTimePrice(0, x, y, subWindow, time, selectedPrice)) {
CControlsDialog dialog;
ExtDialog.m_edit.Text(DoubleToString(selectedPrice, _Digits));
//selectedPrice = NormalizeDouble(selectedPrice, _Digits);
isEnableEvent = true;
//Print("Clicked at Time: ", TimeToString(time, TIME_DATE | TIME_MINUTES),
// " | Price: ", DoubleToString(selectedPrice, _Digits));
}
}
}
if(id == CHARTEVENT_KEYUP) {
int key = (int)lparam;
if(key == 'A') {
if(!ChartGetInteger(0, CHART_AUTOSCROLL, 0)) {
ChartSetInteger(0, CHART_AUTOSCROLL, true);
} else {
ChartSetInteger(0, CHART_AUTOSCROLL, false);
}
} else if(key == 'S') {
if(!ChartGetInteger(0, CHART_SHIFT, 0)) {
ChartSetInteger(0, CHART_SHIFT, true);
} else {
ChartSetInteger(0, CHART_SHIFT, false);
}
} else if(key == 'D') {
fixTrendLine();
} else if(key == 'L') {
if(ChartGetInteger(0, CHART_SCALEFIX, 0))
ChartSetInteger(0, CHART_SCALEFIX, false);
else
ChartSetInteger(0, CHART_SCALEFIX, true);
ChartRedraw(0);
} else if(key == 'U') {
if(TEMPLATE_NAME1 != NULL){
updateZoneColors(TEMPLATE_COLOR1, TEMPLATE_FILL1, STYLE_SOLID, TEMPLATE_NAME1);
}
} else if(key == 'I') {
if(TEMPLATE_NAME2 != NULL){
updateZoneColors(TEMPLATE_COLOR2, TEMPLATE_FILL2, STYLE_SOLID, TEMPLATE_NAME2);
}
}else if(key == 'O') {
if(TEMPLATE_NAME3 != NULL){
updateZoneColors(TEMPLATE_COLOR3, TEMPLATE_FILL3, STYLE_SOLID, TEMPLATE_NAME3);
}
}else if(key == 'P') {
if(TEMPLATE_NAME4 != NULL){
updateZoneColors(TEMPLATE_COLOR4, TEMPLATE_FILL4, STYLE_SOLID, TEMPLATE_NAME4);
}
}else if(key == 'J') {
if(TEMPLATE_NAME5 != NULL){
updateZoneColors(TEMPLATE_COLOR5, TEMPLATE_FILL5, STYLE_SOLID, TEMPLATE_NAME5);
}
}
else if(key == 'K') {
if(TEMPLATE_NAME6 != NULL){
updateZoneColors(TEMPLATE_COLOR6, TEMPLATE_FILL6, STYLE_SOLID, TEMPLATE_NAME6);
}
}
else if(key == 'H') {
HideZone();
}
}
ExtDialog.ChartEvent(id,lparam,dparam,sparam);
if(g_pos != NULL)
g_pos.HandleChartEvent(id, lparam, dparam, sparam);
}
bool ClickInsideButton(int x, int y)
{
if (x >= 5 && x <= 80 && y >= 22 && y <= 49)
return true;
if (x >= 85 && x <= 160 && y >= 22 && y <= 49)
return true;
return false;
}
void placeOrders(double sl, double tp, double price, ENUM_POSITION_TYPE pos_type) {
MqlTick tick;
double volume = 0.0;
if (!SymbolInfoTick(_Symbol, tick)) {
Print("Failed to get latest tick for ", _Symbol);
return;
}
if(pos_type == POSITION_TYPE_BUY) {
if(price == 0) {
double currPrice = tick.ask;
if(currPrice > sl){
double slPoints = tick.ask-sl;
volume = calcLots(slPoints);
trade.Buy(volume, _Symbol, NULL, sl, tp);
} else {
Print("Error buying: Market price should be greater than sl!");
}
} else if(price != 0) {
if(price > sl){
double slPoints = price-sl;
volume = calcLots(slPoints);
if(price > tick.ask)
trade.BuyStop(volume, price, NULL, sl, tp, ORDER_TIME_GTC);
else if(price < tick.bid)
trade.BuyLimit(volume, price, NULL, sl, tp, ORDER_TIME_GTC);
} else {
Print("Error: stop loss should be smaller than price!");
}
}
} else if(pos_type == POSITION_TYPE_SELL) {
if(price == 0) {
double currPrice = tick.bid;
if(currPrice < sl){
double slPoints = sl - tick.bid;
volume = calcLots(slPoints);
trade.Sell(volume, _Symbol, NULL, sl, tp);
} else {
Print("Error Selling: Market price should be smaller than sl!");
}
} else if(price != 0) {
if(price < sl) {
double slPoints = sl - price;
volume = calcLots(slPoints);
if((price < tick.bid))
trade.SellStop(volume, price, NULL, sl, tp, ORDER_TIME_GTC);
else if((price > tick.ask))
trade.SellLimit(volume, price, NULL, sl, tp, ORDER_TIME_GTC);
} else {
Print("Error: SL should be smaller than price!");
}
}
}
if(isLogTrades){
TRADE_DETAIL trd_detail;
MqlDateTime tm={};
datetime currentServerTime = TimeTradeServer(tm);
trd_detail.symbol = _Symbol;
trd_detail.price = price;
trd_detail.sl = sl;
trd_detail.tp = tp;
trd_detail.volume = volume;
trd_detail.time = currentServerTime;
logHistory(trd_detail);
}
Print("sl: ", sl, "tp: ", tp, "price: ", price, "volume: ", volume);
}
double calcLots(double slPoints) {
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double lotStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
if(tickSize == 0 || tickValue == 0 || lotStep == 0) {
return 0;
}
double riskMoney = AccountInfoDouble(ACCOUNT_BALANCE)*riskPercent / 100;
double moneyLotStep = (slPoints/tickSize) * tickValue * lotStep;
if(moneyLotStep == 0)
return 0;
double lots = MathFloor(riskMoney / moneyLotStep) * lotStep;
int lotDigits = (int)MathRound(-MathLog10(lotStep));
lots = NormalizeDouble(lots, lotDigits);
Print("Risk Money: ", riskMoney);
return lots;
}
void GetElementRect(int row, int col, int& x1, int& y1, int& x2, int& y2, ENUM_OBJ_TYPE obj_type)
{
int height = BUTTON_HEIGHT;
int width = BUTTON_WIDTH;
if(obj_type == TYPE_RAD_BUTTONS)
height = RAD_BTN_HEIGHT;
if(obj_type == TYPE_SMALL_BUTTONS)
width = SMALL_BUTTON_WIDTH;
if(obj_type == TYPE_LARGE_BUTTONS)
width = LARGE_BUTTON_WIDTH;
if(obj_type == TYPE_EDIT_FIELDS)
width = EDIT_FIELD_WIDTH;
if(obj_type == TYPE_LABEL)
width = LABEL_WIDTH;
x1 = START_X + col * (BUTTON_WIDTH + H_SPACING);
y1 = START_Y + row * (height + V_SPACING);
x2 = x1 + width;
y2 = y1 + height;
}
void fixTrendLine() {
int obj_total = ObjectsTotal(0, 0, OBJ_TREND);
for(int i=obj_total-1; i>=0; i--) {
string objName = ObjectName(0, i, 0, OBJ_TREND);
if(ObjectGetInteger(0, objName, OBJPROP_SELECTED, true)) {
double value1 = EMPTY_VALUE;
double value2 = EMPTY_VALUE;
if(ObjectFind(0, objName) != -1){
value1 = ObjectGetDouble(0, objName, OBJPROP_PRICE, 0);
value2 = ObjectGetDouble(0, objName, OBJPROP_PRICE, 1);
if(value1 != value2)
ObjectSetDouble(0, objName, OBJPROP_PRICE, 1, value1);
ObjectSetInteger(0, objName, OBJPROP_SELECTED, false);
}
}
}
ChartRedraw(0);
}
void modifyZoneNames(string obj_name, string zone_name) {
string result[];
ushort sep = '-';
int k = StringSplit(obj_name, sep, result);
if(ArraySize(result)==0){
Print("Empty array!", obj_name);
return;
}
if(ArraySize(result)>1) {
if(StringLen(result[1]) > 0 && StringCompare(result[1], zone_name, false) == 0){
return;
}
}
string new_obj_name = result[0] + "-" + zone_name;
ObjectSetString(0, obj_name, OBJPROP_NAME, new_obj_name);
}
void updateZoneColors(color zone_clr, bool isObjectFill, ENUM_LINE_STYLE style, string zone_name){
if(StringLen(zone_name)==0)
return;
int obj_total = ObjectsTotal(0, 0, OBJ_RECTANGLE);
for(int i=obj_total-1; i>=0; i--) {
string obj_name = ObjectName(0, i, 0, OBJ_RECTANGLE);
if(ObjectGetInteger(0, obj_name, OBJPROP_SELECTED, true)){
color obj_color = (color)ObjectGetInteger(0, obj_name, OBJPROP_COLOR);
ObjectSetInteger(0, obj_name, OBJPROP_COLOR, zone_clr);
ObjectSetInteger(0, obj_name, OBJPROP_FILL, isObjectFill);
ObjectSetInteger(0, obj_name, OBJPROP_STYLE, style);
ObjectSetInteger(0, obj_name, OBJPROP_SELECTED, false);
modifyZoneNames(obj_name, zone_name);
}
}
ChartRedraw(0);
}
void modifyLines(color clr, ENUM_LINE_STYLE style, long width, string zone_name){
int objTotal = ObjectsTotal(0, 0, OBJ_TREND);
for(int i=objTotal-1; i>=0; i--){
string objName = ObjectName(0, i, 0, OBJ_TREND);
if(ObjectGetInteger(0, objName, OBJPROP_SELECTED, true)){
ObjectSetInteger(0, objName, OBJPROP_COLOR, clr);
ObjectSetInteger(0, objName, OBJPROP_STYLE, style);
ObjectSetInteger(0, objName, OBJPROP_WIDTH, width);
modifyZoneNames(objName, zone_name);
}
}
fixTrendLine();
}
void logHistory(TRADE_DETAIL &trd_detail){
string fileName = "trade_history.txt";
int fileHandle = FileOpen(fileName, FILE_READ|FILE_WRITE|FILE_TXT|FILE_ANSI|FILE_COMMON|FILE_SHARE_WRITE);
if(fileHandle < 0){
Print("invalid filehandle!");
return;
}
FileSeek(fileHandle, 0, SEEK_END);
string history = "";
history+=trd_detail.symbol+", ";
if(trd_detail.price != 0)
history+="price: "+DoubleToString(trd_detail.price, _Digits)+", ";
if(trd_detail.sl != 0)
history+="sl: "+DoubleToString(trd_detail.sl, _Digits)+", ";
if(trd_detail.tp != 0)
history+="tp: "+DoubleToString(trd_detail.tp, _Digits)+", ";
if(trd_detail.volume != 0)
history+="volume: "+DoubleToString(trd_detail.volume, 2)+", ";
history+=TimeToString(trd_detail.time);
FileWriteString(fileHandle, history+"\r\n");
FileClose(fileHandle);
}
void HideZone(){
int objTotal = ObjectsTotal(0, 0, OBJ_RECTANGLE);
long mask = GetMask(Period());
for(int i=objTotal-1; i>=0; i--){
string objName = ObjectName(0, i, 0, OBJ_RECTANGLE);
ObjectSetInteger(ChartID(), objName, OBJPROP_TIMEFRAMES, mask);
}
}
void HideOtherTFZones() {
int objTotal = ObjectsTotal(0, 0, OBJ_RECTANGLE);
for(int i=objTotal-1; i>=0; i--){
string objName = ObjectName(0, i, 0, OBJ_RECTANGLE);
ObjectSetInteger(ChartID(), objName, OBJPROP_TIMEFRAMES, GetCurrentObjectPeriod(Period()));
}
}
long GetCurrentObjectPeriod(ENUM_TIMEFRAMES period) {
switch(period){
case PERIOD_D1:
return OBJ_PERIOD_D1;
case PERIOD_H4:
return OBJ_PERIOD_H4;
case PERIOD_H1:
return OBJ_PERIOD_H1;
case PERIOD_M30:
return OBJ_PERIOD_M30;
case PERIOD_M15:
return OBJ_PERIOD_M15;
case PERIOD_M5:
return OBJ_PERIOD_M5;
case PERIOD_M1:
return OBJ_PERIOD_M1;
default:
return NULL;
}
}
long GetMask(ENUM_TIMEFRAMES period) {
long mask = LONG_MIN;
static ENUM_TIMEFRAMES prevPeriod;
if(prevPeriod == period){
mask = OBJ_ALL_PERIODS;
prevPeriod = NULL;
return mask;
}
if(prevPeriod != period)
prevPeriod = period;
switch(period){
case PERIOD_D1:
mask = OBJ_PERIOD_H4|OBJ_PERIOD_H1|OBJ_PERIOD_M30|OBJ_PERIOD_M15|OBJ_PERIOD_M5|OBJ_PERIOD_M1;
break;
case PERIOD_H4:
mask = OBJ_PERIOD_D1|OBJ_PERIOD_H1|OBJ_PERIOD_M30|OBJ_PERIOD_M15|OBJ_PERIOD_M5|OBJ_PERIOD_M1;
break;
case PERIOD_H1:
mask = OBJ_PERIOD_D1|OBJ_PERIOD_H4|OBJ_PERIOD_M30|OBJ_PERIOD_M15|OBJ_PERIOD_M5|OBJ_PERIOD_M1;
break;
case PERIOD_M30:
mask = OBJ_PERIOD_D1|OBJ_PERIOD_H4|OBJ_PERIOD_H1|OBJ_PERIOD_M15|OBJ_PERIOD_M5|OBJ_PERIOD_M1;
break;
case PERIOD_M15:
mask = OBJ_PERIOD_D1|OBJ_PERIOD_H4|OBJ_PERIOD_H1|OBJ_PERIOD_M30|OBJ_PERIOD_M5|OBJ_PERIOD_M1;
break;
case PERIOD_M5:
mask = OBJ_PERIOD_D1|OBJ_PERIOD_H4|OBJ_PERIOD_H1|OBJ_PERIOD_M30|OBJ_PERIOD_M15|OBJ_PERIOD_M1;
break;
default:
mask = OBJ_ALL_PERIODS;
}
return mask;
}
int getPeriodMultiplier(ENUM_TIMEFRAMES timeframe) {
switch(timeframe) {
case PERIOD_M1:
return 60;
break;
case PERIOD_M5:
return 300;
break;
case PERIOD_M15:
return 900;
break;
case PERIOD_M30:
return 1800;
break;
case PERIOD_H1:
return 3600;
break;
case PERIOD_H4:
return 14400;
break;
case PERIOD_D1:
return 86400;
break;
default:
return 0;
break;
}
}
void OnDeinit(const int reason) {
//Print(__FUNCTION__," Deinitialization reason code = ",reason);
ExtDialog.Destroy(reason);
if(g_pos!=NULL)
delete g_pos;
}