I need help to close the reverse position after the reset

Created at 20 Aug 2024, 08:04
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!
dr.erick.rd's avatar

dr.erick.rd

Joined 09.10.2015

I need help to close the reverse position after the reset
20 Aug 2024, 08:04


Hello, I need help to make my code work when I stop the cbot and turn it back on, close the reverse position that is open (the code works well but does not recognize the position after restarting the cbot)

 

if (CloseOpositePosition)
{
   if (obj.Position.TradeType == TradeType.Buy)
   {
       foreach (Position p in sellPositions)
       {
           if (DontCloseSJPositionsOnCloseOposite && firstNormalStopLossJumpCollection.Contains(p.Id))
               break;

           if (CloseOpositePositionOnWin)
           {
               if (p.NetProfit > 0)
                   ClosePosition(p);
           }
           else if (DontCloseOpositePositionOnWin)
           {
               if (p.NetProfit > 0 && p.Pips < FirstNormalStopLossJumpPositionPips)
                   ClosePosition(p);
           }
           else
               ClosePosition(p);
       }
   }
   else
   {
       foreach (Position p in buyPositions)
       {
           if (DontCloseSJPositionsOnCloseOposite && firstNormalStopLossJumpCollection.Contains(p.Id))
               break;

           if (CloseOpositePositionOnWin)
           {
               if (p.NetProfit > 0)
                   ClosePosition(p);
           }
           else if (DontCloseOpositePositionOnWin)
           {
               if (p.NetProfit > 0 && p.Pips < FirstNormalStopLossJumpPositionPips)
                   ClosePosition(p);
           }
           else
               ClosePosition(p);
       }
   }
}


@dr.erick.rd
Replies

PanagiotisCharalampous
21 Aug 2024, 05:01

Hi there,

Please share the complete cBot code since it is not possible understand everything you are doing just from a snippet.

Best regards,

Panagiotis


@PanagiotisCharalampous

dr.erick.rd
21 Aug 2024, 08:06 ( Updated at: 21 Aug 2024, 13:38 )

RE: I need help to close the reverse position after the reset

PanagiotisCharalampous said: 

Hi there,

Please share the complete cBot code since it is not possible understand everything you are doing just from a snippet.

Best regards,

Panagiotis

Hello. The only thing I really want is for my cTrader account to only allow operations in one direction and when I open an operation, the ones in the opposite direction are closed. I used to have that type of account but now they all allow hedging.


@dr.erick.rd

PanagiotisCharalampous
21 Aug 2024, 14:45

RE: RE: I need help to close the reverse position after the reset

dr.erick.rd said: 

PanagiotisCharalampous said: 

Hi there,

Please share the complete cBot code since it is not possible understand everything you are doing just from a snippet.

Best regards,

Panagiotis

Hello. The only thing I really want is for my cTrader account to only allow operations in one direction and when I open an operation, the ones in the opposite direction are closed. I used to have that type of account but now they all allow hedging.

Hi there,

Here is an example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class CloseOpposite : Robot
    {

        protected override void OnStart()
        {
            Positions.Closed += Positions_Closed;
        }

        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            foreach (var position in Positions.Where(p => p.TradeType != obj.Position.TradeType))
            {
                position.Close();
            }
        }

        protected override void OnTick()
        {
            // Handle price updates here
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

Best regards,

Panagiotis


@PanagiotisCharalampous

dr.erick.rd
22 Aug 2024, 16:45

RE: RE: RE: I need help to close the reverse position after the reset

PanagiotisCharalampous said: 

dr.erick.rd said: 

PanagiotisCharalampous said: 

Hi there,

Please share the complete cBot code since it is not possible understand everything you are doing just from a snippet.

Best regards,

Panagiotis

Hello. The only thing I really want is for my cTrader account to only allow operations in one direction and when I open an operation, the ones in the opposite direction are closed. I used to have that type of account but now they all allow hedging.

Hi there,

Here is an example

using System;using System.Collections.Generic;using System.Linq;using System.Text;using cAlgo.API;using cAlgo.API.Collections;using cAlgo.API.Indicators;using cAlgo.API.Internals;namespace cAlgo.Robots{    [Robot(AccessRights = AccessRights.None)]    public class CloseOpposite : Robot    {        protected override void OnStart()        {            Positions.Closed += Positions_Closed;        }        private void Positions_Closed(PositionClosedEventArgs obj)        {            foreach (var position in Positions.Where(p => p.TradeType != obj.Position.TradeType))            {                position.Close();            }        }        protected override void OnTick()        {            // Handle price updates here        }        protected override void OnStop()        {            // Handle cBot stop here        }    }}

Best regards,

Panagiotis

Thank you very much, you saved me


@dr.erick.rd