Recalculate volume on multiple instruments

Created at 04 Apr 2022, 14:37
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!
WA

waym77

Joined 22.07.2021

Recalculate volume on multiple instruments
04 Apr 2022, 14:37


I have a bot that places pending orders using _hlbars_BarOpened(BarOpenedEventArgs obj). Using this, on every bar open the volume to be used in the next pending order is recalculated based on the new account balance.

This method works well. However, I began forward testing the bot on multiple instruments and realized that I need volume to be updated every time the account balance is updated.

For example, if I run two instances (one on EURUSD, one on GBPUSD), starting with $1,000 in the account, and win $50 on EURUSD, the existing Pending Order on GBPUSD will need to be updated immediately to reflect volume based on $1,050.

Any advice would be appreciated, thanks.


@waym77
Replies

amusleh
05 Apr 2022, 09:52

Hi,

The account balance is updated when you close a position, or you deposit/withdraw funds, for later two there is no way to know that but for position close you can use the Positions Closed event.

So what you need to do is first subscribe to Positions.Closed event and then move your calculation logic to Positions closed event handler.


@amusleh

waym77
07 Apr 2022, 07:58

RE:

amusleh said:

Hi,

The account balance is updated when you close a position, or you deposit/withdraw funds, for later two there is no way to know that but for position close you can use the Positions Closed event.

So what you need to do is first subscribe to Positions.Closed event and then move your calculation logic to Positions closed event handler.

Hi,

Thanks for your response. I am, however, unfamiliar with Positions.Closed events. Do you maybe have a code sample so I can see where and how to place the calculation logic?


@waym77

amusleh
07 Apr 2022, 08:00

Hi,

Check the API references for a sample: Event Closed | API reference | cTrader Community


@amusleh

waym77
07 Apr 2022, 16:10

RE:

amusleh said:

Hi,

Check the API references for a sample: Event Closed | API reference | cTrader Community

Thanks for the inclusion of the API reference.

The updated code looks to be running correctly, thanks.


@waym77