# Screening based on Volume and OI Data and take position based on Price movement

Dear Forum Members,

I am learning AFL programming and progressing gradually. I have been working on my requirement which is mentioned as below:

1. Select Future stocks based on Volume & OI data
2. Convert the Future stocks to Cash market ( As i wanted to trade on stocks in cash market which has high Volume and OI )
3. Categorise and create 2 watchlist,, one for Buy and one for sell based on..
-- Increase in price and Increase in Volume and Increase in OI --- for Buy Watchlist
-- Decrease in Price and Increase in Volume and Increase in OI -- for Sell Watchlist

*Note : STocks once created in watchlist will remain in the watchlist even there is change in V, OI or price

1. Take an entry on Buy side only from the stocks in Buy watchlist and Short entry from the stocks that are in Sell watchlost .. as soon as they are listed in the watchlist..

I am using Daily Timeframe for scanning the stocks and sorting them into watchlist.. So far worked up on code is mentioned below

``````_SECTION_BEGIN("Stocks Screening");

PCP = Param("PCP",0.5,-10,100);
PCV = Param("PCV",5,0,100);
PCO = Param("PCO",1,0,100);

A1 = (C-Ref(C,-1))*100/Ref(C,-1);

A2 = (V - MA(V,20))*100/V ;

A3 = (V-Ref(V,-1))*100/Ref(V,-1);

A4 = (OI - Ref(OI,-1))*100/Ref(OI,-1);

A = C > 100 AND C < 2000;

Buy = (A1 > PCP AND A3 > PCV AND A4 > PCO ) AND A ;
Sell = (A1 < -PCP AND A3 > PCV AND A4 > PCO ) AND A ;

InWatchList( 67 );
Symbol = Name();
Symbol = StrTrim(StrReplace(Name(),"-FUT",""),"",2);

_SECTION_END();

_SECTION_BEGIN("Bullish Stocks");

F1 = Filter = ( Buy )  ;
listnum = 1;    // watchlist to use

if ( Status( "stocknum" ) == 0 )
{
oldlist = CategoryGetSymbols( categoryWatchlist, listnum );

Symbol = Name();
Symbol = StrTrim(StrReplace(oldlist,"-FUT",""),"",2);

for ( i = 0; ( sym = StrExtract( Symbol, i ) ) != ""; i++ );

}

if ( LastValue( Cum( Filter AND Status( "barinrange" ) ) )  )

_SECTION_END();

_SECTION_BEGIN("Bearish Stocks");

F2 = Filter = ( Sell ) ;
listnum = 2;    // watchlist to use

if ( Status( "stocknum" ) == 0 )
{
oldlist = CategoryGetSymbols( categoryWatchlist, listnum );

for ( i = 0; ( sym = StrExtract( oldlist, i ) ) != ""; i++ );

}

if ( LastValue( Cum( Filter AND Status( "barinrange" ) ) ) )

_SECTION_END();

_SECTION_BEGIN("All Stocks");

F3 = Filter = ( Buy OR Sell ) ;
listnum = 3;    // watchlist to use

if ( Status( "stocknum" ) == 0 )
{
oldlist = CategoryGetSymbols( categoryWatchlist, listnum );

Symbol = Name();
Symbol = StrTrim(StrReplace(oldlist,"-FUT",""),"",2);

for ( i = 0; ( sym = StrExtract( oldlist, i ) ) != ""; i++ );

}

if ( LastValue( Cum( Filter AND Status( "barinrange" ) ) ) )

_SECTION_END();
``````

What is achieved so far -

1. I am able to scan future stocks based on Price, Volume and OI
2. Able to convert them to Cash market stocks
3. Able to create 2 different watchlist for Buy and Sell respectively

What I am looking for -

1. Place Buy or Sell order for the stocks that are added to the respective watchlist with a definite target %.. and SL %

Issue part:

I do have various algos that actually work on list of stocks which are constant.. but stocks are screened based on the price, volume and oi, the existing algos are failing to generate a fresh signal once the stock is created in the watchlist. Many times, the entries are way before the stocks being listed..

Any suggestion or guidance on the code part is very much appreciated.. Thanks in advance..

Hey CodeJunkie,

Thanks for the referal link... I am yet to find what I was looking for.. I want to automate the orders as soon as a stock listed in respectiive watchlist with TP & SL.. any help?

If I understand correctly you want to place your order directly to exchange once your buy and sell signal is generated.

If that's the case which broker terminal are you using as Amibroker officially only support Interactive Broker, however if you are using any other broker terminal then you can achieve it either by window scripting or through API (if your broker support).

However before doing so why don't you paper trade and simulate your strategy on csv file in real time market as there is no snippet written for same so that you can validate whether your strategy works on real time or not as backtest and real time auto trade give contrast results on same strategy.

Also you are using daily time frame then it is only going to be true at the end of the day so if you are looking for your algo to generate buy and sell during the market it wouldn't do till the time market is not close and your new daily candle is not formed.

Anant...

Let me rephrase my concern...

I want to automate the entries as soon as a stock is listed in respectiive watchlist with TP & SL.. So, to put it in other words, my scanner sorting becomes my Buy or Sell logic for trade entries..

I do trade manually so quite comfortable to take my strategy live... Thanks for the advice though..

Any help?

Try below i have not tested the code as i don't have OI symbol to test but the code is not throwing any syntax error, so may be it can help you.

For TL and SL search the forum many examples are available and add it to your Buy_cond.

``````_SECTION_BEGIN("Stocks Screening");

PCP = Param("PCP",0.5,-10,100);
PCV = Param("PCV",5,0,100);
PCO = Param("PCO",1,0,100);

A1 = (C-Ref(C,-1))*100/Ref(C,-1);

A2 = (V - MA(V,20))*100/V ;

A3 = (V-Ref(V,-1))*100/Ref(V,-1);

A4 = (OI - Ref(OI,-1))*100/Ref(OI,-1);

A = C > 100 AND C < 2000;

Buy_cond = (A1 > PCP AND A3 > PCV AND A4 > PCO ) AND A ;
Sell_cond = (A1 < -PCP AND A3 > PCV AND A4 > PCO ) AND A ;

InWatchList( 67 );
Symbol = Name();
Symbol = StrTrim(StrReplace(Name(),"-FUT",""),"",2);

_SECTION_END();

_SECTION_BEGIN("Bullish Stocks");

F1 = Filter = ( Buy_cond )  ;
listnum = 1;    // watchlist to use

Bullish_Stocks = 0;
if ( Status( "stocknum" ) == 0 )
{
oldlist = CategoryGetSymbols( categoryWatchlist, listnum );

Symbol = Name();
Symbol = StrTrim(StrReplace(oldlist,"-FUT",""),"",2);
Bullish_Stocks = 1;

for ( i = 0; ( sym = StrExtract( Symbol, i ) ) != ""; i++ );

}

if ( LastValue( Cum( Filter AND Status( "barinrange" ) ) )  )

_SECTION_END();

_SECTION_BEGIN("Bearish Stocks");

Bearish_Stocks= 0;
F2 = Filter = ( Sell_cond );
listnum = 2;    // watchlist to use

if ( Status( "stocknum" ) == 0 )
{
oldlist = CategoryGetSymbols( categoryWatchlist, listnum );
Bearish_Stocks =1;
for ( i = 0; ( sym = StrExtract( oldlist, i ) ) != ""; i++ );

}

if ( LastValue( Cum( Filter AND Status( "barinrange" ) ) ) )

_SECTION_END();

_SECTION_BEGIN("All Stocks");

All_Stocks = 0;
F3 = Filter = ( Buy_Cond OR Sell_Cond ) ;
listnum = 3;    // watchlist to use

if ( Status( "stocknum" ) == 0 )
{
oldlist = CategoryGetSymbols( categoryWatchlist, listnum );
Symbol = Name();
Symbol = StrTrim(StrReplace(oldlist,"-FUT",""),"",2);
All_Stocks = 1;
for ( i = 0; ( sym = StrExtract( oldlist, i ) ) != ""; i++ );

}

if ( LastValue( Cum( Filter AND Status( "barinrange" ) ) ) )

Sell = Sell_cond && (Bearish_Stocks OR All_Stocks);

_SECTION_END();
``````
1 Like

Thanks a lot Anant..

Will give it a try... however looking at the code, I am wondering if this can place a buy or sell order as soon as a stock is listed in either watchlist 1 or 2 respectively...

Hey Anant,

Another question in addition to the above..

I want the scan condition part to be on ..lets say a higher time frame like 15 m but the execution trade entry should be on lower timeframe like 5 or 3 mins..from each watchlist after getting listed ( post meeting the scan condition ) ..

For ex,.. Scan part to list stocks in respective watchlist is being done using 15 min TF and once listed for the order placement, each of the listed stock should be able to check another condition on 3 mins TF...

Can you help me on this?

Take a look at TimeFrameCompress(), TimeFrameExpand(), and other TimeFrame* functions.

You can achieve it through timeframeset and then expand link there is a nice solution given by @fxshrat in in below

``````TimeFrameSet(in15Minute);
A1 = (C-Ref(C,-1))*100/Ref(C,-1);
A2 = (V - MA(V,20))*100/V ;
A3 = (V-Ref(V,-1))*100/Ref(V,-1);
A4 = (OI - Ref(OI,-1))*100/Ref(OI,-1);
A = C > 100 AND C < 2000;
TimeFrameRestore();

A1 = TimeFrameExpand(A1,in15Minute);
A2 = TimeFrameExpand(A2,in15Minute);
A3 = TimeFrameExpand(A3,in15Minute);
A4 = TimeFrameExpand(A4,in15Minute);
A = TimeFrameExpand(A,in15Minute);
``````

Hi Anant,

I have been testing the suggested since couple of days... first and foremost, i am able to get half results after implementing the timeframe functionalities.. but I still have the following issues..

1. Lets say, the OI change of SBIN which is set as 0.5 happened at 10am and as in when it happened, it got listed into our buy side watchlist...

-> Now , I just want to place a buy order for the stock with a simple check of whether close is > super trend and once confirmed place buy order...

The issue part is, Close would hv been greater than Super trend for SBIN way before it got listed into watchlist.. lets say Close > SuperTrend since 9:30 but where as watchlist creation for SBIN happened at 10 am... I am missing this entry..

I wanna rectify this...

What I actually need is, once the watchlist creation, listed stocks should have a check wthr they are > or < than ST from the time it entered watchlist ...

I am unable to emphasize the fact that > or < should be verified only after the time of watchlist entry...

kindly suggest

Just an other pointer..

Entry time for any stocks should be the time it get listed in the watchlist.. so that I wont miss an entry..

Currently my entry time rules are as follows: