I don't use cross or exrem. I use relationships instead. When I use cross
or exrem and the system turns back in the original direction AFL cannot
recognize that and I stay long when the system turns back down in the same
bar.
Now as for look ahead. No I am not sure, really sure, or really
really sure that my system is not looking ahead. Ed, I don't understand why
you say it is looking ahead. I do have my back test system set to use the
price of the cross, or the price of the faster signal where the lines cross.
But I am not sure that looks ahead. My tests trading TF with market orders
there is very little slippage between the lastValue of close when I send the
order and the fill price. Same with STP orders and a tick or two above or
below the stop. When the market changes trends enough to fire the order then
it is usually going in that direction long enough to get the order price.
What is killing my system more than anything is the multiple signals
when the trend changes. I can send 20 orders a minute under those conditions.
The worst case scenario would be for me to set the system to trade on the open
or close of the bar or use the high or low as the trigger price rather than
the close, since in real time the close is not the close until the bar
officially closes. During the bar it is the value of the last tick to arrive.
And he high and low can change many times in a bar too.
I have
concentrated so much on reducing the extra signals that I had not thought of
changing the buy/sell strategy. I was assuming it could get a good part of the
back test gains. Not even close.
I like the worse case idea. Thanks, I
had not thought of doing that. I was looking for the golden goose. It does lay
an egg too. I will change my back test to use those methods to see what
happens. I had used the cross price +/- a tick or two to make sure it really
crossed. That didn't help much in the real world.
I really don't want
to use 1 minute charts in trading. 15 minutes is the shortest because I don't
want more than 10 trades a day and had rather have less than 5.
Why oh
why did you mention 1987! I am really sorry you brought it up. I bought
$13,000 in calls after the first little dip. That was one of the times I KNEW
where the market was going. Do you know how hard it is to get out of calls
when you KNOW the market is going up and it heads into the toilet. I didn't
believe it! I was listening to Prechter and the Elliot Wave and was up the 13K
in six months from a $1000. I thought I really knew what was going on. NOT!
Lost it all. That an another blunder in 2002 got me into Ami and TA and now
auto trading. Emotions are poison to a trader. Hopefully I won't be so stupid
again.
Thanks for the advice. I will give it a go,
Barry
---
In amibroker@xxxxxxxxxps.com,
"Edward Pottasch" <empottasch@...> wrote:
>
> I have
also put a lot of time in systems that make 20000% per year or more even
though I know they look into the future. If you test a system in for instance
the 1-minute timeframe and use a cross as and entry/exit trigger and use the
cross price as the entry price you are looking into the future. Only if the
price falls back to the cross price the entry or exit will happen, or at the
moment the cross occurs the price is history and in many cases the price moves
right through a cross without even touching it. You can expand the backtest to
give you the worst case scenario. If the worse case scenario still works than
it is worth a shot in my opinion. I liked this approach. First make a system
that works in theory even though it looks into the future and then follow
strict rules when you do not get in or out of a trade at a cross.
>
> In the practice it is tough. I have good days but when the system has
a bad day and on top of that you are even doing worse because trades are not
entered. These systems also trade like 20 times a day. Worst case scenario
returns are not very impressive although still over 100% per year but with 20
trades per day not a lot has to happen or change to make this disappear in
thin air as well. Should have watched Wallstreet (1987) more closely at the
time. Gorden Gekko had it all figured out. Insider information and fraud is
the way to riches on Wallstreet.
>
> rgds, Ed
>
>
>
>
>
> ----- Original Message -----
>
From: Keith McCombs
> To: amibroker@xxxxxxxxxps.com
> Sent: Sunday, March 22, 2009 7:43 AM
> Subject: Re: [amibroker]
Re: Help with barssince
>
>
> Barry --
> Are you
sure, really sure, really really sure that your system, as coded, is not
looking ahead? That's often been my problem. And sometimes it can be a real
b..h to find it.
> -- Keith
>
> Barry Scarborough wrote:
> Currently I design systems with basic AFL, modify it to allow it to
drive auto trading and then design it again for analysis using BarReplay. I am
trying to design one system that can be used in all three environments.
>
> In the auto trading program I use states to keep track of
what is going on and I use numPositions to verify the states since the trading
program can cancel an order if it is not filled by the time the trend changes.
That blows AFLs mind. I also use states to eliminate multiple signals due to
indicator flopping around and causing multiple, unwanted trades during a bar.
That blows AFLs mind too since ALF never sees this with static data.
>
> Also, my trading program does not perform anywhere near a back test
of the system. A number of my programs back test over 20,000% / year based on
trading about 50 times a day and a 1 minute chart. I read about IO and wanted
to try to use the intelligent optimizer to develop a more robust system. I
assume that since I have such a high back test result that the system is over
optimized, even though I did not use optimize. When I run the formula with
live data it barely makes money. It can gain $4000 one day and down that much
the next. Another reason to use IO.
>
> So I was trying to
figure out how to write my trading program so that it was compatible with auto
analysis, set my program up in AFL so that I could define the states and keep
track of the positions in an attempt to use AA and IO for analysis. The more I
try the more incompatibilities I find in AFL. I guess AFL was designed as a
charting and analysis program using static data, intraday or EOD and loses its
mind when someone tries to analyze a live trading program.
>
> I
am not new at this. I have been coding AFL since 2003 and live trading
programs for over a year. In all that time I have not tried to use AA because
I knew it would not help with live trading programming. But I was intrigued
with the ability of IO.
>
> The code I appended to the last post
works and keeps the states in an array. But for some inexplicable reason AFL
will not back test and the chart will only shows the active state. That
probably has something to do with using static variables to save the state
from scan to scan. I really don't understand why AFL will not allow an array
built from a static variables to be used by AA. It could be that AFL is
building the arrays during every scan and only plots the values from the
current state. That seems to be what is happening. Coming from C++ I don't
understand why I can't put data in array cells and have it stay there and be
used.
>
> So that is where I am coming from and what I am trying
to do.
>
> Thanks for the help,
> Barry
>
>
--- In amibroker@xxxxxxxxxps.com,
"Mike" <sfclimbers@> wrote:
> >
> > Yeah, Flip is
a tricky one to understand. But, the end result is that you have an array
alternating between all 1's and all 0's
> >
> >
e.g.
> > AmLong = Flip(Buy, Sell); might give the following:
>
>
> > 1111111111000111110000000001111111
> >
> > indicating that you were long 10 days, then flat for 3, then
long for 5, then...
> >
> > What is it that you are trying
to do? If this exercise is for anything more than display purposes, then you
may be begging for trouble. What you are asking for is to implement the
AmiBroker backtester in a few lines of AFL. This has been discussed a few
times in the forum. To my knowledge, it's never been done.
> >
> > One thing that did come out of a prior discussion was the
following article by Tomasz. Perhaps you can modify it to iterate through the
trades (either open trades bar by bar, or closed trades after all is
completed), and construct a status composite for each symbol in which you have
taken a position. I haven't tried this. But it may be do-able.
> >
> > http://www.amibroker.com/kb/2008/05/19/historical-portfolio-backtest-metrics/
>
>
> > What that reiterates is that until you have actually run
the backtester, you won't know for sure whether a position was/is/will be
held.
> >
> > Mike
> >
> > --- In amibroker@xxxxxxxxxps.com,
"Barry Scarborough" <razzbarry@> wrote:
> > >
> >
> I will have to digest what you are saying. I don't understand flip
yet.
> > >
> > > While I was waiting for an answer to
my post I tried something else. It keeps track of the states but will not back
test and the shape arrows are not displayed correctly. If I am in long state
the sell and short arrows are shown. If I am short the buy and cover arrows
are shown. I have seen this before but I do not know why Ami does not back
test and show the arrows correctly. Any ideas why?
> > >
>
> > Thanks,
> > > Barry
> > >
> > >
The code:
> > > r1 = Param( "Fast avg", 3, 2, 20, 1 );
>
> > r2 = Param( "Slow avg", 9, 2, 50, 1 );
> > > r3 = Param(
"Signal avg", 2, 2, 20, 1 );
> > >
> > > fMacd =
MACD(r1, r2);
> > > fSig = Signal(r1,r2,r3);
> >
> Up = fMacd > fSig;
> > > Dn = fMacd < fSig;
>
> >
> > > pStoch = Param("Stoch period", 10, 1, 30, 1
);
> > > pKavg = Param("%K avg", 3, 1, 20, 1 );
> > >
Line = Param("Line", 15, 5, 40, 1);
> > > fStoch =
StochK(pStoch, pKavg);
> > > stoUp = fStoch > Ref(fStoch,
-1);
> > > stoDn = fStoch < Ref(fStoch, -1);
> > >
> > > state = Nz(StaticVarGet("State"));
> >
> Buy = (state == 0 OR state == 4) AND fMACD > fSig AND stoUp;
>
> > state = IIf(Buy, 1, state); // set long state
> > > Sell
= state == 1 AND fSig > fMACD;
> > > state = IIf(Sell, 2,
state); // flat after long state
> > > Short = (state == 2 OR
state == 0) AND fSig > fMACD AND stoDn;
> > > state =
IIf(Short, 3, state); // set short state
> > > Cover = state == 3
AND fMACD > fSig;
> > > state = IIf(Cover, 4, state); // flat
after short state
> > > printf("\nState1=" + NumToStr(state,
1) + " State2=" + NumToStr(Ref(state, -1), 1) + " State3=" +
NumToStr(Ref(state, -2), 1));
> > >
StaticVarSet("state", LastValue(state));
> > >
>
> > printf("\nbuy=" + NumToStr(Buy, 1) + " sell=" + NumToStr(Sell,
1) + " short=" + NumToStr(Short, 1) + " Cover=" + NumToStr(Cover, 1));
>
> >
> > > Plot(fMacd, StrFormat(_SECTION_NAME()+
"(%g,%g)", r1, r2), ParamColor("MACD color", colorRed ),
ParamStyle("MACD style") );
> > > Plot(fSig, "Signal" +
_PARAM_VALUES(), ParamColor("Signal color", colorBlue ),
ParamStyle("Signal style") );
> > > Plot( fMacd - fSig, "MACD
Histogram", ParamColor("Histogram color", colorBlack ), styleNoTitle |
ParamStyle("Histogram style", styleHistogram | styleNoLabel,
maskHistogram ) );
> > > Plot(0,"",colorBlack);
> >
> Plot(state, "State", colorGreen, styleOwnScale) | styleStaircase;
>
> >
> > > PlotShapes(Buy * shapeUpArrow, colorGreen, 0,
fMACD, 5 );
> > > PlotShapes(Sell * shapeDownArrow, colorRed, 0,
fMACD, 20 );
> > > PlotShapes(Short *
shapeHollowDownArrow,colorRed, 0, fMACD, -5 );
> > >
PlotShapes(Cover * shapeHollowUpArrow, colorGreen, 0, fMACD, -20 );
>
> >
> > >
> > > --- In amibroker@xxxxxxxxxps.com,
"Mike" <sfclimbers@> wrote:
> > > >
> >
> > A quick follow up to prevent (hopefully) any misinterpretation of
what I was trying to say.
> > > >
> > > > 1.
The ExRem is only there for the plotting. You generally shouldn't use it for
the Buy/Sell/Short/Cover arrays since the backtester does it
better.
> > > >
> > > > 2. I did not attempt to
prevent a Short if already in a Buy or vice versa because you cannot know
which takes precedence without looping bar by bar through the range under
analysis (to see which would have occurred first). These are the decisions
that the backtester makes with full information.
> > > >
> > > > Mike
> > > >
> > > >
--- In amibroker@xxxxxxxxxps.com,
"Mike" <sfclimbers@> wrote:
> > > > >
>
> > > >
> > > > > Barry,
> > > >
>
> > > > > You cannot reliably do what you are
attempting. The only way to truely
> > > > > know whether or
not you are in a position is via iterating through the
> > > >
> open position list within custom backtester code. The backtester
employs
> > > > > numerous rules that affect whether or not
a position is actually taken,
> > > > > despite the signals
given (e.g. availability of funds, max positions,
> > > > >
limits on number of positions per symbol, etc.).
> > > > >
> > > > > In answer to your other questions, BarsSince is
entirely dependent upon
> > > > > the state of the arrays at
the time that you call it. So, of course, it
> > > > >
matters a great deal where you place the call in your code.
> > >
> >
> > > > > To illustrate the problem, here is code
that will do what you want. But,
> > > > > you will notice
that it is wrong since it allows simultaneous positions
> > > >
> of both long and short, does not take into consideration availability
of
> > > > > funds, etc. all of which only the custom
backtester would be able to
> > > > > filter
reliably.
> > > > >
> > > > > MA1 =
MA(Close, 5);
> > > > > MA2 = MA(Close, 25);
> >
> > > MA3 = MA(Close, 7);
> > > > > MA4 = MA(Close,
35);
> > > > >
> > > > > Buy = Cross(MA1,
MA2);
> > > > > Sell = Cross(MA2, MA1);
> > >
> > AmLong = Flip(Buy, Sell);
> > > > >
> >
> > > Short = Cross(MA4, MA3);
> > > > > Cover =
Cross(MA3, MA4);
> > > > > AmShort = Flip(Short,
Cover);
> > > > >
> > > > > State =
IIF(AmLong, 1, IIF(AmShort, 2, 0));
> > > > >
> >
> > > Buy = ExRem(Buy, Sell);
> > > > > Sell =
ExRem(Sell, Buy);
> > > > > Short = ExRem(Short,
Cover);
> > > > > Cover = ExRem(Cover, Short);
> >
> > >
> > > > > Plot(Close, "Close",
colorLightGrey, styleBar);
> > > > > Plot(State, "State",
colorRed, styleStairCase | styleOwnScale);
> > > > >
PlotShapes(Buy * shapeUpArrow, colorGreen);
> > > > >
PlotShapes(Sell * shapeDownArrow, colorRed);
> > > > >
PlotShapes(Short * shapeHollowDownArrow, colorRed);
> > >
> > PlotShapes(Cover * shapeHollowUpArrow, colorGreen);
> >
> > >
> > > > > Mike
> > > > >
> > > > >
> > > > > --- In amibroker@xxxxxxxxxps.com,
"Barry Scarborough" <razzbarry@>
> > > > >
wrote:
> > > > > >
> > > > > > I am
trying to set states based on whether I have a position and the
> >
> > > position type. If AFL has a getPositions function I can't find
it. I
> > > > > tried to create states using the following
code but barssince does not
> > > > > return a valid
value.
> > > > > >
> > > > > > //
buy control
> > > > > > BarsSB = Nz(BarsSince(Buy),
0);
> > > > > > BarsSSl = Nz(BarsSince(Sell),
0);
> > > > > > BarsSSh = Nz(BarsSince(Short),
0);
> > > > > > BarsSC = Nz(BarsSince(Cover),
0);
> > > > > >
> > > > > > state =
0;
> > > > > > // 0 = no position, 1 = buy last, 2 =
short last;
> > > > > > state = IIf(BarsSB == BarsSSh, 0,
IIf(BarsSB < BarsSSh, 1, 2));
> > > > > > // if sell
after buy we are flat
> > > > > > state = IIf(state == 1
AND BarsSSl < BarsSB, 0, state);
> > > > > > // if
cover after short we are flat
> > > > > > state =
IIf(state == 2 AND BarsSC < BarsSSh, 0, state);
> > > > >
>
> > > > > > If I don't use NZ then BarsSince returns
Empty.
> > > > > > When I use NZ then I can see trades
using plot shapes. But all the
> > > > > barsSince returns 0
so state is always 0. What am I doing wrong?
> > > > >
>
> > > > > > The really interesting thing is that I
can put the barsince lines
> > > > > below the logic where I
set buy, sell, short and cover, then I get
> > > > > correct
values returned. Why does the position matter?
> > > > >
>
> > > > > > I can use static vars to keep the state
but when I do that
> > > > > AutoAnalysis will not work and
the shapes don't show properly. So I am
> > > > > tryng to
find a way that allows auto analysis to work. I tried to use
> > >
> > switch and found it only works with numbers and not arrays.
>
> > > > >
> > > > > > Thanks,
> >
> > > > Barry
> > > > > >
> > >
> >
> > > >
> > >
>
>
>