[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Trying to isolate Buy occurences in order to dump data to file



PureBytes Links

Trading Reference Links

Mike,

That code is very helpful. Thanks. I have played around with it, and archived it.

LastValue is a bit tricky, it seems.

What I really want to do is just check the behavior of the base code, to ensure that it would work in a live setting. It seems that Bar Replay might be the way to go? Wouldn't  LastValue behave correctly in that mode?

When I apply the indicator of the code below and do a Bar Replay, the ProfitDump.txt does get filled in real time. However, the numbers are not as expected, e.g.:  10, 20, 15...    Instead, I'm getting:  0.002, 0.002, 0.002...

Is the code below correct for Bar Replay? If so, why are the numbers in ProfitDump.txt screwed up? And if not, is LastValue still the problem?


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

Stop             =
0.0050;

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, ProfitStop ) );


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 amibroker@xxxxxxxxxxxxxxx, "Mike" <sfclimbers@xxx> wrote:
>
>
> Ozzy,
>
> You cannot backtest your code since LastValue will always return the
> same value (that's why I put the assumption that you would be running
> this once every new bar for the *current* stop target).
>
> If you want to see the historical values, then write a loop iterating
> over the bars in range (see Status function) inside of which you dump
> the ProfitStop array. Or, just create a composite symbol of the running
> ProfitStop as in the code below. I've also added a ribbon and some color
> coding just for kicks :)
>
> Mike
>
> Buy = Sell = PositionScore = ProfitStop = 0;
> MyMA1 = MA( O, 4 );
>
> // Set up Buying Ranges and Profit Targets
>
> RangeIncr = 0.05;
> TargetIncr = 5;
>
> for ( i = 1; i < 6; i++ )
> {
> Range = ( ( O >= MyMA1 + ( i * RangeIncr ) ) && ( O < MyMA1 + ( ( i
> + 1 ) * RangeIncr ) ) );
> VarSet( "Range" + i, Range );
> VarSet( "Profit" + i, TargetIncr * i );
> }
>
> Range = O >= MyMA1 + ( i * RangeIncr );
>
> VarSet( "Range" + i, Range );
> VarSet( "Profit" + i, TargetIncr * i );
>
> // Enter a Long Position when a Range is True:
>
> for ( i = 1; i <= 6; i++ )
> {
> ProfitTarget = VarGet( "Profit" + i );
> TestLong = VarGet( "Range" + i );
>
> Buy = Buy || TestLong;
>
> ProfitStop = IIf( TestLong , ProfitTarget, ProfitStop );
> PositionScore = IIf( TestLong, i, PositionScore );
> }
>
> // Make a pretty graph ;)
>
> RunningStop = ValueWhen( Buy, ProfitStop );
> AddToComposite(RunningStop, "~ProfitStop", "X", atcFlagDefaults |
> atcFlagEnableInBacktest);
>
> Colors = IIF( Range1,
> colorDarkGreen,
> IIF( Range2,
> colorGreen,
> IIF( Range3,
> colorOrange,
> IIF( Range4,
> colorLightOrange,
> IIF( Range5,
> colorDarkYellow,
> IIF( Range6,
> colorYellow,
> colorDarkGrey
> )
> )
> )
> )
> )
> );
>
> Plot( Open, "Open", IIF( Buy, colorDarkGreen, colorDarkGrey ), styleLine
> );
> Plot( MyMA1, "MA1", colorBlue, styleLine );
> Plot( Foreign("~ProfitStop", "X"), "Profit Stop", colorRed,
> styleLeftAxisScale | styleStairCase);
> Plot( 1, "Range", Colors, styleArea | styleOwnScale | styleNoLabel |
> styleNoTitle );
> PlotShapes( shapeUpArrow * Buy, Colors );
>
>
> --- In amibroker@xxxxxxxxxxxxxxx, "ozzyapeman" zoopfree@ wrote:
> >
> > The change you suggest makes sense to me. However, my profitdump text
> file only receives a single value, in a backtest that generates 100
> Buy/Sells. And that value is not even one of the profit targets, which
> is really strange.
> >
> > The file receives "0.002".
> >
> > If you run a quick backtest on this code, do you get correct results
> in the text file?
> >
> > I'm testing Forex, but I suppose it would work on any symbol with
> minor mods.
> >
> >
> > --- In amibroker@xxxxxxxxxxxxxxx, "Mike" sfclimbers@ wrote:
> > >
> > > Change:
> > > LastProfitTarget = LastValue( ValueWhen( Buy, ProfitTarget ) );
> > >
> > > To:
> > > LastProfitTarget = LastValue( ValueWhen( Buy, ProfitStop ) );
> > >
> > > The idea is that the ProfitStop array is now holding the applicable
> ProfitTarget scaler for each bar. Whereas ProfitTarget is left holding
> only the value of Profit6.
> > >
> > > Note also that your loop condition needs to be (...; i <= 6; ...)
> else the 6th condition will not be checked.
> > >
> > > Mike
> > >
> > > --- In amibroker@xxxxxxxxxxxxxxx, "ozzyapeman" <zoopfree@> wrote:
> > > >
> > > > 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 amibroker@xxxxxxxxxxxxxxx, "Mike" <sfclimbers@> 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 amibroker@xxxxxxxxxxxxxxx, "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 );
> > > > > > }
> > > > > > }
> > > > > >
> > > > >
> > > >
> > >
> >
>


__._,_.___


**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/





Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___