Kindly support for get the ATM option symbol when the entry time

Dear Experts,

Kindly help me for getting the At the money(ATM) option symbol when the entry conditions are met.

currently it is working the ATM symbol for each bar, but i required the symbol when the entry conditions are met.
kindly suggest the alternate solution for getting the text value like as value when function.

Capture

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
SetPositionSize(25,spsShares);
SetBarsRequired(-2,-2);

_SECTION_BEGIN("Strike Selection");

///section begins for options; 
bsym= ParamStr("Base Symbol", "BANKNIFTY");
expiry= ParamStr("Expiry(dd-mm-yy)","WK");
strikedist= Param("ATM Strike Dist",0, -1000, 1000);

Entry = TimeNum() ==093000;
Exit = TimeNum() ==151500;

SetForeign("BANKNIFTY");
BD = O;
RestorePriceArrays();
for(i=0;i<BarCount;i++)
{
ltp = BD[i];
ATMCE= round(ltp/100)*100 + strikedist;
ATMPE= round(ltp/100)*100 - strikedist;
VarSetText("ATMCE"+i,bsym + expiry +StrFormat("%.0f",ATMCE)+"CE");
VarSetText("ATMPE"+i,bsym + expiry +StrFormat("%.0f",ATMPE)+"PE");
}
printf("\nATM pstrike : "+ VarGetText("ATMPE"+BarIndex()));
printf("\nATM cstrike : "+ VarGetText("ATMCE"+BarIndex()));

Csym = ValueWhen(entry,VarGetText("ATMCE"+BarIndex()));
Psym = ValueWhen(entry,VarGetText("ATMPE"+BarIndex()));

SetForeign("csym");
COP= O;
CHP=H; 
CLP=L; 
CCP = C;
RestorePriceArrays();
SetForeign("Psym");
POP= O; 
PHP=H; 
PLP=L; 
PCP = C;
RestorePriceArrays();

First you are using mutliple timeframe functions incorrectly. You need to read Multiple Time Frame support (especially text in orange boxes). In your code you should be using TimeFrameEXPAND as docs say.

Dear @Tomasz
Thank you for reply, currently i am learning the afl,
The indian market opening time is 091500, i need to get atm symbol value at 09300
can you please help me with the code,

i have tried with time frame expand function as like below, still the value when cant able use it here,
Capture

@balakrishnannatesan you don't need a bar by bar process to calculate the atm, your can simply try the below snippet.

You even don't need any varget or varset.

_Time	= 093000;
tn = TimeNum();
dn = DateNum();
newday = dn != Ref( dn, -1 );
CP = Foreign( "BANKNIFTY", "C" );
strikedist = 100;

OP_atm = Round( ValueWhen( newDay, CP ) / strikedist ) * strikedist; // Newday Opening Atm
atm = Round( CP / strikedist ) * strikedist; // Current Price Atm
atmattime = ValueWhen( tn == _Time, atm ); // Atm at particular time

Dear @deepuanant ,

Thank you for support,
Previously i have tried with this option, but while back testing it will take last day symbol only as shown in image, so only i have tried with for loop function.

and also while debugging in afl, it is not showing the strike value, can you help me with the back testing code.
Untitled

@balakrishnannatesan why did you change the foreign array from close to open there will be no candle starting at 9.30 it will be completed candle hence you need to access the close price any intraday new candle which will be forming will have the open time of 093001?

Also you dont need the setforeign to acces the price field of you custom symbol you can directly access the same through foreign function.

@deepuanant , HI, if the i change the foreign array value as close price also getting the same error.

can you help me for resolving this issue.
Untitled

here is the complete snippet.

_Time	= 093000;
tn = TimeNum();
dn = DateNum();
newday = dn != Ref( dn, -1 );
CP = Foreign( "BANKNIFTY", "C" );
strikedist = 100;

OP_atm = Round( ValueWhen( newDay, CP ) / strikedist ) * strikedist; // Newday Opening Atm
atm = Round( CP / strikedist ) * strikedist; // Current Price Atm
atmattime = ValueWhen( tn == _Time, atm ); // Atm at particular time

Symbol = StrReplace( Name(), "-FUT", "" );
//Symbol = StrReplace( Name(), "-FUT", "WK" );

CustomSym = Symbol + atm + "CE";
_TRACE( "CUSTOM SYMBOL : " + CustomSym);

// Call Option Price for Calculation
iCEC = ValueWhen( tn == _Time, Foreign( Symbol + atm + "CE", "C" ));
iPEC = ValueWhen( tn == _Time, Foreign( Symbol + atm + "PE", "C" ));

_TRACE( "ATM AT NEW DAY OPEN : " + OP_atm);
_TRACE( "ATM AT CLOSE : " + atm);
_TRACE( "ATM AT 9:30 : " + atmattime);
_TRACE( "ATM CE Close : " + iCEC);
_TRACE( "ATM PE Close : " + iPEC);

ABC

1 Like

Dear @deepuanant ,

Thank you for your effort, can you please help with exploration value, because still in my system, the trading symbol is same for last 10 days and please find the afl code here.

and it is working fantastic in the chart window ,but i cant able to bactest due to same symbol in the exploration window.

can you please help me to resolve this issue

01

_Time	= 093000;
tn = TimeNum();
dn = DateNum();
newday = dn != Ref( dn, -1 );
CP = Foreign( "BANKNIFTY", "C" );
strikedist = 100;
Plot( C, "Price", colorDefault, styleCandle );

OP_atm = Round( ValueWhen( newDay, CP ) / strikedist ) * strikedist; // Newday Opening Atm
atm = Round( CP / strikedist ) * strikedist; // Current Price Atm
atmattime = ValueWhen( tn == _Time, atm ); // Atm at particular time

SYMBOL = "BANKNIFTY";
CE_Symbol = Symbol + "WK" + atmattime + "CE";
PE_Symbol = Symbol + "WK" + atmattime + "PE";

// Call Option Price for Calculation
iCEC = ValueWhen( tn == _Time, Foreign( Symbol + "WK" + atm + "CE", "C" ) );
iPEC = ValueWhen( tn == _Time, Foreign( Symbol + "WK" + atm + "PE", "C" ) );

_TRACE( "ATM AT NEW DAY OPEN : " + OP_atm );
_TRACE( "ATM AT CLOSE : " + atm );
_TRACE( "ATM AT 9:30 : " + atmattime );
_TRACE( "ATM CE Close : " + iCEC );
_TRACE( "ATM PE Close : " + iPEC );

///Option trading condition for Set No :1
Short_CE_1 =  Name() == CE_Symbol;
Short_PE_1 = Name() == PE_Symbol;
ShortPrice_CE_1 = ValueWhen( Short_CE_1, iCEC );
ShortPrice_PE_1 = ValueWhen( Short_PE_1, iPEC );

Cover_CE_1 = TimeNum() >= 151500;
Cover_PE_1 = TimeNum() >= 151500 ;

Short = Short_CE_1 OR Short_PE_1;
Cover = Cover_CE_1 OR Cover_PE_1;

Filter = Short AND TimeNum() == 093000;
AddColumn( iCEC, "Open CE option" );
Premium = iCEC + iPEC;

I have also tried backtest for option symbol but still not succeed.
I think there is no support for backtest with variable symbol on each bar.

In general terms we can't store array of string in memory unless we use matrix of characters and I think still character matrix is not supported in amibroker
I am trying with the following code but as per the analysis window it uses the last option symbol for the whole period which should be different on diff days (ATM Option on each day):

/*
I will buy atm call option when the market open and sell at the last candle open when the market close
I want to backtest this system using amibroker backtester
*/

STRST = 100;
BSMBL = "BANKNIFTY";
EXPIRD = "21818";

TN = TimeNum();
DN = DateNum();

FC = TN==91500;
LC = TN==151500;

Buy = FC;
Sell = LC;

ATMSTRIKE = ValueWhen(Buy, round(O/STRST)*STRST, 1);
CESYM = BSMBL + EXPIRD + NumToStr(ATMSTRIKE, 1.0, False) + "CE";

BuyPrice = ValueWhen(Buy, Foreign(CESYM, "O", False));
SellPrice = IIf(Sell, Foreign(CESYM, "O", False), Null);

Filter = 1;
AddTextColumn(CESYM, "CE SYMBOL", 77);
AddColumn(Buy, "Buy", 1.0);
AddColumn(Sell, "Sell", 1.0);
AddColumn(BuyPrice, "BuyPrice", 1.2);
AddColumn(SellPrice, "SellPrice", 1.2);

Analysis

What you wrote is nonsense. You don't know how to use backtesting. You have a lot to learn:

  1. Backtest is NOT exploration Scan vs Exploration vs Backtest - summary of differences

  2. You don't need arrays of strings because this is NOT how backtest works at all (you dont' use Foreign!!!)

  3. To backtest multiple symbols you have to PUT ALL those symbols into WATCH LIST (or use "All symbols" Apply To Setting"), then you need to code buy sell rules so CURRENT symbol (one of those in "Apply to" gets traded). You do NOT use Foreign and you should NOT set BuyPrice to Foreign price. That is all wrong.

Read the documentation first
http://www.amibroker.com/guide/h_backtest.html

If you have different rules for different symbols you code this like shown here:

n = Name(); // get the symbol

if( n == "SYMBOL1" )
{
  // the "Close" price here is Symbol1 close NO FOREIGN NEEDED
  Buy = ...rules for symbol1;
  Sell = ...rules for symbol1;
}

if( n == "SYMBOL2" )
{
  // the "Close" price here is Symbol2 close NO FOREIGN NEEDED
  Buy = ...rules for symbol2;
  Sell = ...rules for symbol2;
}

See also How do I get symbol specific values to work in the backtester? - #2 by fxshrat

3 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.