Trade from Watchlist

I added option chain in the watchlist. I want to trade one of the options on predefined conditions. I am not able to get the result (buy the put/call option) in either exploration/backtesting. Please guide me.

listname = "NIFTY18DEC";
category = categoryWatchlist;
listnum = CategoryFind( listname, category );
list = CategoryGetSymbols( category, listnum);
Call = "NIFTY18DEC" + (round(C /100)+ 5 )*100 + "CE";
Put =  "NIFTY18DEC" + (round(C /100)- 5 )*100 + "PE";

for( n = 0; ( symbol = StrExtract( list, n ) ) != ""; n++ )
{
  	if ( buycall[i-1] == 1 && symbol == Call)
	{
		AddTextColumn(Symbol, "Symbol");
		SetForeign(symbol);
		Buy = timeok;
	    RestorePriceArrays();
	}	
	else if ( Buyput[i-1] == 1 && symbol == Put)
	{
		AddTextColumn(Symbol, "Symbol");
		SetForeign(symbol);
		Buy = timeok;
        RestorePriceArrays();
		
	}
}
Sell = something
SellPrice = Open;
Short = 0;
Cover = 0;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

Where is this variable I defined ?

Have you checked if the actual output of this string matches such a symbol in DB?
Call = "NIFTY18DEC" + (round(C /100)+ 5 )*100 + "CE";

AddTextColumn(Symbol, "Symbol");
Instead of using this line multiple times in code blocks, you should use a variable that stores the string, and call this line once at the end of the code along with other Column definitions.

It will work and probably add columns but what about the various rows that may generate, anyway, its your partial code but I feel its not a good coding practice.

for( i = 0; i < BarCount; i++ ) 
{ 
total = ntot60 + ntot15 + ntot10 +ntot5;
}
Buycall = IIf( (total >= 3), 1 , 0 );
Buyput = IIf( (total <= 2), 1 , 0);

The total is calucation of indicators.

Yes Call = "NIFTY18DEC" + (round(C /100)+ 5 )*100 + "CE" is the actual match with DB.

AddtextColumn is just added to check result whether the code fetches correct symbol or not.

I am newbie. I will gain proficiency simultaneously as learning is never ending process. But I am stuck with this part. I had gone through various KBs but not able to get how to go forward.

Edit: my bad. post didn't show up correctly. just ignore this post.

Let me review it once. But i'd rather you post a full working code.

In this bit of code

      SetForeign(symbol);
      Buy = timeok;
      RestorePriceArrays();

can you explain what you are trying to do?

By setting a variable in Foreign scope will not change the variable for that symbol, this is what I think you're trying ?

I dont know whether it is correct or not. I am facing problem in this part only. the basic idea what I want to do as follows:

We will search particular symbol in list, if we find a match then we will buy that particular symbol

You should read this Topic in details.
You can't trade a foreign symbol, like the way you have written. All trades are for current symbol.

1 Like

Gone though KB. I am doing the inverse of what is given.

So What I am doing is not possible ?? Should I search for any other platform ??

Problem Is not with AB, but what you are doing will not happen that way as of now. There are logical reasons for it.

You have to design your code in the way described if you want to Backtest it.

If you are trading live and just want to fire a signal for example, then you can call your Brokers API from that IF() and fire a trade right away but these are two very different things.

There are more code examples in the Forum, but you need to write a CBT (Mid-level atleast) and that is fairly advanced.


Multi symbols discussed here as well.

1 Like

Thanks for your insights.

One more thing, Suppose in multi-stock trading if we purchase one stock out of 5 stocks on t day.
How to code if we want to sell that particular stock on very next day ie t+1 as soon as market open.

Read here
www.amibroker.com/guide/h_portfolio.html

All the common scenarios are described.
In your case, look for an example with something like this: it should work
SetOption( "HoldMinBars", 1 );

1 Like

Reworked on the whole strategy try to execute trade in exploration mode :

  1. Is this SetOption( "HoldMinBars", 1 ); work in exploration also. I want to sell the stock which i purchased in the previous day.
  2. If I want to generate buy signal in the specific time frame will following works ??
tn = TimeNum();
startTime = 120000; // start in HHMMSS format
endTime = 123000;  // end in HHMMSS format
Opentime  = 091700;
timeOK = tn >= startTime AND tn <= endTime;

Q2 is fine, you can AND it with the Buy signal code.

Q1. This will create problems because now you are in intraday timeframe and you are not back testing either.
1 day and 1 bar in intra TF are two very different things.

Also, hold min N bars will not make much sense in an intra TF because there are very many bars.

Because you have changed your strategy and therefore your code, I don't have a specific context but its better to go with a more generalized approach.

newDay = Day() != Ref( Day(), -1);

barsSinceToday = BarsSince( newDay );

barsSinceBuy   = BarsSince( Buy == 1 );

If barsSinceBuy > barsSinceToday, you know its a previous day (or more). You will have finer control as you need and any intra TF wont matter or need adjustment instead of hardcoding number of bars.

3 Likes