mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-07-28 02:57:56 +00:00
Issue #94: Added event handler OnBookEvent (MT5).
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
{
|
||||
internal enum Mt5EventTypes
|
||||
{
|
||||
OnTradeTransaction = 1
|
||||
OnTradeTransaction = 1,
|
||||
OnBookEvent = 2
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace MtApi5.Events
|
||||
{
|
||||
internal class OnBookEvent
|
||||
{
|
||||
public string Symbol { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace MtApi5
|
||||
{
|
||||
public class Mt5BookEventArgs : EventArgs
|
||||
{
|
||||
public int ExpertHandle { get; set; }
|
||||
public string Symbol { get; set; } //Symbol of OnBookEvent event.
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CopyTicksFlag.cs" />
|
||||
<Compile Include="Events\OnBookEvent.cs" />
|
||||
<Compile Include="Events\OnTradeTransactionEvent.cs" />
|
||||
<Compile Include="MqlBookInfo.cs" />
|
||||
<Compile Include="MqlParam.cs" />
|
||||
@@ -55,6 +56,7 @@
|
||||
<Compile Include="MqlTick.cs" />
|
||||
<Compile Include="MqlTradeCheckResult.cs" />
|
||||
<Compile Include="MqlTradeTransaction.cs" />
|
||||
<Compile Include="Mt5BookEventArgs.cs" />
|
||||
<Compile Include="Mt5TimeConverter.cs" />
|
||||
<Compile Include="Mt5Enums.cs" />
|
||||
<Compile Include="Mt5ConnectionEventArgs.cs" />
|
||||
|
||||
@@ -2387,6 +2387,7 @@ namespace MtApi5
|
||||
public event EventHandler<Mt5QuoteEventArgs> QuoteRemoved;
|
||||
public event EventHandler<Mt5ConnectionEventArgs> ConnectionStateChanged;
|
||||
public event EventHandler<Mt5TradeTransactionEventArgs> OnTradeTransaction;
|
||||
public event EventHandler<Mt5BookEventArgs> OnBookEvent;
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
@@ -2464,6 +2465,9 @@ namespace MtApi5
|
||||
case Mt5EventTypes.OnTradeTransaction:
|
||||
ReceivedOnTradeTransaction(e.ExpertHandle, e.Payload);
|
||||
break;
|
||||
case Mt5EventTypes.OnBookEvent:
|
||||
ReceivedOnBookEvent(e.ExpertHandle, e.Payload);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
@@ -2481,6 +2485,16 @@ namespace MtApi5
|
||||
});
|
||||
}
|
||||
|
||||
private void ReceivedOnBookEvent(int expertHandler, string payload)
|
||||
{
|
||||
var e = JsonConvert.DeserializeObject<OnBookEvent>(payload);
|
||||
OnBookEvent?.Invoke(this, new Mt5BookEventArgs
|
||||
{
|
||||
ExpertHandle = expertHandler,
|
||||
Symbol = e.Symbol
|
||||
});
|
||||
}
|
||||
|
||||
private void Connect(string host, int port)
|
||||
{
|
||||
var client = new MtClient(host, port);
|
||||
|
||||
@@ -175,6 +175,7 @@ namespace MtApi5TestClient
|
||||
_mtApiClient.QuoteRemoved += mMtApiClient_QuoteRemoved;
|
||||
_mtApiClient.QuoteUpdated += mMtApiClient_QuoteUpdated;
|
||||
_mtApiClient.OnTradeTransaction += mMtApiClient_OnTradeTransaction;
|
||||
_mtApiClient.OnBookEvent += _mtApiClient_OnBookEvent;
|
||||
|
||||
_quotesMap = new Dictionary<string, QuoteViewModel>();
|
||||
|
||||
@@ -1014,6 +1015,11 @@ namespace MtApi5TestClient
|
||||
AddLog($"OnTradeTransaction: ExpertHandle = {e.ExpertHandle}.{Environment.NewLine}Transaction = {e.Trans}.{Environment.NewLine}Request = {e.Request}.{Environment.NewLine}Result = {e.Result}.");
|
||||
}
|
||||
|
||||
private void _mtApiClient_OnBookEvent(object sender, Mt5BookEventArgs e)
|
||||
{
|
||||
AddLog($"OnBookEvent: ExpertHandle = {e.ExpertHandle}, Symbol = {e.Symbol}");
|
||||
}
|
||||
|
||||
private void AddQuote(Mt5Quote quote)
|
||||
{
|
||||
if (quote == null)
|
||||
|
||||
Binary file not shown.
+39
-8
@@ -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;
|
||||
|
||||
@@ -85,12 +85,23 @@ void OnTradeTransaction(
|
||||
)
|
||||
{
|
||||
#ifdef __DEBUG_LOG__
|
||||
Print("%s:", __FUNCTION__);
|
||||
PrintFormat("%s:", __FUNCTION__);
|
||||
#endif
|
||||
|
||||
OnTradeTransactionEvent* transEvent = new OnTradeTransactionEvent(trans, request, result);
|
||||
SendMtEvent(ON_TRADE_TRANSACTION_EVENT, transEvent);
|
||||
delete transEvent;
|
||||
MtOnTradeTransactionEvent* trans_event = new MtOnTradeTransactionEvent(trans, request, result);
|
||||
SendMtEvent(ON_TRADE_TRANSACTION_EVENT, trans_event);
|
||||
delete trans_event;
|
||||
}
|
||||
|
||||
void OnBookEvent(const string& symbol)
|
||||
{
|
||||
#ifdef __DEBUG_LOG__
|
||||
PrintFormat("%s: %s", __FUNCTION__, symbol);
|
||||
#endif
|
||||
|
||||
MtOnBookEvent * book_event = new MtOnBookEvent(symbol);
|
||||
SendMtEvent(ON_BOOK_EVENT, book_event);
|
||||
delete book_event;
|
||||
}
|
||||
|
||||
int preinit()
|
||||
@@ -6043,7 +6054,8 @@ string ExecuteRequest_IndicatorCreate(JSONObject *jo)
|
||||
|
||||
enum MtEventTypes
|
||||
{
|
||||
ON_TRADE_TRANSACTION_EVENT = 1
|
||||
ON_TRADE_TRANSACTION_EVENT = 1,
|
||||
ON_BOOK_EVENT = 2
|
||||
};
|
||||
|
||||
class MtEvent
|
||||
@@ -6052,10 +6064,10 @@ public:
|
||||
virtual JSONObject* CreateJson() = 0;
|
||||
};
|
||||
|
||||
class OnTradeTransactionEvent : public MtEvent
|
||||
class MtOnTradeTransactionEvent : public MtEvent
|
||||
{
|
||||
public:
|
||||
OnTradeTransactionEvent(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result)
|
||||
MtOnTradeTransactionEvent(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result)
|
||||
{
|
||||
_trans = trans;
|
||||
_request = request;
|
||||
@@ -6077,6 +6089,25 @@ private:
|
||||
MqlTradeResult _result;
|
||||
};
|
||||
|
||||
class MtOnBookEvent : public MtEvent
|
||||
{
|
||||
public:
|
||||
MtOnBookEvent(const string& symbol)
|
||||
{
|
||||
_symbol = symbol;
|
||||
}
|
||||
|
||||
virtual JSONObject* CreateJson()
|
||||
{
|
||||
JSONObject *jo = new JSONObject();
|
||||
jo.put("Symbol", new JSONString(_symbol));
|
||||
return jo;
|
||||
}
|
||||
|
||||
private:
|
||||
string _symbol;
|
||||
};
|
||||
|
||||
void SendMtEvent(MtEventTypes eventType, MtEvent* mtEvent)
|
||||
{
|
||||
#ifdef __DEBUG_LOG__
|
||||
|
||||
Reference in New Issue
Block a user