TradeManager.ProcessEvents  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
stephenfsams
Posts: 1
Joined: 27 Sep 2017
Has thanked: 1 time

TradeManager.ProcessEvents

Postby stephenfsams » 03 Oct 2017

I have a signal I've coded using a sample TradeManager script. It has Added and Changed "event handlers" and does its ProcessEvents in CalcBar. I want to access the balance for my IB account, which it does correctly when IOG is disabled.

However, I want IOG enabled (to trigger an order only once intra-bar...I believe the other class attributes are set correctly). When I do that, ProcessEvents triggers repeatedly (as it did per bar, now per tick). However, instead of keeping the correct account balance with each trigger, it goes to zero (0).

I need help understanding how ProcessEvents works, what causes Adds and Changes, and how to trigger ProcessEvents only once (or maybe there are other ways to maintain the account balance through multiple triggers...?).

Thank you...here is the signal showing what I mean:

Code: Select all

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using PowerLanguage;
using PowerLanguage.Function;
using PowerLanguage.TradeManager;
using ATCenterProxy.interop;

namespace PowerLanguage.Strategy {
   [MouseEvents(false), RecoverDrawings(true)]
   [CalcAtOpenNextBar(false)]
   [IOGMode(IOGMode.Enabled)]
   [ExitFromOneEntryOnce(ExitFromOneEntryOnce.StandardCalculation)]
   [AllowSendOrdersAlways(false)]

   public class __S__TMTest : SignalObject {
      private IAccounts BrokerAccounts;
      private VariableObject<double> m_balance;
      private string AccountName = "U1234567"; //Interactive Brokers

      public __S__TMTest(object ctx) : base(ctx) { }

      protected override void Create() {
         m_balance = new VariableObject<double>(this);
      }

      protected override void StartCalc() {
         BrokerAccounts = TradeManager.TradingData.Accounts;
         BrokerAccounts.Added += BrokerAccounts_Added;
         BrokerAccounts.Changed += BrokerAccounts_Changed;
      }

      void BrokerAccounts_Added(params Account[] _items) {
         foreach (Account acc in _items) {
            String ExtAttributes = "";

            foreach (String key in acc.ExtAttributes.Keys)
               ExtAttributes += string.Format(" {0}: {1}", key, acc.ExtAttributes[key]);

            if (acc.Name == AccountName)
               m_balance.Value = (double)acc.Balance;

            /**/Output.WriteLine("Added " + acc.Name + " " + acc.Balance);
      }   }

      void BrokerAccounts_Changed(params Account[] _items) {
         foreach (Account acc in _items) {
            String ExtAttributes = "";

            foreach (String key in acc.ExtAttributes.Keys)
               ExtAttributes += string.Format(" {0}: {1}", key, acc.ExtAttributes[key]);

            if (acc.Name == AccountName)
               m_balance.Value = (double)acc.Balance;

            /**/Output.WriteLine("Changed " + acc.Name + " " + acc.Balance);
      }   }

      protected override void CalcBar() {
         TradeManager.ProcessEvents();

         /**/Output.WriteLine(AccountName + " " + m_balance[0].ToString());
      }

      protected override void StopCalc() {
         if (BrokerAccounts != null) {
            BrokerAccounts.Added -= BrokerAccounts_Added;
            BrokerAccounts.Changed -= BrokerAccounts_Changed;
}   }   }   }

User avatar
Anna MultiCharts
Posts: 73
Joined: 14 Jul 2017
Has thanked: 1 time
Been thanked: 14 times

Re: TradeManager.ProcessEvents  [SOLVED]

Postby Anna MultiCharts » 04 Oct 2017

Hello, stephenfsams!

The engineers had a look at your code and they say that you’re using the balance variable as VariableObject:
m_balance = new VariableObject<double>(this).

This type of variables are only calculated on the bar Close, in all other cases the calculated value is always zero. That is why without IOG the calculation returns good results and with IOG you receive 0.
Please, refer to this link to find more about this type of variables:
viewtopic.php?f=19&p=70279#p70248
These users thanked the author Anna MultiCharts for the post (total 2):
stephenfsamsHenry MultiСharts


Return to “MultiCharts .NET”