158 lines
6 KiB
MQL5
158 lines
6 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| MTBox.mqh |
|
|
//| Copyright 2018, MetaQuotes Software Corp. |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#property library
|
|
#property version "1.2.5"
|
|
|
|
#include <Trade\SymbolInfo.mqh>
|
|
#include "MTUtils.mqh"
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Definitions |
|
|
//+------------------------------------------------------------------+
|
|
|
|
#define UPDATE_BASE_W 0
|
|
#define UPDATE_SHORT_W 1
|
|
#define UPDATE_LONG_W 2
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
class DBox
|
|
{
|
|
public:
|
|
int height;
|
|
int length;
|
|
double l_multiplier;
|
|
double w_multiplier;
|
|
|
|
protected:
|
|
double long_signal;
|
|
double short_signal;
|
|
int length_left;
|
|
bool is_first_trade;
|
|
long cid;
|
|
CSymbolInfo s_info;
|
|
public:
|
|
bool shouldGoLong(double price) {return price >= long_signal; }
|
|
bool shouldGoShort(double price) {return price <= short_signal; }
|
|
bool shouldUpdateBox() {return length_left <= 0;}
|
|
bool isFirstBox() { return is_first_trade; }
|
|
double getLongSignal() {return long_signal; }
|
|
double getShortSignal() {return short_signal; }
|
|
double getLengthLeft() {return length_left; }
|
|
void Init(int i_height,int i_length,double il_multiplier,double iw_multiplier);
|
|
void reduceBoxLife() {length_left --;}
|
|
void resetLength() {length_left = length;}
|
|
void doFirstBox(double price, double ask, double bid);
|
|
void DrawBox(void);
|
|
void UpdateBox(double price,double ask,double bid, int update_status);
|
|
void PrintTheBox(double price,double ask,double bid);
|
|
};
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void DBox::Init(int i_height, int i_length, double il_multiplier, double iw_multiplier)
|
|
{
|
|
height = i_height;
|
|
length = i_length;
|
|
l_multiplier = il_multiplier;
|
|
w_multiplier = iw_multiplier;
|
|
long_signal = 0.0;
|
|
short_signal = 0.0;
|
|
length_left = i_length;
|
|
is_first_trade = true;
|
|
cid = ChartID();
|
|
|
|
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void DBox::doFirstBox(double price,double ask,double bid)
|
|
{
|
|
UpdateBox(price,ask,bid);
|
|
is_first_trade = false;
|
|
return;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void DBox::DrawBox()
|
|
{
|
|
long box_plot = cid;
|
|
|
|
if(ObjectFind(box_plot, "top_line")>=0)
|
|
{
|
|
ObjectMove(box_plot, "top_line", 0,0, long_signal);
|
|
}
|
|
else
|
|
{
|
|
ObjectCreate(box_plot,"top_line", OBJ_HLINE, 0, 0,long_signal);
|
|
}
|
|
|
|
|
|
|
|
if(ObjectFind(box_plot, "bottom_line") >= 0)
|
|
{
|
|
ObjectMove(box_plot, "bottom_line", 0,0,short_signal);
|
|
}
|
|
else
|
|
{
|
|
ObjectCreate(box_plot,"bottom_line", OBJ_HLINE, 0, 0,short_signal);
|
|
}
|
|
|
|
ObjectSetInteger(box_plot, "bottom_line", OBJPROP_COLOR, clrPurple);
|
|
ObjectSetInteger(box_plot, "top_line", OBJPROP_COLOR, clrPurple);
|
|
|
|
ChartRedraw(box_plot);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void DBox::UpdateBox(double price, double ask, double bid, int update_status=UPDATE_BASE_W)
|
|
{
|
|
switch(update_status)
|
|
{
|
|
|
|
case UPDATE_SHORT_W:
|
|
short_signal = NormalizeDouble(price - (w_multiplier* height*_Point), _Digits);
|
|
long_signal = NormalizeDouble(price + (l_multiplier * height * _Point), _Digits);
|
|
break;
|
|
case UPDATE_LONG_W:
|
|
short_signal = NormalizeDouble(price - (l_multiplier* height*_Point), _Digits);
|
|
long_signal = NormalizeDouble(price + (w_multiplier * height * _Point), _Digits);
|
|
break;
|
|
default:
|
|
long_signal = NormalizeDouble(price +(0.5*height*_Point), _Digits);
|
|
short_signal = NormalizeDouble(price - (0.5*height*_Point), _Digits);
|
|
break;
|
|
}
|
|
|
|
resetLength();
|
|
DrawBox();
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
void DBox::PrintTheBox(double price, double ask, double bid)
|
|
{
|
|
|
|
double sell_volume = s_info.SessionSellOrdersVolume();
|
|
double buy_volume = s_info.SessionBuyOrdersVolume();
|
|
int buy_orders = s_info.SessionBuyOrders();
|
|
|
|
Comment("Box : Long Signal = ", getLongSignal(), " | Short Signal = ", getShortSignal(), " | Price : ", price, " | Lenght : ", getLengthLeft(),
|
|
"\nSell Volume : ", sell_volume, " | Buy Volume : ", buy_volume,"| Total Used : ", buy_orders,
|
|
"\nPRICE :", PRICE);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
|
|
//+------------------------------------------------------------------+
|