Cannot figure out how to update account balance properly

Created at 31 Dec 2023, 11:51
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!
OC

OceanHerring

Joined 31.12.2023

Cannot figure out how to update account balance properly
31 Dec 2023, 11:51


Dear community,

I have created a very simple sample of my issue. I'm daily opening a trade for both sell and buy, at a fixed time (13h server time), and only allow one trade for each of these positions at this very given moment. When I use 1k for trade volume, it works perfectly. When I try to use my account balance for the buy limit order it only executes the buy limit order once, on the first day. Account balance is set at 10k. Tests ran with replacing ‘accountVal’ with 10k work. So this hints at the issue that my account balance is not updated properly. Or that I'm doing something wrong with the conversion to integer.

Anybody any idea how I can solve my issue? Happy new year to y'all.

All the best, OceanHerring.

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(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class EURUSD_bot_basic : Robot
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }
        int hourTrack = 0;

        protected override void OnStart()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate

            Print(Message);
        }

        protected override void OnTick()
        {     
            int hour = Server.Time.Hour;
            
            if(hour == 13 & hourTrack == 0){
                int accountVal = Convert.ToInt32(Account.Balance);
                double targetPriceBuy = Symbol.Bid - 5*Symbol.PipSize;
                double targetPriceSell = Symbol.Bid + 5*Symbol.PipSize;
                
                PlaceLimitOrder(TradeType.Buy, Symbol.Name, accountVal, targetPriceBuy, "Buy_Limit_Order", 11, 11, Server.Time.AddHours(3));
                PlaceLimitOrder(TradeType.Sell, Symbol.Name, 1000, targetPriceSell, "Sell_Limit_Order", 11, 11, Server.Time.AddHours(3));

                hourTrack++;
            }
            
            if(hour == 12){
                hourTrack = 0;
            }                    
        }

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


@OceanHerring
Replies

PanagiotisCharalampous
31 Dec 2023, 16:24

Hi there,

You should always normalize your volume, before placing orders, using NormalizeVolumeInUnits method. Else the volume will not be a valid tradable volume.

Best regards,

Panagiotis


@PanagiotisCharalampous

OceanHerring
01 Jan 2024, 09:06 ( Updated at: 02 Jan 2024, 06:23 )

RE: Cannot figure out how to update account balance properly

PanagiotisCharalampous said: 

Hi there,

You should always normalize your volume, before placing orders, using NormalizeVolumeInUnits method. Else the volume will not be a valid tradable volume.

Best regards,

Panagiotis

Hi Panagiotis,

Thanks so much! It works like a charm :).

All the best, OceanHerring


@OceanHerring