Articles in this section
Category / Section

How to access data from metatrader to WPF Chart (SfChart)?

2 mins read

You can accessing the data that fetched from the Metatrader into an ObservableCollection, and setting that collection to the ItemsSource for WPF Chart (SfChart) series. It has been explained in following steps.

Step 1: Use the following code example as Model class.

Model.cs

public class Model
{
   public int OrderTicket { get; set; }
   public double TakeProfit { get; set; }
   public double Profit { get; set; }
   public double OpenPrice { get; set; }
   public double ClosePrice { get; set; }
}

Step 2: Fetch the data from Metatrader, and add it into a collection.

MainWindow.cs

public partial class MainWindow : Window
{
   private MetaTrader4 metaTrader4;
   private int OrderCount = 0;
   public ObservableCollection<Model> Collection { get; set; }
 
   public MainWindow()
   {
     InitializeComponent();
     this.DataContext = this;
     Collection = new ObservableCollection<Model>();
     Bridge.InitializeHosts(true);
 
     //Corresponding account number and its respective symbol has to be passed, in order to make connection with required account
     metaTrader4 = Bridge.GetTerminal(2088782777,"EURUSD.arm");
     metaTrader4.QuoteRecieved += metaTrader4_QuoteRecieved;
   }
 
   private void metaTrader4_QuoteRecieved(QuoteListener mql)
   {
     if (OrderCount != mql.OrdersTotal())
     {            
       for (int i = mql.OrdersTotal() - 1; i >= 0; i--)
       {
          if (mql.OrderSelect(i, SELECT_BY.SELECT_BY_POS))
          {
        int orderTicket = mql.OrderTicket();
             double orderProfit = mql.OrderProfit();
             double takeProfit = mql.OrderTakeProfit();
             double openPrice = mql.OrderOpenPrice();
             double closePrice = mql.OrderClosePrice();
             var data = new Model
             {
               OrderTicket = orderTicket,
               Profit = orderProfit,
               TakeProfit = takeProfit,
               ClosePrice = closePrice,
               OpenPrice = openPrice
             };
             //Corresponding data fetched from MetaTrader is added to Collection and this ObservableCollection is set as ItemsSource for SfChart
             Collection.Add(data);
          }
       }
     }
   }
}

Step 3: Bind the corresponding collection (with data fetched from Metatrader) to series’ ItemsSource.

MainWindow.xaml

<chart:SfChart Margin="10" >
    <chart:SfChart.PrimaryAxis>
        <chart:CategoryAxis/>
    </chart:SfChart.PrimaryAxis>
    <chart:SfChart.SecondaryAxis>
        <chart:NumericalAxis/>
    </chart:SfChart.SecondaryAxis>
 
    <!--ObservableCollection consists of fetched data from MetaTrader is set as ItemsSource for series-->
    <!--Corresponding property field is set as the binding path for series-->
   <chart:CandleSeries ItemsSource="{Binding Collection}"
                                      XBindingPath="OrderTicket" 
                                      High="Profit" Low="TakeProfit"
                                      Open="OpenPrice" Close="ClosePrice"/>
</chart:SfChart>

Acessing Data from MetaTrader in WPF Chart

C#: Sample link.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied