Ozzy,
I assume that yoru dynamic variables are all arrays.
I assume that you are re-running this every bar.
I also assume that you can check for an existing buy in your exit logic
rather than depending on the presence or absence of a value in this file
(probably a good idea since you don't want to blindly sell without
verifying that you *actually* have a position, regardless of what the
file on disk says!).
Given the above, you can just leave a running value in the file of the
last target, regardless of when the last buy was, and regardless as to
whether or not the position has already been closed.
That being the case, try the following and see if it does the job.
Mike
Buy = Targets = 0;
for ( i = 1; i <= 500; ++i )
{
TestCondition = VarGet( "Condition" + i );
Target = VarGet( "Profit" + i );
Buy |= ( TestCondition > 0 );
Targets += IIF( TestCondition, Target, 0 );
}
LastTarget = LastValue( ValueWhen( Buy, Targets ) );
if ( LastTarget > 0 )
{
// Dump the profit target of the most recent buy.
fh = fopen( "F:\\ProfitDump.txt", "w" );
if ( fh )
{
TargetStr = NumToStr( LastTarget );
fputs( TargetStr, fh );
}
fclose( fh );
}
--- In [email protected], "ozzyapeman" <zoopf...@...> wrote:
>
> Hello, hoping someone may be able to help out with this. I have a
> trading system that cycles through a number of unique conditions, to
> look for a Buy. Each condition also has a specific associated profit
> target. I am trying to dump the true profit target to an external
file,
> any time there is a Buy. Right now I am using LastValue( ) to do that,
> and maybe that is where the problem lies - because it does not work.
The
> file is always empty.
>
> Below is a much simplified version. The part in red is where the
problem
> is. Right now I am using this in backtesting. And save for the Fput
> subroutine, the system otherwise works fine. But in order to convert
> this to a live auto trading system, I need to be able to isolate and
> dump the correct Profit Target to file, so that I can pull it during
the
> subsequent exit subroutines.
>
> Any input much appreciated:
>
> // Enter a Long Position if any one of 500 Conditions are true.
> // Conditions are generated from another algorithm. Only one Condition
> can
> // be true on any bar. Each Condition has an associated Profit Target.
>
> // Whenever we enter a Position, dump the ProfitTarget to file.
>
>
> for(i=1; i < 500 + 1; ++i)
> {
>
> TestCondition = VarGet( "Condition" + i );
> Profit = VarGet( "Profit" + i );
>
> Buy = Buy || TestCondition;
>
> ProfitTarget = IIf(TestCondition , Profit, ProfitTarget);
>
> // if we bought, dump the profit target that we used, to file:
>
> if(LastValue(Buy) > 0)
> {
> ProfitNum = LastValue(ProfitTarget);
>
> ProfitStr = NumToStr(ProfitNum);
>
> fh = fopen( "F:\\ProfitDump.txt", "w");
>
> if( fh )
> {
> fputs( ProfitStr, fh );
> }
> fclose( fh );
> }
> }
>