in Search

Convert system from Amibroker language

Last post 05-17-2008, 21:11 by wabbit. 3 replies.
Sort Posts: Previous Next
  •  05-03-2008, 3:40 27115

    Convert system from Amibroker language

    I know this is long, but does anybody know how to convert this system written in Amibroker code into Metastock? 

     

    /*==============================================================================
    Global Settings
    ==============================================================================*/
    SetOption("InitialEquity", 1000000);
    SetOption("MinShares", 50);
    SetOption("NoDefaultColumns", True );
    SetOption("CommissionMode", 2); //$$ per trade
    SetOption("CommissionAmount", 0); // commission is accounted for in skid
    SetOption("MarginRequirement", 10);
    SetOption("UsePrevBarEquityForPosSizing", True);
    SetOption("UseCustomBacktestProc", True );

    SetTradeDelays( 1, 1, 1, 1 );

    /*==============================================================================
    User-defined Functions
    ==============================================================================*/
    function EMA0(A, p)
    {
    r[0] = a[0];
    ep = 2/(p+1);
    for(i = 1; i < BarCount; i++)
    {
    rIdea [I] = r[i-1] + (aIdea [I] - r[i-1]) * ep;
    }
    return r;
    }

    function OptimizeNot(a1, a2, a3, a4, a5)
    {
    return a2;
    }

    /*==============================================================================
    Entry and Exit Rules
    ==============================================================================*/
    tr = Max(H-L, Max(abs(H-Ref(C, -1)), abs(Ref(C, -1)-L)));
    tr[0] = H[0] - L[0];

    fast = EMA0(C, Optimize("FastEMA", 15, 20, 140, 5));
    slow = EMA0(C, Optimize("SlowEMA", 150, 150, 1000, 10));
    Buy = Cross(fast, slow);
    Sell = Cross(slow, fast);
    Buy[1] = 0; // to avoid false signal at the beginning
    //ApplyStop(stopTypeLoss, stopModePoint, ATR_multi*Ref(ATR0, -1), True, True
    );

    /*==============================================================================
    Skid of Executions
    ==============================================================================*/
    BuyPrice = (H+O)/2;
    SellPrice = (L+O)/2;

    /*==============================================================================
    Position Sizing
    ==============================================================================*/
    ATR_multi = OptimizeNot("ATP Multi", 5, 1, 9, 1);
    ATR0 = EMA0(tr, 20);

    Risk_Per_Share = Ref(ATR0, -1) * ATR_multi;
    Heat = OptimizeNot("Heat", 0.10, 0.01, 0.50, 0.01);

    PosSizeFactor = Heat / Risk_Per_Share;
    // the real position size value is calculated within CBT
    SetPositionSize(PosSizeFactor, spsValue);

    /*==============================================================================
    Automatic Analysis Action Options
    ==============================================================================*/
    AAAction = Status("action");
    if(AAAction == actionIndicator)
    {
    Plot(fast, "FastEMA", colorRed);
    Plot(slow, "SlowEMA", colorYellow);
    }
    else if(AAAction == actionExplore)
    {
    Filter = 1;
    AddColumn( DateTime(), "Date", formatDateTime );
    //AddColumn(DayOfWeek(), "DayOfWeek", 1);
    AddColumn(O, "Open");
    AddColumn(H, "High");
    AddColumn(L, "Low");
    AddColumn(C, "Close");
    //AddColumn(Avg, "AVG");
    AddColumn(fast, "FastEMA", 1.3);
    AddColumn(slow, "SlowEMA", 1.3);
    AddColumn(ATR0, "ATR", 1.3);
    //AddColumn(Risk_Per_Share, "Risk/Share");
    AddColumn(IIf(Buy, 111, IIf(Sell, 222, 0)) , "Buy1Sell2", 1);
    AddColumn(PosSize, "PosSize%Eq");
    AddColumn(Equity() , "Equity");
    }
    else if(AAAction == actionPortfolio)
    {
    bo = GetBacktesterObject();
    bo.PreProcess(); // Initialize backtester
    for( bar=0; bar < BarCount; bar++)
    {
    eq =  bo.Equity;
    for ( sig=bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar) )
    {
    if (sig.isExit())
    {
                if(bo.ExitTrade(bar,sig.symbol,sig.Price))
    {
    _TRACE("EXIT: " + sig.symbol + "@" + sig.Price);
    }
    }
    }

            // update stats after closing trades
         bo.UpdateStats(bar, 1 );
          
         for ( sig=bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar))
         {
    if (sig.isEntry())
    {
    // sig.PosSize is passed from Phase I.
    ps = Round(( eq * sig.PosSize)/250)*250 * sig.Price;

    if(bo.EnterTrade(bar, sig.symbol, True, sig.Price, ps,
    sig.PosScore,sig.RoundLotSize))
    {
    _TRACE("ENTRY: " + sig.symbol + " @" + sig.Price + " PosScore=" +
    sig.PosScore + " PosSize=" + ps);
                }
    }
    }

    //bo.HandleStops(bar); // MUST BE PLACED HERE TO WORK FOR N-BAR STOPS (not
    before enter/exit trades)
    bo.UpdateStats(bar,1); // MAE/MFE is updated when timeinbar is set to 1.
    bo.UpdateStats(bar,2);
       }
    bo.PostProcess(); // Finalize backtester

    }
    /*==============================================================================
    End of Formula
    ==============================================================================*/

  •  05-03-2008, 4:04 27116 in reply to 27115

    Re: Convert system from Amibroker language

    Hi HKGuy,

    It's been a while since I played with AFL, but by the looks of things, this is just an EMA crossover system with position size defined using ATR.

    You should be able to modify the "Equis - Moving Average Crossover" system test to acheive the same thing.

    Be careful of tripping over the fine line between system optimisation and "curve fitting"


    Hope this helps.

    wabbit :D


    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status : wabbit.com.au SkyPE online status
    My SkyPE account : wabbit.com.au

  •  05-17-2008, 11:42 27199 in reply to 27116

    Re: Convert system from Amibroker language

    Hi Wabbit. Thanks for the reply. I have been trying to build the system, with some success as far as the indicators are concerned. For the moving averages (where I have to work around the null bars) I have used this :

    prd:=150;
    data:=CLOSE;
    m:=2/(prd+1);
    ema:=If(Cum(IsDefined(data))=1,data,(m*data)+((1-m)*PREV));
    {plot}
    ema;

    Using periods 150 and 15 as the system calls for. My problem now is that the system is supposed to Buy one day after the 15 day crosses over the 150 day average. According to the number values generated this happens on August 31st, 1982 for the specific data that I am using. Therefore, the first Buy should be executed on September 1st.

    However, when I test the system using this in the system tester:

    cross( Fml( "My EMA(2)") ,  Fml( "My EMA(1)") )

    The trade is sometimes done a day later than it should be. I have set the system tester to delay the actual trade 1 day after the signal is issued.

    But, I sometimes get, "considered", "placed", "opened", and "executed". This usually is over the span of 3 days, making the actual execution one day later than it should be. Any ideas on how to resolve this problem, so I get a signal issued on "DAY 1" and then executed on "DAY 2"?

    Appreciate the help.

    Thanks,

    HKguy

     

  •  05-17-2008, 21:11 27200 in reply to 27199

    Re: Convert system from Amibroker language

    Hi again,

    I don't know too much about exactly how the EST works; by the number of questions on the forum I guess that not many people know exactly how the EST works! Wink [;)]  One of the things on my very long list of things to do is to take the EST a part and try to discover exactly how it works, but it is very low on my list of priorities.

    The only thing I can suggest is to have a read of Sprunger's guide to the EST available in the downloads section, check your settings and experiment to see if you can get the EST to play your game.  Other ideas might be to try out Roy Larsen's Trade Equity system available from his website, http://www.metastocktips.co.nz/ or even TradeSim, http://www.compuvision.com.au/ and compare the results with the EST.


    Hope this helps.

    wabbit Big Smile [:D]


    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status : wabbit.com.au SkyPE online status
    My SkyPE account : wabbit.com.au

View as RSS news feed in XML