Position average price triggers
Position average price triggers
24 Aug 2023, 23:55
Hi guys, I am trying to develop a cbot that will perform 2 actions based on average price but is not working.
Here is the code I tried so far:
- Double the open position volume if average price goes below -10 pips
protected override void OnBar()
{
if ( fastMa.Result.LastValue > mediumMA.Result.LastValue)
{
ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumeInUnits, "Label", StopLoss, TakeProfit);
foreach (var position in Positions)
{
var averagePrice = Positions.Average(x => x.Pips);
if (averagePrice < -10)
{
ModifyVolume(double 2.0);
Print("Positions Average Price{0}", averagePrice);
}
}
}
}
2. Take profit and close all open positions if current open positions reach an average price higher than 11 pips
protected override void OnBar()
{
if ( fastMa.Result.LastValue > mediumMA.Result.LastValue)
{
ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumeInUnits, "Label", StopLoss, TakeProfit);
foreach (var position in Positions)
{
var averagePrice = Positions.Average(x => x.Pips);
if (averagePrice > 11)
{
ClosePositionAsync(position);
}
}
}
}
Thank you!
Replies
MongolTrader
31 May 2024, 04:20
RE: Position average price triggers
firemyst said:
I think your logic in both loops is wrong because you're changing the average price every time you do the loop.
That is, look at #1. When you have 10 positions open and the averagePrice (in pips) is < -10, you modify one position.
Then you do another loop and calculate the average again. The average will change because you've already modified one position.
Same with #2. The average will change each time when you close a position.
What you should do is calculate the average once with all the current positions, and then do you loop. This way, the average won't change on each loop iteration.
For example:
double averagePrice = Positions.Average(x => x.Pips);if (averagePrice > 11){ foreach (var position in Positions) { ClosePositionAsync(position); }}
Please tell me how to add filter when label starts with some prefix when i try use startswith function it gives error as like as below.
AveragePips = Positions.Where(x => x.Label.StartsWith(Name)).Average(y => y.Pips);
Error message is → | Crashed in OnBarClosed with NullReferenceException: Object reference not set to an instance of an object.
@MongolTrader
firemyst
31 May 2024, 05:58
( Updated at: 31 May 2024, 08:09 )
RE: RE: Position average price triggers
MongolTrader said:
Please tell me how to add filter when label starts with some prefix when i try use startswith function it gives error as like as below.
AveragePips = Positions.Where(x => x.Label.StartsWith(Name)).Average(y => y.Pips);
Error message is → | Crashed in OnBarClosed with NullReferenceException: Object reference not set to an instance of an object.
When you place an order, you can create a “label” for the order. Example:
https://clickalgo.com/ctrader-market-orders
Your code will crash because if there's no label, “Label” will be null. Also, if you have no position, you're telling the code to look through the Positions collection, which will be null. So you need to check to make sure you actually have at least one open position first.
@firemyst
firemyst
27 Aug 2023, 15:22 ( Updated at: 27 Aug 2023, 15:23 )
I think your logic in both loops is wrong because you're changing the average price every time you do the loop.
That is, look at #1. When you have 10 positions open and the averagePrice (in pips) is < -10, you modify one position.
Then you do another loop and calculate the average again. The average will change because you've already modified one position.
Same with #2. The average will change each time when you close a position.
What you should do is calculate the average once with all the current positions, and then do you loop. This way, the average won't change on each loop iteration.
For example:
@firemyst