• cBot Coding
  • Close Positions Works but I then want to open new

Have some code that will close all positions once equity reaches a certain level above account balance which works. However I have additional code to open more trades but nothing happens after the original open trades have been closed. Not sure what I am missing here so any help appreciated.

if (Account.Equity > (Account.Balance + (Account.Balance * (Profiteer / 100))))

            foreach (var openedPosition in Positions)
            {
                ClosePosition(openedPosition);
            }


        if (_stochastic.PercentK.Last(2) > _stochastic.PercentD.Last(2) && _stochastic.PercentK.Last(1) < _stochastic.PercentD.Last(1))
        {
            Print("_stochastic.PercentD.Last(1) {0}", _stochastic.PercentD.Last(1));
            Print("_stochastic.PercentD.Last(2) {0}", _stochastic.PercentD.Last(2));
            if (_stochastic.PercentD.Last(2) > Up_Level && _stochastic.PercentK.Last(2) > Up_Level)

Can you provide the code where you open the trades, your snippet above does not help.
Also, explain the logic of how new trades open.

protected override void OnBar()
{
var volumne = Math.Floor(Account.Equity * (VolumePercent / 100));
if (Account.Equity > (Account.Balance + (Account.Balance * (Profiteer / 100))))

            foreach (var openedPosition in Positions)
            {
                ClosePosition(openedPosition);
            }


        if (_stochastic.PercentK.Last(2) > _stochastic.PercentD.Last(2) && _stochastic.PercentK.Last(1) < _stochastic.PercentD.Last(1))
        {
            Print("_stochastic.PercentD.Last(1) {0}", _stochastic.PercentD.Last(1));
            Print("_stochastic.PercentD.Last(2) {0}", _stochastic.PercentD.Last(2));
            if (_stochastic.PercentD.Last(2) > Up_Level && _stochastic.PercentK.Last(2) > Up_Level)
            {

                ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "DragonStochastic", StopLoss, TakeProfit);

            }

        }

        if (_stochastic.PercentK.Last(2) < _stochastic.PercentD.Last(2) && _stochastic.PercentK.Last(1) > _stochastic.PercentD.Last(1))
        {
            Print("_stochastic.PercentD.Last(1) {0}", _stochastic.PercentD.Last(1));
            Print("_stochastic.PercentD.Last(2) {0}", _stochastic.PercentD.Last(2));
            if (_stochastic.PercentD.Last(2) < Down_Level && _stochastic.PercentK.Last(2) < Down_Level)
            {

                ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "DragonStochastic", StopLoss, TakeProfit);

            }
        }
    }

The reason no trades open is because of your trading rules.

  1. (Account.Equity > (Account.Balance + (Account.Balance * (Profiteer / 100))))
  2. _stochastic.PercentK.Last(2) > _stochastic.PercentD.Last(2) && _stochastic.PercentK.Last(1) < _stochastic.PercentD.Last(1)

if the above rules are not true at the close of the candle then no buy trade will open.

Take a look at your cBot log information for the printed statements.

Thank you for replying Mike. Without rule 1, buy and sell trading works fine from rule 2. I have a second bot set up this way.

I thought once all open positions are closed when rule 1 kicks in, the Equity and the Balance becomes the same. Once this happens rule 1 should no longer apply and rule 2 continues to open and close trades.

The aim was to clean up losing trades should the opportunity arise when equity was higher than balance. I am new to this but enjoy seeing the effect these rules have,

I will check the log information so thank you for your advice.

Looks like it was an issue regarding abnormal volume and now working.

2 months later
ClickAlgo Limited - Copyright © 2025. All rights reserved.
Privacy Policy | Cookies | Risk Disclosure