Skip to content

Commit

Permalink
Issue #106: Extended structure Mt5Quote with fields from MqlTick (vol…
Browse files Browse the repository at this point in the history
…ume, last, time)
  • Loading branch information
vdemydiuk committed May 8, 2018
1 parent fd34f00 commit 6023bff
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 40 deletions.
3 changes: 2 additions & 1 deletion MtApi5/Events/Mt5EventTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
internal enum Mt5EventTypes
{
OnTradeTransaction = 1,
OnBookEvent = 2
OnBookEvent = 2,
OnTick = 3
}
}
9 changes: 9 additions & 0 deletions MtApi5/Events/OnTickEvent.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
10 changes: 8 additions & 2 deletions MtApi5/Mt5Quote.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MTApiService;
using System;
using MTApiService;

namespace MtApi5
{
Expand All @@ -8,8 +9,13 @@ public class Mt5Quote
public double Bid { get; }
public double Ask { get; }
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;
Bid = bid;
Expand Down
1 change: 1 addition & 0 deletions MtApi5/MtApi5.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<ItemGroup>
<Compile Include="CopyTicksFlag.cs" />
<Compile Include="Events\OnBookEvent.cs" />
<Compile Include="Events\OnTickEvent.cs" />
<Compile Include="Events\OnTradeTransactionEvent.cs" />
<Compile Include="MqlBookInfo.cs" />
<Compile Include="MqlParam.cs" />
Expand Down
36 changes: 24 additions & 12 deletions MtApi5/MtApi5Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@ public class MtApi5Client
private volatile bool _isBacktestingMode;
private Mt5ConnectionState _connectionState = Mt5ConnectionState.Disconnected;
private int _executorHandle;
private readonly Dictionary<Mt5EventTypes, Action<int, string>> _mtEventHandlers =
new Dictionary<Mt5EventTypes, Action<int, string>>();

#endregion

#region Public Methods
public MtApi5Client()
{
LogConfigurator.Setup(LogProfileName);

_mtEventHandlers[Mt5EventTypes.OnBookEvent] = ReceivedOnBookEvent;
_mtEventHandlers[Mt5EventTypes.OnTick] = ReceivedOnTickEvent;
_mtEventHandlers[Mt5EventTypes.OnTradeTransaction] = ReceivedOnTradeTransaction;
}

///<summary>
Expand Down Expand Up @@ -3002,21 +3009,11 @@ private void Connect(MtClient client)
}
}


private void _client_MtEventReceived(MtEvent e)
{
var eventType = (Mt5EventTypes)e.EventType;

switch (eventType)
{
case Mt5EventTypes.OnTradeTransaction:
ReceivedOnTradeTransaction(e.ExpertHandle, e.Payload);
break;
case Mt5EventTypes.OnBookEvent:
ReceivedOnBookEvent(e.ExpertHandle, e.Payload);
break;
default:
throw new ArgumentOutOfRangeException();
}
_mtEventHandlers[eventType](e.ExpertHandle, e.Payload);
}

private void ReceivedOnTradeTransaction(int expertHandler, string payload)
Expand All @@ -3041,6 +3038,21 @@ private void ReceivedOnBookEvent(int expertHandler, string payload)
});
}

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)
{
var client = new MtClient(host, port);
Expand Down
11 changes: 5 additions & 6 deletions TestClients/MtApi5TestClient/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ public ViewModel()
_mtApiClient.ConnectionStateChanged += mMtApiClient_ConnectionStateChanged;
_mtApiClient.QuoteAdded += mMtApiClient_QuoteAdded;
_mtApiClient.QuoteRemoved += mMtApiClient_QuoteRemoved;
_mtApiClient.QuoteUpdated += mMtApiClient_QuoteUpdated;
_mtApiClient.QuoteUpdate += mMtApiClient_QuoteUpdate;
_mtApiClient.OnTradeTransaction += mMtApiClient_OnTradeTransaction;
_mtApiClient.OnBookEvent += _mtApiClient_OnBookEvent;
Expand Down Expand Up @@ -1427,13 +1426,13 @@ private static void RunOnUiThread<T>(Action<T> action, params object[] 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)
{
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))
{
var qvm = _quotesMap[e.Quote.ExpertHandle];
Expand Down
Binary file modified mq5/MtApi5.ex5
Binary file not shown.
53 changes: 34 additions & 19 deletions mq5/MtApi5.mq5
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#property copyright "Vyacheslav Demidyuk"
#property link ""

#property version "1.5"
#property version "1.6"
#property description "MtApi (MT5) connection expert"

#include <json.mqh>
Expand Down Expand Up @@ -41,7 +41,7 @@
bool getBooleanValue(int expertHandle, int paramIndex, bool& res, string& err);
#import

//#define __DEBUG_LOG__
#define __DEBUG_LOG__

input int Port = 8228;

Expand Down Expand Up @@ -74,7 +74,13 @@ void OnDeinit(const int reason)

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();
}

Expand Down Expand Up @@ -213,21 +219,6 @@ int deinit()
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()
{
while(true)
Expand Down Expand Up @@ -6980,7 +6971,8 @@ string ExecuteRequest_ChartXYToTimePrice(JSONObject *jo)
enum MtEventTypes
{
ON_TRADE_TRANSACTION_EVENT = 1,
ON_BOOK_EVENT = 2
ON_BOOK_EVENT = 2,
ON_TICK_EVENT = 3
};

class MtEvent
Expand Down Expand Up @@ -7033,6 +7025,29 @@ private:
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)
{
#ifdef __DEBUG_LOG__
Expand Down

0 comments on commit 6023bff

Please sign in to comment.