Welcome Features News Download Registration Support FAQ Wish list Links
Advanced stock charting and analysis program

AFL Library

This is read-only version of AFL library entry. Ability to add user formulas and comment is only available from members-only area.

Details:

Formula name: interactively test discretionary trading
Author/Uploader: DD - (email hidden)
Date/Time added: 2009-03-08 11:15:28
Origin:
Keywords: interactive discretionary trading
Level: medium
Flags: system,indicator,function

DISCLAIMER: Most formulas present in AFL on-line library are submitted by the users and are provided here on an "as is" and "as available" basis. AmiBroker.com makes no representations or warranties of any kind to the contents or the operation of material presented here. We do not maintain nor provide technical support for 3rd party formulas.
Description:

Add manual buy/short signals. Useful to test your performance as a discretionary trader, or to get a feeling of the behavior of indicators [since by looking at the chart, one is being misled by the future bars].
Insert this formula into your favorite chart setup, then either select the last visible bar and advance the bar selection, or use the bar replay tool.
Open the parameters window and place buy/short trades. Triggering either of these while in a trade will close the trade. The current realized profit and open trade (unrealized) profit are displayed in the Interpretation window. After an interactive session, one can also run the portfolio backtester (Analysis) for more statistics.
This formula requires Amibroker 5.24 beta or later.
Ideas, comments, bug reports are welcome.
Thanks Tomasz for this excellent software!

Formula:


SetBarsRequired(-1,1);

lvBar = Status( "LastVisibleBar" );
lvbar = Max(0, Min(Status( "LastVisibleBar" ),BarCount-1));

Vnbuy   = Nz(StaticVarGet("vnbuy"));
vnsell  = Nz(StaticVarGet("vnsell"));
Vnbuyp  = Nz(StaticVarGet("vnbuyp"));
vnsellp = Nz(StaticVarGet("vnsellp"));

Vnshort = Nz(StaticVarGet("Vnshort"));
vncover = Nz(StaticVarGet("vncover"));
Vnshortp= Nz(StaticVarGet("vnshortp"));
vncoverp= Nz(StaticVarGet("vncoverp"));

inbuy = Nz(StaticVarGet("inbuy"));
inshort = Nz(StaticVarGet("inshort"));

profit = Nz(StaticVarGet("profit"));

procedure CloseTrade() {
global vnsell;
global vnsellp;
global vnbuy;
global vnbuyp;
global Vnshort;
global Vncover;
global Vnshortp;
global Vncoverp;
global inbuy;
global inshort;
global profit;

    if (inbuy) {
    inbuy = 0;
    vnsell[lvBar] = 1;
    vnsellp = C[Lvbar];
    profit = profit + vnsellp-Vnbuyp;
 
    StaticVarSet("vnsell", Vnsell);
    StaticVarSet("vnsellp", Vnsellp);
    StaticVarSet("inbuy", 0);
    StaticVarSet("profit", profit);
    } else if (inshort) {
    inshort = 1;
    vncover[lvBar] = 1;
    vncoverp = C[Lvbar];
    profit = profit + vnshortp - vncoverp;
 
    StaticVarSet("vncover", vncover);
    StaticVarSet("vncoverp", vncoverp);
    StaticVarSet("inshort", 0);
    StaticVarSet("profit", profit);
    }
}

mBuy = ParamTrigger("buy", "Buy");
if (mBuy){
  if (!inbuy && !inshort){
    inbuy = 1;
    vnbuy[lvBar] = 1;
    vnbuyp = C[Lvbar];
    StaticVarSet("vnbuy", Vnbuy );
    StaticVarSet("vnbuyp", Vnbuyp);
    StaticVarSet("inbuy", 1);
  } else {
    CloseTrade();
  }
}

mShort = ParamTrigger("Short", "Short");
if (mShort){
  if (!inbuy && !inshort){
    inshort = 1;
    vnshort[lvBar] = 1;
    vnshortp = C[Lvbar];
    StaticVarSet("vnshort", Vnshort);
    StaticVarSet("vnshortp", vnshortp);
    StaticVarSet("inshort", 1);
  } else {
    CloseTrade();
  }
}

reset = ParamTrigger("reset", "reset");
if (reset){
  vnbuy = 0;   vnsell = 0; vnshort  = 0; vncover  = 0; 
  vnbuyp = 0; vnsellp = 0; vnshortp = 0; vncoverp = 0; 
  inbuy = 0; inshort = 0; profit = 0;

  StaticVarSet("vnbuy", Vnbuy);
  StaticVarSet("vnbuyp", Vnbuyp);
  StaticVarSet("vnsell", Vnsell);
  StaticVarSet("vnsellp", Vnsellp);
  StaticVarSet("inbuy", 0);

  StaticVarSet("vnshort", Vnshort);
  StaticVarSet("vnshortp", vnshortp);
  StaticVarSet("vncover", vncover);
  StaticVarSet("vncoverp", vncoverp);
  StaticVarSet("inshort", 0);

  StaticVarSet("profit", profit);

}

setABArrays= ParamToggle("setABArrays", "on|off");
if (setABArrays == 0){
  Buy = vnbuy; Sell = vnsell; Short = vnshort; Cover = vncover; 
  BuyPrice = vnbuyp; SellPrice = vnsellp; ShortPrice = vnshortp; CoverPrice =
vncoverp; 
}

UProfit = 0;
if (inbuy){
  UProfit = C[lvbar] - Vnbuyp[Lvbar];
} else if (inshort){
  UProfit = Vnshortp[Lvbar] - C[lvbar];
}

printf("R profit: %g\n", profit);
printf("U profit: %g\n", UProfit);

PlotShapes(shapeSmallUpTriangle*Nz(Vnbuy),colorBrightGreen,   0, L, -10);
PlotShapes(shapeSmallDownTriangle*Nz(Vnsell),colorRed,   0, H, -10);
PlotShapes(shapeHollowSmallDownTriangle*Nz(Vnshort),colorRed,   0, H, -10);
PlotShapes(shapeHollowSmallUpTriangle*Nz(Vncover),colorBrightGreen,   0, L,
-10);

//Plot( C, "Close", colorBlack, styleNoTitle | ParamStyle("Style") |
GetPriceStyle()); 

Comments:

Peter
pmxgs1 [at] iol.pt
2009-04-28 14:40:48
Hi,
thanks very much for this code.
For a long time I wanted a feature like this in Amibroker.
I\\\'ve have been using Forex Tester for this manual/discretionary backtesting, but I prefer AB since I can code my own indicators easily.
If we could place market and pending orders and keep a log file generated automatically of the trades, date, time p&L, balance, etc that we can analyse after each round of testing in a spreadsheet it would be great!! (similar to what forextester does)
Hopefully we will be able to do that in amibroker in the future.
Thanks a lot


About | Privacy | Terms of Use | Contact information
Copyright © 2001 AMIBROKER.COM