Positions.Count ?!?
Positions.Count ?!?
04 Jun 2016, 10:27
Hi,
I am trying to code a cBot with a limit number of trades open at the same time.
What I did so far:
private int longPositions = 0;
private int shortPositions = 0;
[Parameter("Short Trades", DefaultValue = 2, MinValue = 0)]
public int MaxShortTrades { get; set; }
[Parameter("Total Trades", DefaultValue = 2, MinValue = 0)]
public int MaxTrades { get; set; }
OnBar()
var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
if (longPosition < MaxLongTrades)
{
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label, StopLoss, TakeProfit);
Replies
jeremy.vostik
04 Jun 2016, 10:29
//EDIT2:
of course I have
Parameter("Long Trades", DefaultValue = 2, MinValue = 0)]
public int MaxLongTrades { get; set; }
It was a typo in the opening post.
@jeremy.vostik
jeremy.vostik
04 Jun 2016, 11:19
Now I tried
var longPositionsCount = Positions.Count;
It works, but it Counts ALL trades, even closed ones.
So after 5 Trades, my cBot stops.
@jeremy.vostik
... Deleted by UFO ...
jeremy.vostik
04 Jun 2016, 13:43
Looks like it is working. Thank you VERY VERY much. Now on to my next Problem :o)
@jeremy.vostik
whis.gg
04 Jun 2016, 14:54
var longPosition = Positions.FindAll(Label, Symbol, TradeType.Buy);
This variable holds array of all positions found.
longPosition.Length returns you number of items in the array.
Personally I would use LINQ query.
int numberOfLongPositions = Positions.Where(pos => pos.Label == Label) .Where(pos => pos.Symbol == Symbol) .Where(pos => pos.TradeType == TradeType.Buy) .Count(); int numberOfShortPositions = Positions.Where(pos => pos.Label == Label) .Where(pos => pos.Symbol == Symbol) .Where(pos => pos.TradeType == TradeType.Sell) .Count();
@whis.gg
jeremy.vostik
04 Jun 2016, 10:28
//EDIT:
The above gives an Operand error.
I also tried with
var shortPositionsCount = Positions.Count(p => p.TradeType == TradeType.Sell);
var longPositionsCount = Positions.Count(p => p.TradeType == TradeType.Buy);
This compiles, but in backtesting no orders are entred. at all.
@jeremy.vostik