mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-15 19:58:10 +00:00
Added function CopyRates. Implemented Backtesting
This commit is contained in:
@@ -8,44 +8,6 @@ namespace MtApi
|
||||
{
|
||||
static class ExtensionMethods
|
||||
{
|
||||
#region Event Methods
|
||||
|
||||
public static async void FireEvent(this MtApiQuoteHandler evenHandler, object sender, string symbol, double bid, double ask)
|
||||
{
|
||||
if (evenHandler != null)
|
||||
{
|
||||
await Task.Factory.StartNew(() =>
|
||||
{
|
||||
evenHandler(sender, symbol, bid, ask);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static async void FireEvent(this EventHandler eventHandler, object sender)
|
||||
{
|
||||
if (eventHandler != null)
|
||||
{
|
||||
await Task.Factory.StartNew(() =>
|
||||
{
|
||||
eventHandler(sender, EventArgs.Empty);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static async void FireEvent<T>(this EventHandler<T> eventHandler, object sender, T e)
|
||||
where T : EventArgs
|
||||
{
|
||||
if (eventHandler != null)
|
||||
{
|
||||
await Task.Factory.StartNew(() =>
|
||||
{
|
||||
eventHandler(sender, e);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public static MtQuote Parse(this MTApiService.MtQuote quote)
|
||||
{
|
||||
return (quote != null) ? new MtQuote(quote.Instrument, quote.Bid, quote.Ask) : null;
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
|
||||
namespace MtApi
|
||||
{
|
||||
public class MqlRates
|
||||
{
|
||||
public int MtTime { get; set; }
|
||||
|
||||
public DateTime Time
|
||||
{
|
||||
get { return MtApiTimeConverter.ConvertFromMtTime(MtTime); }
|
||||
}
|
||||
|
||||
public double Open { get; set; }
|
||||
public double High { get; set; }
|
||||
public double Low { get; set; }
|
||||
public double Close { get; set; }
|
||||
public long TickVolume { get; set; }
|
||||
public int Spread { get; set; }
|
||||
public long RealVolume { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -60,6 +60,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="ChartPeriod.cs" />
|
||||
<Compile Include="ExtensionMethods.cs" />
|
||||
<Compile Include="MqlRates.cs" />
|
||||
<Compile Include="MtConnectionEventArgs.cs" />
|
||||
<Compile Include="MtConnectionException.cs" />
|
||||
<Compile Include="MtConnectionState.cs" />
|
||||
@@ -76,6 +77,7 @@
|
||||
<Compile Include="MtApiColorConverter.cs" />
|
||||
<Compile Include="OrderSelectMode.cs" />
|
||||
<Compile Include="OrderSelectSource.cs" />
|
||||
<Compile Include="Requests\CopyRatesRequest.cs" />
|
||||
<Compile Include="Requests\GetOrderRequest.cs" />
|
||||
<Compile Include="Requests\GetOrdersRequest.cs" />
|
||||
<Compile Include="Requests\ICustomRequest.cs" />
|
||||
@@ -86,6 +88,7 @@
|
||||
<Compile Include="Requests\OrderSendRequest.cs" />
|
||||
<Compile Include="Requests\RequestBase.cs" />
|
||||
<Compile Include="Requests\RequestType.cs" />
|
||||
<Compile Include="Responses\CopyRatesResponse.cs" />
|
||||
<Compile Include="Responses\GetOrderResponse.cs" />
|
||||
<Compile Include="Responses\GetOrdersResponse.cs" />
|
||||
<Compile Include="Responses\ICustomResponse.cs" />
|
||||
|
||||
+68
-8
@@ -16,6 +16,7 @@ namespace MtApi
|
||||
public sealed class MtApiClient
|
||||
{
|
||||
private const int DoubleArrayLimit = 64800;
|
||||
private volatile bool IsBacktestingMode = false;
|
||||
|
||||
#region MetaTrader Constants
|
||||
|
||||
@@ -1365,6 +1366,47 @@ namespace MtApi
|
||||
return SendCommand<bool>(MtCommandType.RefreshRates, null);
|
||||
}
|
||||
|
||||
public List<MqlRates> CopyRates(string symbol_name, ENUM_TIMEFRAMES timeframe, int start_pos, int count)
|
||||
{
|
||||
var response = SendRequest<CopyRatesResponse>(new CopyRates1Request
|
||||
{
|
||||
SymbolName = symbol_name,
|
||||
Timeframe = timeframe,
|
||||
StartPos = start_pos,
|
||||
Count = count
|
||||
});
|
||||
return response != null ? response.Rates : null;
|
||||
}
|
||||
|
||||
public List<MqlRates> CopyRates(string symbol_name, ENUM_TIMEFRAMES timeframe, DateTime start_time, int count)
|
||||
{
|
||||
var response = SendRequest<CopyRatesResponse>(new CopyRates2Request
|
||||
{
|
||||
SymbolName = symbol_name,
|
||||
Timeframe = timeframe,
|
||||
StartTime = MtApiTimeConverter.ConvertToMtTime(start_time),
|
||||
Count = count
|
||||
});
|
||||
return response != null ? response.Rates : null;
|
||||
}
|
||||
|
||||
public List<MqlRates> CopyRates(string symbol_name, ENUM_TIMEFRAMES timeframe, DateTime start_time, DateTime stop_time)
|
||||
{
|
||||
var response = SendRequest<CopyRatesResponse>(new CopyRates3Request
|
||||
{
|
||||
SymbolName = symbol_name,
|
||||
Timeframe = timeframe,
|
||||
StartTime = MtApiTimeConverter.ConvertToMtTime(start_time),
|
||||
StopTime = MtApiTimeConverter.ConvertToMtTime(stop_time)
|
||||
});
|
||||
return response != null ? response.Rates : null;
|
||||
}
|
||||
|
||||
private void BaBacktestingReady()
|
||||
{
|
||||
SendCommand<object>(MtCommandType.BacktestingReady, null);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Checkup
|
||||
@@ -1385,7 +1427,6 @@ namespace MtApi
|
||||
private void Connect(string host, int port)
|
||||
{
|
||||
UpdateConnectionState(MtConnectionState.Connecting, string.Format("Connecting to {0}:{1}", host, port));
|
||||
|
||||
try
|
||||
{
|
||||
_client.Open(host, port);
|
||||
@@ -1396,14 +1437,13 @@ namespace MtApi
|
||||
UpdateConnectionState(MtConnectionState.Failed, string.Format("Failed connection to {0}:{1}. {2}", host, port, e.Message));
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateConnectionState(MtConnectionState.Connected, string.Format("Connected to {0}:{1}", host, port));
|
||||
OnConnected();
|
||||
}
|
||||
|
||||
private void Connect(int port)
|
||||
{
|
||||
UpdateConnectionState(MtConnectionState.Connecting, string.Format("Connecting to 'localhost':{0}", port));
|
||||
|
||||
try
|
||||
{
|
||||
_client.Open(port);
|
||||
@@ -1414,8 +1454,17 @@ namespace MtApi
|
||||
UpdateConnectionState(MtConnectionState.Failed, string.Format("Failed connection to 'localhost':{0}. {1}", port, e.Message));
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateConnectionState(MtConnectionState.Connected, string.Format("Connected to 'localhost':{0}", port));
|
||||
OnConnected();
|
||||
}
|
||||
|
||||
private void OnConnected()
|
||||
{
|
||||
IsBacktestingMode = IsTesting();
|
||||
if (IsBacktestingMode)
|
||||
{
|
||||
BaBacktestingReady();
|
||||
}
|
||||
}
|
||||
|
||||
private void Disconnect()
|
||||
@@ -1440,7 +1489,8 @@ namespace MtApi
|
||||
|
||||
if (changed)
|
||||
{
|
||||
ConnectionStateChanged.FireEvent(this, new MtConnectionEventArgs(state, message));
|
||||
var handler = ConnectionStateChanged;
|
||||
handler?.BeginInvoke(this, new MtConnectionEventArgs(state, message), (a) => handler.EndInvoke(a), null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1522,7 +1572,15 @@ namespace MtApi
|
||||
{
|
||||
if (quote != null)
|
||||
{
|
||||
QuoteUpdated.FireEvent(this, quote.Instrument, quote.Bid, quote.Ask);
|
||||
var handler = QuoteUpdated;
|
||||
if (IsBacktestingMode)
|
||||
{
|
||||
handler?.Invoke(this, quote.Instrument, quote.Bid, quote.Ask);
|
||||
}
|
||||
else
|
||||
{
|
||||
handler?.BeginInvoke(this, quote.Instrument, quote.Bid, quote.Ask, (a) => handler.EndInvoke(a), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1538,12 +1596,14 @@ namespace MtApi
|
||||
|
||||
private void mClient_QuoteRemoved(MTApiService.MtQuote quote)
|
||||
{
|
||||
QuoteRemoved.FireEvent(this, new MtQuoteEventArgs(quote.Parse()));
|
||||
var handler = QuoteRemoved;
|
||||
handler?.BeginInvoke(this, new MtQuoteEventArgs(quote.Parse()), (a) => handler.EndInvoke(a), null);
|
||||
}
|
||||
|
||||
private void mClient_QuoteAdded(MTApiService.MtQuote quote)
|
||||
{
|
||||
QuoteAdded.FireEvent(this, new MtQuoteEventArgs(quote.Parse()));
|
||||
var handler = QuoteAdded;
|
||||
handler?.BeginInvoke(this, new MtQuoteEventArgs(quote.Parse()), (a) => handler.EndInvoke(a), null);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -192,6 +192,9 @@ namespace MtApi
|
||||
SymbolInfoString = 154,
|
||||
|
||||
//Requests
|
||||
MtRequest = 155
|
||||
MtRequest = 155,
|
||||
|
||||
//Backtesting
|
||||
BacktestingReady = 156
|
||||
}
|
||||
}
|
||||
|
||||
+27
-10
@@ -1,14 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace MtApi
|
||||
namespace MtApi
|
||||
{
|
||||
class MtTypes
|
||||
{
|
||||
}
|
||||
|
||||
public enum ENUM_TERMINAL_INFO_STRING
|
||||
{
|
||||
TERMINAL_LANGUAGE = 13,
|
||||
@@ -27,4 +18,30 @@ namespace MtApi
|
||||
SYMBOL_DESCRIPTION = 20,
|
||||
SYMBOL_PATH = 21
|
||||
}
|
||||
|
||||
public enum ENUM_TIMEFRAMES
|
||||
{
|
||||
PERIOD_CURRENT = 0,
|
||||
PERIOD_M1 = 1,
|
||||
PERIOD_M2 = 2,
|
||||
PERIOD_M3 = 3,
|
||||
PERIOD_M4 = 4,
|
||||
PERIOD_M5 = 5,
|
||||
PERIOD_M6 = 6,
|
||||
PERIOD_M10 = 10,
|
||||
PERIOD_M12 = 12,
|
||||
PERIOD_M15 = 15,
|
||||
PERIOD_M20 = 20,
|
||||
PERIOD_M30 = 30,
|
||||
PERIOD_H1 = 60,
|
||||
PERIOD_H2 = 120,
|
||||
PERIOD_H3 = 180,
|
||||
PERIOD_H4 = 240,
|
||||
PERIOD_H6 = 360,
|
||||
PERIOD_H8 = 480,
|
||||
PERIOD_H12 = 720,
|
||||
PERIOD_D1 = 1440,
|
||||
PERIOD_W1 = 10080,
|
||||
PERIOD_MN1 = 43200
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MtApi.Requests
|
||||
{
|
||||
public abstract class CopyRatesRequestBase : RequestBase
|
||||
{
|
||||
public enum CopyRatesTypeEnum
|
||||
{
|
||||
CopyRates_1 = 1,
|
||||
CopyRates_2 = 2,
|
||||
CopyRates_3 = 3,
|
||||
}
|
||||
|
||||
public override RequestType RequestType
|
||||
{
|
||||
get { return RequestType.CopyRates; }
|
||||
}
|
||||
|
||||
public abstract CopyRatesTypeEnum CopyRatesType { get; }
|
||||
|
||||
public string SymbolName { get; set; }
|
||||
public ENUM_TIMEFRAMES Timeframe { get; set; }
|
||||
}
|
||||
|
||||
public class CopyRates1Request : CopyRatesRequestBase
|
||||
{
|
||||
public override CopyRatesTypeEnum CopyRatesType
|
||||
{
|
||||
get { return CopyRatesTypeEnum.CopyRates_1; }
|
||||
}
|
||||
|
||||
public int StartPos { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
|
||||
public class CopyRates2Request : CopyRatesRequestBase
|
||||
{
|
||||
public override CopyRatesTypeEnum CopyRatesType
|
||||
{
|
||||
get { return CopyRatesTypeEnum.CopyRates_2; }
|
||||
}
|
||||
|
||||
public int StartTime { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
|
||||
public class CopyRates3Request : CopyRatesRequestBase
|
||||
{
|
||||
public override CopyRatesTypeEnum CopyRatesType
|
||||
{
|
||||
get { return CopyRatesTypeEnum.CopyRates_3; }
|
||||
}
|
||||
|
||||
public int StartTime { get; set; }
|
||||
public int StopTime { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
OrderCloseBy = 5,
|
||||
OrderDelete = 6,
|
||||
OrderModify = 7,
|
||||
iCustom = 8
|
||||
iCustom = 8,
|
||||
CopyRates = 9
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MtApi.Responses
|
||||
{
|
||||
public class CopyRatesResponse: ResponseBase
|
||||
{
|
||||
public List<MqlRates> Rates { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user