SEND signals to CSV from AFL ,whats wrong with my code

SetBarsRequired(100000,0);

GraphXSpace = 15;

SetChartOptions(0,chartShowArrows|chartShowDates);

SetChartBkColor(ParamColor("bkcolor",ColorRGB(0,0, 0)));

GfxSetBkMode(0); 

GfxSetOverlayMode(1);

SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));

Plot(C,"\nPrice",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);



//SetTradeDelays(1,1,1,1);

SetPositionSize(100,spsShares);

FirstTradeTime = 094500;				// Earliest time to take a trade
LastTradeTime = 150000;					// Latest time to take new trades
ExitAllPositionsTime = 152500;		// Exit all trades

//parameters

//10min - 3,50
//15min - 2,36

P = ParamField("Price field",-1);
per1 = Param("EMA1",2,1,20,1);
per2 = Param("EMA2",36,1,50,1);
file = "C:/emacrossover.csv" ;

//
fh = fopen( file, "r" );
//
if( fh )
{
 

Buy = Cross(EMA(C,per1),EMA(C,per2)) AND (TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime );
Sell= Cross(EMA(C,per2),EMA(C,per1)) OR  TimeNum() >= ExitAllPositionsTime;


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

Short=Sell AND (TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime );
Cover=Buy OR 	 TimeNum() >= ExitAllPositionsTime;

     while( ! feof( fh ) )
     {
         line = fgets( fh );
         // get the ticker symbol from the file
         sym = StrExtract( line, 0 );
         // if ticker matches current symbol
         if ( Name() == sym )
         {
             // extract data from line of text
             trade = StrExtract( line, 1 );
             trade_datetime = StrToDateTime( StrExtract( line, 2 ) );
             price = StrToNum( StrExtract( line, 3 ) );              
             shares = StrToNum( StrExtract( line, 4 ) );
             //
             if ( trade == "Buy" )
             {
                 newbuy = dt == trade_datetime;
                 Buy = Buy OR newbuy; // combine previous buy signals with new
                 BuyPrice = IIf( newbuy, price, BuyPrice );
                 possize = IIf( newbuy, shares, possize );
             }
             //
             if ( trade == "Sell" )
             {
                 newsell = dt == trade_datetime;
                 Sell = Sell OR newsell; // combine previous sell signals with new
                 SellPrice = IIf( newsell, price, SellPrice );
             }
         }
     }
     //
     fclose( fh );
}
 else
  {
      Error("ERROR: file can not be open");
  }
/*
SetPositionSize( possize, spsShares );*/

BuyPrice=ValueWhen(Buy,C);

SellPrice=ValueWhen(Sell,C);

ShortPrice=ValueWhen(Short,C);

CoverPrice=ValueWhen(Cover,C);

Plot( EMA( P, per1 ), "EMA1", ParamColor( "Color1", colorCycle ), ParamStyle("Style") ); 
Plot( EMA( P, per2 ), "EMA2", ParamColor( "Color2", colorCycle ), ParamStyle("Style") ); 


PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);


PlotShapes(IIf(TimeNum() >= ExitAllPositionsTime,5,-1e10),colorGreen,0 ,H,5);
PlotShapes(IIf(TimeNum() >= ExitAllPositionsTime,6,-1e10),colorRed,0,L,5);

did you search the forum for fopen? Example: Fopen() - Filename specification format - AFL Programming - AmiBroker Community Forum

Moderator comment: we have added CODE TAGS that are REQUIRED. Next time you post YOU MUST add code tags YOURSELF, otherwise your post will be deleted

Thank you , fopen() is corrected but I am only a learner. The code is not writing signals continuously whenever they come. Please find what is went wrong.

/********Writing to File *****************/
filepath = "C:\\signals.csv";
if(Status("stocknum") ==0)
{
  {
    fdelete(filepath);
  }
//Open file in "Share-aware " append mode//
  fh = fopen( filepath,"a", True );
 //Proceed if file handle is correct
 if (fh ) 
  {
   buyDT = LastValue(valueWhen(Buy, DateTime() ) );
   // write to file
    fputs( Name() +", Buy: " + DateTimeToStr( buyDT ) + "\n", fh);
    sellDT = LastValue(valueWhen(Sell, DateTime() ) );
   // write to file
    fputs( Name() +", Sell: " + DateTimeToStr( sellDT ) + "\n", fh);
    
    shortDT = LastValue(valueWhen(Short, DateTime() ) );
   //write to file
    fputs( Name() +", Short: " + DateTimeToStr( shortDT ) + "\n", fh);
    coverDT = LastValue(valueWhen(Cover, DateTime() ) );
   // write to file
    fputs( Name() +", Cover: " + DateTimeToStr( coverDT ) + "\n", fh);
    
    // close file handle
    fclose( fh );
  }
else  
  {
    _TRACE("Failed to open file");
    
  } 
/************End of File writing******************/    
  
  }

It is extremely important

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

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