Skip to content

Commit

Permalink
Added TradeHistory indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
9nix6 committed Apr 2, 2022
1 parent 804df80 commit 7c43826
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 62 deletions.
154 changes: 154 additions & 0 deletions Include/AZ-INVEST/SDK/HistoryHandler.mqh
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#include <Generic/ArrayList.mqh>
#include <Trade/DealInfo.mqh>

struct TradeInfoObject
{
long Ticket;
ENUM_ORDER_TYPE Type;
datetime OpenTime;
double OpenPrice;
double OpenVolume;
datetime CloseTime;
double ClosePrice;
double CloseVolume;
};

class CHistoryHandler
{
private:

datetime _startDate;
CArrayList<long> _historyList;

public:

void CHistoryHandler(datetime startDate = 0);
void ~CHistoryHandler();

void GetHistoryDiff(string symbol = "");
bool HistoryChanged();
int Count() { return _historyList.Count(); };
bool SelectByPositionIndex(int ix);
TradeInfoObject GetTradeInfoObject();

private:

void Reset();
bool AddItem(CDealInfo &dealInfo);
bool ContainsElement(long positionId);
};

void CHistoryHandler::CHistoryHandler(datetime startDate = 0)
{
_startDate = startDate;
Reset();
}

void CHistoryHandler::~CHistoryHandler()
{
Reset();
}

bool CHistoryHandler::AddItem(CDealInfo &dealInfo)
{

return _historyList.Add(dealInfo.PositionId());
}

void CHistoryHandler::GetHistoryDiff(string symbol="")
{
HistorySelect(_startDate, TimeCurrent());
CDealInfo dealInfo;

for(int pos=HistoryDealsTotal()-1; pos>=0; pos--)
{
if(!dealInfo.SelectByIndex(pos) || dealInfo.PositionId() == 0)
continue;

bool contains = ContainsElement(dealInfo.PositionId());

if(contains || (symbol != "" && dealInfo.Symbol() != symbol))
continue;

_historyList.Add(dealInfo.PositionId());
}
}

bool CHistoryHandler::ContainsElement(long positionId)
{
if(_historyList.Count() == 0)
return false;

return _historyList.Contains(positionId);
}

bool CHistoryHandler::HistoryChanged()
{
static int _prevCount = 0;

int currentCount = _historyList.Count();
if(currentCount != _prevCount)
{
_prevCount = currentCount;
return true;
}

return false;
}

void CHistoryHandler::Reset()
{
long temp;

while(_historyList.Count() > 0)
{
if(_historyList.TryGetValue(0, temp))
{
_historyList.RemoveAt(0);
}
}
}

bool CHistoryHandler::SelectByPositionIndex(int ix)
{
long positionId = 0;
bool result = _historyList.TryGetValue(ix, positionId);

if(!result || !HistorySelectByPosition(positionId))
return false;

return true;
}

TradeInfoObject CHistoryHandler::GetTradeInfoObject()
{
TradeInfoObject trade;
trade.Ticket = 0;

for(int i = 0; i<HistoryDealsTotal(); i++)
{
ulong ticket = HistoryDealGetTicket(i);
if(ticket == 0)
continue;

ENUM_DEAL_ENTRY dealEntry = (ENUM_DEAL_ENTRY)HistoryDealGetInteger(ticket, DEAL_ENTRY);

if(dealEntry == DEAL_ENTRY_IN)
{
trade.Ticket = HistoryDealGetInteger(ticket, DEAL_POSITION_ID);
trade.Type = (ENUM_ORDER_TYPE)HistoryDealGetInteger(ticket, DEAL_TYPE);
trade.OpenPrice = HistoryDealGetDouble(ticket, DEAL_PRICE);
trade.OpenTime = (datetime)HistoryDealGetInteger(ticket, DEAL_TIME);
trade.OpenVolume = HistoryDealGetDouble(ticket, DEAL_VOLUME);
}
else if(dealEntry == DEAL_ENTRY_OUT || dealEntry == DEAL_ENTRY_OUT_BY)
{
trade.ClosePrice = HistoryDealGetDouble(ticket, DEAL_PRICE);
trade.CloseTime = (datetime)HistoryDealGetInteger(ticket, DEAL_TIME);
trade.CloseVolume = HistoryDealGetDouble(ticket, DEAL_VOLUME);
}
}

return trade;
}

86 changes: 24 additions & 62 deletions Include/AZ-INVEST/SDK/MedianRenkoIndicator.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class MedianRenkoIndicator
double& price // Price oncustom chart
);
bool TimeToCustomChartTime(datetime canvasTime, datetime& customChartTime);
bool CustomChartTimeToCanvasTime(datetime customChartTime, datetime& canvasTime);

bool IsNewBar;

Expand Down Expand Up @@ -97,8 +98,6 @@ class MedianRenkoIndicator
double CalcAppliedPrice(const MqlRates &_rates, ENUM_APPLIED_PRICE applied_price);
double CalcAppliedPrice(const double &o,const double &l,const double &h,const double &c,ENUM_APPLIED_PRICE applied_price);

ENUM_TIMEFRAMES TFMigrate(int tf);
datetime iTime(string symbol,int tf,int index);
double GetArrayValueDouble(double &arr[], int index);
long GetArrayValueLong(long &arr[], int index);
datetime GetArrayValueDateTime(datetime &arr[], int index);
Expand Down Expand Up @@ -684,66 +683,6 @@ int MedianRenkoIndicator::GetOLHCAndApplPriceForIndicatorCalc(double &o[],double
return _count;
}

// TFMigrate:
// https://www.mql5.com/en/forum/2842#comment_39496
//
ENUM_TIMEFRAMES MedianRenkoIndicator::TFMigrate(int tf)
{
switch(tf)
{
case 0: return(PERIOD_CURRENT);
case 1: return(PERIOD_M1);
case 5: return(PERIOD_M5);
case 15: return(PERIOD_M15);
case 30: return(PERIOD_M30);
case 60: return(PERIOD_H1);
case 240: return(PERIOD_H4);
case 1440: return(PERIOD_D1);
case 10080: return(PERIOD_W1);
case 43200: return(PERIOD_MN1);

case 2: return(PERIOD_M2);
case 3: return(PERIOD_M3);
case 4: return(PERIOD_M4);
case 6: return(PERIOD_M6);
case 10: return(PERIOD_M10);
case 12: return(PERIOD_M12);
case 16385: return(PERIOD_H1);
case 16386: return(PERIOD_H2);
case 16387: return(PERIOD_H3);
case 16388: return(PERIOD_H4);
case 16390: return(PERIOD_H6);
case 16392: return(PERIOD_H8);
case 16396: return(PERIOD_H12);
case 16408: return(PERIOD_D1);
case 32769: return(PERIOD_W1);
case 49153: return(PERIOD_MN1);

default: return(PERIOD_CURRENT);
}
}

datetime MedianRenkoIndicator::iTime(string symbol,int tf,int index)
{
if(index < 0)
{
return(-1);
}

ENUM_TIMEFRAMES timeframe=TFMigrate(tf);

datetime Arr[];

if(CopyTime(symbol, timeframe, index, 1, Arr) > 0)
{
return(Arr[0]);
}
else
{
return(-1);
}
}

//
// Function used for calculating the Apllied Price based on Renko OLHC values
//
Expand Down Expand Up @@ -863,3 +802,26 @@ bool MedianRenkoIndicator::TimeToCustomChartTime(datetime canvasTime, datetime&
return true;
}

bool MedianRenkoIndicator::CustomChartTimeToCanvasTime(datetime customChartTime, datetime& canvasTime)
{
ArraySetAsSeries(Time,true);

int ix = -1;
int count = ArraySize(Time);

for(int i=0; i<count; i++)
{
if(Time[i] <= customChartTime)
{
ix = i;
break;
}
}

if(ix == -1)
return false;

canvasTime = iTime(_Symbol, _Period, ix);
return true;
}

Binary file added Indicators/MedianRenko/TradeHistory.ex5
Binary file not shown.
Binary file added Indicators/MedianRenko/TradeHistory.mq5
Binary file not shown.

0 comments on commit 7c43826

Please sign in to comment.