Page 1 of 1

Portfolio Trader: Trading on close / accessing partial bars

Posted: May 03 2016
by dnickless
Hi there

I have this strategy which is supposed to trade every day just before the exchange (Nasdaq) closes. I want it to trade all Nasdaq components which is why I'm using your wonderful Portfolio Trader.
In backtest scenarios this works well. Here, I can simply rely on the CalcBar() event and pretent to trade literally "on close".

In real trading, however, after numerous attempts to make that thing behave the way I want, including various combinations of
- viewtopic.php?f=1&t=8414
- https://www.tradingcode.net/multicharts ... ior-close/
- attributes like AllowSendOrdersAlways and IOGMode
- changing the IB plug in settings to allow trading outside RTH
- tweaking the exchange opening hours in QuoteManager e.g. from 16:00 to 15:57
- using a second data series (1 min)
I find myself stuck with no real success.

I may well have missed some combinations of the above options. But the closest I could get to make this somehow work was - if I remember correctly - using ExecControl.RecalcLastBarAfter() and wait for a specific time to execute my strategy. However, I obviously need access to the current partial (!) daily bar to make my trading decision and wasn't able to find out how to access that information.

Any help on the topic is greatly appreciated.

Re: Portfolio Trader: Trading on close / accessing partial b  [SOLVED]

Posted: May 04 2016
by Henry MultiСharts
Hello dnickless,

Basics of portfolio operation:
Portfolio trader does not support IOG and BarMagnifier for trading signals.
If you want the order to be sent at the close of a daily bar you should use “This bar on close” command.
If “Next bar” command is generated on daily bar close – the order will be sent at the open of the next daily bar.

In order to implement a trading strategy for 1 day bars you need to follow these guidelines:
  • Tweak the exchange closing hours in the symbol sessions to 1-5 minutes earlier regular closing time to make sure the orders are received by the broker before session close.
  • Portfolio Money Management Signal is calculated on every tick of each data series, therefore it requires no RecalcLastBarAfter.
  • Individual trading signals require RecalcLastBarAfter to trigger calculations inside 1 day bar. Timeout from 1 to 30 seconds should satisfy most of the cases. We do not recommend triggering Recalc more often or rarely.
  • The base trading strategy should calculate the required functions and variables (i.e. criteria) that will be evaluated by the PMM signal for allowing/denying order creation.
  • PММ signal should request these criteria, evaluate them and send back the trading instructions whether the symbol is allowed to generate orders or not (ex. certain variable value, like a flag).
  • When session closes the trading signals are calculated and act based on the trading instructions received from the PMM code (send orders or not).

Re: Portfolio Trader: Trading on close / accessing partial b

Posted: May 09 2016
by dnickless
Thank you for the pointers. We're having issues with our data feed at the moment. Once they are resolved, I shall try out your solution - it's implemented already - and give you proper feedback.

Re: Portfolio Trader: Trading on close / accessing partial b

Posted: May 20 2016
by dnickless
Ha! You just received your 2500th "thank you" from me. ;) Well deserved. The strategy actually did send a number of orders today.

However, some things I haven't fully understood just yet:

1) I am not using RecalcLastBarAfter anywhere. But I noticed in my logs that when the calculations fired (15:55), all symbols would get calculated *several times* over a period of roughly 22 seconds - how can this be explained? My money management signal also fired a number of times. I was under the impression that all events would fire only once (I'm using one single series of daily bars only)? I know about the Environment.CalcReason property and will see what it returns on Monday...

2) In the CalcBar() method I am using something like

Code: Select all

if(StrategyInfo.MarketPosition == 0)
{
// there is no open position so check if we want to buy
}
else
{
// there is an open position so check if we want to sell
}
After reading your documentation on https://www.multicharts.com/trading-sof ... utoTrading I wonder if that is enough/correct. Or would I need to replace the if-condition with the below code to achieve what I want here?

Code: Select all

Environment.IsAutoTradingMode ? StrategyInfo.MarketPositionAtBrokerForTheStrategy : StrategyInfo.MarketPosition
3) Is there a way to make the "Output window" in the PowerLanguage .NET Editor accept more information before it starts dropping older entries? Or do I need to write my logs to a file to make sure I get everything?

Thanks again!

Re: Portfolio Trader: Trading on close / accessing partial b

Posted: May 23 2016
by Henry MultiСharts
Hello dnickless,

1) Please send me the following information for analysis to support@multicharts.com:
- workspace you are using;
- in QuoteManager select the symbols you are using, make a right click on the selection->Export data->Export instrument (with data). Send me the QMD export file for analysis;
- in PowerLanguage .NET editor->File->Export->export the studies you are using in the workspace you are providing. Send me the study export file;
- specify the version and build number of MultiCharts you are running (in MultiCharts go to Help tab-> About);
- your output log that demonstrates the issue;
- instructions for replicating it.

2) What exactly do you want to achieve with this exact code?

3) PLEditor window Output tab is removing older entries. If you want to have all logs - you need to write them into a file.

Re: Portfolio Trader: Trading on close / accessing partial b

Posted: May 23 2016
by dnickless
1) Will be on the way in a few minutes. Thank you in advance.

3) Understood. No problem.

2) My understanding is that the CalcBar method gets called once upon the bar close event. In this very moment I want to check if I have an open position and then:
- open position: possibly close it or keep it open.
- no open position: possibly open a new position.

That's all. Pretty trivial on this level. ;) What got me wondering is this: On the website I mentioned (https://www.multicharts.com/trading-sof ... utoTrading) you guys write:
If the strategy is automated: Environment.IsAutoTradingMode=true.

In automated trading mode a signal can access three market positions via StrategyInfo:

1. Current position on the chart in the BackTesting Engine:
- StrategyInfo.MarketPosition;
- StrategyInfo.AvgEntryPrice;
2. Current position in AutoTrading system, for the current strategy:
- StrategyInfo.MarketPositionAtBrokerForTheStrategy;
- StrategyInfo.AvgEntryPriceAtBrokerForTheStrategy;
3. Current position for the traded instrument at the broker:
- StrategyInfo.MarketPositionAtBroker;
- StrategyInfo.AvgEntryPriceAtBroker;
That's why I was wondering if I had to differentiate between AutoTradingMode true and false in my code and access different properties to check if I have an open position...

Re: Portfolio Trader: Trading on close / accessing partial b

Posted: Jun 01 2016
by dnickless
1) Just to help others: The symptoms could be resolved by simply removing all signals from the portfolio and adding them back again.

2) Could you please shed some light on that topic, too?

Re: Portfolio Trader: Trading on close / accessing partial b

Posted: Jun 03 2016
by Henry MultiСharts
Hello dnickless,

If you want to synchronize your code position with the broker position then that is recommended to differentiate between AutoTradingMode True and False in your code like it is done in the prebuilt signal "From_Broker_To_Strategy_MP_Synchronizer". More details on the subject are available in the wiki page you were referring to.

Additional information in the following topics:
viewtopic.php?f=1&t=10817
viewtopic.php?f=16&t=8499#p56745