|
hello murthysuresh,
Code is attached and listed below the capture for those who do not receive attachments. This is how far I got with this program. I tested it and it works. Be sure to set the time offset... you may just have to vary it to make sure it is set correctly. You can see the offset from the trade listing in the title. It would be best for you to play with it and see how it can be turned in an easy to use function or #Include.
Others readers are invited to suggest options that they think are useful.
Have fun,
herman
Copy the code below to an indicator and Apply. Open the param window.
Select the simulated.trades TWS exported filename
Set your local time offset wrt GMT
Click to Show trade sizes and Info
Open your TWS simulated account and make a few trades.
Wait about a minute for TWS to update the exported file (open your explorer to see it is there)
Click the REFRESH Param to read the file.
You should now have a display as shown below. A green Dot where you executed a Buy and a Red dot where you executed a Sell.
Turn on Tooltip and you can see the trade parameters, including the real fill price!
T/B Show the number of trades per bar (they may fall withing the same minute bar).

// PlotTWSTrades.afl
Filename = StrLeft(_DEFAULT_NAME(),StrLen(_DEFAULT_NAME())-2);
InIndicator = Status("Action") == 1;
StaticVarKey = Name();
procedure xStaticVarSet( SName, SValue ) { if( InIndicator ) StaticVarSet(Sname+StaticVarKey, Svalue); }
function xStaticVarGet( SName ) { return StaticVarGet(Sname+StaticVarKey); }
procedure xStaticVarSetText( SName, SValue ) { if( InIndicator ) StaticVarSetText(Sname+StaticVarKey, Svalue); }
function xStaticVarGetText( SName ) { return StaticVarGetText(Sname+StaticVarKey); }
function TimeNumToSeconds( TN )
{;
Seconds = int(TN%100);
Minutes = int(TN/100%100);
Hours = int(TN/10000%100);
SecondNum= int(Hours*3600+Minutes*60+Seconds);
return SecondNum;
}
function SecondsToTimeNum( Seconds )
{
Hours = int(Seconds/3600);
Minutes = int((Seconds - Hours*3600)/60);
Seconds = Seconds - Hours*3600 - Minutes*60;
TimeNumber = 10000*Hours+100*Minutes+Seconds;
return TimeNumber;
}
function ReadTWSTradeList( Ticker1 )
{
global TWSExportFilename, GMTTimeOS;
TimeFrame = Interval();
InputPath = "C:\\Jts\\"+TWSExportFilename;
//_TRACE( "Opening: "+InputPath);
fh1 = fopen( InputPath, "r");
if( fh1 )
{
//_TRACE( "File Opened");
TradesPerBar=1;
TradeNumber=0;
while( ! feof( fh1 ) )
{
TradeNumber++;
Line = StrReplace(L1=fgets( fh1 ),";",",");
//_TRACE( "Reading Trades; "+Line);
if( Line == "" ) break;
Ticker = StrExtract(Line,0);
if( StrToUpper(Ticker) == StrToUpper(Ticker1) )
{
Action = StrExtract(Line,1);
Qty = StrToNum(StrExtract(Line,2));
Price = StrToNum(StrExtract(Line,3));
DateStr = StrExtract(Line,5);
YearNum = StrToNum(StrLeft(DateStr,4));
MonthNum = StrToNum(StrMid(DateStr,4,2));
DayNum = StrToNum(StrRight(DateStr,2));
DateNumber = (YearNum-1900)*10000+100*MonthNum+DayNum;
DateNumStr = NumToStr(DateNumber,1.0,False);
TimeStr = StrExtract(Line,4);
Minutes = StrToNum(StrMid(TimeStr,3,2));
Seconds = StrToNum(StrRight(TimeStr,2));
Hours = StrToNum(StrLeft(TimeStr,2));
Hours = StrToNum(StrLeft(TimeStr,2))+GMTTimeOS;
Hours = Max(0,Hours); // Keep time GTE to zero Hours
Hours = Min(24,Hours); // Keep time LTE 24 Hours
SecondNum= 3600*Hours+60*Minutes+Seconds;
TimeNumber = SecondsToTimeNum( SecondNum );
RoundSecondNum = int(SecondNum/TimeFrame)*TimeFrame;
TimeNumber = SecondsToTimeNum( RoundSecondNum );
TimeNumStr = NumToStr(TimeNumber,1.0,False);
DateTimeString = DateNumStr+TimeNumStr;
StaticNameBase = Ticker+DateTimeString;
PrevStaticNameBase = StaticVarGetText("StaticNameBase");
if( PrevStaticNameBase == StaticNameBase ) TradesPerBar++; else TradesPerBar=1;
StaticVarSetText("StaticNameBase",StaticNameBase);
StaticVarSet(StaticNameBase,TradesPerBar); // Trade marker
if( TradesPerbar > 1 )
{
TradeInfo = StaticVarGetText("Info-"+StaticNameBase);
TradeInfo = TradeInfo + Line;
StaticVarSetText("Info-"+StaticNameBase,TradeInfo); // Trade info
}
else StaticVarSetText("Info-"+StaticNameBase,Line);
TradesPerbar = StaticVarGet(StaticNameBase); // Verify
StaticName = NumToStr(TradesPerBar,1.0,False)+"-"+StaticNameBase;
StaticVarSet("Price"+StaticName, Price);
if( Action == "BOT" ) StaticVarSet("BOT"+StaticName, Qty);
else if( Action == "SLD" ) StaticVarSet("SLD"+StaticName, Qty);
_TRACE("# "+NumToStr(TradeNumber,4.0,False)+", PeriodTrade#: "+NumToStr(TradesPerBar,1.0,False)+", "+StaticNameBase);
}
}
}
else _TRACE(" Cannot Open File: "+InputPath);
}
function CreateTradeComposites( Ticker )
{
global PriceArray, QtyArray;
DN=DateNum();
TN=TimeNum();
Ticker = StrToUpper(Ticker);
PriceArray = Null;
QTYBOT = Null;
QTYSLD = Null;
TPB = Null;
DN = DateNum();
TN = TimeNum();
FirstVisibleBar = Status( "FirstVisibleBar" );
Lastvisiblebar = Status("LastVisibleBar");
for( b = Firstvisiblebar; b <= Lastvisiblebar AND b < BarCount; b++)
{
DateNumStr = NumToStr(DN[b],1.0,False);
TimeNumStr = NumToStr(TN[b],1.0,False);
DateTimeString = DateNumStr+TimeNumStr;
StaticName = Ticker+DateTimeString;
TradesPerbar = Nz(StaticVarGet(StaticName));
TPB[b] = TradesPerBar;
_TRACE("Creating Comp, Checking, "+StaticName+", bar #: "+b+"Trades/Bar: "+TradesPerBar );
if( TradesPerbar )
{
BaseName = NumToStr(TradesPerBar,1.0,False)+"-"+StaticName;
PriceArray[b] = Nz(StaticVarGet("Price"+BaseName));
QtyBOT[b] = Nz(StaticVarGet("BOT"+BaseName));
QtySLD[b] = Nz(StaticVarGet("SLD"+BaseName));
_TRACE("# "+NumToStr(TradesPerbar,1.0,False)+
", "+StaticName +
" BOT " + NumToStr(QtyBOT[b],6.0,False) +
" SLD "+NumToStr(QtySLD[b],6.0,False) +
" Price $" + NumToStr(PriceArray[b],1.2));
}
}
AddToComposite( PriceArray,"~" + Ticker + "-TWS", "O", atcFlagEnableInIndicator | atcFlagCompositeGroup | atcFlagDeleteValues );
AddToComposite( QtyBOT, "~" + Ticker + "-TWS", "H", atcFlagEnableInIndicator | atcFlagCompositeGroup | atcFlagDeleteValues );
AddToComposite( QtySLD, "~" + Ticker + "-TWS", "L", atcFlagEnableInIndicator | atcFlagCompositeGroup | atcFlagDeleteValues );
AddToComposite( TPB, "~" + Ticker + "-TWS", "C", atcFlagEnableInIndicator | atcFlagCompositeGroup | atcFlagDeleteValues );
}
function GetFileStatus( FilePath )
{
PrevFDT = Nz(StaticVarGet("ExportFileDT"));
FDT = fgetstatus( filePath, 1, 5 );
StaticVarSet("ExportFileDT",FDT);
return PrevFDT != FDT;
}
global TWSExportFilename, PriceArray, QtyArray, test, GMTTimeOS;
SetBarsRequired(sbrAll,sbrAll);
GraphXSpace = 15;
TWSExportFilename = ParamList("TWS-Exported Tradelist","Real.Trades|Simulated.Trades|Demo.Trades",1);
ReadTradeListTrigger = ParamTrigger("Refresh TWS Trades","REFRESH");
GMTTimeOS = Param("GMT Time Offset",-4,-24,24,1);
ShowTradeSizes = ParamToggle("Trade Sizes","HIDE|SHOW",0);
ShowTradeInfo = ParamToggle("Trade Info","HIDE|SHOW",0);
Ticker = Name();
FileChanged = GetFileStatus( "C:\\Jts\\"+TWSExportFilename );
if( ReadTradeListTrigger OR FileChanged )
{
_TRACE("#DBGVIEWCLEAR");
ReadTWSTradeList( Ticker );
CreateTradeComposites( Ticker );
}
Price = Foreign("~"+Ticker+"-TWS","O",False);
QtyBOT = Foreign("~"+Ticker+"-TWS","H",False);
QtySLD = Foreign("~"+Ticker+"-TWS","L",False);
NumTrades= Foreign("~"+Ticker+"-TWS","C",False);
TradeNum = Cum(QtyBOT>0 OR QtySLD>0);
Plot(C,"",1,128);
TCo = IIf(QtyBOT, 5,IIf(QtySLD,4,1));
PlotShapes(IIf(Price>0,shapeSmallCircle,shapeNone),TCo,0,Price,0);
if( ShowTradeSizes )
{
Plot(QtySLD,"",4,styleArea|styleOwnScale|styleNoLabel,0,5000);
Plot(QtyBOT,"",5,styleArea|styleOwnScale|styleNoLabel,0,5000);
SP=SelectedValue(Price);
Plot(IIf(SP>0,SP,Null),"",2,1);
}
if(ShowTradeInfo )
{
DN=DateNum();
TN=TimeNum();
DateNumStr = NumToStr(DN,1.0,False);
TimeNumStr = NumToStr(TN,1.0,False);
DateTimeString = DateNumStr+TimeNumStr;
StaticName = Ticker+DateTimeString;
TradeInfo = StaticVarGetText("Info-"+StaticName);
}
else TradeInfo = "";
DT=DateTime();
ToolTip=
"GMT os: "+NumToStr(GMTTimeOS,1.0)+"\n"+
"Trade#: "+NumToStr(TradeNum,1.0,False)+"\n"+
" Time: "+DateTimeToStr(SelectedValue(DT))+"\n"+
" T/B: "+NumToStr(NumTrades,1.0,False)+"\n"+
" BOT: "+NumToStr(QtyBOT,1.0,False)+"\n"+
" SLD: "+NumToStr(QtySLD,1.0,False)+"\n"+
" $: "+NumToStr(Price,1.2);
Title = "\n"+Filename+"\n"+
"TWS Export Filename: "+"C:\\Jts\\"+TWSExportFilename+"\n"+
WriteIf(ShowTradeInfo,TradeInfo+"\n","")+
ToolTip;
|