Added MtEvent into MtApiService. Added event LastTimeBar into MtApi (MT4)

This commit is contained in:
DW
2016-08-30 16:01:23 +03:00
parent e24502f303
commit edc7311104
18 changed files with 238 additions and 180 deletions
+2 -8
View File
@@ -1,14 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MtApi
namespace MtApi
{
static class ExtensionMethods
{
public static MtQuote Parse(this MTApiService.MtQuote quote)
public static MtQuote Convert(this MTApiService.MtQuote quote)
{
return (quote != null) ? new MtQuote(quote.Instrument, quote.Bid, quote.Ask) : null;
}
+3
View File
@@ -66,10 +66,12 @@
<Compile Include="MtConnectionState.cs" />
<Compile Include="MtCommandType.cs" />
<Compile Include="MtErrorCode.cs" />
<Compile Include="MtEventTypes.cs" />
<Compile Include="MtExecutionException.cs" />
<Compile Include="MtOrder.cs" />
<Compile Include="MtQuote.cs" />
<Compile Include="MtQuoteEventArgs.cs" />
<Compile Include="MtTimeBar.cs" />
<Compile Include="MtTypes.cs" />
<Compile Include="PriceConstantsType.cs" />
<Compile Include="MarketInfoModeType.cs" />
@@ -95,6 +97,7 @@
<Compile Include="Responses\OrderSendResponse.cs" />
<Compile Include="Responses\ResponseBase.cs" />
<Compile Include="SeriesIdentifier.cs" />
<Compile Include="TimeBarArgs.cs" />
<Compile Include="TradeOperation.cs" />
<Compile Include="MtApiTimeConverter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+31 -8
View File
@@ -29,11 +29,12 @@ namespace MtApi
public MtApiClient()
{
_client.QuoteAdded +=new MtClient.MtQuoteHandler(mClient_QuoteAdded);
_client.QuoteRemoved += new MtClient.MtQuoteHandler(mClient_QuoteRemoved);
_client.QuoteUpdated += new MtClient.MtQuoteHandler(mClient_QuoteUpdated);
_client.ServerDisconnected += new EventHandler(mClient_ServerDisconnected);
_client.ServerFailed += new EventHandler(mClient_ServerFailed);
_client.QuoteAdded += mClient_QuoteAdded;
_client.QuoteRemoved += mClient_QuoteRemoved;
_client.QuoteUpdated += mClient_QuoteUpdated;
_client.ServerDisconnected += mClient_ServerDisconnected;
_client.ServerFailed += mClient_ServerFailed;
_client.MtEventReceived += _client_MtEventReceived;
}
#endregion
@@ -81,7 +82,7 @@ namespace MtApi
public IEnumerable<MtQuote> GetQuotes()
{
var quotes = _client.GetQuotes();
return quotes != null ? (from q in quotes select q.Parse()) : null;
return quotes != null ? (from q in quotes select q.Convert()) : null;
}
#endregion
@@ -1597,14 +1598,35 @@ namespace MtApi
private void mClient_QuoteRemoved(MTApiService.MtQuote quote)
{
var handler = QuoteRemoved;
handler?.BeginInvoke(this, new MtQuoteEventArgs(quote.Parse()), (a) => handler.EndInvoke(a), null);
handler?.BeginInvoke(this, new MtQuoteEventArgs(quote.Convert()), (a) => handler.EndInvoke(a), null);
}
private void mClient_QuoteAdded(MTApiService.MtQuote quote)
{
var handler = QuoteAdded;
handler?.BeginInvoke(this, new MtQuoteEventArgs(quote.Parse()), (a) => handler.EndInvoke(a), null);
handler?.BeginInvoke(this, new MtQuoteEventArgs(quote.Convert()), (a) => handler.EndInvoke(a), null);
}
private void _client_MtEventReceived(object sender, MtEventArgs e)
{
MtEventTypes eventType = (MtEventTypes) e.Event.EventType;
switch(eventType)
{
case MtEventTypes.LastTimeBar:
{
FireOnLastTimeBar(JsonConvert.DeserializeObject<MtTimeBar>(e.Event.Payload));
}
break;
}
}
private void FireOnLastTimeBar(MtTimeBar timeBar)
{
var handler = OnLastTimeBar;
handler?.BeginInvoke(this, new TimeBarArgs(timeBar), (a) => handler.EndInvoke(a), null);
}
#endregion
#region Events
@@ -1612,6 +1634,7 @@ namespace MtApi
public event EventHandler<MtQuoteEventArgs> QuoteAdded;
public event EventHandler<MtQuoteEventArgs> QuoteRemoved;
public event EventHandler<MtConnectionEventArgs> ConnectionStateChanged;
public event EventHandler<TimeBarArgs> OnLastTimeBar;
#endregion
#region Private Fields
+7
View File
@@ -0,0 +1,7 @@
namespace MtApi
{
public enum MtEventTypes
{
LastTimeBar = 1
}
}
+26
View File
@@ -0,0 +1,26 @@
using System;
namespace MtApi
{
public class MtTimeBar
{
public string Symbol { get; set; }
public int MtOpenTime { get; set; }
public int MtCloseTime { get; set; }
public double Open { get; set; }
public double Close { get; set; }
public double High { get; set; }
public double Low { get; set; }
public DateTime OpenTime
{
get { return MtApiTimeConverter.ConvertFromMtTime(MtOpenTime); }
}
public DateTime CloseTime
{
get { return MtApiTimeConverter.ConvertFromMtTime(MtCloseTime); }
}
}
}
+18
View File
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MtApi
{
public class TimeBarArgs: EventArgs
{
public TimeBarArgs(MtTimeBar timeBar)
{
TimeBar = timeBar;
}
public MtTimeBar TimeBar { get; private set; }
}
}