I'v got it down well. All your help, time and patience were greatly appreciated!
Charles --- In [email protected], "Mike" <sfclimb...@...> wrote: > > Looks like your revised code should be fine. Are you sure that MAFlag is ever > 1 on the bars that you buy? > > For debugging, it is always a good idea to add charting code to visually > verify your assumptions. > > e.g. Add this to the bottom of your code and verify that a vertical bar is > drawn on the same bar as a green up arrow. > > Plot(Close, "Price", colorDarkGrey, styleBar); > Plot(MAFlag, "MAFlag", colorBlue, styleHistogram | styleOwnScale); > PlotShapes(ExRem(Buy, Sell) * shapeUpArrow, colorGreen); > PlotShapes(ExRem(Sell, Buy) * shapeDownArrow, colorRed); > > Mike > > --- In [email protected], "chuck_win" <chaoy@> wrote: > > > > Hi, > > > > 1. You are right that my code count more than one time. I don't really know > > how to capture a data at buy exactly. I just follow Tomasz's suggestion in > > his reply to another person. > > > > 2. According to help file, staticvarget(...) return a number or string. If > > it is a static array, how to access its element? I change the CTB block as > > following and get 0.0 under MAup column. > > > > if ( Status( "action" ) == actionPortfolio ) > > { > > bo = GetBacktesterObject(); > > //run default back tester > > bo.Backtest(); > > Dates = DateTime(); > > bi = BarIndex(); > > MAup = 0; > > > > //loop on all trades of ONE interation at one time. > > for ( trade = bo.GetFirstTrade( ); trade; trade = > > bo.GetNextTrade( ) ) > > { > > entryBar = LastValue( ValueWhen( trade.EntryDateTime == dates, > > bi )); > > //add up MA flag from static var > > myArray = StaticVarGet("MA_" + trade.symbol); > > MAup = MAup + myArray[entryBar]; > > _TRACE(trade.Symbol + ": " + MAUp); > > } > > > > //add custom column to optimize result > > bo.AddCustomMetric("MAUP", MAup); > > //bo.listTrades(); > > } > > > > 3. I fixed it. > > > > Could you drop couple more lines as you get time? > > > > All your help were really appreciated! > > > > Charles > > > > --- In [email protected], "Mike" <sfclimbers@> wrote: > > > > > > Looks like you have a few potential problems: > > > > > > 1. You are double (triple, quadruple, etc.) counting whenever you have > > > multiple trades for the same symbol. > > > > > > 2. I believe that custom backtest metrics need to be scalers (i.e. single > > > value, not an array). > > > > > > 3. No need to delay the printing of trades (i.e. bo.Backtest(1)) since > > > you are not adding individual trade metrics. > > > > > > Mike > > > > > > --- In [email protected], "chuck_win" <chaoy@> wrote: > > > > > > > > Hi, > > > > > > > > What I am doing is: > > > > 1). count MA(close, 6) < MA(close, 9) at buy when open gap > %x. > > > > 2). show the count on optimize results. > > > > > > > > I use static var to store MA flag outside CTB, and read it inside CTB. > > > > the code below doesn't work. > > > > > > > > I don't know if I create a single static var or a static array when I > > > > write: > > > > StaticVarSet ("MA_" + Name(), MAFlag); > > > > > > > > It could be wrong in other place. > > > > > > > > Any help would be greatly appreciated! > > > > > > > > Charles > > > > > > > > > > > > > > > > > > > > //========================================= optimize > > > > Perc = Optimize("Perc", 8, 8, 8, 2); > > > > MALen = Optimize("MALen", 6, 6, 6, 1); > > > > > > > > //========================================= set option > > > > //SetOption("RequireDeclarations", True ); > > > > //must use it for buyprice = ref(close, -1). > > > > SetOption("PriceBoundChecking", False); > > > > > > > > //================= buy rules > > > > Buyrule1 = TimeNum() == 93000; > > > > Buyrule2 = Close >= Ref(Close, -1) * (1 + Perc/100); > > > > > > > > Buyrule = Buyrule1 AND Buyrule2; > > > > > > > > //========================================= set static var to store MA > > > > //check MA steps > > > > MAFlag = IIf(MA(Close, MAlen) < MA(Close, MAlen + 3), 1, 0); > > > > //store MA flag into static var. > > > > StaticVarSet ("MA_" + Name(), MAFlag); > > > > > > > > //================= sell rules > > > > Sellrule1 = TimeNum() == 93000; > > > > Sellrule = Sellrule1; > > > > > > > > //================= trade > > > > Buy = Buyrule; > > > > BuyPrice = Ref(Close, -1); > > > > Sell = Sellrule; > > > > SellPrice = Close; > > > > > > > > //======================================== custom back tester > > > > SetCustomBacktestProc( "" ); > > > > > > > > if ( Status( "action" ) == actionPortfolio ) > > > > { > > > > bo = GetBacktesterObject(); > > > > //run default back tester > > > > bo.Backtest(1); > > > > MAup = 0; > > > > > > > > //loop on all trades of ONE interation at one time. > > > > for ( trade = bo.GetFirstTrade( ); trade; trade = > > > > bo.GetNextTrade( ) ) > > > > { > > > > //add up MA flag from static var > > > > MAup = MAup + StaticVarGet("MA_" + > > > > trade.symbol); > > > > _TRACE(trade.Symbol + ": " + MAUp); > > > > } > > > > > > > > //add custom column to optimize result > > > > bo.AddCustomMetric("MAUP", MAup); > > > > bo.listTrades(); > > > > } > > > > > > > > --- In [email protected], "chuck_win" <chaoy@> wrote: > > > > > > > > > > Thanks so much Mike for your help. It works on backtester now. I will > > > > > try to make it work on optimize next. > > > > > > > > > > Charles > > > > > > > > > > > > > > > > > > > > --- In [email protected], "Mike" <sfclimbers@> wrote: > > > > > > > > > > > > Charles, > > > > > > > > > > > > You got scared away from the earlier example that I referred you to > > > > > > ( http://finance.groups.yahoo.com/group/amibroker/message/146164 ). > > > > > > Yet, with a one line change, it would give you exactly what you > > > > > > want. > > > > > > > > > > > > Just change: > > > > > > > > > > > > foreignATR = ATR( 14 ); > > > > > > > > > > > > To > > > > > > > > > > > > foreignATR = MA( Close, 6 ); > > > > > > > > > > > > Do this and, I believe, you would have exactly what you're asking > > > > > > for. You would likely then want to change the names and output > > > > > > strings to refer to MA instead of ATR. But, there would be no > > > > > > further change in logic other than that one line. > > > > > > > > > > > > Mike > > > > > > > > > > > > --- In [email protected], "chuck_win" <chaoy@> wrote: > > > > > > > > > > > > > > Hi Mike, > > > > > > > > > > > > > > I need to run optimize, and have spent a lot of time to add a > > > > > > > custom column of MA on optimize result list and no luck so far. > > > > > > > > > > > > > > Is it possible to find the bar index or bar number of buying and > > > > > > > use the number to refer the MA like > > > > > > > > > > > > > > myMA = ref(MA(close, 6), -17); > > > > > > > > > > > > > > Thank you very much! > > > > > > > > > > > > > > Charles > > > > > > > > > > > > > > > > > > > > > > > > > > > > --- In [email protected], "Mike" <sfclimbers@> wrote: > > > > > > > > > > > > > > > > Hi, > > > > > > > > > > > > > > > > There are a couple of problems: > > > > > > > > > > > > > > > > 1. When trying to do an equivalence test, use the equivalence > > > > > > > > operator '==' not the assignment operator '='. > > > > > > > > > > > > > > > > 2. buyrule is an array. You cannot use the if statement on an > > > > > > > > array. > > > > > > > > > > > > > > > > If you just want to see what the value is, then run an > > > > > > > > exploration: > > > > > > > > > > > > > > > > Filter = buyrule; > > > > > > > > AddColumn(MA(Close, 6), "MA(6)"); > > > > > > > > > > > > > > > > http://www.amibroker.com/guide/h_exploration.html > > > > > > > > > > > > > > > > Otherwise, add a custom metric to your backtest. > > > > > > > > > > > > > > > > Mike > > > > > > > > > > > > > > > > --- In [email protected], "chuck_win" <chaoy@> wrote: > > > > > > > > > > > > > > > > > > Hi, > > > > > > > > > > > > > > > > > > I do backtesting, and want to check MA at the time of buying. > > > > > > > > > > > > > > > > > > My code looks like > > > > > > > > > > > > > > > > > > buyrule = cross(MACD, signal); > > > > > > > > > buy = buyrule; > > > > > > > > > buyprice = close; > > > > > > > > > sellrule = cross(signal, MACD); > > > > > > > > > sell = sellrule; > > > > > > > > > sellprice = close; > > > > > > > > > if (buyrule = true) { > > > > > > > > > str = StrFormat("%0.2f", MA(Close,6)); > > > > > > > > > _TRACE(NumToStr( DateTime(), formatDateTime ) + ", " + str); > > > > > > > > > > > > > > > > > > } > > > > > > > > > > > > > > > > > > I always get the MA at the ending time of last bar instead. > > > > > > > > > > > > > > > > > > Thanks for any help! > > > > > > > > > > > > > > > > > > Charles > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
