Issue #106: Extended structure Mt5Quote with fields from MqlTick (volume, last, time)

This commit is contained in:
vdemydiuk
2018-05-08 14:53:31 +03:00
parent fd34f009cd
commit 6023bffed6
8 changed files with 83 additions and 40 deletions
+2 -1
View File
@@ -3,6 +3,7 @@
internal enum Mt5EventTypes internal enum Mt5EventTypes
{ {
OnTradeTransaction = 1, OnTradeTransaction = 1,
OnBookEvent = 2 OnBookEvent = 2,
OnTick = 3
} }
} }
+9
View File
@@ -0,0 +1,9 @@
namespace MtApi5.Events
{
internal class OnTickEvent
{
public MqlTick Tick { get; set; }
public string Instrument { get; set; }
public int ExpertHandle { get; set; }
}
}
+8 -2
View File
@@ -1,4 +1,5 @@
using MTApiService; using System;
using MTApiService;
namespace MtApi5 namespace MtApi5
{ {
@@ -8,8 +9,13 @@ namespace MtApi5
public double Bid { get; } public double Bid { get; }
public double Ask { get; } public double Ask { get; }
public int ExpertHandle { get; set; } public int ExpertHandle { get; set; }
public DateTime Time { get; set; }
public double Last { get; set; }
public ulong Volume { get; set; }
// public long TimeMsc { get; set; }
// public uint Flags { get; set; }
private Mt5Quote(string instrument, double bid, double ask) internal Mt5Quote(string instrument, double bid, double ask)
{ {
Instrument = instrument; Instrument = instrument;
Bid = bid; Bid = bid;
+1
View File
@@ -49,6 +49,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="CopyTicksFlag.cs" /> <Compile Include="CopyTicksFlag.cs" />
<Compile Include="Events\OnBookEvent.cs" /> <Compile Include="Events\OnBookEvent.cs" />
<Compile Include="Events\OnTickEvent.cs" />
<Compile Include="Events\OnTradeTransactionEvent.cs" /> <Compile Include="Events\OnTradeTransactionEvent.cs" />
<Compile Include="MqlBookInfo.cs" /> <Compile Include="MqlBookInfo.cs" />
<Compile Include="MqlParam.cs" /> <Compile Include="MqlParam.cs" />
+24 -12
View File
@@ -40,12 +40,19 @@ namespace MtApi5
private volatile bool _isBacktestingMode; private volatile bool _isBacktestingMode;
private Mt5ConnectionState _connectionState = Mt5ConnectionState.Disconnected; private Mt5ConnectionState _connectionState = Mt5ConnectionState.Disconnected;
private int _executorHandle; private int _executorHandle;
private readonly Dictionary<Mt5EventTypes, Action<int, string>> _mtEventHandlers =
new Dictionary<Mt5EventTypes, Action<int, string>>();
#endregion #endregion
#region Public Methods #region Public Methods
public MtApi5Client() public MtApi5Client()
{ {
LogConfigurator.Setup(LogProfileName); LogConfigurator.Setup(LogProfileName);
_mtEventHandlers[Mt5EventTypes.OnBookEvent] = ReceivedOnBookEvent;
_mtEventHandlers[Mt5EventTypes.OnTick] = ReceivedOnTickEvent;
_mtEventHandlers[Mt5EventTypes.OnTradeTransaction] = ReceivedOnTradeTransaction;
} }
///<summary> ///<summary>
@@ -3002,21 +3009,11 @@ namespace MtApi5
} }
} }
private void _client_MtEventReceived(MtEvent e) private void _client_MtEventReceived(MtEvent e)
{ {
var eventType = (Mt5EventTypes)e.EventType; var eventType = (Mt5EventTypes)e.EventType;
_mtEventHandlers[eventType](e.ExpertHandle, e.Payload);
switch (eventType)
{
case Mt5EventTypes.OnTradeTransaction:
ReceivedOnTradeTransaction(e.ExpertHandle, e.Payload);
break;
case Mt5EventTypes.OnBookEvent:
ReceivedOnBookEvent(e.ExpertHandle, e.Payload);
break;
default:
throw new ArgumentOutOfRangeException();
}
} }
private void ReceivedOnTradeTransaction(int expertHandler, string payload) private void ReceivedOnTradeTransaction(int expertHandler, string payload)
@@ -3041,6 +3038,21 @@ namespace MtApi5
}); });
} }
private void ReceivedOnTickEvent(int expertHandler, string payload)
{
var e = JsonConvert.DeserializeObject<OnTickEvent>(payload);
var quote = new Mt5Quote(e.Instrument, e.Tick.bid, e.Tick.ask)
{
ExpertHandle = expertHandler,
Volume = e.Tick.volume,
Time = e.Tick.time,
Last = e.Tick.last
};
QuoteUpdated?.Invoke(this, quote.Instrument, quote.Bid, quote.Ask);
QuoteUpdate?.Invoke(this, new Mt5QuoteEventArgs(quote));
}
private void Connect(string host, int port) private void Connect(string host, int port)
{ {
var client = new MtClient(host, port); var client = new MtClient(host, port);
+5 -6
View File
@@ -240,7 +240,6 @@ namespace MtApi5TestClient
_mtApiClient.ConnectionStateChanged += mMtApiClient_ConnectionStateChanged; _mtApiClient.ConnectionStateChanged += mMtApiClient_ConnectionStateChanged;
_mtApiClient.QuoteAdded += mMtApiClient_QuoteAdded; _mtApiClient.QuoteAdded += mMtApiClient_QuoteAdded;
_mtApiClient.QuoteRemoved += mMtApiClient_QuoteRemoved; _mtApiClient.QuoteRemoved += mMtApiClient_QuoteRemoved;
_mtApiClient.QuoteUpdated += mMtApiClient_QuoteUpdated;
_mtApiClient.QuoteUpdate += mMtApiClient_QuoteUpdate; _mtApiClient.QuoteUpdate += mMtApiClient_QuoteUpdate;
_mtApiClient.OnTradeTransaction += mMtApiClient_OnTradeTransaction; _mtApiClient.OnTradeTransaction += mMtApiClient_OnTradeTransaction;
_mtApiClient.OnBookEvent += _mtApiClient_OnBookEvent; _mtApiClient.OnBookEvent += _mtApiClient_OnBookEvent;
@@ -1427,13 +1426,13 @@ namespace MtApi5TestClient
Application.Current?.Dispatcher.Invoke(action, args); Application.Current?.Dispatcher.Invoke(action, args);
} }
private static void mMtApiClient_QuoteUpdated(object sender, string symbol, double bid, double ask)
{
Console.WriteLine(@"Quote: Symbol = {0}, Bid = {1}, Ask = {2}", symbol, bid, ask);
}
private void mMtApiClient_QuoteUpdate(object sender, Mt5QuoteEventArgs e) private void mMtApiClient_QuoteUpdate(object sender, Mt5QuoteEventArgs e)
{ {
var q = e.Quote;
Console.WriteLine(@"Quote: Symbol = {0}, Bid = {1}, Ask = {2}, Volume = {3}, Time = {4}, Last = {5}"
, q.Instrument, q.Bid, q.Ask, q.Volume, q.Time, q.Last);
if (_quotesMap.ContainsKey(e.Quote.ExpertHandle)) if (_quotesMap.ContainsKey(e.Quote.ExpertHandle))
{ {
var qvm = _quotesMap[e.Quote.ExpertHandle]; var qvm = _quotesMap[e.Quote.ExpertHandle];
BIN
View File
Binary file not shown.
+34 -19
View File
@@ -1,7 +1,7 @@
#property copyright "Vyacheslav Demidyuk" #property copyright "Vyacheslav Demidyuk"
#property link "" #property link ""
#property version "1.5" #property version "1.6"
#property description "MtApi (MT5) connection expert" #property description "MtApi (MT5) connection expert"
#include <json.mqh> #include <json.mqh>
@@ -41,7 +41,7 @@
bool getBooleanValue(int expertHandle, int paramIndex, bool& res, string& err); bool getBooleanValue(int expertHandle, int paramIndex, bool& res, string& err);
#import #import
//#define __DEBUG_LOG__ #define __DEBUG_LOG__
input int Port = 8228; input int Port = 8228;
@@ -74,7 +74,13 @@ void OnDeinit(const int reason)
void OnTick() void OnTick()
{ {
start(); MqlTick last_tick;
SymbolInfoTick(Symbol(),last_tick);
MtOnTickEvent * tick_event = new MtOnTickEvent(Symbol(), last_tick);
SendMtEvent(ON_TICK_EVENT, tick_event);
delete tick_event;
if (IsTesting()) OnTimer(); if (IsTesting()) OnTimer();
} }
@@ -213,21 +219,6 @@ int deinit()
return (0); return (0);
} }
int start()
{
MqlTick last_tick;
SymbolInfoTick(Symbol(),last_tick);
double Bid = last_tick.bid;
double Ask = last_tick.ask;
if (!updateQuote(ExpertHandle, Symbol(), Bid, Ask, _error))
{
Print("updateQuote: [ERROR] ", _error);
}
return (0);
}
void OnTimer() void OnTimer()
{ {
while(true) while(true)
@@ -6980,7 +6971,8 @@ string ExecuteRequest_ChartXYToTimePrice(JSONObject *jo)
enum MtEventTypes enum MtEventTypes
{ {
ON_TRADE_TRANSACTION_EVENT = 1, ON_TRADE_TRANSACTION_EVENT = 1,
ON_BOOK_EVENT = 2 ON_BOOK_EVENT = 2,
ON_TICK_EVENT = 3
}; };
class MtEvent class MtEvent
@@ -7033,6 +7025,29 @@ private:
string _symbol; string _symbol;
}; };
class MtOnTickEvent : public MtEvent
{
public:
MtOnTickEvent(string symbol, const MqlTick& tick)
{
_symbol = symbol;
_tick = tick;
}
virtual JSONObject* CreateJson()
{
JSONObject *jo = new JSONObject();
jo.put("Tick", MqlTickToJson(_tick));
jo.put("Instrument", new JSONString(_symbol));
jo.put("ExpertHandle", new JSONNumber(ExpertHandle));
return jo;
}
private:
string _symbol;
MqlTick _tick;
};
void SendMtEvent(MtEventTypes eventType, MtEvent* mtEvent) void SendMtEvent(MtEventTypes eventType, MtEvent* mtEvent)
{ {
#ifdef __DEBUG_LOG__ #ifdef __DEBUG_LOG__