Mike,
Thanks, as always, for your input.
One of my dynamic variables is an array. And the other, the profit
target, is a scalar.
I tried implementing your suggestions in my code, but am still having
some difficulty making it work. Below is a much simplified version of my
actual code, with the changes implemented. Hopefully you (or anyone)
might be able to spot my mistake. I'm sure it is something simple that I
am overlooking.
You can ignore the ProfitStop var and ApplyStops functions. Those are
just there to make the backtest functional for now.
The actual system is much more complex, but if I can get this simple one
working, I know I can get the bigger system working.
For now, I want to 'append' the ProfitDump.txt file, so I can ensure
that it is dumping all the profit targets to file. Right now it's not
doing that - only dumping a single value, even though there are hundreds
of Buy/Sells in the backtest.
Once I am sure the dump is working correctly, then I will change
'append' to 'write' to just keep the latest profit target in the file,
for Sell/Cover extraction of the live trading version.
Hopefully this code is not too difficult to follow:
// ----------------------------------------------------------- //
// ADD THE CUSTOM COLUMN, "RANGE #" TO BACKTEST REPORT //
// ----------------------------------------------------------- //
SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.Backtest( 1 );
for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade())
{
trade.AddCustomMetric( "Range#", trade.Score );
}
bo.ListTrades();
}
// ----------------------------------------------------------- //
// BEGIN TRADING SYSTEM FORMULA //
// ----------------------------------------------------------- //
TickSize = 0.0001;
Buy = Sell = Short = Cover =
PositionScore = ProfitTargets = LastProfitTarget = 0;
myMA1 = MA(O, 4);
// Set up Buying Ranges and Profit Targets
Range1 = O >= myMA1 + 0.0001 && O < myMA1 + 0.0002;
Profit1 = 5;
Range2 = O >= myMA1 + 0.0002 && O < myMA1 + 0.0003;
Profit2 = 10;
Range3 = O >= myMA1 + 0.0003 && O < myMA1 + 0.0004;
Profit3 = 15;
Range4 = O >= myMA1 + 0.0004 && O < myMA1 + 0.0005;
Profit4 = 20;
Range5 = O >= myMA1 + 0.0005 && O < myMA1 + 0.0006;
Profit5 = 25;
Range6 = O >= myMA1 + 0.0006;
Profit6 = 30;
ProfitTarget = ProfitStop = Null;
// Enter a Long Position when a Range is True:
for(i=1; i < 6 + 1; ++i)
{
ProfitTarget = VarGet( "Profit" + i);
TestLong = VarGet( "Range" + i );
Buy = Buy || TestLong;
ProfitStop = IIf(TestLong , ProfitTarget * TickSize, ProfitStop);
PositionScore = IIf( TestLong, i, PositionScore );
}
LastProfitTarget = LastValue( ValueWhen( Buy, ProfitTarget ) );
if ( LastProfitTarget > 0 )
{
// Dump ProfitTargets to file.
fh = fopen( "F:\\ProfitDump.txt", "a" );
if ( fh )
{
ProfitTargetStr = NumToStr( LastProfitTarget );
fputs( ProfitTargetStr, fh );
}
fclose( fh );
}
ApplyStop( stopTypeProfit, stopModePoint, ProfitStop, 1, 0, 0 );
ApplyStop( stopTypeLoss, stopModePoint, Stop, 1, 0, 0 );
--- In [email protected], "Mike" <sfclimb...@...> wrote:
>
>
> 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" zoopfree@ 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 );
> > }
> > }
> >
>