Close Position with a Label

Created at 08 Dec 2024, 20:27
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
TH

thecaffeinatedtrader

Joined 22.07.2020

Close Position with a Label
08 Dec 2024, 20:27


Hi, I have a bot that will be using a moving average as the exit criteria. Below is the exit lines that will be running during the back testing, but I want to be able to determine which moving average provides the best result… is there a way to use a label so in the events tab I can see which MA was the one that actually closed the position instead of just seeing ‘position closed’?? I know I could test all individually but wondering if this is possible.

        private void Exit1(int index1)
        {              
                    {
                    var positions = Positions.FindAll(InstanceName, SymbolName);
              
                    foreach (var position in positions)

                    if ((position.TradeType == TradeType.Buy && Bars.ClosePrices[index1] < _ExitMA1.Result.LastValue) || (position.TradeType == TradeType.Sell && Bars.ClosePrices[index1] > _ExitMA1.Result.LastValue))
                    ClosePosition(position);
                    }
                    
        }
        private void Exit2(int index2)
        {              
                    {
                    var positions = Positions.FindAll(InstanceName, SymbolName);
              
                    foreach (var position in positions)

                    if ((position.TradeType == TradeType.Buy && Bars.ClosePrices[index2] < _ExitMA2.Result.LastValue) || (position.TradeType == TradeType.Sell && Bars.ClosePrices[index2] > _ExitMA2.Result.LastValue))
                    ClosePosition(position);
                    }
                    
        }
        private void Exit3(int index3)
        {              
                    {
                    var positions = Positions.FindAll(InstanceName, SymbolName);
              
                    foreach (var position in positions)

                    if ((position.TradeType == TradeType.Buy && Bars.ClosePrices[index3] < _ExitMA3.Result.LastValue) || (position.TradeType == TradeType.Sell && Bars.ClosePrices[index3] > _ExitMA3.Result.LastValue))
                    ClosePosition(position);
                    }
                    
        }
        private void Exit4(int index4)
        {              
                    {
                    var positions = Positions.FindAll(InstanceName, SymbolName);
              
                    foreach (var position in positions)

                    if ((position.TradeType == TradeType.Buy && Bars.ClosePrices[index4] < _ExitMA4.Result.LastValue) || (position.TradeType == TradeType.Sell && Bars.ClosePrices[index4] > _ExitMA4.Result.LastValue))
                    ClosePosition(position);
                    }
                    
        }
        private void Exit5(int index5)
        {              
                    {
                    var positions = Positions.FindAll(InstanceName, SymbolName);
              
                    foreach (var position in positions)

                    if ((position.TradeType == TradeType.Buy && Bars.ClosePrices[index5] < _ExitMA5.Result.LastValue) || (position.TradeType == TradeType.Sell && Bars.ClosePrices[index5] > _ExitMA5.Result.LastValue))
                    ClosePosition(position);
                    }
                    
        }
        private void Exit6(int index6)
        {              
                    {
                    var positions = Positions.FindAll(InstanceName, SymbolName);
              
                    foreach (var position in positions)

                    if ((position.TradeType == TradeType.Buy && Bars.ClosePrices[index6] < _ExitMA6.Result.LastValue) || (position.TradeType == TradeType.Sell && Bars.ClosePrices[index6] > _ExitMA6.Result.LastValue))
                    ClosePosition(position);
                    }
                    
        }
        private void Exit7(int index7)
        {              
                    {
                    var positions = Positions.FindAll(InstanceName, SymbolName);
              
                    foreach (var position in positions)

                    if ((position.TradeType == TradeType.Buy && Bars.ClosePrices[index7] < _ExitMA7.Result.LastValue) || (position.TradeType == TradeType.Sell && Bars.ClosePrices[index7] > _ExitMA7.Result.LastValue))
                    ClosePosition(position);
                    }
                    
        }
        private void Exit8(int index8)
        {              
                    {
                    var positions = Positions.FindAll(InstanceName, SymbolName);
              
                    foreach (var position in positions)

                    if ((position.TradeType == TradeType.Buy && Bars.ClosePrices[index8] < _ExitMA8.Result.LastValue) || (position.TradeType == TradeType.Sell && Bars.ClosePrices[index8] > _ExitMA8.Result.LastValue))
                    ClosePosition(position);
                    }
                    
        }

@thecaffeinatedtrader
Replies

firemyst
23 Dec 2024, 01:21

As a work around, you could always write your own log output to see if that meets your needs?

 

if ((position.TradeType == TradeType.Buy && Bars.ClosePrices[index1] < _ExitMA1.Result.LastValue) || (position.TradeType == TradeType.Sell && Bars.ClosePrices[index1] > _ExitMA1.Result.LastValue))
{
    ClosePosition(position);
    Print("Exit EMA 1 closed position. ClosePrice {0}, EMA Value {1}", Bars.ClosePrices[index1], _ExitMA1.Result.LastValue);
}


@firemyst