In my application, i need to show made calls to the user. The user can arrange some filters, according to what they want to see. The problem is that i find it quite hard to filter the calls without losing performance. This is what i am using now :
private void ProcessFilterChoice()
{
_filteredCalls = ServiceConnector.ServiceConnector.SingletonServiceConnector.Proxy.GetAllCalls().ToList();
if (cboOutgoingIncoming.SelectedIndex > -1)
GetFilterPartOutgoingIncoming();
if (cboInternExtern.SelectedIndex > -1)
GetFilterPartInternExtern();
if (cboDateFilter.SelectedIndex > -1)
GetFilteredCallsByDate();
wbPdf.Source = null;
btnPrint.Content = "Pdf preview";
}
private void GetFilterPartOutgoingIncoming()
{
if (cboOutgoingIncoming.SelectedItem.ToString().Equals("Outgoing"))
for (int i = _filteredCalls.Count - 1; i > -1; i--)
{
if (_filteredCalls[i].Caller.E164.Length > 4 || _filteredCalls[i].Caller.E164.Equals("0"))
_filteredCalls.RemoveAt(i);
}
else if (cboOutgoingIncoming.SelectedItem.ToString().Equals("Incoming"))
for (int i = _filteredCalls.Count - 1; i > -1; i--)
{
if (_filteredCalls[i].Called.E164.Length > 4 || _filteredCalls[i].Called.E164.Equals("0"))
_filteredCalls.RemoveAt(i);
}
}
private void GetFilterPartInternExtern()
{
if (cboInternExtern.SelectedItem.ToString().Equals("Intern"))
for (int i = _filteredCalls.Count - 1; i > -1; i--)
{
if (_filteredCalls[i].Called.E164.Length > 4 || _filteredCalls[i].Caller.E164.Length > 4 || _filteredCalls[i].Caller.E164.Equals("0"))
_filteredCalls.RemoveAt(i);
}
else if (cboInternExtern.SelectedItem.ToString().Equals("Extern"))
for (int i = _filteredCalls.Count - 1; i > -1; i--)
{
if ((_filteredCalls[i].Called.E164.Length < 5 && _filteredCalls[i].Caller.E164.Length < 5) || _filteredCalls[i].Called.E164.Equals("0"))
_filteredCalls.RemoveAt(i);
}
}
private void GetFilteredCallsByDate()
{
DateTime period = DateTime.Now;
switch (cboDateFilter.SelectedItem.ToString())
{
case "Today":
period = DateTime.Today;
break;
case "Last week":
period = DateTime.Today.Subtract(new TimeSpan(7, 0, 0, 0));
break;
case "Last month":
period = DateTime.Today.AddMonths(-1);
break;
case "Last year":
period = DateTime.Today.AddYears(-1);
break;
default:
return;
}
for (int i = _filteredCalls.Count - 1; i > -1; i--)
{
if (_filteredCalls[i].Start < period)
_filteredCalls.RemoveAt(i);
}
}
_filtered calls is a list of "calls". Calls is a class that looks like this :
[DataContract]
public class Call
{
private User caller, called;
private DateTime start, end;
private string conferenceId;
private int id;
private bool isNew = false;
[DataMember]
public bool IsNew
{
get { return isNew; }
set { isNew = value; }
}
[DataMember]
public int Id
{
get { return id; }
set { id = value; }
}
[DataMember]
public string ConferenceId
{
get { return conferenceId; }
set { conferenceId = value; }
}
[DataMember]
public DateTime End
{
get { return end; }
set { end = value; }
}
[DataMember]
public DateTime Start
{
get { return start; }
set { start = value; }
}
[DataMember]
public User Called
{
get { return called; }
set { called = value; }
}
[DataMember]
public User Caller
{
get { return caller; }
set { caller = value; }
}
Can anyone direct me to a better solution or make some suggestions.
Try using a foreach loop instead of an explicit for loop with an iterator variable. If _filteredCalls is a List, then _filteredCalls[i] will take O(n) time. Using a foreach will use the built in iterator of the list and will likely get you a speed increase.
Edit: I see you are actually looping backward through _filteredCalls, and also modifying the list.
So something like this would be better:
_filteredCalls = _filteredCalls.Where(c => c.Caller.E164.Length > 4 || c.Caller.E164.Equals("0"))
Related
Max open trades
Hi, guys.. I have to modify a bot to set a maximum trades of whatever number I give it.
At the moment it opens max one trade but I want to be able to tell the bot to have 3 open trades at the same time, for example...
Is there a way to do it?
CODE BELOW!
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class ThreeBarInsideBar : Robot
{
int upClose;
int upCloseBefore;
int insideBar;
int downClose;
int downCloseBefore;
int counter =0;
Position position;
[Parameter(DefaultValue = 10000)]
public int Volume { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 10)]
public int StopLoss { get; set; }
[Parameter("Take Profit (pips)", DefaultValue = 10)]
public int TakeProfit { get; set; }
protected override void OnBar()
{
if(Trade.IsExecuting){
return;
}
if(MarketSeries.Close[MarketSeries.Close.Count-1] > MarketSeries.Close[MarketSeries.High.Count-2]){
upClose = 1;
}else{
upClose = 0;
}
if(MarketSeries.Close[MarketSeries.Close.Count-3] > MarketSeries.Close[MarketSeries.Close.Count-4]){
upCloseBefore = 1;
}else{
upCloseBefore = 0;
}
if((MarketSeries.High[MarketSeries.Close.Count-2] < MarketSeries.High[MarketSeries.Close.Count-3])
&&(MarketSeries.Low[MarketSeries.Close.Count-2]> MarketSeries.Low[MarketSeries.Close.Count-3])){
insideBar = 1;
}else{
insideBar = 0;
}
if(MarketSeries.Close[MarketSeries.Close.Count-1] < MarketSeries.Close[MarketSeries.Low.Count-2]){
downClose = 1;
}else{
downClose = 0;
}
if(MarketSeries.Close[MarketSeries.Close.Count-3] < MarketSeries.Close[MarketSeries.Close.Count-4]){
downCloseBefore = 1;
}else{
downCloseBefore = 0;
}
if(counter == 0){
if(upClose == 1 && insideBar == 1 && upCloseBefore == 1){
Trade.CreateMarketOrder(TradeType.Buy,Symbol,Volume);
}
if( downClose == 1 && insideBar == 1 && downCloseBefore == 1){
Trade.CreateMarketOrder(TradeType.Sell,Symbol,Volume);
}
}
}
protected override void OnPositionOpened(Position openedPosition)
{
position = openedPosition;
counter = 1;
Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), GetAbsoluteTakeProfit(openedPosition, TakeProfit));
}
protected override void OnPositionClosed(Position position)
{
counter=0;
}
private double GetAbsoluteStopLoss(Position position, int stopLossInPips)
{
return position.TradeType == TradeType.Buy
? position.EntryPrice - Symbol.PipSize * stopLossInPips
: position.EntryPrice + Symbol.PipSize * stopLossInPips;
}
private double GetAbsoluteTakeProfit(Position position, int takeProfitInPips)
{
return position.TradeType == TradeType.Buy
? position.EntryPrice + Symbol.PipSize * takeProfitInPips
: position.EntryPrice - Symbol.PipSize * takeProfitInPips;
}
}
}
Have not tried much since I do not know how to do it!
I'm trying to use the keyword value in the set accessor and as long as the user entered value is greater than 0, I want to set it to the variable Quantity.
I can not seem to find what it is I am doing wrong. I keep getting a traceback error to for this Quantity = value;. Hoping someone can see what I don't. Thanks.
using System;
namespace Invoice
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("How many parts would you like to " +
"enter into the system: ");
int newParts = int.Parse(Console.ReadLine());
Invoice[] invoice = new Invoice[newParts];
for (int i = 0; i < newParts; i++)
{
invoice[i] = new Invoice();
Console.WriteLine("Enter the part number: ");
invoice[i].PartNumber = Console.ReadLine();
Console.WriteLine("Enter description of item: ");
invoice[i].PartDescription = Console.ReadLine();
Console.WriteLine("Enter the quantity: ");
invoice[i].Quantity = int.Parse(Console.ReadLine());
Console.WriteLine("Enter in the price of the item: ");
invoice[i].PricePerItem = decimal.Parse(Console.ReadLine());
}
for (int i = 0; i < newParts; i++)
{
invoice[i].DisplayOrder();
}
}
}
}
using System;
namespace Invoice
{
public class Invoice
{
public string PartNumber { get; set; }
public string PartDescription { get; set; }
public int Quantity
{
get { return Quantity; }
set
{
if (value >= 0)
{
Quantity = value;
}
if (value <= 0)
{
Quantity = Quantity;
}
}
}
public decimal PricePerItem
{
get
{
return PricePerItem;
}
set
{
if(value >= 0.0m)
{
PricePerItem = value;
}
if (value <= 0.0m)
{
PricePerItem = PricePerItem;
}
}
}
public Invoice(String PartNumber, String PartDescription, int Quantity, decimal PricePerItem)
{
this.PartNumber = PartNumber;
this.PartDescription = PartDescription;
this.Quantity = Quantity;
this.PricePerItem = PricePerItem;
}
public Invoice()
{
}
public decimal GetInvoiceAmount(int numberOfItems, decimal priceOfItem)
{
return numberOfItems * priceOfItem;
}
public void DisplayOrder()
{
decimal total = GetInvoiceAmount(Quantity, PricePerItem);
// Display Receipt
Console.Write("\nOrder Receipt: ");
Console.WriteLine($"\nPart Number: {PartNumber}");
Console.WriteLine($"Unit Price: {PricePerItem:C}");
Console.WriteLine($"Quantity: {Quantity}");
Console.WriteLine($"Part Description: {PartDescription}");
Console.WriteLine($"Total price: {total:C}");
}
}
}
This makes no sense:
if (value >= 0)
{
Quantity = value;
}
if (value <= 0)
{
Quantity = Quantity;
}
Why would you set a property to itself? That can't achieve anything useful. You say that you want to set the property if and only if the assigned value is greater than zero, so why would you be checking value for anything but being greater than zero?
if (value > 0)
{
Quantity = value;
}
That's it, that's all.
That said, you also ought to be throwing an ArgumentOutOfRangeException if the value is not valid, rather than just silently not setting the property. The logical way to do that would be like so:
if (value <= 0)
{
throw new ArgumentOutOfRangeException(...);
}
Quantity = value;
Now the property value will only be set if an exception is not thrown.
I also just realised that you have no backing field for this property, so that's wrong. The whole thing should look like this:
private int quantity;
public int Quantity
{
get { return quantity; }
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(...);
}
quantity = value;
}
}
The error is because in your set {} you are invoking the same setter recursively.
private int quantity;
public int Quantity
{
get { return this.quantity; }
set
{
if (value >= 0)
{
this.quantity= value;
}
}
}
private decimal pricePerItem;
public decimal PricePerItem
{
get
{
return this.pricePerItem;
}
set
{
if(value >= 0.0m)
{
this.pricePerItem= value;
}
}
}
How can I swap two elements of a two-linked list(by switching links)? I realized that I need to consider four cases: change with the first / last elements, change neighboring elements, and all other cases.
The cell structure is as follows:
public class Item<T>
{
private T _Data;
private Item<T> _Next;
private Item<T> _Prev;
public T Value
{
get { return _Data; }
set { this._Data = value; }
}
public Item(T Data)
{
this._Data = Data;
}
public Item()
{
this._Data = default;
}
public Item<T> Next
{
get { return this._Next; }
set { this._Next = value; }
}
public Item<T> Prev
{
get { return this._Prev; }
set { this._Prev = value; }
}
}
This is an implementation attempt. What's wrong?(Here I also do insert sort, but it all depends on the exchange of two elements)
public override D_List<T> Sorting(D_List<T> a)
{
for (int top = 1; top < a.Count; top++)
{
int k = top;
while (k > 0 && a[k-1] > a[k])
{
k--;
}
if (k == 0)
{
a[top].Prev.Next = a[top].Next;
a[top].Next.Prev = a[top].Prev;
a[top].Next = a[k];
a[top].Prev = null;
a[k].Prev = a[top];
}
else if(k == a.Count - 2)
{
a[k].Prev.Next = a[top];
a[top].Prev = a[k].Prev;
a[k].Next = null;
a[k].Prev = a[top];
}
else if(k+1 == top)//стоят подряд
{
a[k].Prev.Next = a[top];
a[top].Prev = a[k].Prev;
a[k].Next = a[top].Next;
a[top].Next = a[k];
a[k].Next.Prev = a[k];
}
else
{
a[k].Prev = a[top];
a[top].Prev.Next = a[top].Next;
a[top].Next.Prev = a[top].Prev;
a[top].Prev = a[k].Prev;
a[top].Next = a[k];
a[top].Next.Prev = a[top];
}
}
return a;
}
i have a "Player" class, which is supposed to manage my global music player.
This also works so far. The class is at the bottom, if you have any suggestions for improvement, feel free to give it to us.
I want to start a second song while the current song ends.
So a FadeIn into the next song and a FadeOut from the current song, which makes the song quieter.
My approach at the moment is that one song is running in the "waveOutDevice1" object and the second one is waiting in the second object. And as soon as the current song is about to end, the second WavePlayer starts. But I don't know how I can react to it, as soon as the current song is about to end.
Do you have an idea or suggestions?
With kind regards
my Player class:
public class Player
{
#region Properties, Fields
private IWavePlayer waveOutDevice1;
private IWavePlayer waveOutDevice2;
private AudioFileReader fileReader1;
private AudioFileReader fileReader2;
public AudioFile CurrentSong { get; private set; }
public Playlist CurrentPlaylist { get; private set; }
public List<AudioFile> lstPastSongs { get; private set; }
public List<AudioFile> lstNextSongs { get; set; }
public PlaybackState PlaybackState { get; private set; }
public bool Muted { get; private set; }
private float OldVolume = 0.0f;
#endregion
public Player()
{
this.lstNextSongs = new List<AudioFile>();
this.lstPastSongs = new List<AudioFile>();
}
#region Methods
public void Play(int index)
{
if (this.lstNextSongs.Count > 0 && index >= 0 && index < this.lstNextSongs.Count)
{
this.ResetFileReader();
this.CurrentSong = this.lstNextSongs[index];
this.CurrentPlaylist = this.CurrentSong.Playlist;
this.lstNextSongs.RemoveAt(index);
this.fileReader1 = new AudioFileReader(this.CurrentSong.Path);
this.waveOutDevice1 = new WaveOut();
this.waveOutDevice1.PlaybackStopped += WaveOutDevice1_PlaybackStopped;
this.waveOutDevice1.Init(this.fileReader1);
this.PlaybackState = PlaybackState.Playing;
this.waveOutDevice1.Play();
}
}
private void WaveOutDevice1_PlaybackStopped(object sender, StoppedEventArgs e)
{
this.Next();
}
private void ResetFileReader()
{
if (this.fileReader1 != null)
{
this.fileReader1.Dispose();
this.fileReader1 = null;
}
if (this.fileReader2 != null)
{
this.fileReader2.Dispose();
this.fileReader2 = null;
}
if(this.waveOutDevice1 != null)
{
this.waveOutDevice1.Dispose();
this.waveOutDevice1 = null;
}
if(this.waveOutDevice2 != null)
{
this.waveOutDevice2.Dispose();
this.waveOutDevice2 = null;
}
}
public void Pause()
{
if(this.waveOutDevice1 != null)
if (this.waveOutDevice1.PlaybackState == PlaybackState.Playing)
this.waveOutDevice1.Pause();
if(this.waveOutDevice2 != null)
if (this.waveOutDevice2.PlaybackState == PlaybackState.Playing)
this.waveOutDevice2.Pause();
this.PlaybackState = PlaybackState.Paused;
}
public void Continue()
{
if (this.waveOutDevice1 != null)
if (this.waveOutDevice1.PlaybackState == PlaybackState.Paused)
this.waveOutDevice1.Play();
if (this.waveOutDevice2 != null)
if (this.waveOutDevice2.PlaybackState == PlaybackState.Paused)
this.waveOutDevice2.Play();
this.PlaybackState = PlaybackState.Playing;
}
public void Next()
{
if(this.lstNextSongs.Count > 0)
{
if (this.CurrentSong != null)
this.lstPastSongs.Add(this.CurrentSong);
if (GlobalSettings.Shuffle)
{
System.Random random = new System.Random();
int randomNumber = random.Next(0, this.lstNextSongs.Count - 1);
this.Play(randomNumber);
}
else
this.Play(0);
}
else
{
if(GlobalSettings.Replay)
if(GlobalSettings.CurrentPlaylist != null)
for (int i = 0; i < GlobalSettings.CurrentPlaylist.panPlaylist.SongPlaylist.NumberOfSongs; i++)
this.lstNextSongs.AddRange(GlobalSettings.CurrentPlaylist.panPlaylist.SongPlaylist.AllSongs);
}
}
public void Previous()
{
if(this.CurrentSong == null)
{
if(this.lstPastSongs.Count > 0)
{
this.lstNextSongs.Insert(0, this.lstPastSongs[this.lstPastSongs.Count - 1]);
this.lstPastSongs.RemoveAt(this.lstPastSongs.Count - 1);
this.Play(0);
}
}
else
{
if(this.fileReader1 != null)
this._Previous(this.waveOutDevice1, this.fileReader1);
else if(this.fileReader2 != null)
this._Previous(this.waveOutDevice2, this.fileReader2);
}
}
private void _Previous(IWavePlayer waveOutDevice, AudioFileReader fileReader)
{
if (fileReader.CurrentTime.Seconds >= 10 || this.lstPastSongs.Count == 0)
{
waveOutDevice.Pause();
fileReader.CurrentTime = new System.TimeSpan(0, 0, 0);
waveOutDevice.Play();
}
else
{
this.lstNextSongs.Insert(0, this.CurrentSong);
this.lstNextSongs.Insert(0, this.lstPastSongs[this.lstPastSongs.Count - 1]);
this.lstPastSongs.RemoveAt(this.lstPastSongs.Count - 1);
this.Play(0);
}
}
public void SetVolume(int Volume)
{
if (Volume > -1 && Volume < 101)
{
float vol = (float)Volume / 100;
if (this.fileReader1 != null)
this.fileReader1.Volume = vol;
if (this.fileReader2 != null)
this.fileReader2.Volume = vol;
this.Muted = false;
}
}
public void Mute()
{
if(this.Muted)
{
if(this.fileReader1 != null)
{
this.fileReader1.Volume = this.OldVolume;
this.Muted = false;
}
else if(this.fileReader2 != null)
{
this.fileReader2.Volume = this.OldVolume;
this.Muted = false;
}
}
else
{
this.Muted = true;
if(this.fileReader1 != null)
{
this.OldVolume = this.fileReader1.Volume;
this.fileReader1.Volume = 0;
}
else if(this.fileReader2 != null)
{
this.OldVolume = this.fileReader2.Volume;
this.fileReader2.Volume = 0;
}
}
}
#endregion
}
If you can get the duration of the song when it starts playing, you can set a timer for duration - fadeInTime and start your fade when the timer fires.
Another approach is to use a single wave out device and MixingSampleProvider to add in the second song as a new input to the mixer as the first one is ending. You can do this by creating your own wrapper ISampleProvider whose Read method can keep accurate track of where you are up to.
I have the following class:
public class State {
public DateTime Created { get; set; }
public Double A { get; set; }
public Double B { get; set; }
}
Then I have two states:
State start = new State { Created = DateTime.UtcNow, A = 10.2, B = 20.5 }
State end = new State { Created = DateTime.UtcNow.AddDays(40), A = 1, B = 80 }
I would like to create a List between start and end where the values of A and B evolve in a linear way between their start and end values.
I was able to create a List as follows:
IList<DateTime> dates = new Range<DateTime>(start.Created, end.Created,).Expand(DateInterval.Day, 1)
Range and Expand are a few helpers I created ...
What is the best way to create the values evolution?
My idea would to do something like:
IList<State> states = new Range<State>( // here "tell" how each property would evolve ...)
UPDATE
Range Class
public class Range<T> where T : IComparable<T> {
private T _minimum;
private T _maximum;
public T Minimum { get { return _minimum; } set { value = _minimum; } }
public T Maximum { get { return _maximum; } set { value = _maximum; } }
public Range(T minimum, T maximum) {
_minimum = minimum;
_maximum = maximum;
} // Range
public Boolean Contains(T value) {
return (Minimum.CompareTo(value) <= 0) && (value.CompareTo(Maximum) <= 0);
} // Contains
public Boolean ContainsRange(Range<T> Range) {
return this.IsValid() && Range.IsValid() && this.Contains(Range.Minimum) && this.Contains(Range.Maximum);
} // ContainsRange
public Boolean IsInsideRange(Range<T> Range) {
return this.IsValid() && Range.IsValid() && Range.Contains(this.Minimum) && Range.Contains(this.Maximum);
} // IsInsideRange
public Boolean IsValid() {
return Minimum.CompareTo(Maximum) <= 0;
} // IsValid
public override String ToString() {
return String.Format("[{0} - {1}]", Minimum, Maximum);
} // ToString
} // Range
A few Range Extensions:
public static IEnumerable<Int32> Expand(this Range<Int32> range, Int32 step = 1) {
Int32 current = range.Minimum;
while (current <= range.Maximum) {
yield return current;
current += step;
}
} // Expand
public static IEnumerable<DateTime> Expand(this Range<DateTime> range, TimeSpan span) {
DateTime current = range.Minimum;
while (current <= range.Maximum) {
yield return current;
current = current.Add(span);
}
} // Expand
var magicNumber = 40;
var start = new State {Created = DateTime.UtcNow, A = 10.2, B = 20.5};
var end = new State {Created = DateTime.UtcNow.AddDays(magicNumber), A = 1, B = 80};
var stateList = new List<State>(magicNumber);
for (var i = 0; i < magicNumber; ++i)
{
var newA = start.A + (end.A - start.A) / magicNumber * (i + 1);
var newB = start.B + (end.B - start.B) / magicNumber * (i + 1);
stateList.Add(new State { Created = DateTime.UtcNow.AddDays(i + 1), A = newA, B = newB });
}
So you have moved to the stage of genericization where a "mutation step" injectable method that will apply your interpolation becomes logical. Because you don't constrain far enough to know how to interpolate steps, you may need a factory method to create your Func<T,T> instances.
For example, if there are two properties whose values shift in your T, you would be able to control their rate of change this way.
public class Point
{
int X {get;set;}
int Y {get;set;}
}
public static IEnumerable<T> Expand(this Range<T> range, Func<T,T> stepFunction)
where T: IComparable<T>
{
var current = range.Minimum;
while (range.Contains(current))
{
yield return current;
current = stepFunction(current);
}
}
// in method
var pointRange = new Range<Point>(
new Point{ X=0,Y=0}, new Point{ X=5, Y=20});
foreach (Point p in pointRange.Expand(p => new Point{ p.X + 1, p.Y + 4})