Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Manual Tester in English for Amibroker (AFL)
crave
over 3 years ago
Amibroker (AFL)

Rating:
5 / 5 (Votes 1)
Tags:
amibroker

Manual Tester formula – English translation from Russian
Not tested as yet … The results do not show up in the file.

Indicator / Formula

Copy & Paste Friendly
_SECTION_BEGIN("Manual Tester");
//  Manual Tester.afl - created by maxign
//  Manual Tester for AmiBroker
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

Plot(C,"",colorBlack,styleCandle);

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
if(TickSize == 0)
{
    PopupWindow("\n(Symbol -> Information) parameter TickSize!", "Error");
}
else
{
    // Parameters
    StartX = Param("Buttons X", 240, 0, 1000, 5);
    StartY = Param("Buttons Y", 20, 0, 1000, 5);
    Spread = Param("Spread (pips)", 0, 0, 1000) * TickSize;
    ScanMode = ParamList("Scan Mode", "Test|Clear all signals|Save to file|Load from file", 0);
    fileName = ParamStr("File path", "D://AmiBroker/Tester/Trades.txt");
 
    // Initialisation
    BarDateTime = DateTime();
    Buy = Sell = Short = Cover = 0;
    BuyPrice = SellPrice = ShortPrice = CoverPrice = C;
 
    // Code Execution Mode
    formulaAction = Status("ActionEx");
 
    // -------------- INDICATOR ---------------
 
    if(formulaAction == actionIndicator)
    {
        // Graphics parameters
        RequestTimedRefresh(1);
        GfxSetOverlayMode(0);
        GfxSetBkMode(1);
 
        // Buttons Size and the Distance between them
        CellHeight = 24;
        CellWidth = 70;
        CellXSpace = 10;
 
        // Buttons Color
        BorderColor = colorBlack;
        InactiveButtonColor = colorCustom6;
        ActiveButtonColor = colorRed;
 
        // Number of Active Button
        ActiveButton = StaticVarGet("ActiveButton");
 
        // Button Drawing Parameters
        GfxSelectPen(BorderColor, 2);
        GfxSelectFont( "Tahoma", 11 , 700);
 
        // Button Execution
        for(btn = 1; btn <= 6; btn++)
        {
            if(ActiveButton == btn)
            {
                GfxSelectSolidBrush(ActiveButtonColor);
                GfxSetTextColor(colorWhite);
            }
            else
            {
                GfxSelectSolidBrush(InactiveButtonColor);
                GfxSetTextColor(ColorRGB(40,40,40));
            }
 
            CellX = StartX + (btn - 1) * (CellWidth + CellXSpace);
            GfxRectangle(CellX, StartY, CellX + CellWidth, StartY + CellHeight);
 
            switch(btn)
            {
            case 1:
                BtnText = "Buy";
                break;
            case 2:
                BtnText = "Sell";
                break;
            case 3:
                BtnText = "Short";
                break;
            case 4:
                BtnText = "Cover";
                break;
            case 5:
                BtnText = "Clear";
                break;
            case 6:
                BtnText = "Clear All";
                break;
            }
 
            GfxDrawText(BtnText, CellX, StartY, CellX + CellWidth, StartY + CellHeight, 32+1+4);
        }
 
        // Left Button Press Execution
        if(GetCursorMouseButtons() & 8)
        {
            // Button Coordinates in Pixels
            px = GetCursorXPosition(1);
            py = GetCursorYPosition(1);
 
            buttonPressed = False;
 
            if(py >= StartY AND py <= StartY + CellHeight)
            {
                for(btn = 1; btn <= 6; btn++)
                {
                    CellX = StartX + (btn - 1) * (CellWidth + CellXSpace);
                    if(px >= CellX AND px <= CellX + CellWidth)
                    {
                        // Button Activation or Deactivation
                        if(ActiveButton == btn)
                            StaticVarSet("ActiveButton", 0);
                        else
                            StaticVarSet("ActiveButton", btn);
 
                        buttonPressed = True;
 
                        break;
                    }
                }
            }
 
            // Button Press "Clear All"
            if(buttonPressed == True AND StaticVarGet("ActiveButton") == 6)
            {
                for(i = 0; i < BarCount; i++)
                {
                    StaticVarRemove(Name() + "Buy"   + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "Sell"  + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "Short" + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "Cover" + DateTimeToStr(BarDateTime[i]));
 
                    StaticVarRemove(Name() + "BuyPrice"   + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "SellPrice"  + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "ShortPrice" + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "CoverPrice" + DateTimeToStr(BarDateTime[i]));
                }
 
                StaticVarSet("ActiveButton", 0);
            }
 
            // Transaction Record in Static Variable
            if(buttonPressed == False)
            {
                // Cursor Coordinates Price/Date
                x = GetCursorXPosition(0);
                y = GetCursorYPosition(0);
                y = round(y / TickSize) * TickSize;    // Rounding to TickSize
 
                // Buy
                if(ActiveButton == 1)
                {
                    VarBuy = Name() + "Buy" + DateTimeToStr(x);
                    VarBuyPrice = Name() + "BuyPrice" + DateTimeToStr(x);
 
                    StaticVarSet(VarBuy, 1);
                    StaticVarSet(VarBuyPrice, y);
                }
 
                // Sell
                if(ActiveButton == 2)
                {
                    VarSell = Name() + "Sell" + DateTimeToStr(x);
                    VarSellPrice   = Name() + "SellPrice" + DateTimeToStr(x);
 
                    StaticVarSet(VarSell, 1);
                    StaticVarSet(VarSellPrice, y);
                }
 
                // Short
                if(ActiveButton == 3)
                {
                    VarShort = Name() + "Short" + DateTimeToStr(x);
                    VarShortPrice = Name() + "ShortPrice" + DateTimeToStr(x);
 
                    StaticVarSet(VarShort, 1);
                    StaticVarSet(VarShortPrice, y);
                }
 
                // Cover
                if(ActiveButton == 4)
                {
                    VarCover   = Name() + "Cover" + DateTimeToStr(x);
                    VarCoverPrice = Name() + "CoverPrice" + DateTimeToStr(x);
 
                    StaticVarSet(VarCover, 1);
                    StaticVarSet(VarCoverPrice, y);
                }
 
                // Clear
                if(ActiveButton == 5)
                {
                    VarBuy = Name() + "Buy" + DateTimeToStr(x);
                    VarBuyPrice = Name() + "BuyPrice" + DateTimeToStr(x);
 
                    VarSell = Name() + "Sell" + DateTimeToStr(x);
                    VarSellPrice = Name() + "SellPrice" + DateTimeToStr(x);
 
                    VarShort = Name() + "Short" + DateTimeToStr(x);
                    VarShortPrice = Name() + "ShortPrice" + DateTimeToStr(x);
 
                    VarCover = Name() + "Cover" + DateTimeToStr(x);
                    VarCoverPrice = Name() + "CoverPrice" + DateTimeToStr(x);
 
                    StaticVarRemove(VarBuy);
                    StaticVarRemove(VarSell);
                    StaticVarRemove(VarShort);
                    StaticVarRemove(VarCover);
 
                    StaticVarRemove(VarBuyPrice);
                    StaticVarRemove(VarSellPrice);
                    StaticVarRemove(VarShortPrice);
                    StaticVarRemove(VarCoverPrice);       
                }
 
                StaticVarSet("ActiveButton", 0);    // Deactivation of the Button after Action Execution 
            }
        }
 
        // Copying of Transactions from Static Variables into Trading Matrix
        for(i = 0; i < BarCount; i++)
        {
            BuySig   = StaticVarGet(Name() + "Buy"   + DateTimeToStr(BarDateTime[i]));
            SellSig  = StaticVarGet(Name() + "Sell"  + DateTimeToStr(BarDateTime[i]));
            ShortSig = StaticVarGet(Name() + "Short" + DateTimeToStr(BarDateTime[i]));
            CoverSig = StaticVarGet(Name() + "Cover" + DateTimeToStr(BarDateTime[i]));
 
            if(BuySig)
            {
                Buy[i] = 1;
                BuyPrice[i] = StaticVarGet(Name() + "BuyPrice" + DateTimeToStr(BarDateTime[i])) + spread;
            }
 
            if(SellSig)
            {
                Sell[i] = 1;
                SellPrice[i]  = StaticVarGet(Name() + "SellPrice"  + DateTimeToStr(BarDateTime[i]));
            }
 
            if(ShortSig)
            {
                Short[i] = 1;
                ShortPrice[i] = StaticVarGet(Name() + "ShortPrice" + DateTimeToStr(BarDateTime[i]));
            }
 
            if(CoverSig)
            {
                Cover[i] = 1;
                CoverPrice[i] = StaticVarGet(Name() + "CoverPrice" + DateTimeToStr(BarDateTime[i])) + spread;
            }
        }
 
        // ------ Arrows Drawing
        PlotShapes(Buy * shapeUpArrow, colorLime, 0, BuyPrice, -8);
        PlotShapes(Sell * shapeDownArrow, colorCustom12, 0, SellPrice, -8);
        PlotShapes(Short * shapeHollowDownArrow, colorCustom12, 0, ShortPrice, -8);
        PlotShapes(Cover * shapeHollowUpArrow, colorLime, 0, CoverPrice, -8);
    }
 
    // -------------- TEST MODE or SCAN ---------------
 
    if(formulaAction == actionScan OR formulaAction == actionBacktest)
    {
        // Testing
        if(ScanMode == "Test")
        {
            // Tester settings
            SetOption("InitialEquity", 10000);
            SetOption("AllowPositionShrinking", False);
            SetOption("ReverseSignalForcesExit", True);
            SetOption("AllowSameBarExit", True);
            SetOption("ActivateStopsImmediately", True);
            SetOption("MaxOpenPositions", 100);
            SetOption("PriceBoundChecking", False);
            SetOption("FuturesMode", True);
            SetOption("CommissionMode", 2);
            SetOption("CommissionAmount", 0);
            SetTradeDelays(0, 0, 0, 0);
 
            PositionSize = 100;
            MarginDeposit = 100;
            RoundLotSize = 1;
            PointValue = 1/TickSize;
 
            // Reading Transactions from Static Variables
            for(i = 0; i < BarCount; i++)
            {
                BuySig   = StaticVarGet(Name() + "Buy"   + DateTimeToStr(BarDateTime[i]));
                SellSig  = StaticVarGet(Name() + "Sell"  + DateTimeToStr(BarDateTime[i]));
                ShortSig = StaticVarGet(Name() + "Short" + DateTimeToStr(BarDateTime[i]));
                CoverSig = StaticVarGet(Name() + "Cover" + DateTimeToStr(BarDateTime[i]));
 
                if(BuySig)
                {
                    Buy[i] = 1;
                    BuyPrice[i] = StaticVarGet(Name() + "BuyPrice" + DateTimeToStr(BarDateTime[i])) + spread;
                }
 
                if(SellSig)
                {
                    Sell[i] = 1;
                    SellPrice[i]  = StaticVarGet(Name() + "SellPrice"  + DateTimeToStr(BarDateTime[i]));
                }
 
                if(ShortSig)
                {
                    Short[i] = 1;
                    ShortPrice[i] = StaticVarGet(Name() + "ShortPrice" + DateTimeToStr(BarDateTime[i]));
                }
 
                if(CoverSig)
                {
                    Cover[i] = 1;
                    CoverPrice[i] = StaticVarGet(Name() + "CoverPrice" + DateTimeToStr(BarDateTime[i])) + spread;
                }
            }
        }
 
        //  Static Variables Cleaning
        if(ScanMode == "Clear all signals")
        {
            for(i = 0; i < BarCount; i++)
            {
                StaticVarRemove(Name() + "Buy"   + DateTimeToStr(BarDateTime[i]));
                StaticVarRemove(Name() + "Sell"  + DateTimeToStr(BarDateTime[i]));
                StaticVarRemove(Name() + "Short" + DateTimeToStr(BarDateTime[i]));
                StaticVarRemove(Name() + "Cover" + DateTimeToStr(BarDateTime[i]));
 
                StaticVarRemove(Name() + "BuyPrice"   + DateTimeToStr(BarDateTime[i]));
                StaticVarRemove(Name() + "SellPrice"  + DateTimeToStr(BarDateTime[i]));
                StaticVarRemove(Name() + "ShortPrice" + DateTimeToStr(BarDateTime[i]));
                StaticVarRemove(Name() + "CoverPrice" + DateTimeToStr(BarDateTime[i]));
            }
 
            StaticVarSet("ActiveButton", 0);
        }
 
        // Recording Transactions to the File
        if(ScanMode == "Save to file")
        {
            // The number of digits after the decimal point
            digits = Max(log10(1/TickSize), 0);
 
            // Opening File to Write
            fh = fopen(fileName, "w");
         
            if(fh)
            {
                for(i = 0; i < BarCount; i++)
                {
                    BuySig   = StaticVarGet(Name() + "Buy"   + DateTimeToStr(BarDateTime[i]));
                    SellSig  = StaticVarGet(Name() + "Sell"  + DateTimeToStr(BarDateTime[i]));
                    ShortSig = StaticVarGet(Name() + "Short" + DateTimeToStr(BarDateTime[i]));
                    CoverSig = StaticVarGet(Name() + "Cover" + DateTimeToStr(BarDateTime[i]));
 
                    if(BuySig)
                    {
                        Price  = StaticVarGet(Name() + "BuyPrice"  + DateTimeToStr(BarDateTime[i]));
                        fputs(Name() + "Buy," + DateTimeToStr(BarDateTime[i]) + "," + NumToStr(Price, digits/10, False) + "\n", fh);
                    }
 
                    if(SellSig)
                    {
                        Price  = StaticVarGet(Name() + "SellPrice"  + DateTimeToStr(BarDateTime[i]));
                        fputs(Name() + "Sell," + DateTimeToStr(BarDateTime[i]) + "," + NumToStr(Price, digits/10, False) + "\n", fh);
                    }
 
                    if(ShortSig)
                    {
                        Price = StaticVarGet(Name() + "ShortPrice" + DateTimeToStr(BarDateTime[i]));
                        fputs(Name() + "Short," + DateTimeToStr(BarDateTime[i]) + "," + NumToStr(Price, digits/10, False) + "\n", fh);
                    }
 
                    if(CoverSig)
                    {
                        Price = StaticVarGet(Name() + "CoverPrice" + DateTimeToStr(BarDateTime[i]));
                        fputs(Name() + "Cover," + DateTimeToStr(BarDateTime[i]) + "," + NumToStr(Price, digits/10, False) + "\n", fh);
                    }
                }
 
                fclose(fh);
            }
            else
                PopupWindow("\n\nFile Opening or Creating Error" + fileName + "\n\nCreate a Specified Folder", "Export Error", 60);
        }
 
        // Downloading transactions from a file
        if(ScanMode == "Load from file")
        {
            // Opening File for reading
            fh = fopen(fileName, "r");
 
            if(fh)
            {
                // Cleaning Signals from Graphics
                for(i = 0; i < BarCount; i++)
                {
                    StaticVarRemove(Name() + "Buy"   + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "Sell"  + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "Short" + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "Cover" + DateTimeToStr(BarDateTime[i]));
 
                    StaticVarRemove(Name() + "BuyPrice"   + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "SellPrice"  + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "ShortPrice" + DateTimeToStr(BarDateTime[i]));
                    StaticVarRemove(Name() + "CoverPrice" + DateTimeToStr(BarDateTime[i]));
                }
 
                StaticVarSet("ActiveButton", 0);
 
                // Reading signals from the file
                while(! feof(fh))
                {
                    // Reading lines from the file
                    stringTrade = fgets(fh);
                     
                    // parsing
                    sName = StrExtract(stringTrade, 0);
                    sSig = StrExtract(stringTrade, 1);
                    sDate = StrExtract(stringTrade, 2);
                    sPrice = StrExtract(stringTrade, 3);
 
                    VarSig = sName + sSig + sDate;
                    VarPrice = sName + sSig + "Price" + sDate;
 
                    // Static Variables Writing
                    StaticVarSet(VarSig, 1);
                    StaticVarSet(VarPrice, StrToNum(sPrice));
                }
 
                fclose(fh);
            }
            else
                PopupWindow("\n\nError opening file " + fileName + "\n\nThis file does not exist.", "Import error", 60);
        }
    }
}
_SECTION_END();

0 comments

Leave Comment

Please login here to leave a comment.

Back