vendredi 18 novembre 2016

Backtesting Mistakes

Hi Friends,

I feel there is something wrong in the backtesting procedure in the below afl.

Timeframe: 5 minute

Analysis range: 30 trading days

Symbol list(30): NIFTY-I,BANKNIFTY-I,YESBANK-I,RELIANCE-I,AXISBANK-I,SBIN-I,ICICIBANK-I,RELINFRA-I,TATASTEEL-I,TATAMOTORS-I,HDFCBANK-I,SUNPHARMA-I,RELCAPITAL-I,AUROPHARMA-I,MARUTI-I,PNB-I,VEDL-I,BANKBARODA-I,INFY-I,LT-I,CANBK-I,BHARATFIN-I,TCS-I,BHEL-I,DLF-I,HINDALCO-I,LUPIN-I,CASTROLIND-I,ONGC-I,HDFC-I





Code:

//OPTIMIZATION PART



//maxpositions =  Optimize( "maxpositions", 3, 1, 10, 1 ); // note that default value is 2
maxpositions =  Param( "maxpositions", 100 , 1, 10, 1 );


//TARGET_RATIO = Optimize( "TARGET_RATIO", 2.7, 2.0, 4.0, 0.1 );
TARGET_RATIO = Param( "TARGET_RATIO", 2, 1, 10, 1 );


TRADE_START_TIME =  091900 ;
//TRADE_START_TIME = Optimize( "TRADE_START_TIME", 091900,  091900, 095900, 500 );


TRADE_END_TIME =  150000 ;
//TRADE_END_TIME = Optimize( "TRADE_END_TIME", 151400,  150900, 151400, 100 );

SQUARE_OFF_TIME = 151400 ;
//MAX_AMOUNT =  Param( "MAX_AMOUNT", 10000, 1000, 100000, 1000 );


TRADING_CAPITAL = 100000;
STOPLOSS_PERCENT = 1 ;



//***********************************************************************
// OPTIMIZATON PART
//***********************************************************************
SetOption( "WarningLevel", 1 );


SetOption( "MCEnable", 0 );
SetOption( "PriceBoundChecking", False );
SetOption( "ExtraColumnsLocation", 1 );


SetOption( "MaxOpenPositions", maxpositions );  // MAXIMUM ALLOWED OPEN POSITIONS AT A TIME


//************************************************************************
// GENERAL TRADE VARIABLES
//************************************************************************
STOPLOSS_AMOUNT = ( TRADING_CAPITAL / 100 ) * STOPLOSS_PERCENT;  // 1000 RS FOR 1L




TRIGGER_OPEN = ( Ref( Open, 0 ) );  // LAST BAR'S OPEN

TRIGGER_LOW = ( Ref( Low, 0 ) );  // LAST BAR'S LOW
TRIGGER_HIGH = ( Ref( High, 0 ) );  // LAST BAR'S HIGH

ENTRY_HIGH = ( Ref( High, -1 ) ); // HIGH OF 2ND BAR FROM LAST

ENTRY_LOW = ( Ref( Low, -1 ) );  // LOW OF 2ND BAR FROM LAST


ENTRY_OPEN = ( Ref( Open, -1 ) );    // OPEN OF 2ND BAR FROM LAST
ENTRY_CLOSE = ( Ref( Close, -1 ) );    // CLOSE OF 2ND BAR FROM LAST

PREVIOUS_HIGH = ( Ref( High, -2 ) );  // HIGH OF 3RD BAR FROM LAST
PREVIOUS_LOW = ( Ref( Low, -2 ) );  // LOW OF 3RD BAR FROM LAST

ENTRY_BAR_RANGE = abs( ENTRY_HIGH - ENTRY_LOW );  // SETUP BAR'S RANGE

TARGET_RANGE = abs( TARGET_RATIO * ENTRY_BAR_RANGE );  // TARGET DISTANCE FROM ENTRY PRICE

TIME = Ref( TimeNum(), 0 );  // LAST BAR'S TIME NUMBER

NO_OF_SHARES = STOPLOSS_AMOUNT / ENTRY_BAR_RANGE;  // NO OF SHARES FOR THE STOPLOSS AMOUNT 1000

POSITION_VALUE = NO_OF_SHARES * ENTRY_LOW;



//************************************************************************
// SHORT TRADE VARIABLES
//************************************************************************
Short_TRADE_ON = 0;
Short = 0;
ShortPrice = 0;
Cover = 0;
CoverPrice = 0;
S_ENTRY_BAR_NUMBER = 0;
S_targetprice = 0;
S_stopprice = 0;



Short = ExRem( Short, Cover );
Cover = ExRem( Cover, Short );

S_STOPLOSS = ENTRY_HIGH ;

S_TARGET = ENTRY_LOW - TARGET_RANGE;



//************************************************************************
// LONG TRADE VARIABLES
//************************************************************************
Long_TRADE_ON = 0 ;
Buy = 0;
BuyPrice = 0;
Sell = 0;
SellPrice = 0;
L_ENTRY_BAR_NUMBER = 0;
L_targetprice = 0;
L_stopprice = 0;



Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

L_STOPLOSS = ENTRY_LOW ;

L_TARGET = ENTRY_HIGH + TARGET_RANGE;


//******************************************************************************
// YOUR TRADING SYSTEM HERE
//******************************************************************************

Short_CONDITION =
    (


        TIME > TRADE_START_TIME// 5
        AND
        TIME < TRADE_END_TIME// 6


        // TRIGGER OPEN INSIDE ENTRY HIGH & LOW
        AND
        TRIGGER_OPEN <= ENTRY_HIGH// 8  // TRIGGER OPEN LOWER THAN ENTRY HIGH
        AND
        TRIGGER_OPEN >= ENTRY_LOW// 9  // TRIGGER OPEN HIGHER THAN ENTRY LOW


        // SETUP BAR
        AND
        TRIGGER_HIGH < ENTRY_HIGH// 7  // INITIATE CONDITION
        AND
        PREVIOUS_HIGH < ENTRY_HIGH

        // SHORT TRIGGER RULE
        AND
        TRIGGER_LOW <= ENTRY_LOW// 10    // SHORT TRIGGER RULE
    ) ;
/////////////////////////////////////////////////////////////////////////////////////////////////

Buy_CONDITION =
    (


        TIME > TRADE_START_TIME// 5
        AND
        TIME < TRADE_END_TIME// 6


        // TRIGGER OPEN INSIDE ENTRY HIGH & LOW
        AND
        TRIGGER_OPEN >= ENTRY_LOW// 8  // TRIGGER OPEN HIGHER THAN ENTRY LOW
        AND
        TRIGGER_OPEN <= ENTRY_HIGH// 9  // TRIGGER OPEN LOWER THAN ENTRY HIGH


        // SETUP BAR
        AND
        TRIGGER_LOW > ENTRY_LOW
        AND
        PREVIOUS_LOW > ENTRY_LOW

        ////////////
        AND
        TRIGGER_HIGH >= ENTRY_HIGH        // 10        // LONG TRIGGER CONDITION
    ) ;

Initiated_Bar_Number = 0;



for( i = 0; i < BarCount; i++ )
{










    if( Short_TRADE_ON == 0 AND  Short_CONDITION[ i ] == True )
    {
        Short_TRADE_ON = 1;
        S_ENTRY_BAR_NUMBER = i;
        Initiated_Bar_Number = i;
        S_stopprice[ i ] = ENTRY_HIGH[i] ;
        S_targetprice[ i ] = S_TARGET[i];
        BAR_RANGE[i] = abs( ENTRY_HIGH[i] - ENTRY_LOW[i] );
        NO_OF_SHARES[ i ] = STOPLOSS_AMOUNT / BAR_RANGE[i];
        SHORT_POSITION_VALUE[ i ] = NO_OF_SHARES[ i ] * ENTRY_LOW[ i ];
        ShortPrice[ i ] = ENTRY_LOW[i];
        PositionSize[ i ] = SHORT_POSITION_VALUE[ i ];
        Short[ i ] = 1;
    }

//*********************************************

    if( Long_TRADE_ON == 0 AND  Buy_CONDITION[ i ] == True )
    {
        Long_TRADE_ON = 1;
        L_ENTRY_BAR_NUMBER = i;
        Initiated_Bar_Number = i;
        L_stopprice[ i ] = ENTRY_LOW[i] ;
        L_targetprice[ i ] = L_TARGET[i];
        BAR_RANGE[i] = abs( ENTRY_HIGH[i] - ENTRY_LOW[i] );
        NO_OF_SHARES[ i ] = STOPLOSS_AMOUNT / BAR_RANGE[i];
        LONG_POSITION_VALUE[ i ] = NO_OF_SHARES[ i ] * ENTRY_HIGH[ i ];
        BuyPrice[ i ] = ENTRY_HIGH[i];
        PositionSize[ i ] = LONG_POSITION_VALUE[ i ];
        Buy[ i ] = 1;
    }

//************************************************



///////////////////////////////////////////////////
// EXIT CONDITONS
///////////////////////////////////////////////////


    // SHORT TARGET CONDITION
    TARGET_Cover_condition = Low[ i ]  < S_targetprice[ S_ENTRY_BAR_NUMBER ];
    // SHORT STOPLOSS CONDITION
    STOP_Cover_condition =  High[ i ]  > S_stopprice[ S_ENTRY_BAR_NUMBER ];

//************************************************

    // LONG TARGET CONDITION
    TARGET_Sell_condition = High[ i ] > L_targetprice[ L_ENTRY_BAR_NUMBER ];
    // LONG STOPLOSS CONDITION
    STOP_Sell_condition =  Low[ i ] < L_stopprice[ L_ENTRY_BAR_NUMBER ];



//************************************************
// SHORT EXITS
//**********************************************


    // EXIT SHORT TRADE WHEN TARGET REACHED
    if( Short_TRADE_ON == 1 AND TARGET_Cover_condition[ i ] == True )
    {
        Cover[ i ] = 1;
        CoverPrice[ i ] = S_targetprice[ S_ENTRY_BAR_NUMBER ];

        Short_TRADE_ON = 0;
    }
    // EXIT SHORT TRADE WHEN STOPLOSS HIT
    else
        if( Short_TRADE_ON == 1 AND STOP_Cover_condition[ i  == True ] )
        {
            Cover[ i ] = 1;
            CoverPrice[ i ] = S_stopprice[ S_ENTRY_BAR_NUMBER ];
            Short_TRADE_ON = 0;
        }
        // EXIT SHORT TRADE ON SQUARE OFF TIME
        else
            if( Short_TRADE_ON == 1 AND TIME[i] > SQUARE_OFF_TIME )
            {
                Cover[ i ] = 1;
                CoverPrice[ i ] = Avg[i];
                Short_TRADE_ON = 0;
            }
            // ELSE DO NOTHING
            else
            {
                Cover[ i ] = 0;
            }

//}

//************************************************
// LONG EXITS
//************************************************

    // EXIT LONG TRADE WHEN TARGET HIT
    if( Long_TRADE_ON == 1 AND TARGET_Sell_condition[ i ] == True )
    {
        Sell[ i ] = 1;
        SellPrice[ i ] = L_targetprice[ L_ENTRY_BAR_NUMBER ];
        Long_TRADE_ON = 0;
    }
        // EXIT LONG TRADE WHEN STOPLOSS HIT
    else
        if( Long_TRADE_ON == 1 AND  STOP_Sell_condition[ i ] == True )
        {
            Sell[ i ] = 1;
            SellPrice[ i ] = L_stopprice[ L_ENTRY_BAR_NUMBER ];
            Long_TRADE_ON = 0;
        }
        // EXIT LONG TRADE ON SQUARE OFF TIME
        else
            if( Long_TRADE_ON == 1 AND  TIME[i] > SQUARE_OFF_TIME )
            {
                Sell[ i ] = 1;
                SellPrice[ i ] = Avg[i];
                Long_TRADE_ON = 0;
            }
            // ELSE DO NOTHING
            else
            {
                Sell[ i ] = 0;
            }
}

//************************************************


Please correct the mistake.


Backtesting Mistakes

Aucun commentaire:

Enregistrer un commentaire