Number of open "Positions"
Number of open "Positions"
07 May 2019, 10:43
Hello Panagiotis
My cBot is running on 28 assets. However, I limit the number of assets may simultaneously be opened with the code below. Some times, in high volatility situations, it opens 1-2 trades more than the current limit. Is this because the "Positions" array it is not updated fast enough? What can I do to make sure, it will no open more than the max number?
Thank you
Patrick
//Check if max number of assets open reached Dictionary<string, int> assets = new Dictionary<string, int>(); foreach (Position pos in Positions) { if (!assets.ContainsKey(pos.SymbolCode)) { assets.Add(pos.SymbolCode, 1); } else assets[pos.SymbolCode] += 1; } if (assets.Count >= Max_Assets_Open) { Print("Max number of assets open reached. " + Symbol.Code + " couldn't not open Position"); return; }
Replies
sifneos4fx
07 May 2019, 15:45
RE:
This is exactly what happens some times in high volatitliy situations, it is selten but I want to fix it. So, any suggestions where to start?
Thank you
Patrick
Panagiotis Charalampous said:
Hi Patrick,
Positions property is updated after the order is successfully executed and the relevant message sent back to the application. If you execute trading in between, there is a change to send orders before Positions is updated. Therefore, if you need to be strict with the number of positions, you would need to implement more complicated checks to avoid this situation.
Best Regards,
Panagiotis
@sifneos4fx
PanagiotisCharalampous
07 May 2019, 17:42
Hi Patrick,
There is no single line solution for this. In general, you should keep track of the orders sent and the responses received. If e.g. you have a max number of 50, 48 positions open and 2 orders in progress, for which a response has not beed received, them probably you will have 50 positions soon. In this case, you should not open any further positions. You could have a global counter for the orders in progress (static variable?) which will be increased just before sending the order and decreased after a TradeResult is received.
Let me know if the above helps
Best Regards,
Panagiotis
@PanagiotisCharalampous
sifneos4fx
07 May 2019, 17:59
RE:
The hint to track responses is the key in my solution!
Thank you!
Patrick
Panagiotis Charalampous said:
Hi Patrick,
There is no single line solution for this. In general, you should keep track of the orders sent and the responses received. If e.g. you have a max number of 50, 48 positions open and 2 orders in progress, for which a response has not beed received, them probably you will have 50 positions soon. In this case, you should not open any further positions. You could have a global counter for the orders in progress (static variable?) which will be increased just before sending the order and decreased after a TradeResult is received.
Let me know if the above helps
Best Regards,
Panagiotis
@sifneos4fx
PanagiotisCharalampous
07 May 2019, 15:32
Hi Patrick,
Positions property is updated after the order is successfully executed and the relevant message sent back to the application. If you execute trading in between, there is a change to send orders before Positions is updated. Therefore, if you need to be strict with the number of positions, you would need to implement more complicated checks to avoid this situation.
Best Regards,
Panagiotis
@PanagiotisCharalampous