Can anybody modify this backtest afl for intrabar backtesting

#1
Hi Friends,

After lot of head ache I came to this decision of posting here.

My problem:
I am using hourly timeframe in backtester settings.
My trade entry times are shown as 10 am,11am,12pm,1pm,2pm,3pm like this.
What i want is my trades should be entered when at the time of trade triggered in 1 min interval.
Instead of 11am which is bar close time i want the exact time like 10.43 am.

Also all my trades are exiting in full hour time only. That is close of exit bar.

So I want intrabar entry and intrabar exit in hourly timeframe.
Is this possible.

If possible please modify below backtesting code which is not working for min by min backtesting.
Don't tell me to try hard . I already tried it very hardly and not able to succeed. I think it is not possible too.
 
#2
my backtesting afl first half
Code:
//RSI_PERIODS =Optimize( "RSI_PERIODS", 19 , 1, 100, 1 );
RSI_PERIODS =Param( "RSI_PERIODS", 16 , 1, 100, 1 );


//Short_RSI_SETUP_VALUE =Optimize( "Short_RSI_SETUP_VALUE", 19 , 1, 100, 1 );
Short_RSI_SETUP_VALUE =Param( "Short_RSI_SETUP_VALUE", 74 , 1, 100, 1 );
                                

//LONG_RSI_SETUP_VALUE =Optimize( "LONG_RSI_SETUP_VALUE", 19 , 1, 100, 1 );
LONG_RSI_SETUP_VALUE =Param( "LONG_RSI_SETUP_VALUE", 48 , 1, 100, 1 );

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


//TARGET_RATIO = Optimize( "TARGET_RATIO", 2.7, 1.0, 10.0, 0.1 );
TARGET_RATIO = Param( "TARGET_RATIO", 1.4, 1, 10, 1 );



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


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

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

SCRIP_NAME1 = Name();
SCRIP_NAME2 = StrReplace( SCRIP_NAME1, "-I", "" );
//SCRIP_NAME2 = StrReplace( SCRIP_NAME1, "_1M", "" );
SCRIP_NAME3 = StrReplace( SCRIP_NAME2, "-", "" );
SCRIP_NAME = StrReplace( SCRIP_NAME3, "&", "" );
/////////////////////////////////////////////////////

TRADING_CAPITAL = 100000;
STOPLOSS_PERCENT = 1 ;

SPREAD_MULTIPLIER=0;
SD_PERCENT=0;

COST_PER_CRORE = 1457.08;


CRUDEOIL=100;


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

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


SetOption( "MaxOpenPositions", maxpositions );


//************************************************************************
// GENERAL TRADE VARIABLES
//************************************************************************
STOPLOSS_AMOUNT = ( TRADING_CAPITAL / 100 ) * STOPLOSS_PERCENT;
//BAR_RANGE=1;

//PositionSize = 0;
SETUP_OPEN = ( Ref( Open, -1 ) );
SETUP_CLOSE = ( Ref( Close, -1 ) );

PREV1_CLOSE = ( Ref( Close, -2 ) );



SETUP_HIGH = ( Ref( High, -1 ) );

SETUP_LOW = ( Ref( Low, -1 ) );

SETUP_AVG = (SETUP_HIGH + SETUP_LOW)/2;


SETUP_BAR_RANGE = abs( SETUP_HIGH - SETUP_LOW ) ;
SETUP_BAR_SIZE = abs( SETUP_HIGH - SETUP_LOW ) ;
SETUP_BAR_RANGE_PERCENT = ( 100 * SETUP_BAR_RANGE ) / SETUP_HIGH;





///////////////////
TRIGGER_OPEN = ( Ref( Open, 0 ) );

TRIGGER_LOW = ( Ref( Low, 0 ) );
TRIGGER_HIGH = ( Ref( High, 0 ) );

ENTRY_HIGH1 = ( Ref( High, -1 ) );
ENTRY_HIGH =ENTRY_HIGH1 + 0.05   ;

ENTRY_LOW1 = ( Ref( Low, -1 ) );
ENTRY_LOW = ENTRY_LOW1 -0.05   ;


ENTRY_OPEN = ( Ref( Open, -1 ) );
ENTRY_CLOSE = ( Ref( Close, -1 ) );

PREVIOUS_HIGH = ( Ref( High, -2 ) );
PREVIOUS_LOW = ( Ref( Low, -2 ) );



//TIME = TimeNum();
TIME = Ref ( TimeNum(), 0 );





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



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





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


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



///////////
LOTUNIT = VarGet( SCRIP_NAME );

POINT_VALUE_AMOUNT = ( STOPLOSS_AMOUNT / SETUP_BAR_RANGE  );
TRADE_LOT = round( abs( POINT_VALUE_AMOUNT / LOTUNIT ) );
//TRADE_LOT = 1 ;

REAL_LOT = ( abs( POINT_VALUE_AMOUNT / LOTUNIT ) );
RISK_AMOUNT = ( TRADE_LOT * STOPLOSS_AMOUNT ) / REAL_LOT;

PREVIOUS_HIGH0 = ( Ref( High, - 1 ) );
PREVIOUS_LOW0 = ( Ref( Low, - 1 ) );

PREVIOUS_HIGH1 = ( Ref( HIGH, - 2 ) );
PREVIOUS_LOW1 = ( Ref( LOW, - 2 ) );



////////////////////////SHORT VARIABLES/////////////////////////////////
S_TRIGGER_PRICE = SETUP_LOW - 0 ;


S_ENTRY_PRICE = S_TRIGGER_PRICE ;
S_STOPLOSS_PRICE = S_TRIGGER_PRICE + SETUP_BAR_RANGE ;
//S_TARGET_PRICE_FRACTION = S_TRIGGER_PRICE - ( TARGET_RATIO * SETUP_BAR_RANGE  );
//S_TARGET_REMINDER = S_TARGET_PRICE_FRACTION % 0.05;
S_TARGET_PRICE = S_TRIGGER_PRICE - (TARGET_RATIO*SETUP_BAR_RANGE) ;
S_POINT_VALUE_AMOUNT = ( STOPLOSS_AMOUNT / SETUP_BAR_RANGE  );
S_TRADE_LOT = round( abs( S_POINT_VALUE_AMOUNT / LOTUNIT ) );
//S_TRADE_LOT = 1 ;
S_REAL_LOT = ( abs( S_POINT_VALUE_AMOUNT / LOTUNIT ) );
S_TRADE_QUANTITY = S_TRADE_LOT * LOTUNIT;
S_RISK_AMOUNT = ( S_TRADE_LOT * STOPLOSS_AMOUNT ) / S_REAL_LOT;


///////////////////LONG VARIABLES/////////////////////
L_TRIGGER_PRICE = SETUP_HIGH + 0 ;

L_ENTRY_PRICE = L_TRIGGER_PRICE ;
L_STOPLOSS_PRICE = L_TRIGGER_PRICE - SETUP_BAR_RANGE ;
//L_TARGET_PRICE_FRACTION = L_TRIGGER_PRICE + ( TARGET_RATIO * SETUP_BAR_RANGE  );
//L_TARGET_REMINDER = L_TARGET_PRICE_FRACTION % 0.05;
//L_TARGET_PLUS = IIf( L_TARGET_REMINDER == 0, 0, 0.05 );
L_TARGET_PRICE = L_TRIGGER_PRICE + (TARGET_RATIO*SETUP_BAR_RANGE) ;
L_POINT_VALUE_AMOUNT = ( STOPLOSS_AMOUNT / SETUP_BAR_RANGE  );
L_TRADE_LOT = round( abs( L_POINT_VALUE_AMOUNT / LOTUNIT ) );
//L_TRADE_LOT = 1 ;
L_REAL_LOT = ( abs( L_POINT_VALUE_AMOUNT / LOTUNIT ) );
L_TRADE_QUANTITY = L_TRADE_LOT * LOTUNIT;
L_RISK_AMOUNT = ( L_TRADE_LOT * STOPLOSS_AMOUNT ) / L_REAL_LOT;

/////////////////////////////////////////////////////////////

TRIGGER_CLOSE = ( Ref( Close, 0 ) );
TRIGGER_OPEN = ( Ref( Open, 0 ) );
//--------------------------------------------
SETUP_CLOSE = ( Ref( Close, -1 ) );
SETUP_Open = ( Ref( Open, -1 ) );



SETUP_HIGH = ( Ref( High, -1 ) );

SETUP_LOW = ( Ref( Low, -1 ) );


////////////////////////////////////

PREVIOUS3_HIGH = ( Ref( High, -4 ) );
PREVIOUS3_LOW = ( Ref( Low, -4 ) );

PREVIOUS3_BAR_RANGE = abs( PREVIOUS3_HIGH - PREVIOUS3_LOW );

//PREVIOUS3_BAR_LOWEXCESS = abs( OUTSIDE_LOW - PREVIOUS3_LOW );
//PREVIOUS3_BAR_HIGHEXCESS = abs(PREVIOUS3_HIGH - OUTSIDE_HIGH );

PREVIOUS3_BAR_LOWEXCESS = abs(PREVIOUS3_LOW - PREVIOUS3_BAR_RANGE );
PREVIOUS3_BAR_HIGHEXCESS = abs(PREVIOUS3_HIGH + PREVIOUS3_BAR_RANGE );

////////////////////////////////////
SETUP_MID = (SETUP_HIGH + SETUP_LOW)/2;
//--------------------------------------


//--------------------------------------


PREVIOUS_CLOSE1 = ( Ref( Close, -2 ) );

PREVIOUS_HIGH1 = ( Ref( High, -2 ) );

PREVIOUS_LOW1 = ( Ref( Low, -2 ) );

PREVIOUS_MID1 = (PREVIOUS_HIGH1 + PREVIOUS_LOW1)/2;

//------------------------------------------


//---------------------------------------------
//SETUP_BAR_RANGE = ( SETUP_HIGH - SETUP_LOW ) + 0.10;
SETUP_BAR_RANGE_PERCENT = ( 100 * SETUP_BAR_RANGE ) / SETUP_HIGH;



    



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







//////////////////////////////////////////////////
///////////////// RSI STARTS /////////////////////
//////////////////////////////////////////////////

RSI_INDICATOR = RSI(RSI_PERIODS) ;

RSI_TRIGER = ( REF( RSI_INDICATOR, - 0 ) );
RSI_SETUP = ( REF( RSI_INDICATOR, - 1 ) );
RSI_PREVIOUS_1 = ( REF( RSI_INDICATOR, - 2 ) );
RSI_PREVIOUS_2 = ( REF( RSI_INDICATOR, - 3 ) );
RSI_PREVIOUS_3 = ( REF( RSI_INDICATOR, - 4 ) );
RSI_PREVIOUS_4 = ( REF( RSI_INDICATOR, - 5 ) );
RSI_PREVIOUS_5 = ( REF( RSI_INDICATOR, - 6 ) );
 
#3
my backtesting afl second half
Code:
Short_CONDITION =

    (


        TIME > TRADE_START_TIME// 5

        AND

        TIME < TRADE_END_TIME// 6




AND

TRIGGER_OPEN  <= SETUP_HIGH

AND

TRIGGER_OPEN  >= SETUP_LOW



AND         

TRIGGER_HIGH < SETUP_HIGH// 9   // INITIATE CONDITION

AND

PREVIOUS_HIGH1 < SETUP_HIGH// 7   // INITIATE CONDITION

AND

PREVIOUS_LOW1 < SETUP_LOW

AND     

TRIGGER_LOW <  SETUP_LOW





AND

(RSI_SETUP < Short_RSI_SETUP_VALUE) // IMPORTANT FILTER

 





      ) ;

                

        

Buy_CONDITION =


    (


        TIME > TRADE_START_TIME// 5

        AND

        TIME < TRADE_END_TIME// 6


AND

TRIGGER_OPEN  <= SETUP_HIGH

AND

TRIGGER_OPEN  >= SETUP_LOW


AND     

TRIGGER_LOW > SETUP_LOW// 9   // INITIATE CONDITION

AND

PREVIOUS_LOW1 > SETUP_LOW// 7   // INITIATE CONDITION

AND

PREVIOUS_HIGH1 > SETUP_HIGH   

AND   

TRIGGER_HIGH > SETUP_HIGH



 

AND

(RSI_SETUP > LONG_RSI_SETUP_VALUE)   // IMPORTANT FILTER

                    






      ) ;     

              


global Last_Bar_Number;

Initiated_Bar_Number =0;

global Same_Bar_Number;

global Different_Bar_Number;



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

//i = LastValue( BarIndex() );

{


LastBarNumber = i;






//_TRACE( "SCAN: LAST VALUE OF BARINDEX: =" + i );





    if(   Short_TRADE_ON==0 AND   Short_CONDITION[ i ] == True   )

    {   

        Short_TRADE_ON = 1;

        S_EBARNUM = i;

        Initiated_Bar_Number = i;

        S_stopprice[ i ] = S_STOPLOSS_PRICE[i] ;

        S_targetprice[ i ] = S_TARGET_PRICE[i];

        BAR_RANGE[i] = SETUP_BAR_RANGE[i] ;

        NO_OF_SHARES[ i ] = STOPLOSS_AMOUNT / BAR_RANGE[i];

        SHORT_POSITION_VALUE[ i ] = NO_OF_SHARES[ i ] * ENTRY_LOW[ i ];

        ShortPrice[ i ] = S_ENTRY_PRICE[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_EBARNUM = i;

        Initiated_Bar_Number = i;

        L_stopprice[ i ] = L_STOPLOSS_PRICE[i] ;

        L_targetprice[ i ] = L_TARGET_PRICE[i];

        BAR_RANGE[i] = SETUP_BAR_RANGE[i] ;

        NO_OF_SHARES[ i ] = STOPLOSS_AMOUNT / BAR_RANGE[i];

        LONG_POSITION_VALUE[ i ] = NO_OF_SHARES[ i ] * ENTRY_HIGH[ i ];

        BuyPrice[ i ] = L_ENTRY_PRICE[i];

        PositionSize[ i ] = LONG_POSITION_VALUE[ i ];

        Buy[ i ] = 1;

    }


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




///////////////////////////////////////////////////


    TARGET_Cover_condition = Low[ i ]   < S_targetprice[ S_EBARNUM ];

    STOP_Cover_condition =  High[ i ]  > S_stopprice[ S_EBARNUM ];

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

    TARGET_Sell_condition = High[ i ] > L_targetprice[ L_EBARNUM ];

    STOP_Sell_condition =  Low[ i ] < L_stopprice[ L_EBARNUM ];


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


    if( Short_TRADE_ON == 1 AND TARGET_Cover_condition[ i ] == True  )

    {

        Cover[ i ] = 1;

        CoverPrice[ i ] = S_targetprice[ S_EBARNUM ];

      

        Short_TRADE_ON = 0;

    }


    else

        if( Short_TRADE_ON == 1 AND STOP_Cover_condition[ i  == True ] )

        {

            Cover[ i ] = 1;

            CoverPrice[ i ] = S_stopprice[ S_EBARNUM ];

            Short_TRADE_ON = 0;

        }

        else

            if( Short_TRADE_ON == 1 AND TIME[i] > SQUARE_OFF_TIME )

            {

                Cover[ i ] = 1;

                CoverPrice[ i ] = Avg[i];

                Short_TRADE_ON = 0;

            }

            else

            {

                Cover[ i ] = 0;

            }


//}


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


    if( Long_TRADE_ON == 1 AND TARGET_Sell_condition[ i ] == True  )

    {

        Sell[ i ] = 1;

        SellPrice[ i ] = L_targetprice[ L_EBARNUM ];

        Long_TRADE_ON = 0;

    }


else

    if( Long_TRADE_ON == 1 AND  STOP_Sell_condition[ i ] == True  )

    {

        Sell[ i ] = 1;

        SellPrice[ i ] = L_stopprice[ L_EBARNUM ];

        Long_TRADE_ON = 0;

    }

    else

        if( Long_TRADE_ON == 1 AND  TIME[i] > SQUARE_OFF_TIME )

        {

            Sell[ i ] = 1;

            SellPrice[ i ] = Avg[i];

            Long_TRADE_ON = 0;

        }

        else

            {

                Sell[ i ] = 0;

            }

}


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

pannet1

Well-Known Member
#6
Hi Friends,

After lot of head ache I came to this decision of posting here.

My problem:
I am using hourly timeframe in backtester settings.
My trade entry times are shown as 10 am,11am,12pm,1pm,2pm,3pm like this.
What i want is my trades should be entered when at the time of trade triggered in 1 min interval.
Instead of 11am which is bar close time i want the exact time like 10.43 am.

Also all my trades are exiting in full hour time only. That is close of exit bar.

So I want intrabar entry and intrabar exit in hourly timeframe.
Is this possible.

If possible please modify below backtesting code which is not working for min by min backtesting.
Don't tell me to try hard . I already tried it very hardly and not able to succeed. I think it is not possible too.
i dont know if this will be of help or i undertood your problem properly

if you keep the timeframe in backtester settings as 1 Min and find the Hourly OHLC with AFL code will that help?
 

Similar threads

Broker Special Offers

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Name:Phone:
Email:City:
State:
Are you a day trader?