Amibroker Alertif

#1
I am trying to export real time buy sell signals to csv/txt from amibroker.

I found "Alertif", function does the same job as it avoids repeated signals which
scan function cannot do. However, one cannot copy or export from alertif output window.
Therefore, I am trying to exec / open a file using alertif and write to file.

Alertif(buy, exec c://test.csv,"buy, trade price, date()", 1,1+2+3+4,0);

Above line opens the test file but doesn't write to it. can you please guide through.

Any help would be much appreciated.

Thank you.
 

lemondew

Well-Known Member
#2
use filewriting functions fopen, fputs.

Thats the best way

I am trying to export real time buy sell signals to csv/txt from amibroker.

I found "Alertif", function does the same job as it avoids repeated signals which
scan function cannot do. However, one cannot copy or export from alertif output window.
Therefore, I am trying to exec / open a file using alertif and write to file.

Alertif(buy, exec c://test.csv,"buy, trade price, date()", 1,1+2+3+4,0);

Above line opens the test file but doesn't write to it. can you please guide through.

Any help would be much appreciated.

Thank you.
 
#3
Use the following snippet. Modify according ot your requirement. Add commas for CSV and Carraige Return/LineFeed characters.


alreadyopenbuy = StaticVarGetText("alreadyopenbuy"+GetChartID());
alreadyopensell = StaticVarGetText("alreadyopensell"+GetChartID());

fh = fopen( "C:\\Users\\Admin\\Desktop\\buysellsignals.txt", "w" );


if ( LastValue(Buy) == True AND alreadyopenbuy == "")
{

StaticVarSetText("alreadyopenbuy"+GetChartID() , "1");
StaticVarSetText("alreadyopensell"+GetChartID() , "");

y = Year();
m = Month();
d = Day();

ds = StrFormat("%02.0f-%02.0f-%02.0f,",
y[ i ], m[ i ], d[ i ] );
fputs( ds, fh );

qs = StrFormat("%.4f, %.4f, %.4f, %.4f, %.0f\n",
O[ i ], H[ i ], L[ i ], C[ i ], V[ i ] );
fputs( qs, fh );
}

if ( LastValue(Sell) == True AND alreadyopensell == "")
{
StaticVarSetText("alreadyopenbuy"+GetChartID() , "");
StaticVarSetText("alreadyopensell"+GetChartID() , "1");

y = Year();
m = Month();
d = Day();

ds = StrFormat("%02.0f-%02.0f-%02.0f,",
y[ i ], m[ i ], d[ i ] );
fputs( ds, fh );

qs = StrFormat("%.4f, %.4f, %.4f, %.4f, %.0f\n",
O[ i ], H[ i ], L[ i ], C[ i ], V[ i ] );
fputs( qs, fh );
}

fclose(fh);
 

mastermind007

Well-Known Member
#4
Why would you even want to use alert if for writing to file?
 
#7
Use the following snippet. Modify according ot your requirement. Add commas for CSV and Carraige Return/LineFeed characters.


alreadyopenbuy = StaticVarGetText("alreadyopenbuy"+GetChartID());
alreadyopensell = StaticVarGetText("alreadyopensell"+GetChartID());

fh = fopen( "C:\\Users\\Admin\\Desktop\\buysellsignals.txt", "w" );


if ( LastValue(Buy) == True AND alreadyopenbuy == "")
{

StaticVarSetText("alreadyopenbuy"+GetChartID() , "1");
StaticVarSetText("alreadyopensell"+GetChartID() , "");

y = Year();
m = Month();
d = Day();

ds = StrFormat("%02.0f-%02.0f-%02.0f,",
y[ i ], m[ i ], d[ i ] );
fputs( ds, fh );

qs = StrFormat("%.4f, %.4f, %.4f, %.4f, %.0f\n",
O[ i ], H[ i ], L[ i ], C[ i ], V[ i ] );
fputs( qs, fh );
}

if ( LastValue(Sell) == True AND alreadyopensell == "")
{
StaticVarSetText("alreadyopenbuy"+GetChartID() , "");
StaticVarSetText("alreadyopensell"+GetChartID() , "1");

y = Year();
m = Month();
d = Day();

ds = StrFormat("%02.0f-%02.0f-%02.0f,",
y[ i ], m[ i ], d[ i ] );
fputs( ds, fh );

qs = StrFormat("%.4f, %.4f, %.4f, %.4f, %.0f\n",
O[ i ], H[ i ], L[ i ], C[ i ], V[ i ] );
fputs( qs, fh );
}

fclose(fh);



@ startsystem.

Thanks for your time.
It is writing to file but missing many signals.

I guess it is giving Buy then sell then buy only...
irrespective of symbols..

ACC buy given
RIL buy missed
RELCAP buy missed
YES SELL given
BOI SELL missed
Ibu BUY given

Below is the code:


_SECTION_BEGIN("Price");
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() );
_SECTION_END();

_SECTION_BEGIN("5/10 EMA TrenNotify");

W=EMA(C,5);
X=EMA(C,10);

Buy = Ref(c, -1)> X AND W>X AND L<X;

Sell = Cross(X, W);

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


alreadyopenbuy = StaticVarGetText("alreadyopenbuy"+GetChartID());
alreadyopensell = StaticVarGetText("alreadyopensell"+GetChartID());

fh = fopen( "C:\\Users\\Admn\\Desktop\\tradelog.csv", "a" ); //-----> changed it to csv and "a" append mode, Admn instead of Admin

if(fh)
{
if ( LastValue(Buy) == True AND alreadyopenbuy == "")
{

StaticVarSetText("alreadyopenbuy"+GetChartID() , "1");
StaticVarSetText("alreadyopensell"+GetChartID() , "");

fputs( "BUY" + " ," + Name() + " ," +Close+ "\n", fh );

}

if ( LastValue(Sell) == True AND alreadyopensell == "")
{
StaticVarSetText("alreadyopenbuy"+GetChartID() , "");
StaticVarSetText("alreadyopensell"+GetChartID() , "1");

fputs( "SELL" + " ," + Name() + " ," +Close+ "\n", fh );

}

fclose(fh);

}

else

{
_TRACE("Failed to open the file");

}

remark = "SELL" + " " + Name() + " " +Close;

AlertIf(BUY,"",remark,1,1+2+4+8,0);

barcomplete = Barindex() < lastvalue(Barindex());

AlertIf(barcomplete AND Sell,"",remark,2);

Filter=Buy OR Sell;

AddColumn(Buy,"BUY",1.2);
AddColumn(Sell,"SELL",1.2);

PlotShapes( shapeUpArrow*Buy, colorGreen,0, L, -20);
PlotShapes( shapeDownArrow*Sell, colorRed,0,H, -20) ;

Plot(EMA(Close,5),"5-EMA",colorOrange,styleLine);
Plot(EMA(Close,10),"10-EMA",colorGreen,styleLine);

_SECTION_END();

///////////////////////////

Sorry to bother you again.

Thank you
 

mastermind007

Well-Known Member
#8
Use Name() in place of getchartid()
 
#10
how one can read lastvalue for the previous to lastcandle i.e, barindex/barcount -1?
as i am using a cross for exit and i want to read the signal only on the bar completion.
on bar completion it becomes previous to last candle.

i tried few:

Ref(lastvalue, -1)
bi=barindex()-1;
bi=barcount-1;

but unsuccessful.

Pls guide.

Thanks in advance.
 

Similar threads