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;
} } } }