How to use fgets and fopen in the same code for rotaional trading

Hi ,
I am new to amibroker and have been stuck for a while.
Would appreciate some help.

i want to run a rotational trading system on a watchlist of 30 stocks .

I do ranking based on some parameters on week 1 and calculate the quantity to buy stocks and then save the ranking as prevranking and prevqtty.

The next week i want to fget the prevranking and prevqtty.

Based on the current new week ranking i need to recalculate the qtty
and sell if the ranking for the particular stock held has gone down below 20 and if the ranking has not gone below 20 then keep holding the stock.

if some new stocks have come which are below or equal to rank 15 i want to buy them.

Then i want to fput the prevranking, prevqtty, newranking and newqtty.

i need to do this every week .
Thanks.

EnableRotationalTrading();
include = 0;


Maxpos = 15;
Worstrank = Maxpos + 5;
Leverage = 1;
Initialcap = 500000;


SetOption("Maxopenpositions",Maxpos);
SetOption("Worstrankheld",Worstrank);
SetOption("Initialequity",Initialcap);

Lb = 12;
Lti = ROC(Close,Lb);

PositionSize = -100/worstrank;
PositionScore = Max(Lti,0); 
allocation = Initialcap/ Maxpos ;
afl = "SNQGF";

//RANKING CODE
watchlist = 0;
List = CategoryGetSymbols(categoryWatchlist,watchlist);

if ( Status("stocknum") == 1 ) // Generate ranking when we are on the very first symbol
{
	StaticVarRemove( "qtty*" );

	for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    )
	{
		SetForeign ( symbol );
		
		
		lb = 12; 
		lti = ROC(Close,lb);
		
		PositionSize = -100/worstrank;
		PositionScore = Max(Lti,0); 
		
		allocation = initialcap / maxpos;
		qtty = allocation / Close  ;
		qttycal = round(qtty);
		
		RestorePriceArrays();
		
		StaticVarSet ("lti" + symbol ,lti);
		_TRACE( symbol );
     }
	StaticVarGenerateRanks( "rank", "lti", 0, 1234 );
}

symbol = Name();
newrank = StaticVarGet ( "ranklti" +  symbol );
qttycal = StaticVarGet("qtty" + symbol);
lti = StaticVarGet("lti"+ symbol);

allocation = initialcap / maxpos;
qtty = allocation / Close  ;
qttycal = round(qtty);




//////// CODE TO FGET THE PREVIOUS RANKS AND THE PREVQTTY

file = "C:\\Users\\A\\Desktop\\quotes1\\SN Quotes.csv";

fh = fopen( file, "r" );

if( fh )
 {
     while( ! feof( fh ) )
     {
         line = fgets( fh );
         sym = StrExtract( line, 0 );
		 watchlist = 0;
		 list = CategoryGetSymbols(categoryWatchlist,watchlist);
		 if(Status("stocknum")==1)
					{
						for(n = 0; (symbol = StrExtract(list,n)) !=""; n++)		
							{
								SetForeign(symbol);
         
								if ( symbol == sym )
									{
			 
			 
			 
             
										prevrank = StrToNum( StrExtract( line, 4 ) ) ;
										prevqtty = StrToNum(StrExtract(line,5));
			 
										StaticVarSet("prevrank"+symbol,prevrank);
										StaticVarSet("prevqtty"+symbol,prevqtty);
      		
										RestorePriceArrays();	 
									}
							}
					}
     }
     
     fclose( fh );
 }
 else
 {
     Error( "ERROR: file can not be opened" );
 }

prevrank = StaticVarGet("prevrank"+Name());
prevqtty = StaticVarGet("prevqtty"+Name());





Buy = prevqtty == 0 AND newrank <=15;
Sell = prevqtty >0 AND newrank >20;

newqtty = IIf(Buy==1,qttycal,IIf(Sell==1,0,prevqtty));



////// CODE TO FPUT THE CHANGED RANK AND THE UPDATED QTTY

fp = fopen("C:\\Users\\A\\Desktop\\quotes1\\SN Quotes.csv", "w" );
if(fp)
{
   
	
   
   fputs( "Ticker,Date/Time,Prevrank,Prevqtty,Newrank,Newqtty\n", fp );	
  
   watchlist = 0;
   list = CategoryGetSymbols(categoryWatchlist,watchlist);
   for ( i = 1; i <= 1; i++ )
   if(Status("stocknum")== 1)
   
   
    {
		for(n = 0; (symbol = StrExtract(list,n)) !=""; n++)		
			{
				SetForeign(symbol);
				
				y = Year(); 
				m = Month(); 
				d = Day(); 
				
				fputs( Name() + ",", fp );
				
				ds = StrFormat("%02.0f-%02.0f-%02.0f,", m, d, y ); 
				
				fputs( ds, fp );
				
				prevrank = StaticVarGet("prevrank"+Name());
				prevqtty = StaticVarGet("prevqtty"+Name());

			
				
				
				Buy = prevqtty == 0 AND newrank <=15;
				Sell = prevqtty >0 AND newrank >20;
				
				
				newrank = newrank;
				newqtty = IIf(Buy==1,qttycal,IIf(Sell==1,0,prevqtty));
				
				Line = StrFormat("%g,%g,%g,%g\n",prevrank,prevqtty,newrank,newqtty) ;
				
				fputs(line,fp);
				 
		    RestorePriceArrays();
			}
   	   		
   	   		

    }
		
fclose(fp);
		
  }


Filter =1;
AddColumn(prevrank,"prevrank");
AddColumn(prevqtty,"prevqtty");
AddColumn(newrank,"newrank");
AddColumn(newqtty,"newqtty");




@Karan, IMO, you should remove the reference to EnableRotationalTrading();.

The rotational model provided by the above function, as documented here,, does not use custom ranking, but relies on PositionScore to establish the Buy/Sell rotation:

Then all scores are sorted according to absolute value of PositionScore . Then top N are choosen to be traded. N depends on available funds and "max. open positions" setting. Then all scores are sorted according to absolute value of PositionScore . Then top N are choosen to be traded. N depends on available funds and "max. open positions" setting.

Since it appears that you want to apply your own logic, you should entirely write your own "rotational system" from scratch.

Moreover note this remark:

From then on using of buy/sell/short/cover variables is not allowed. Only PositionScore variable will be used to rank securities and trade top N securities..

I did not look/tested at your code, since as said, IMO it mixes unrelated things.

2 Likes

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