2016-05-10 17:56:39 +03:00
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Windows ;
using MtApi5 ;
using System.Collections.ObjectModel ;
2016-10-06 14:04:17 +03:00
using System.Globalization ;
2016-10-17 11:57:54 +03:00
using System.Threading.Tasks ;
2016-05-10 17:56:39 +03:00
namespace MtApi5TestClient
{
public class ViewModel : INotifyPropertyChanged
{
#region Commands
public DelegateCommand ConnectCommand { get ; private set ; }
public DelegateCommand DisconnectCommand { get ; private set ; }
public DelegateCommand OrderSendCommand { get ; private set ; }
2018-02-04 14:39:09 +02:00
public DelegateCommand HistoryOrderGetIntegerCommand { get ; private set ; }
2018-01-11 15:28:57 +02:00
public DelegateCommand HistoryDealGetDoubleCommand { get ; private set ; }
public DelegateCommand HistoryDealGetIntegerCommand { get ; private set ; }
public DelegateCommand HistoryDealGetStringCommand { get ; private set ; }
2018-02-08 18:04:15 +02:00
public DelegateCommand HistoryDealMethodsCommand { get ; private set ; }
2016-05-10 17:56:39 +03:00
public DelegateCommand AccountInfoDoubleCommand { get ; private set ; }
public DelegateCommand AccountInfoIntegerCommand { get ; private set ; }
public DelegateCommand AccountInfoStringCommand { get ; private set ; }
public DelegateCommand CopyRatesCommand { get ; private set ; }
public DelegateCommand CopyTimesCommand { get ; private set ; }
public DelegateCommand CopyOpenCommand { get ; private set ; }
public DelegateCommand CopyHighCommand { get ; private set ; }
public DelegateCommand CopyLowCommand { get ; private set ; }
public DelegateCommand CopyCloseCommand { get ; private set ; }
public DelegateCommand CopyTickVolumeCommand { get ; private set ; }
public DelegateCommand CopyRealVolumeCommand { get ; private set ; }
public DelegateCommand CopySpreadCommand { get ; private set ; }
2016-11-05 00:52:52 +02:00
public DelegateCommand CopyTicksCommand { get ; private set ; }
2016-05-10 17:56:39 +03:00
public DelegateCommand SymbolsTotalCommand { get ; private set ; }
public DelegateCommand SymbolNameCommand { get ; private set ; }
public DelegateCommand SymbolSelectCommand { get ; private set ; }
public DelegateCommand SymbolIsSynchronizedCommand { get ; private set ; }
public DelegateCommand SymbolInfoDoubleCommand { get ; private set ; }
public DelegateCommand SymbolInfoIntegerCommand { get ; private set ; }
public DelegateCommand SymbolInfoStringCommand { get ; private set ; }
public DelegateCommand SymbolInfoTickCommand { get ; private set ; }
public DelegateCommand SymbolInfoSessionQuoteCommand { get ; private set ; }
public DelegateCommand SymbolInfoSessionTradeCommand { get ; private set ; }
2016-10-06 14:04:17 +03:00
public DelegateCommand MarketBookAddCommand { get ; private set ; }
2016-05-10 17:56:39 +03:00
public DelegateCommand MarketBookReleaseCommand { get ; private set ; }
2016-10-06 14:04:17 +03:00
public DelegateCommand MarketBookGetCommand { get ; private set ; }
public DelegateCommand PositionOpenCommand { get ; private set ; }
2016-10-21 17:52:54 +03:00
public DelegateCommand PrintCommand { get ; private set ; }
2016-05-10 17:56:39 +03:00
#endregion
#region Properties
2016-10-06 14:04:17 +03:00
private Mt5ConnectionState _connectionState ;
2016-05-10 17:56:39 +03:00
public Mt5ConnectionState ConnectionState
{
2016-10-06 14:04:17 +03:00
get { return _connectionState ; }
2016-05-10 17:56:39 +03:00
set
{
2016-10-06 14:04:17 +03:00
_connectionState = value ;
2016-05-10 17:56:39 +03:00
OnPropertyChanged ( "ConnectionState" );
}
}
2016-10-06 14:04:17 +03:00
private string _connectionMessage ;
2016-05-10 17:56:39 +03:00
public string ConnectionMessage
{
2016-10-06 14:04:17 +03:00
get { return _connectionMessage ; }
2016-05-10 17:56:39 +03:00
set
{
2016-10-06 14:04:17 +03:00
_connectionMessage = value ;
2016-05-10 17:56:39 +03:00
OnPropertyChanged ( "ConnectionMessage" );
}
}
2016-10-06 14:04:17 +03:00
private string _host ;
2016-05-10 17:56:39 +03:00
public string Host
{
2016-10-06 14:04:17 +03:00
get { return _host ; }
2016-05-10 17:56:39 +03:00
set
{
2016-10-06 14:04:17 +03:00
_host = value ;
2016-05-10 17:56:39 +03:00
OnPropertyChanged ( "Host" );
}
}
2016-10-06 14:04:17 +03:00
private int _port ;
2016-05-10 17:56:39 +03:00
public int Port
{
2016-10-06 14:04:17 +03:00
get { return _port ; }
2016-05-10 17:56:39 +03:00
set
{
2016-10-06 14:04:17 +03:00
_port = value ;
2016-05-10 17:56:39 +03:00
OnPropertyChanged ( "Port" );
}
}
2016-10-06 14:04:17 +03:00
public ObservableCollection < QuoteViewModel > Quotes { get ; } = new ObservableCollection < QuoteViewModel >();
2016-05-10 17:56:39 +03:00
2016-10-06 14:04:17 +03:00
private QuoteViewModel _selectedQuote ;
2016-05-10 17:56:39 +03:00
public QuoteViewModel SelectedQuote
{
2016-10-06 14:04:17 +03:00
get { return _selectedQuote ; }
2016-05-10 17:56:39 +03:00
set
{
2016-10-06 14:04:17 +03:00
_selectedQuote = value ;
2016-05-10 17:56:39 +03:00
OnPropertyChanged ( "SelectedQuote" );
OnSelectedQuoteChanged ();
}
}
2016-10-06 14:04:17 +03:00
public ObservableCollection < string > History { get ; } = new ObservableCollection < string >();
2016-05-10 17:56:39 +03:00
2016-10-06 14:04:17 +03:00
public ObservableCollection < MqlTradeRequestViewModel > TradeRequests { get ; } = new ObservableCollection < MqlTradeRequestViewModel >();
2016-05-10 17:56:39 +03:00
2016-10-06 14:04:17 +03:00
private MqlTradeRequestViewModel _tradeRequest ;
2016-05-10 17:56:39 +03:00
public MqlTradeRequestViewModel TradeRequest
{
2016-10-06 14:04:17 +03:00
get { return _tradeRequest ; }
2016-05-10 17:56:39 +03:00
set
{
2016-10-06 14:04:17 +03:00
_tradeRequest = value ;
2016-05-10 17:56:39 +03:00
OnPropertyChanged ( "TradeRequest" );
}
}
public ENUM_ACCOUNT_INFO_DOUBLE AccountInfoDoublePropertyId { get ; set ; }
public ENUM_ACCOUNT_INFO_INTEGER AccountInfoIntegerPropertyId { get ; set ; }
public ENUM_ACCOUNT_INFO_STRING AccountInfoStringPropertyId { get ; set ; }
public TimeSeriesValueViewModel TimeSeriesValues { get ; set ; }
2016-10-06 14:04:17 +03:00
public ObservableCollection < string > TimeSeriesResults { get ; } = new ObservableCollection < string >();
2016-10-21 17:52:54 +03:00
private string _messageText = "Print some text in MetaTrader expert console" ;
public string MessageText
{
get { return _messageText ; }
set
{
_messageText = value ;
OnPropertyChanged ( "MessageText" );
}
}
2016-05-10 17:56:39 +03:00
#endregion
#region Public Methods
public ViewModel ()
{
// Init MtApi client
2016-10-06 14:04:17 +03:00
_mtApiClient = new MtApi5Client ();
2016-05-10 17:56:39 +03:00
2016-10-06 14:04:17 +03:00
_mtApiClient . ConnectionStateChanged += mMtApiClient_ConnectionStateChanged ;
_mtApiClient . QuoteAdded += mMtApiClient_QuoteAdded ;
_mtApiClient . QuoteRemoved += mMtApiClient_QuoteRemoved ;
_mtApiClient . QuoteUpdated += mMtApiClient_QuoteUpdated ;
2016-05-10 17:56:39 +03:00
_quotesMap = new Dictionary < string , QuoteViewModel >();
2016-10-06 14:04:17 +03:00
ConnectionState = _mtApiClient . ConnectionState ;
2016-05-10 17:56:39 +03:00
ConnectionMessage = "Disconnected" ;
Port = 8228 ; //default local port
InitCommands ();
var request = new MqlTradeRequest { Action = ENUM_TRADE_REQUEST_ACTIONS . TRADE_ACTION_DEAL
, Type = ENUM_ORDER_TYPE . ORDER_TYPE_BUY
, Volume = 0.1
, Comment = "Test Trade Request"
};
TradeRequest = new MqlTradeRequestViewModel ( request );
2016-10-06 14:04:17 +03:00
TimeSeriesValues = new TimeSeriesValueViewModel { Count = 100 };
2016-05-10 17:56:39 +03:00
}
public void Close ()
{
2016-10-06 14:04:17 +03:00
_mtApiClient . BeginDisconnect ();
2016-05-10 17:56:39 +03:00
}
#endregion
#region Private Methods
private void InitCommands ()
{
ConnectCommand = new DelegateCommand ( ExecuteConnect , CanExecuteConnect );
DisconnectCommand = new DelegateCommand ( ExecuteDisconnect , CanExecuteDisconnect );
OrderSendCommand = new DelegateCommand ( ExecuteOrderSend );
2018-02-04 14:39:09 +02:00
HistoryOrderGetIntegerCommand = new DelegateCommand ( ExecuteHistoryOrderGetInteger );
2018-01-11 15:28:57 +02:00
HistoryDealGetDoubleCommand = new DelegateCommand ( ExecuteHistoryDealGetDouble );
HistoryDealGetIntegerCommand = new DelegateCommand ( ExecuteHistoryDealGetInteger );
HistoryDealGetStringCommand = new DelegateCommand ( ExecuteHistoryDealGetString );
2018-02-08 18:04:15 +02:00
HistoryDealMethodsCommand = new DelegateCommand ( ExecuteHistoryDealMethods );
2016-05-10 17:56:39 +03:00
AccountInfoDoubleCommand = new DelegateCommand ( ExecuteAccountInfoDouble );
AccountInfoIntegerCommand = new DelegateCommand ( ExecuteAccountInfoInteger );
AccountInfoStringCommand = new DelegateCommand ( ExecuteAccountInfoString );
CopyRatesCommand = new DelegateCommand ( ExecuteCopyRates );
CopyTimesCommand = new DelegateCommand ( ExecuteCopyTime );
CopyOpenCommand = new DelegateCommand ( ExecuteCopyOpen );
CopyHighCommand = new DelegateCommand ( ExecuteCopyHigh );
CopyLowCommand = new DelegateCommand ( ExecuteCopyLow );
CopyCloseCommand = new DelegateCommand ( ExecuteCopyClose );
CopyTickVolumeCommand = new DelegateCommand ( ExecuteCopyTickVolume );
CopyRealVolumeCommand = new DelegateCommand ( ExecuteCopyRealVolume );
CopySpreadCommand = new DelegateCommand ( ExecuteCopySpread );
2016-11-05 00:52:52 +02:00
CopyTicksCommand = new DelegateCommand ( ExecuteCopyTicks );
2016-05-10 17:56:39 +03:00
SymbolsTotalCommand = new DelegateCommand ( ExecuteSymbolsTotal );
SymbolNameCommand = new DelegateCommand ( ExecuteSymbolName );
SymbolSelectCommand = new DelegateCommand ( ExecuteSymbolSelect );
SymbolIsSynchronizedCommand = new DelegateCommand ( ExecuteSymbolIsSynchronized );
SymbolInfoDoubleCommand = new DelegateCommand ( ExecuteSymbolInfoDouble );
SymbolInfoIntegerCommand = new DelegateCommand ( ExecuteSymbolInfoInteger );
SymbolInfoStringCommand = new DelegateCommand ( ExecuteSymbolInfoString );
SymbolInfoTickCommand = new DelegateCommand ( ExecuteSymbolInfoTick );
SymbolInfoSessionQuoteCommand = new DelegateCommand ( ExecuteSymbolInfoSessionQuote );
SymbolInfoSessionTradeCommand = new DelegateCommand ( ExecuteSymbolInfoSessionTrade );
MarketBookAddCommand = new DelegateCommand ( ExecuteMarketBookAdd );
MarketBookReleaseCommand = new DelegateCommand ( ExecuteMarketBookRelease );
2016-10-06 14:04:17 +03:00
MarketBookGetCommand = new DelegateCommand ( ExecuteMarketBookGet );
PositionOpenCommand = new DelegateCommand ( ExecutePositionOpen );
2016-10-21 17:52:54 +03:00
PrintCommand = new DelegateCommand ( ExecutePrint );
2016-05-10 17:56:39 +03:00
}
private bool CanExecuteConnect ( object o )
{
return ConnectionState == Mt5ConnectionState . Disconnected || ConnectionState == Mt5ConnectionState . Failed ;
}
private void ExecuteConnect ( object o )
{
if ( string . IsNullOrEmpty ( Host ))
{
2016-10-06 14:04:17 +03:00
_mtApiClient . BeginConnect ( Port );
2016-05-10 17:56:39 +03:00
}
else
{
2016-10-06 14:04:17 +03:00
_mtApiClient . BeginConnect ( Host , Port );
2016-05-10 17:56:39 +03:00
}
}
private bool CanExecuteDisconnect ( object o )
{
return ConnectionState == Mt5ConnectionState . Connected ;
}
private void ExecuteDisconnect ( object o )
{
2016-10-06 14:04:17 +03:00
_mtApiClient . BeginDisconnect ();
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteOrderSend ( object o )
2016-05-10 17:56:39 +03:00
{
var request = TradeRequest . GetMqlTradeRequest ();
2016-10-17 11:57:54 +03:00
var retVal = await Execute (() =>
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
MqlTradeResult result ;
var ok = _mtApiClient . OrderSend ( request , out result );
return ok ? result : null ;
});
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
var message = retVal != null ? $"OrderSend successed. {MqlTradeResultToString(retVal)}" : "OrderSend failed." ;
AddLog ( message );
2016-05-10 17:56:39 +03:00
}
2018-02-04 14:39:09 +02:00
private async void ExecuteHistoryOrderGetInteger ( object o )
{
const ulong ticket = 12345 ;
const ENUM_ORDER_PROPERTY_INTEGER propertyId = ENUM_ORDER_PROPERTY_INTEGER . ORDER_POSITION_ID ;
var retVal = await Execute (() => _mtApiClient . HistoryOrderGetInteger ( ticket , propertyId ));
AddLog ( $"HistoryOrderGetInteger: {retVal}" );
}
2018-01-11 15:28:57 +02:00
private async void ExecuteHistoryDealGetDouble ( object o )
{
const ulong ticket = 12345 ;
const ENUM_DEAL_PROPERTY_DOUBLE propertyId = ENUM_DEAL_PROPERTY_DOUBLE . DEAL_PROFIT ;
var retVal = await Execute (() => _mtApiClient . HistoryDealGetDouble ( ticket , propertyId ));
AddLog ( $"HistoryDealGetDouble: {retVal}" );
}
private async void ExecuteHistoryDealGetInteger ( object o )
{
const ulong ticket = 12345 ;
const ENUM_DEAL_PROPERTY_INTEGER propertyId = ENUM_DEAL_PROPERTY_INTEGER . DEAL_TICKET ;
var retVal = await Execute (() => _mtApiClient . HistoryDealGetInteger ( ticket , propertyId ));
AddLog ( $"HistoryDealGetInteger: {retVal}" );
}
private async void ExecuteHistoryDealGetString ( object o )
{
const ulong ticket = 12345 ;
const ENUM_DEAL_PROPERTY_STRING propertyId = ENUM_DEAL_PROPERTY_STRING . DEAL_SYMBOL ;
var retVal = await Execute (() => _mtApiClient . HistoryDealGetString ( ticket , propertyId ));
AddLog ( $"HistoryDealGetString: {retVal}" );
}
2018-02-08 18:04:15 +02:00
private async void ExecuteHistoryDealMethods ( object o )
{
try
{
var posId = await Execute (() => _mtApiClient . PositionGetInteger ( ENUM_POSITION_PROPERTY_INTEGER . POSITION_IDENTIFIER )); // posId = 7247951
var history = await Execute (() => _mtApiClient . HistorySelectByPosition ( posId )); // history = true
var historyDealsTotal = await Execute (() => _mtApiClient . HistoryDealsTotal ()); // historyDealsCount = 4
var histDealTicket = await Execute (() => _mtApiClient . HistoryDealGetTicket ( 0 )); // histDealTicket = 6632442
var histDealPrice = await Execute (() => _mtApiClient . HistoryDealGetDouble ( histDealTicket , ENUM_DEAL_PROPERTY_DOUBLE . DEAL_PRICE )); // Exception
}
catch ( Exception ex )
{
// ex.Messsage = "Service connection failed! Ошибка сериализации параметра http://tempuri.org/:command. Сообщение InnerException было \"Тип \"MtApi5.ENUM_DEAL_PROPERTY_DOUBLE\" с именем контракта данных \"ENUM_DEAL_PROPERTY_DOUBLE:http://schemas.datacontract.org/2004/07/MtApi5\" не ожидается. Попробуйте использовать DataContractResolver, если вы используете DataContractSerializer, или добавьте любые статически неизвестные типы в список известных типов - например, используя атрибут KnownTypeAttribute или путем их добавления в список известных типов, передаваемый в сериализатор.\". Подробнее см. InnerException."
AddLog ( ex . Message );
return ;
}
AddLog ( "ExecuteHistoryDealMethods: success." );
}
2016-10-17 11:57:54 +03:00
private async void ExecuteAccountInfoDouble ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var result = await Execute (() => _mtApiClient . AccountInfoDouble ( AccountInfoDoublePropertyId ));
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
var message = $"AccountInfoDouble: property_id = {AccountInfoDoublePropertyId}; result = {result}" ;
AddLog ( message );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteAccountInfoInteger ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var result = await Execute (() => _mtApiClient . AccountInfoInteger ( AccountInfoIntegerPropertyId ));
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
var message = $"AccountInfoInteger: property_id = {AccountInfoDoublePropertyId}; result = {result}" ;
AddLog ( message );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteAccountInfoString ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var result = await Execute (() => _mtApiClient . AccountInfoString ( AccountInfoStringPropertyId ));
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
var message = $"AccountInfoString: property_id = {AccountInfoDoublePropertyId}; result = {result}" ;
AddLog ( message );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteCopyTime ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
if ( string . IsNullOrEmpty ( TimeSeriesValues ?. SymbolValue )) return ;
TimeSeriesResults . Clear ();
var result = await Execute (() =>
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
DateTime [] array ;
var count = _mtApiClient . CopyTime ( TimeSeriesValues . SymbolValue , TimeSeriesValues . TimeFrame , TimeSeriesValues . StartPos , TimeSeriesValues . Count , out array );
return count > 0 ? array : null ;
});
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
if ( result == null )
{
AddLog ( "CopyTime: result is null" );
return ;
}
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
RunOnUiThread (() =>
{
foreach ( var time in result )
{
TimeSeriesResults . Add ( time . ToString ( CultureInfo . CurrentCulture ));
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
});
AddLog ( "CopyTime: success" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteCopyOpen ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
if ( string . IsNullOrEmpty ( TimeSeriesValues ?. SymbolValue )) return ;
TimeSeriesResults . Clear ();
var result = await Execute (() =>
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
double [] array ;
var count = _mtApiClient . CopyOpen ( TimeSeriesValues . SymbolValue , TimeSeriesValues . TimeFrame , TimeSeriesValues . StartPos , TimeSeriesValues . Count , out array );
return count > 0 ? array : null ;
});
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
if ( result == null )
{
AddLog ( "CopyOpen: result is null" );
return ;
}
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
RunOnUiThread (() =>
{
foreach ( var open in result )
{
TimeSeriesResults . Add ( open . ToString ( CultureInfo . CurrentCulture ));
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
});
AddLog ( "CopyOpen: success" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteCopyHigh ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
if ( string . IsNullOrEmpty ( TimeSeriesValues ?. SymbolValue )) return ;
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
TimeSeriesResults . Clear ();
var result = await Execute (() =>
{
2016-05-10 17:56:39 +03:00
double [] array ;
2016-10-06 14:04:17 +03:00
var count = _mtApiClient . CopyHigh ( TimeSeriesValues . SymbolValue , TimeSeriesValues . TimeFrame , TimeSeriesValues . StartPos , TimeSeriesValues . Count , out array );
2016-10-17 11:57:54 +03:00
return count > 0 ? array : null ;
});
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
if ( result == null )
{
AddLog ( "CopyHigh: result is null" );
return ;
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
RunOnUiThread (() =>
{
foreach ( var value in result )
{
TimeSeriesResults . Add ( value . ToString ( CultureInfo . CurrentCulture ));
}
});
AddLog ( "CopyHigh: success" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteCopyLow ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
if ( string . IsNullOrEmpty ( TimeSeriesValues ?. SymbolValue )) return ;
TimeSeriesResults . Clear ();
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
var result = await Execute (() =>
{
2016-05-10 17:56:39 +03:00
double [] array ;
2016-10-06 14:04:17 +03:00
var count = _mtApiClient . CopyLow ( TimeSeriesValues . SymbolValue , TimeSeriesValues . TimeFrame , TimeSeriesValues . StartPos , TimeSeriesValues . Count , out array );
2016-10-17 11:57:54 +03:00
return count > 0 ? array : null ;
});
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
if ( result == null )
{
AddLog ( "CopyLow: result is null" );
return ;
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
RunOnUiThread (() =>
{
foreach ( var value in result )
{
TimeSeriesResults . Add ( value . ToString ( CultureInfo . CurrentCulture ));
}
});
AddLog ( "CopyLow: success" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteCopyClose ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
if ( string . IsNullOrEmpty ( TimeSeriesValues ?. SymbolValue )) return ;
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
TimeSeriesResults . Clear ();
var result = await Execute (() =>
{
2016-05-10 17:56:39 +03:00
double [] array ;
2016-10-17 11:57:54 +03:00
var count = _mtApiClient . CopyClose ( TimeSeriesValues . SymbolValue , TimeSeriesValues . TimeFrame ,
TimeSeriesValues . StartPos , TimeSeriesValues . Count , out array );
return count > 0 ? array : null ;
});
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
if ( result == null )
{
AddLog ( "CopyClose: result is null" );
return ;
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
RunOnUiThread (() =>
{
foreach ( var value in result )
{
TimeSeriesResults . Add ( value . ToString ( CultureInfo . CurrentCulture ));
}
});
AddLog ( "CopyClose: success" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteCopyRates ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
if ( string . IsNullOrEmpty ( TimeSeriesValues ?. SymbolValue )) return ;
TimeSeriesResults . Clear ();
var result = await Execute (() =>
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
MqlRates [] array ;
var count = _mtApiClient . CopyRates ( TimeSeriesValues . SymbolValue , TimeSeriesValues . TimeFrame , TimeSeriesValues . StartPos , TimeSeriesValues . Count , out array );
return count > 0 ? array : null ;
});
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
if ( result == null )
{
AddLog ( "CopyRates: result is null" );
return ;
}
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
RunOnUiThread (() =>
{
foreach ( var rates in result )
{
TimeSeriesResults . Add (
$"time={rates.time}; open={rates.open}; high={rates.high}; low={rates.low}; close={rates.close}; tick_volume={rates.tick_volume}; spread={rates.spread}; real_volume={rates.tick_volume}" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
});
AddLog ( "CopyRates: success" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteCopyTickVolume ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
if ( string . IsNullOrEmpty ( TimeSeriesValues ?. SymbolValue )) return ;
TimeSeriesResults . Clear ();
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
var result = await Execute (() =>
{
2016-05-10 17:56:39 +03:00
long [] array ;
2016-10-17 11:57:54 +03:00
var count = _mtApiClient . CopyTickVolume ( TimeSeriesValues . SymbolValue , TimeSeriesValues . TimeFrame ,
TimeSeriesValues . StartPos , TimeSeriesValues . Count , out array );
return count > 0 ? array : null ;
});
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
if ( result == null )
{
AddLog ( "CopyTickVolume: result is null" );
return ;
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
RunOnUiThread (() =>
{
foreach ( var value in result )
{
TimeSeriesResults . Add ( value . ToString ());
}
});
AddLog ( "CopyTickVolume: success" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteCopyRealVolume ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
if ( string . IsNullOrEmpty ( TimeSeriesValues ?. SymbolValue )) return ;
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
TimeSeriesResults . Clear ();
var result = await Execute (() =>
{
2016-05-10 17:56:39 +03:00
long [] array ;
2016-10-17 11:57:54 +03:00
var count = _mtApiClient . CopyRealVolume ( TimeSeriesValues . SymbolValue , TimeSeriesValues . TimeFrame ,
TimeSeriesValues . StartPos , TimeSeriesValues . Count , out array );
return count > 0 ? array : null ;
});
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
if ( result == null )
{
AddLog ( "CopyRealVolume: result is null" );
return ;
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
RunOnUiThread (() =>
{
foreach ( var value in result )
{
TimeSeriesResults . Add ( value . ToString ());
}
});
AddLog ( "CopyRealVolume: success" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteCopySpread ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
if ( string . IsNullOrEmpty ( TimeSeriesValues ?. SymbolValue )) return ;
TimeSeriesResults . Clear ();
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
var result = await Execute (() =>
{
2016-05-10 17:56:39 +03:00
int [] array ;
2016-10-17 11:57:54 +03:00
var count = _mtApiClient . CopySpread ( TimeSeriesValues . SymbolValue , TimeSeriesValues . TimeFrame ,
TimeSeriesValues . StartPos , TimeSeriesValues . Count , out array );
return count > 0 ? array : null ;
});
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
if ( result == null )
{
AddLog ( "CopySpread: result is null" );
return ;
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
RunOnUiThread (() =>
{
foreach ( var value in result )
{
TimeSeriesResults . Add ( value . ToString ());
}
});
AddLog ( "CopySpread: success" );
2016-05-10 17:56:39 +03:00
}
2016-11-05 00:52:52 +02:00
private async void ExecuteCopyTicks ( object o )
{
if ( string . IsNullOrEmpty ( TimeSeriesValues ?. SymbolValue )) return ;
TimeSeriesResults . Clear ();
var result = await Execute (() => _mtApiClient . CopyTicks ( TimeSeriesValues . SymbolValue ));
if ( result == null )
{
AddLog ( "CopyTicks: result is null" );
return ;
}
AddLog ( "CopyTicks: success." );
RunOnUiThread (() =>
{
foreach ( var v in result )
{
var tickStr = $"time = {v.time}, bid = {v.bid}, ask = {v.ask}, last = {v.last}, volume = {v.volume}" ;
TimeSeriesResults . Add ( tickStr );
}
});
}
2016-10-17 11:57:54 +03:00
private async void ExecuteSymbolsTotal ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var selectedCount = await Execute (() => _mtApiClient . SymbolsTotal ( true ));
AddLog ( $"SymbolsTotal(true) success, result = {selectedCount}" );
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
var commonCount = await Execute (() => _mtApiClient . SymbolsTotal ( false ));
AddLog ( $"SymbolsTotal(false) success, result = {commonCount}" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteSymbolName ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var selectedSymbol = await Execute (() => _mtApiClient . SymbolName ( 5 , true ));
AddLog ( $"SymbolName(5, true) success, result = {selectedSymbol}" );
2016-05-10 17:56:39 +03:00
2016-10-17 11:57:54 +03:00
var commonSymbol = await Execute (() => _mtApiClient . SymbolName ( 5 , false ));
AddLog ( $"SymbolName(5, false) success, result = {commonSymbol}" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteSymbolSelect ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var retVal = await Execute (() => _mtApiClient . SymbolSelect ( "AUDJPY" , true ));
AddLog ( "SymbolSelect(AUDJPY, true) success, result = " + retVal );
2016-05-10 17:56:39 +03:00
2016-10-06 14:04:17 +03:00
//var retVal1 = _mtApiClient.SymbolSelect("AUDJPY", false);
2016-10-17 11:57:54 +03:00
//AddLog("SymbolSelect(AUDJPY, false) success, result = " + retVal1);
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteSymbolIsSynchronized ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var retVal = await Execute (() => _mtApiClient . SymbolIsSynchronized ( "EURUSD" ));
AddLog ( "SymbolIsSynchronized(EURUSD): result = " + retVal );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteSymbolInfoDouble ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var retVal = await Execute (() => _mtApiClient . SymbolInfoDouble ( "EURUSD" , ENUM_SYMBOL_INFO_DOUBLE . SYMBOL_BID ));
AddLog ( $"SymbolInfoDouble(EURUSD, ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_BID): result = {retVal}" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteSymbolInfoInteger ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var retVal = await Execute (() => _mtApiClient . SymbolInfoInteger ( "EURUSD" , ENUM_SYMBOL_INFO_INTEGER . SYMBOL_SPREAD ));
AddLog ( $"SymbolInfoInteger(EURUSD, ENUM_SYMBOL_INFO_INTEGER.SYMBOL_SPREAD): result = {retVal}" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteSymbolInfoString ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var retVal = await Execute (() => _mtApiClient . SymbolInfoString ( "EURUSD" , ENUM_SYMBOL_INFO_STRING . SYMBOL_DESCRIPTION ));
AddLog ( $"SymbolInfoString(EURUSD, ENUM_SYMBOL_INFO_STRING.SYMBOL_DESCRIPTION): result = {retVal}" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteSymbolInfoTick ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var result = await Execute (() =>
{
MqlTick tick ;
var ok = _mtApiClient . SymbolInfoTick ( "EURUSD" , out tick );
return ok ? tick : null ;
});
if ( result == null ) return ;
AddLog ( "SymbolInfoTick(EURUSD) success" );
AddLog ( $"SymbolInfoTick(EURUSD) tick.time = {result.time}" );
AddLog ( $"SymbolInfoTick(EURUSD) tick.bid = {result.bid}" );
AddLog ( $"SymbolInfoTick(EURUSD) tick.ask = {result.ask}" );
AddLog ( $"SymbolInfoTick(EURUSD) tick.last = {result.last}" );
AddLog ( $"SymbolInfoTick(EURUSD) tick.volume = {result.volume}" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteSymbolInfoSessionQuote ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var retVal = await Execute (() =>
{
DateTime from ;
DateTime to ;
var ok = _mtApiClient . SymbolInfoSessionQuote ( "EURUSD" , ENUM_DAY_OF_WEEK . MONDAY , 0 , out from , out to );
if ( ok )
{
AddLog ( $"SymbolInfoSessionQuote(EURUSD) from = {from}" );
AddLog ( $"SymbolInfoSessionQuote(EURUSD) to = {to}" );
}
return ok ;
});
AddLog ( $"SymbolInfoSessionQuote(EURUSD): result = {retVal}" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteSymbolInfoSessionTrade ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var retVal = await Execute (() =>
{
DateTime from ;
DateTime to ;
var ok = _mtApiClient . SymbolInfoSessionTrade ( "EURUSD" , ENUM_DAY_OF_WEEK . MONDAY , 0 , out from , out to );
if ( ok )
{
AddLog ( $"SymbolInfoSessionTrade(EURUSD) from = {from}" );
AddLog ( $"SymbolInfoSessionTrade(EURUSD) to = {to}" );
}
return ok ;
});
AddLog ( "SymbolInfoSessionTrade(EURUSD): result = " + retVal );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteMarketBookAdd ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var retVal = await Execute (() => _mtApiClient . MarketBookAdd ( "CHFJPY" ));
AddLog ( $"MarketBookAdd(CHFJPY): result = {retVal}" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteMarketBookRelease ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var retVal = await Execute (() => _mtApiClient . MarketBookRelease ( "CHFJPY" ));
AddLog ( $"MarketBookRelease(CHFJPY): result = {retVal}" );
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private async void ExecuteMarketBookGet ( object o )
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var result = await Execute (() =>
{
MqlBookInfo [] book ;
var ok = _mtApiClient . MarketBookGet ( "EURUSD" , out book );
return ok ? book : null ;
});
2016-10-06 14:04:17 +03:00
2016-10-17 11:57:54 +03:00
if ( result == null )
{
AddLog ( "MarketBookGet(EURUSD): result is null" );
return ;
}
AddLog ( "MarketBookGet(EURUSD): success" );
2016-10-06 14:04:17 +03:00
2016-10-17 11:57:54 +03:00
for ( var i = 0 ; i < result . Length ; i ++)
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
AddLog ( $"MarketBookGet: book[{i}].price = {result[i].price}" );
AddLog ( $"MarketBookGet: book[{i}].price = {result[i].volume}" );
AddLog ( $"MarketBookGet: book[{i}].price = {result[i].type}" );
2016-05-10 17:56:39 +03:00
}
}
2016-10-17 11:57:54 +03:00
private async void ExecutePositionOpen ( object obj )
2016-05-10 17:56:39 +03:00
{
2016-10-06 14:04:17 +03:00
const string symbol = "EURUSD" ;
const ENUM_ORDER_TYPE orderType = ENUM_ORDER_TYPE . ORDER_TYPE_BUY ;
const double volume = 0.1 ;
2017-09-29 19:05:06 +03:00
const double price = 1.18129 ;
const double sl = 1.1811 ;
const double tp = 1.1814 ;
2016-10-06 14:04:17 +03:00
const string comment = "Test PositionOpen" ;
2017-09-29 19:05:06 +03:00
MqlTradeResult tradeResult = null ;
2016-10-06 14:04:17 +03:00
2017-09-29 19:05:06 +03:00
var retVal = await Execute (() => _mtApiClient . PositionOpen ( symbol , orderType , volume , price , sl , tp , comment , out tradeResult ));
AddLog ( $"PositionOpen: symbol EURUSD retVal = {retVal}, result = {tradeResult}" );
2016-05-10 17:56:39 +03:00
}
2016-10-21 17:52:54 +03:00
private async void ExecutePrint ( object obj )
{
var message = MessageText ;
var retVal = await Execute (() => _mtApiClient . Print ( message ));
AddLog ( $"Print: message print in MetaTrader - {retVal}" );
}
2016-10-06 14:04:17 +03:00
private static void RunOnUiThread ( Action action )
2016-05-10 17:56:39 +03:00
{
2016-10-06 14:04:17 +03:00
Application . Current ?. Dispatcher . Invoke ( action );
2016-05-10 17:56:39 +03:00
}
2016-10-06 14:04:17 +03:00
private static void RunOnUiThread < T >( Action < T > action , params object [] args )
{
Application . Current ?. Dispatcher . Invoke ( action , args );
}
2016-05-10 17:56:39 +03:00
private void mMtApiClient_QuoteUpdated ( object sender , string symbol , double bid , double ask )
{
if ( string . IsNullOrEmpty ( symbol ) == false )
{
2016-10-06 14:04:17 +03:00
if ( _quotesMap . ContainsKey ( symbol ))
2016-05-10 17:56:39 +03:00
{
var qvm = _quotesMap [ symbol ];
qvm . Bid = bid ;
qvm . Ask = ask ;
}
if ( string . Equals ( symbol , TradeRequest . Symbol ))
{
if ( TradeRequest . Type == ENUM_ORDER_TYPE . ORDER_TYPE_BUY )
{
TradeRequest . Price = ask ;
}
else if ( TradeRequest . Type == ENUM_ORDER_TYPE . ORDER_TYPE_SELL )
{
TradeRequest . Price = bid ;
}
}
}
}
private void mMtApiClient_QuoteRemoved ( object sender , Mt5QuoteEventArgs e )
{
2016-10-06 14:04:17 +03:00
RunOnUiThread < Mt5Quote >( RemoveQuote , e . Quote );
2016-05-10 17:56:39 +03:00
}
private void mMtApiClient_QuoteAdded ( object sender , Mt5QuoteEventArgs e )
{
2016-10-06 14:04:17 +03:00
RunOnUiThread < Mt5Quote >( AddQuote , e . Quote );
2016-05-10 17:56:39 +03:00
}
private void mMtApiClient_ConnectionStateChanged ( object sender , Mt5ConnectionEventArgs e )
{
ConnectionState = e . Status ;
ConnectionMessage = e . ConnectionMessage ;
2016-10-06 14:04:17 +03:00
RunOnUiThread ( ConnectCommand . RaiseCanExecuteChanged );
RunOnUiThread ( DisconnectCommand . RaiseCanExecuteChanged );
2016-05-10 17:56:39 +03:00
switch ( e . Status )
{
case Mt5ConnectionState . Connected :
2016-10-06 14:04:17 +03:00
RunOnUiThread ( OnConnected );
2016-05-10 17:56:39 +03:00
break ;
case Mt5ConnectionState . Disconnected :
2016-10-06 14:04:17 +03:00
RunOnUiThread ( OnDisconnected );
2016-05-10 17:56:39 +03:00
break ;
}
}
private void AddQuote ( Mt5Quote quote )
{
if ( quote == null )
return ;
2016-10-06 14:04:17 +03:00
QuoteViewModel qvm ;
2016-05-10 17:56:39 +03:00
if ( _quotesMap . ContainsKey ( quote . Instrument ) == false )
{
qvm = new QuoteViewModel ( quote . Instrument );
_quotesMap [ quote . Instrument ] = qvm ;
Quotes . Add ( qvm );
}
else
{
2016-10-06 14:04:17 +03:00
qvm = _quotesMap [ quote . Instrument ];
2016-05-10 17:56:39 +03:00
}
qvm . FeedCount ++;
qvm . Bid = quote . Bid ;
qvm . Ask = quote . Ask ;
}
private void RemoveQuote ( Mt5Quote quote )
{
2016-10-17 11:57:54 +03:00
if ( quote == null ) return ;
2016-05-10 17:56:39 +03:00
2016-10-06 14:04:17 +03:00
if ( _quotesMap . ContainsKey ( quote . Instrument ))
2016-05-10 17:56:39 +03:00
{
var qvm = _quotesMap [ quote . Instrument ];
qvm . FeedCount --;
if ( qvm . FeedCount <= 0 )
{
_quotesMap . Remove ( quote . Instrument );
Quotes . Remove ( qvm );
}
}
}
private void OnConnected ()
{
2016-10-06 14:04:17 +03:00
var quotes = _mtApiClient . GetQuotes ();
2016-05-10 17:56:39 +03:00
if ( quotes != null )
{
foreach ( var quote in quotes )
{
AddQuote ( quote );
}
}
}
private void OnDisconnected ()
{
_quotesMap . Clear ();
Quotes . Clear ();
}
private static string MqlTradeResultToString ( MqlTradeResult result )
{
return result != null ?
"Retcode = " + result . Retcode + ";"
+ " Comment = " + result . Comment + ";"
+ " Order = " + result . Order + ";"
+ " Volume = " + result . Volume + ";"
+ " Price = " + result . Price + ";"
+ " Deal = " + result . Deal + ";"
+ " Request_id = " + result . Request_id + ";"
+ " Bid = " + result . Bid + ";"
+ " Ask = " + result . Ask + ";" : string . Empty ;
}
private void OnSelectedQuoteChanged ()
{
2016-10-17 11:57:54 +03:00
if ( SelectedQuote == null ) return ;
TradeRequest . Symbol = SelectedQuote . Instrument ;
TimeSeriesValues . SymbolValue = SelectedQuote . Instrument ;
}
private async Task < TResult > Execute < TResult >( Func < TResult > func )
{
return await Task . Factory . StartNew (() =>
2016-05-10 17:56:39 +03:00
{
2016-10-17 11:57:54 +03:00
var result = default ( TResult );
try
{
result = func ();
}
catch ( Exception ex )
{
AddLog ( $"Exception: {ex.Message}" );
}
return result ;
});
2016-05-10 17:56:39 +03:00
}
2016-10-17 11:57:54 +03:00
private void AddLog ( string msg )
{
RunOnUiThread (() =>
{
var time = DateTime . Now . ToString ( "h:mm:ss tt" );
History . Add ( $"[{time}]: {msg}" );
});
}
2016-05-10 17:56:39 +03:00
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged ;
protected virtual void OnPropertyChanged ( string propertyName )
{
2016-10-06 14:04:17 +03:00
PropertyChanged ?. Invoke ( this , new PropertyChangedEventArgs ( propertyName ));
2016-05-10 17:56:39 +03:00
}
#endregion
#region Private Fields
2016-10-06 14:04:17 +03:00
private readonly MtApi5Client _mtApiClient ;
2016-05-10 17:56:39 +03:00
2016-10-06 14:04:17 +03:00
private readonly Dictionary < string , QuoteViewModel > _quotesMap ;
2016-05-10 17:56:39 +03:00
#endregion
}
}