I have try to read share value from the text file .But I got small problem. The last line share value updated to all trades .How to solve it? I have attached image for your references .
Any Idea ?
I have try to read share value from the text file .But I got small problem. The last line share value updated to all trades .How to solve it? I have attached image for your references .
Any Idea ?
@Ambi123 if you post the code you used to read the text file we can probably help you to figure out what is the cause of the error.
Thanks for reply .I will post my code .Please look on it .
_SECTION_BEGIN("Price1");
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();
SetOption( "DisableRuinStop", True );
//**********************************************************************************
TM = TimeNum();
d = Day() ;
m = Month();
y = Year();
dat = DateNum() ;
dt = DateNum();
last_thurs = Lastthursday() ;
Buy=Sell=Short=Cover=0;
line = "";
Buycnd = 0; sellcon = 0;
Shortcnd=0; covercnd = 0;
position = 0;entryprice = 0;MarketPosition = 0;ShortStopPrice = 0;entryprice = 0;
dir = "C:\AmiOptionsFiles\";
_N( list = fdir( dir + ".txt", 1 ) );
len = 0;
//Sharevalue = 0;
possize = 0;
nextmonth_name = "";
sym = "";
function StrExtract2( string, field, char )
{
return StrExtract( StrReplace( string, char, "," ), field );
}
for ( k = 0; ( filename = StrExtract( List, k ) ) != ""; k++ )
{
//fdelete(dir + filename);// path AND file
fh = fopen( dir + filename, "r",True );
while( ! feof( fh ) )
{
line = fgets( fh );
len = StrLen( line );
if(len > 0)
{
symbolname = StrExtract( line, 0);
lastdate = StrExtract( line, 1);
lasttime = StrExtract( line, 2);
Share = StrExtract( line, 3);
symbolyer = StrExtract2(symbolname,0,"$");
datsting = StrExtract2(symbolname,1,"$");
strick = StrExtract2(symbolname,2,"$");
monthstring = StrMid(datsting,3,2);
yesrtring = StrMid(datsting,0,3);
datetring = StrMid(datsting,5,2);
if(monthstring == "01"){nextmonth_name = "JAN";}
if(monthstring == "02"){nextmonth_name = "FEB";}
if(monthstring == "03"){nextmonth_name = "MAR";}
if(monthstring == "04"){nextmonth_name = "APR";}
if(monthstring == "05"){nextmonth_name = "MAY";}
if(monthstring == "06"){nextmonth_name = "JUN";}
if(monthstring == "07"){nextmonth_name = "JUL";}
if(monthstring == "08"){nextmonth_name = "AUG";}
if(monthstring == "09"){nextmonth_name = "SEP";}
if(monthstring == "10"){nextmonth_name = "OCT";}
if(monthstring == "11"){nextmonth_name = "NOV";}
if(monthstring == "12"){nextmonth_name = "DEC";}
symbol =symbolyer+nextmonth_name+strick ;
ReadDate = DateTimeConvert( 1, StrToNum(lastdate));
BarDate = DateTimeConvert( 1, DateNum() );
ReadTime = DateTimeConvert( 1, StrToNum(lasttime));
BarTime = DateTimeConvert( 1, TimeNum() );
Sharevalue = StrToNum(Share);
printf(NumToStr(Sharevalue) + "\n" );
//_TRACE( line );
for( i=1; i<BarCount; i++)
{
//printf(NumToStr(Sharevalue) + "\n" );
if(position == 0 AND Name() == symbol AND BarDate[i] == ReadDate AND BarTime[i] == ReadTime )
{
SetPositionSize( Sharevalue , spsShares);
Buycnd[i] = 1;
position = 1;
entryprice = Close[i];
}
if(position == 1 AND tm[i] >= 150000 )
{
Buycnd[i] = 0;
sellcon[i] = 1;
position = 0;
}
MarketPosition[i] = position;
}
}
}
fclose( fh );
}
@Ambi123,
Did you know that you have a preview on the right side of forum editor before you insert a post? Besides you have 15(!) minutes time to correct a post.
It is beyond me that there are still people who just do not bother for proper post format.
Why should we bother for help then?
Anyway a proper way of reading from text file has been shown in Knowledge Base (click it!) of AmiBroker already. SetPositionSize function rather should be placed outside of loop as seen in KB article as it is taking array as 1st argument.
... other flaws and errors in your code not been looked at (or rather not bothered for)...
Thanks for reply .Before insert a post I seen format is not corrected .I tried but its not working proper way.That's why I posted .. Anyway thanks for your time and effort.
TM = TimeNum();
d = Day() ;
m = Month();
y = Year();
dat = DateNum() ;
dt = DateNum();
last_thurs = Lastthursday() ;
Buy=Sell=Short=Cover=0;
line = "";
Buycnd = 0; sellcon = 0;
Shortcnd=0; covercnd = 0;
position = 0;entryprice = 0;MarketPosition = 0;ShortStopPrice = 0;entryprice = 0;
dir = "C:\AmiOptionsFiles\";
_N( list = fdir( dir + ".txt", 1 ) );
len = 0;
//Sharevalue = 0;
possize = 0;
nextmonth_name = "";
sym = "";
function StrExtract2( string, field, char )
{
return StrExtract( StrReplace( string, char, "," ), field );
}
for ( k = 0; ( filename = StrExtract( List, k ) ) != ""; k++ )
{
//fdelete(dir + filename);// path AND file
fh = fopen( dir + filename, "r" );
while( ! feof( fh ) )
{
line = fgets( fh );
len = StrLen( line );
if(len > 0)
{
symbolname = StrExtract( line, 0);
lastdate = StrExtract( line, 1);
lasttime = StrExtract( line, 2);
Share = StrExtract( line, 3);
symbolyer = StrExtract2(symbolname,0,"$");
datsting = StrExtract2(symbolname,1,"$");
strick = StrExtract2(symbolname,2,"$");
monthstring = StrMid(datsting,3,2);
yesrtring = StrMid(datsting,0,3);
datetring = StrMid(datsting,5,2);
if(monthstring == "01"){nextmonth_name = "JAN";}
if(monthstring == "02"){nextmonth_name = "FEB";}
if(monthstring == "03"){nextmonth_name = "MAR";}
if(monthstring == "04"){nextmonth_name = "APR";}
if(monthstring == "05"){nextmonth_name = "MAY";}
if(monthstring == "06"){nextmonth_name = "JUN";}
if(monthstring == "07"){nextmonth_name = "JUL";}
if(monthstring == "08"){nextmonth_name = "AUG";}
if(monthstring == "09"){nextmonth_name = "SEP";}
if(monthstring == "10"){nextmonth_name = "OCT";}
if(monthstring == "11"){nextmonth_name = "NOV";}
if(monthstring == "12"){nextmonth_name = "DEC";}
symbol =symbolyer+nextmonth_name+strick ;
ReadDate = DateTimeConvert( 1, StrToNum(lastdate));
BarDate = DateTimeConvert( 1, DateNum() );
ReadTime = DateTimeConvert( 1, StrToNum(lasttime));
BarTime = DateTimeConvert( 1, TimeNum() );
Sharevalue = StrToNum(Share);
printf(NumToStr(Sharevalue) + "\n" );
//_TRACE( line );
for( i=1; i<BarCount; i++)
{
//printf(NumToStr(Sharevalue) + "\n" );
if(position == 0 AND Name() == symbol AND BarDate[i] == ReadDate AND BarTime[i] == ReadTime )
{
SetPositionSize( Sharevalue , spsShares);
Buycnd[i] = 1;
position = 1;
entryprice = Close[i];
}
if(position == 1 AND tm[i] >= 150000 )
{
Buycnd[i] = 0;
sellcon[i] = 1;
position = 0;
}
MarketPosition[i] = position;
}
}
}
fclose( fh );
}
Hi Sorry now its clear .its my mistake only ..
you are doing something similar to what is described in this KB article:
How to generate backtest statistics from a list of historical trades stored in a file
Study this example with great attention; note the position of the following line:
SetPositionSize( possize, spsShares );
This (with the correct size assignment to the corresponding "possize" array element in the loop) will solve the issue you are facing.
Besides the example also shows an alternative way to assign the backtest variables avoiding the loop, using some array processing logic instead.
Moreover note also that when you open a file, you should always check for a valid handle. The default code snippet to read a file is as follow:
fh = fopen("filename.txt", "r" );
if( fh )
{
while( ! feof( fh ) )
{
line = fgets( fh ); // read a line of text
_TRACE( line );
}
fclose( fh );
}
else
{
Error("ERROR: file cannot be open");
}
As you can see, the KB example does it properly.
Finally, before posting any code example, in general, it is a good idea, in the Formula code editor to use the "Prettify Selection" menu option (under "Edit") to reformat the code with consistent indenting and spacing.
Then post it ALWAYS using the </> button as suggested by @fxshrat.
Since you seem not to have understood how to do it, I will copy the relevant instructions here:
It is of utmost importance as improperly formatted code results in syntax errors.
Start code block with three back-ticks ``` and end the code block with three back-ticks too, as shown below. All you need to do is to
Just press </> button then paste or type your formula.
Personally, I frequently use the 3 back-ticks option. Just type them above and below your code and check that all the formula lines are included in them.
I think that using the Code button is probably the easiest way.
@Ambi123 take a good look at this example and the preview window allowing you to verify the layout of the post before submitting:
Thanks for your reply .But unfortunately I got same problem . Last line value updated to all values ..
Any idea solve this problem ?
@Ambi123, please take the time to read through the history of your own post! Both @fxshrat and @beppe have provided you with the information to solve your problem.
Thanks for reply .I tried lot but I got same error.If I take entry different symbol its working fine .But same symbol its not working properly ,last line value updated to all values....