Hey guys,
Here is a COT Indicator that grabs data from the web. Just set the data type, 0-Comm, 1-NonComm, 2-NonReportable, and everything is done automatically. Might be slow due to your network connection speed.
Important: Put the Ionic.Zip.dll into C:\Program Files\FXCM\FXCM Strategy Trader. This dll is required for unzipping data.
Code:
using System;
using System.Drawing;
using Broker.StrategyLanguage.Function.BuiltIn;
using Fx2GoCommon;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Data;
using Ionic.Zip;
namespace Broker.StrategyLanguage.Indicator{
public class COT_Indicator : BaseIndicator {
public COT_Indicator(object _ctx):base(_ctx){}
private List<string> m_YearsList;
private Dictionary<string, int> m_HeaderIndex;
private DataTable dt;
private List<string> m_MarketAndExchangeNames;
private Dictionary<string, string> m_SymbolCOT;
private DataRow[] m_SelectedRows;
private IPlot plot0;
private IPlot plot1;
private IPlot plot2;
private IPlot plot3;
private IPlot plot4;
private IPlot plot5;
private IPlot plot6;
private IPlot plot7;
private string m_DataTypeDesc = "0=Comm, 1=NonComm, and 2=NonReport";
[Input]
public string DataTypeDesc
{
get{return m_DataTypeDesc;}
set{m_DataTypeDesc=value;}
}
private int m_DataType = 0;
[Input]
public int DataType
{
get{return m_DataType;}
set{m_DataType=value;}
}
private bool m_ShowHistogram = false;
[Input]
public bool ShowHistogram
{
get{return m_ShowHistogram;}
set{m_ShowHistogram=value;}
}
protected override void Construct() {
m_HeaderIndex = new Dictionary<string, int>();
m_MarketAndExchangeNames = new List<string>();
m_YearsList = new List<string>();
m_SymbolCOT = new Dictionary<string, string>();
plot0 = AddPlot(new PlotInfo("Net Position", PlotType.Line, Color.Yellow));
plot1 = AddPlot(new PlotInfo("Open Interest All", PlotType.Line, Color.Red));
plot2 = AddPlot(new PlotInfo("Comm Positions Long All", PlotType.Histogram, Color.Red));
plot3 = AddPlot(new PlotInfo("Comm Positions Short All", PlotType.Histogram, Color.LightGreen));
plot4 = AddPlot(new PlotInfo("Non Comm Positions Long All", PlotType.Histogram, Color.Red));
plot5 = AddPlot(new PlotInfo("Non Comm Positions Short All", PlotType.Histogram, Color.LightGreen));
plot6 = AddPlot(new PlotInfo("Nonreportable Positions-Long (All)", PlotType.Histogram, Color.Red));
plot7 = AddPlot(new PlotInfo("Nonreportable Positions-Short (All)", PlotType.Histogram, Color.LightGreen));
}
protected override void Initialize() {
CreateCOTDictionary();
int startYear = Bars.TimeValue.Year;
int endYear = Bars.LastBarTime.Year;
string Years = string.Format("{0}-{1}", startYear, endYear);
//parse years
m_YearsList.Clear();
string[] years = Years.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
foreach(string s in years)
{
if(s.Contains("-"))
{
string[] ss = s.Split(new char[]{'-'}, StringSplitOptions.RemoveEmptyEntries);
if(ss.Length == 2)
{
int start = int.Parse(ss[0].Trim());
int end = int.Parse(ss[1].Trim());
for(int i=start;i<=end;i++)
{
m_YearsList.Add(i.ToString("D4"));
}
}
}
else
{
m_YearsList.Add(s.Trim());
}
}
GetFile(m_YearsList);
//MarketSelectorForm selector = new MarketSelectorForm();
//selector.SetDataSource(m_MarketAndExchangeNames);
//DialogResult result = selector.ShowDialog();
//if(result == DialogResult.OK)
//{
// RunApp = true;
// string filterExpression = string.Format("MarketAndExchangeNames='{0}'", selector.MarketAndExchangeName);
// m_Commercial = selector.Commercial.Equals("Commercial", StringComparison.InvariantCultureIgnoreCase);
// m_SelectedRows = dt.Select(filterExpression, "ReportDate ASC");
//}
//else
//{
// RunApp = false;
//}
//selector.Dispose();
string MarketAndExchangeName = string.Empty;
foreach(KeyValuePair<string, string> kvp in m_SymbolCOT)
{
if(kvp.Key.Equals(Bars.Info.Name, StringComparison.InvariantCultureIgnoreCase))
{
MarketAndExchangeName = kvp.Value;
break;
}
}
string filterExpression = string.Format("MarketAndExchangeNames='{0}'", MarketAndExchangeName);
//m_Commercial = selector.Commercial.Equals("Commercial", StringComparison.InvariantCultureIgnoreCase);
m_SelectedRows = dt.Select(filterExpression, "ReportDate ASC");
}
protected override void Execute(){
for(int i=0;i<m_SelectedRows.Length;i++)
{
DateTime dt1;
DateTime dt2;
DataRow dr = m_SelectedRows[i];
if(i < m_SelectedRows.Length - 1)
{
DataRow nextdr = m_SelectedRows[i+1];
dt1 = (DateTime)dr["ReportDate"];
dt2 = (DateTime)nextdr["ReportDate"];
}
else
{
dt1 = (DateTime)dr["ReportDate"];
dt2 = DateTime.MaxValue;
}
if(Bars.TimeValue >= dt1 && Bars.TimeValue < dt2)
{
int OpenInterestAll = (int)dr["OpenInterestAll"];
plot1.Set((double)OpenInterestAll);
if(DataType == 0)
{
int CommPositionsLongAll = (int)dr["CommPositionsLongAll"];
int CommPositionsShortAll = (-1)*(int)dr["CommPositionsShortAll"];
if(ShowHistogram)
{
plot2.Set((double)CommPositionsLongAll, Color.Red, 5);
plot3.Set((double)CommPositionsShortAll, Color.Green, 5);
}
plot0.Set((double)(CommPositionsLongAll + CommPositionsShortAll));
}
else if(DataType == 1)
{
int NonCommPositionsLongAll = (int)dr["NonCommPositionsLongAll"];
int NonCommPositionsShortAll = (-1)*(int)dr["NonCommPositionsShortAll"];
if(ShowHistogram)
{
plot4.Set((double)NonCommPositionsLongAll, Color.Red, 5);
plot5.Set((double)NonCommPositionsShortAll, Color.Green, 5);
}
plot0.Set((double)(NonCommPositionsLongAll + NonCommPositionsShortAll));
}
else if(DataType == 2)
{
int NonreportablePositionsLongAll = (int)dr["NonreportablePositionsLongAll"];
int NonreportablePositionsShortAll = (-1)*(int)dr["NonreportablePositionsShortAll"];
if(ShowHistogram)
{
plot6.Set((double)NonreportablePositionsLongAll, Color.Red, 5);
plot7.Set((double)NonreportablePositionsShortAll, Color.Green, 5);
}
plot0.Set((double)(NonreportablePositionsLongAll + NonreportablePositionsShortAll));
}
}
}
}
protected void GetFile(List<string> years)
{
//Create a data table
dt = new DataTable();
dt.Columns.Add("MarketAndExchangeNames", typeof(string));
dt.Columns.Add("ReportDate", typeof(DateTime));
dt.Columns.Add("OpenInterestAll", typeof(int));
dt.Columns.Add("NonCommPositionsLongAll", typeof(int));
dt.Columns.Add("NonCommPositionsShortAll", typeof(int));
dt.Columns.Add("CommPositionsLongAll", typeof(int));
dt.Columns.Add("CommPositionsShortAll", typeof(int));
dt.Columns.Add("NonreportablePositionsLongAll", typeof(int));
dt.Columns.Add("NonreportablePositionsShortAll", typeof(int));
//Unzip
foreach(string year in years)
{
string URL = string.Format("http://cftc.gov/MarketReports/files/dea/history/deacot{0}.zip", year);
WebClient client = new WebClient();
Stream stream = client.OpenRead(URL);
MemoryStream cacheStream = new MemoryStream();
int bytes;
byte[] buff = new byte[1024];
do{
bytes = stream.Read(buff, 0, buff.Length);
if(bytes>0)
{
cacheStream.Write(buff, 0, bytes);
}
}
while(bytes>0);
cacheStream.Position = 0;
ZipInputStream instream = new ZipInputStream(cacheStream);
ZipEntry entry = instream.GetNextEntry();
if(entry.FileName.Equals("annual.txt", StringComparison.InvariantCultureIgnoreCase))
{
MemoryStream outstream = new MemoryStream();
do{
bytes = instream.Read(buff, 0, buff.Length);
if (bytes > 0)
{
outstream.Write(buff, 0, bytes);
}
}
while(bytes>0);
outstream.Position = 0;
//begin to read file
StreamReader reader = new StreamReader(outstream);
string header = reader.ReadLine();
string[] headerName = header.Split(new char[]{','}); //Get header row column names
m_HeaderIndex.Clear();
for(int i=0; i<headerName.Length; i++)
{
m_HeaderIndex.Add(headerName[i].Trim(new char[]{'\"'}), i);
}
while(!reader.EndOfStream)
{
string str = reader.ReadLine(); //Go through each data line
string[] rawStrings = str.Split(new char[]{','});
string[] values = new string[m_HeaderIndex.Count];
int j=0;
for(int i=0;i<rawStrings.Length;i++)
{
string value = string.Empty;
if(rawStrings[i].Trim().StartsWith("\""))
{
do{
value += rawStrings[i];
i++;
}
while(!rawStrings[i-1].Trim().EndsWith("\""));
i--;
}
else
{
value = rawStrings[i];
}
value = value.Trim(new char[]{' ', '\"'});
values[j++] = value;
}
DataRow row = dt.NewRow();
row["MarketAndExchangeNames"] = values[m_HeaderIndex["Market and Exchange Names"]];
row["ReportDate"] = DateTime.Parse(values[m_HeaderIndex["As of Date in Form YYYY-MM-DD"]]);
row["OpenInterestAll"] = int.Parse(values[m_HeaderIndex["Open Interest (All)"]]);
row["NonCommPositionsLongAll"] = int.Parse(values[m_HeaderIndex["Noncommercial Positions-Long (All)"]]);
row["NonCommPositionsShortAll"] = int.Parse(values[m_HeaderIndex["Noncommercial Positions-Short (All)"]]);
row["CommPositionsLongAll"] = int.Parse(values[m_HeaderIndex["Commercial Positions-Long (All)"]]);
row["CommPositionsShortAll"] = int.Parse(values[m_HeaderIndex["Commercial Positions-Short (All)"]]);
row["NonreportablePositionsLongAll"] = int.Parse(values[m_HeaderIndex["Nonreportable Positions-Long (All)"]]);
row["NonreportablePositionsShortAll"] = int.Parse(values[m_HeaderIndex["Nonreportable Positions-Short (All)"]]);
dt.Rows.Add(row);
}
reader.Close();
}
}
//m_MarketAndExchangeNames.Clear();
//foreach(DataRow dr in dt.Rows)
//{
// string name = dr["MarketAndExchangeNames"].ToString();
// if(!m_MarketAndExchangeNames.Contains(name))
// m_MarketAndExchangeNames.Add(name);
//}
}
private void CreateCOTDictionary()
{
m_SymbolCOT.Clear();
m_SymbolCOT.Add("EURUSD", "EURO FX - CHICAGO MERCANTILE EXCHANGE");
m_SymbolCOT.Add("USDJPY", "JAPANESE YEN - CHICAGO MERCANTILE EXCHANGE");
m_SymbolCOT.Add("GBPUSD", "BRITISH POUND STERLING - CHICAGO MERCANTILE EXCHANGE");
m_SymbolCOT.Add("USDCHF", "SWISS FRANC - CHICAGO MERCANTILE EXCHANGE");
m_SymbolCOT.Add("AUDUSD", "AUSTRALIAN DOLLAR - CHICAGO MERCANTILE EXCHANGE");
m_SymbolCOT.Add("USDCAD", "CANADIAN DOLLAR - CHICAGO MERCANTILE EXCHANGE");
m_SymbolCOT.Add("NZDUSD", "NEW ZEALAND DOLLAR - CHICAGO MERCANTILE EXCHANGE");
m_SymbolCOT.Add("USDMXN", "MEXICAN PESO - CHICAGO MERCANTILE EXCHANGE");
m_SymbolCOT.Add("NAS100", "NASDAQ-100 STOCK INDEX - CHICAGO MERCANTILE EXCHANGE");
m_SymbolCOT.Add("SPX500", "E-MINI S&P 500 STOCK INDEX - CHICAGO MERCANTILE EXCHANGE");
m_SymbolCOT.Add("US30", "DOW JONES INDUSTRIAL AVG- x $5 - CHICAGO BOARD OF TRADE");
m_SymbolCOT.Add("USOil", "CRUDE OIL LIGHT SWEET - NEW YORK MERCANTILE EXCHANGE");
m_SymbolCOT.Add("XAUUSD", "GOLD - COMMODITY EXCHANGE INC.");
m_SymbolCOT.Add("XAGUSD", "SILVER - COMMODITY EXCHANGE INC.");
m_SymbolCOT.Add("JPN225", "NIKKEI STOCK AVERAGE - CHICAGO MERCANTILE EXCHANGE");
}
}
}
Last edited by David Rodriguez; 01-03-2011 at 09:27 AM.
Below download a self-installing version of the Commitment of Traders Indicator for Strategy Trader.
Download the COTIndicator.zip file, unzip. Run the attached .exe file. Open Strategy Trader and the Strategy Trader editor. When prompted by the Editor, hit "okay" to import the "COTIndicator.fxd" file.
Once it has compiled correctly, open the "COTIndicator.fxw" file found in the default "MyWork" directory. If you installed Strategy Trader in the default location, this would be C:\Program Files\FXCM\Strategy Trader\MyWork .
Report any issues and feedback on this forum and thanks in advance.
David Rodriguez is the author of Forex Trading Signals and Forex Trading Weekly Forecast on DailyFX.com.
Please note: As of the current version, all COT data is printed with the futures contract in mind. That is to say, Non Commercial and Commercial positioning on pairs such as the USDJPY and USDCHF are actually on JPYUSD and CHFUSD futures. I will post a fix as soon as I can.
David Rodriguez is the author of Forex Trading Signals and Forex Trading Weekly Forecast on DailyFX.com.
Please note: As of the current version, all COT data is printed with the futures contract in mind. That is to say, Non Commercial and Commercial positioning on pairs such as the USDJPY and USDCHF are actually on JPYUSD and CHFUSD futures. I will post a fix as soon as I can.
Hi David,
Is Forex a part of Future Market ? If so, would you like to give us an example of non-future market ?
Is there any obligation for great speculators situated in Tax Haven(Fiscal Paradise) to communicate their positioning to US regulator(CFTC) once a week ?
Hi Frederic,
To answer your question, yes. This indicator logs any traders that have position on US Futures exchanges.
Best,
James Davis
Hi James,
First thank you for your answer.
I've asked 2 questions. Your answer is for the first one or the second ?
These are my questions:
Is there any obligation for great speculators situated in Tax Haven(Fiscal Paradise) to communicate their positioning to US regulator(CFTC) once a week ?
"The Commitments of Traders (COT) reports provide a breakdown of each Tuesdays open interest for markets in which 20 or more traders hold positions equal to or above the reporting levels established by the CFTC."
My question is the following:
What about SSI ? Are the levels of positions considered the same? I mean: If we have 10 short positions with 10 lots each, and 1 long position of 100 lots; is SSI equal to -10 ? Notice that here: Short + Long = 0; so -10 should not be appropriate.
Is Forex a part of Future Market ? If so, would you like to give us an example of non-future market ?
Thanks in advance,
The trading we do here at FXCM is the spot or cash market and not the futures market. They are two separate markets.
Enroll in our online DailyFX Course today and get personalized instruction from our team of expert traders 24 hours a day. We have taught over 25,000 students and in our online courses in the past. The new DailyFX Course has nearly 600 minutes of content delivered via video so you can learn at your own pace. Join the instructors in live webinars where they will show you how to use the highlighted tool in current market conditions. Click here to get more information.
Is there any obligation for great speculators situated in Tax Haven(Fiscal Paradise) to communicate their positioning to US regulator(CFTC) once a week ?
If no, does this make COT less significant ?
Traders do not report to the CFTC. The firm they trade through reports positions to the CFTC on a daily basis.
Enroll in our online DailyFX Course today and get personalized instruction from our team of expert traders 24 hours a day. We have taught over 25,000 students and in our online courses in the past. The new DailyFX Course has nearly 600 minutes of content delivered via video so you can learn at your own pace. Join the instructors in live webinars where they will show you how to use the highlighted tool in current market conditions. Click here to get more information.
Disclaimer: Trading foreign exchange on margin carries a high level of risk, and may not be suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to trade foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with foreign exchange trading, and seek advice from an independent financial advisor if you have any doubts. Any opinions, news, research, analyses, prices, or other information contained on this website is provided as general market commentary and does not constitute investment advice. Forex Capital Markets LLC. will not accept liability for any loss or damage, including without limitation to, any loss of profit, which may arise directly or indirectly from use of or reliance on such information.