Topics
Replies
The_Dark_Knight
30 Dec 2023, 11:24
( Updated at: 30 Dec 2023, 11:26 )
RE: Multiple cbots and only one position at a time
firemyst said:
If you don't want positions of the same symbol to overlap, then you need to find all positions first of the current symbol, and then doing a Positions.Count on that.
Doing a generic “Positions.Count” will count all open positions, even if you have several not open on the current symbol your bot instance is running.
For example, you could have an AUDUSD position open, a EURUSD position open, and EURJPY position open. If you only want to limit your bot so that you don't open another EURJPY position, then you need to search all positions that are just EURJPY.
Example:
int numPositionsAlreadyOpen = Positions.FindAll(p.Label, Symbol.Name).Count();
Hope that helps you even more :-)
Thanks Firemyst, that's also very helpful. I found something like that in the forum.
For now, I will apply this logic by grouping the long and short positions separately, in order to decrease exposure at times where the pairs are correlated. According to the results of QuantAnalyzer, this would greatly soften the portfolio curve, but I don't know how much to trust those hypothetical results. I'll try it first in demo.
I added it like this in each cbot. Please correct me if that´s wrong:
protected override void OnBar()
{
var longPositionsCount = Positions.Count(p => p.TradeType == TradeType.Buy);
var shortPositionsCount = Positions.Count(p => p.TradeType == TradeType.Sell);
if (longPositionsCount == 0 && ShouldEnterLong()) { logic }
if (ShouldExitLong()) {CloseLongPositions();}
if (shortPositionsCount == 0 && ShouldEnterShort()) { logic }
if (ShouldExitShort()) {CloseShortPositions();}
}
@The_Dark_Knight
The_Dark_Knight
29 Dec 2023, 21:41
( Updated at: 30 Dec 2023, 07:09 )
RE: Multiple cbots and only one position at a time
PanagiotisCharalampous said:
Hi there,
It would be simpler to check if there are any positions open instead of using this method. You can use Positions.Count to do this.
Best regards,
Panagiotis
Thanks Panagiotis, I'll try it and see how it works. I know more about trading than programming, and ChatGPT helps a lot, but sometimes it overcomplicates things.
Happy end of year.
Kind regards, Santiago
@The_Dark_Knight
The_Dark_Knight
31 Dec 2023, 17:00
RE: RE: RE: Multiple cbots and only one position at a time
firemyst said:
Yes, perfect! that's exactly the idea. Thanks Firemyst
@The_Dark_Knight