portfolio trader code help  [SOLVED]

Questions about MultiCharts and user contributed studies.
GiuseppeM
Posts: 20
Joined: 21 Jul 2015
Has thanked: 2 times
Been thanked: 1 time

portfolio trader code help

Postby GiuseppeM » 30 Jul 2015

Hi, i am working with portfolio trader on daily data with SPY as instrument and this code as signal:

Code: Select all

var: time_trigger(2257.00) {put it one minute after the current time to test the code}
if currenttime >= time_trigger and currenttime < sessionendtime(1,1) then
begin
RecalcLastBarAfter(5);
Print( "OK start calculation")
end
else
Print ("do not calculate");
what happens is that if i turn on the strategy automation when the codition is already met it works, and it prints "OK start calculation" every 5 seconds.
But if i turn on the strategy when the time condition is not met, and i wait for the currenttime to be greather than the trigger time it won't print anything even when the time condition is met.

I am doing this to breakdown the problem of using recalclastbarafter to obtain a market entry as near to the close price as possible and i don't want to modify the session duration.
Thank you

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: portfolio trader code help

Postby TJ » 30 Jul 2015

Hi, i am working with portfolio trader on daily data with SPY as instrument and this code as signal:

Code: Select all

var: time_trigger(2257.00) {put it one minute after the current time to test the code}
if currenttime >= time_trigger and currenttime < sessionendtime(1,1) then
begin
RecalcLastBarAfter(5);
Print( "OK start calculation")
end
else
Print ("do not calculate");
what happens is that if i turn on the strategy automation when the codition is already met it works, and it prints "OK start calculation" every 5 seconds.
But if i turn on the strategy when the time condition is not met, and i wait for the currenttime to be greather than the trigger time it won't print anything even when the time condition is met.

I am doing this to breakdown the problem of using recalclastbarafter to obtain a market entry as near to the close price as possible and i don't want to modify the session duration.
Thank you

Tip #1:
You should place the RecalcLastBarAfter at the beginning of the code
instead of inside a condition.

The recalc works on the whole script, not a selected section of codes.

GiuseppeM
Posts: 20
Joined: 21 Jul 2015
Has thanked: 2 times
Been thanked: 1 time

Re: portfolio trader code help

Postby GiuseppeM » 31 Jul 2015

TJ thank you very much for your help.

I have seen a conditional activation on the help page https://www.multicharts.com/trading-sof ... stBarAfter and i thought to do the same with a time condition.

If recalclastbarafter is always active on the instrument it is very difficolut to use it for a popouse like close near the end of day, because every indicator and every calculation is re-timeframed on the parameter of the recalclastbarafter and basically it's no more a daily-based instrument.

So the only way to keep a daily timeframe would be to populate an array where values respect the D <>D[1] condition, but it seems to me that i am not on the right path to solve the problem.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: portfolio trader code help

Postby JoshM » 02 Aug 2015

If recalclastbarafter is always active on the instrument it is very difficolut to use it for a popouse like close near the end of day, because every indicator and every calculation is re-timeframed on the parameter of the recalclastbarafter and basically it's no more a daily-based instrument.
I don't follow. What RecalcLastBarAfter does is essentially the same as when an instruments receives a new tick: it calculates again on the last bar with the recent, new prices.

The instrument itself would still be daily-based; as far as the instrument and Portfolio Trader are concerned there are just more script updates.
So the only way to keep a daily timeframe would be to populate an array where values respect the D <>D[1] condition, but it seems to me that i am not on the right path to solve the problem.
I think you might be overcomplicating things with an array, but to check: is 'the problem' closing a position just before the session closes (first post) or creating a custom time frame in an array to solve something else (your recent post)?

GiuseppeM
Posts: 20
Joined: 21 Jul 2015
Has thanked: 2 times
Been thanked: 1 time

Re: portfolio trader code help

Postby GiuseppeM » 02 Aug 2015

thanks Josh i elaborate better my problem:

I want to use recalclastbarafter to obtain a market entry near the session end time.

In order to do that my first idea was to
  • 1) Check if the currenttime is 5 minute before the session close time.
    2) if this condition is true then start to recalc every 5 seconds, and eventually send the order.
Unfortunately i did't find a way to perferform the point N 1 in a 1 Day timeframe because if i start the signal calculation before 15:55 something like

Code: Select all

var: time_trigger(1555);
condition1 = currenttime >= time_trigger;
never returns a "True" value.

And also TJ says that recalclastbarafer cannot be subsequent to a condition and it has to be applyed to the entire script.
So the logical consequence of that is to follow the example of the help https://www.multicharts.com/trading-sof ... stBarAfter page and start the recalc in the last bar and of course it works, infact it re-calculate the entire script every 5 seconds, and it has no problem to perform the time_trigger condition, unfortunately the Bar prices HLCV are recalculated as well, and also all the indicators of course, so the calculation of the conditions that makes the script to buy or sell are now based on a number of intraday data that should not be considered.

So basically i end up by having a true 1 Day Timeframe until yesterday and a 5 minute timeframe for today.

So the next step that comes into my mind is to start the recalc last bar in the Last Bar of the Chart as in the Help page example, and put a number of seconds corresponding to [( start time - close time) - 5 minutes] as parameter, but it seems a quite huge number of seconds and a no so linear solution.

Right now i'm stuck at this point of having a market entry just before the close time, and i am considering of customizing the sessions make them close 5 minutes before the RTH close time.

GiuseppeM
Posts: 20
Joined: 21 Jul 2015
Has thanked: 2 times
Been thanked: 1 time

Re: portfolio trader code help

Postby GiuseppeM » 02 Aug 2015

Another thing: i have ridden this post :
http://www.multicharts.com/discussion/v ... 380#p61380

I know that intrabarordergeneration is not supported in Portfolio Trader BUT i can happily accept to have many chart loaded if only this code would work but unfortunately i does not for me.

( i post the code here so it'easy to read this post:

Code: Select all

[intrabarordergeneration = true]
input: TimeClosePosition(0);
if (currenttime_s >= TimeClosePosition) then
Begin
RecalcLastBarAfter(1);
sell ("EOD_LX") next bar market;
buytocover("EOD_SX") next bar market;
End;
I have ridden also the reply of Henry http://www.multicharts.com/discussion/v ... 642#p52185 but changing the timeout does not solve the problem.

Another thing is that if i apply the study when the time condition is already met it works fine, and by the way the order are sended at the right time even if intrabarordergeneration is set to false however but of course it's not a valid solution because i cannot wait every day for the close time of the market to launch my signals ..

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: portfolio trader code help

Postby TJ » 02 Aug 2015

::And also TJ says that recalclastbarafer cannot be subsequent to a condition and it has to be applyed to the entire script.
::

Please read carefully my post again.

My message is that the keyword recalclastbarafer is applied to the WHOLE SCRIPT,
I don't want you to have the misunderstanding that just because you have put it in a condition, that would make the recalclastbarafer applied to that specific snippet of code only.

GiuseppeM
Posts: 20
Joined: 21 Jul 2015
Has thanked: 2 times
Been thanked: 1 time

Re: portfolio trader code help

Postby GiuseppeM » 03 Aug 2015

Thank you TJ, actually i have misunderstood that: the command recalc is applyed on the entire code like a whole new bar and not only to a specific "begin .. end" portion of it, but this don't mean that it can't be activated with an if-then condition i've got it.

Now what i really don't understand is why the code that would make everything easy and clean:

Code: Select all

if (currenttime_s >= time_trigger) then
Begin
RecalcLastBarAfter(1);
end;
does not work for me, i have checked that timezones are the same and the PC clock is ok... i don't know

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: portfolio trader code help

Postby TJ » 03 Aug 2015

Thank you TJ, actually i have misunderstood that: the command recalc is applyed on the entire code like a whole new bar and not only to a specific "begin .. end" portion of it, but this don't mean that it can't be activated with an if-then condition i've got it.
::
You are correct that the RecalcLastBarAfter can be triggered by a condition.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: portfolio trader code help  [SOLVED]

Postby JoshM » 03 Aug 2015

So the logical consequence of that is to follow the example of the help https://www.multicharts.com/trading-sof ... stBarAfter page and start the recalc in the last bar and of course it works, infact it re-calculate the entire script every 5 seconds, and it has no problem to perform the time_trigger condition, unfortunately the Bar prices HLCV are recalculated as well, and also all the indicators of course, so the calculation of the conditions that makes the script to buy or sell are now based on a number of intraday data that should not be considered.
I think I get it now. But instead of using a condition to turn RecalcLastBarAfter() on or off, you can also check in your code what the script's calculation reason is.

That circumvents the drawback of this code:

Code: Select all

if (currenttime_s >= time_trigger) then
Begin
RecalcLastBarAfter(1);
end;
The problem with this is that this if statement is only executed (and RecalcLastBarAfter() activated) when there is a tick with a time greater than or equal to 'time_trigger'. When no trade happens at or during that time, then RecalcLastBarAfter() won't be activated. (And then you cannot submit a trade just before the close)

Another approach is to use RecalcLastBarAfter() unconditionally. Then you know for sure that the script is always calculated every x seconds, regardless of whether a trade happened in a certain time period or not.

Filtering on the calculation reason of the script can be done as follows (for example):

Code: Select all

[IntrabarOrderGeneration = true];

Variables:
timeTrigger(1555);

if (GetAppInfo(aiCalcReason) <> CalcReason_Timer) then begin

// This code is not executed when the script
// recalculates due to RecalcLastBarAfter
// Put the values/calculation that you _do not_
// want to update when RecalcLastBarAfter() triggers here

end;

// This code is executed each time the script
// calculates, regardless of the calculation reason
if (CurrentTime >= timeTrigger) then begin

// Market entry near session end time
if (MarketPosition(0) = 0) then begin

// 'Next bar at market' will be executed
// the next time the strategy calculates
// because we use Intra-bar order generation
Buy 1 contracts next bar at market;

end;

end;

RecalcLastBarAfter(5);

GiuseppeM
Posts: 20
Joined: 21 Jul 2015
Has thanked: 2 times
Been thanked: 1 time

Re: portfolio trader code help

Postby GiuseppeM » 03 Aug 2015

I think I get it now...
JoshM thank you very much, your code works and i didn't knew anything about the possibility to separate different calc reasons. Without your help i would be stuck with that problem for a long time.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: portfolio trader code help

Postby JoshM » 04 Aug 2015

JoshM thank you very much, your code works and i didn't knew anything about the possibility to separate different calc reasons. Without your help i would be stuck with that problem for a long time.
I'm glad I could help. If you need more help, just open a new topic.


Return to “MultiCharts”