User regex in Positions.Find
31 Oct 2018, 14:27
Hey guys
Is is possible to use regex somehow inside the Positions.Find()?
Something like:
var p = Positions.Find(regex);
How can I make it work?
Thanks
Replies
irmscher9
31 Oct 2018, 15:43
OK, I'm not a c# pro unfortunately..
So how do I print out the found position label then? Print(position) doesnt work and Print(selected) is prints out some gibberish:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Text.RegularExpressions;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
protected override void OnStart()
{
ExecuteMarketOrder(TradeType.Buy, Symbol, 1, "Buy", 400, 600);
}
protected override void OnTick()
{
var rx = new Regex("\\w+", RegexOptions.IgnoreCase);
var selected = from position in Positions
where rx.IsMatch(position.Label)
select position;
Print(selected);
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
@irmscher9
PanagiotisCharalampous
31 Oct 2018, 16:07
Hi
selected is a collection of positions matching your regular expression, first you need to get an item from there and then print the label. See below
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Text.RegularExpressions;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class NewcBot : Robot
{
[Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }
protected override void OnStart()
{
ExecuteMarketOrder(TradeType.Buy, Symbol, 1, "Buy", 400, 600);
}
protected override void OnTick()
{
var rx = new Regex("\\w+", RegexOptions.IgnoreCase);
var selected = from position in Positions
where rx.IsMatch(position.Label)
select position;
Print(selected.First().Label);
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
Best Regards,
Panagiotis
@PanagiotisCharalampous
irmscher9
31 Oct 2018, 16:44
Thanks Panagiotis, you are a star!
And for those of you who is reading this, it's better to use ".FirstOrDefault()" instead of ".First()" if you want to avoid the excerption.
@irmscher9

PanagiotisCharalampous
31 Oct 2018, 14:49
Hi irmscher9,
You cannot use regex in Find() method. However, you could consider Linq. See below
var rx = new Regex("your regular expression", RegexOptions.IgnoreCase); var selected = from position in Positions where rx.IsMatch(position.Label) select position;Best Regards,
Panagiotis
@PanagiotisCharalampous