Hi,
In my application a C# strategy signals buys or sells by writing to buy and sell command files. A C# trading strategy checks the file modification date at each tick of 10 seconds. If there is a change a “1” is written to the time series. For buys this time series is called “buyCmdFileChanged”. I created a short version of the trading strategy that also writes to a buy command file to create a file changed event. This way signaling and trading are in one place for you to see.
To make sure everything else works I use simple conditions for buys and sells. This works. I can see it in the order manager and also in cleanup that counts positions.
When I change the buy condition to
if ((idx % 10 == 0) || (buyCmdFileChanged[idx] == 1))
In the attached snip there are 5 buys and 2 sells in the order manager. But the chart shows 3 buys and 2 sells. The buy count per cleanup is 83 and sell count is 82. Thus the 2 buys due to the time series, although shown in order manager did not take place.
The target is SPY at the dummy broker. Results were the same in administrative mode.
Can you please indicate where I am going wrong. The program compiles but it does not work as expected.
Thank you!
In my application a C# strategy signals buys or sells by writing to buy and sell command files. A C# trading strategy checks the file modification date at each tick of 10 seconds. If there is a change a “1” is written to the time series. For buys this time series is called “buyCmdFileChanged”. I created a short version of the trading strategy that also writes to a buy command file to create a file changed event. This way signaling and trading are in one place for you to see.
To make sure everything else works I use simple conditions for buys and sells. This works. I can see it in the order manager and also in cleanup that counts positions.
CODE:
if (!HasOpenPosition(bars, PositionType.Long)) { //code your buy conditions here if (idx % 10 == 0) // if (buyCmdFileChanged[idx] == 1) { PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } } else { //code your sell conditions here if ((idx % 10 != 0) && (idx % 5 == 0)) { PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } }
When I change the buy condition to
if ((idx % 10 == 0) || (buyCmdFileChanged[idx] == 1))
In the attached snip there are 5 buys and 2 sells in the order manager. But the chart shows 3 buys and 2 sells. The buy count per cleanup is 83 and sell count is 82. Thus the 2 buys due to the time series, although shown in order manager did not take place.
The target is SPY at the dummy broker. Results were the same in administrative mode.
Can you please indicate where I am going wrong. The program compiles but it does not work as expected.
Thank you!
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; using System.IO; namespace WealthScript18 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { initCounter++; DrawHeaderText("TC experiment says: Hello World at: " + DateTime.Now.ToString() + " Init count: " + initCounter, WLColor.White, 14); WriteToDebugFile("\r\nline 16 init time: " + DateTime.Now.ToString() + " Init count: " + initCounter, 10); if (initOneShot == true) { initOneShot = false; // do things that will get done only once for example file creation File.Delete(cmdBuyFilePath); // we do this to wipe out creation time using (StreamWriter sw = new StreamWriter(cmdBuyFilePath, false)) { string line = $"buy command file"; sw.WriteLine(line); WriteToDebugFile("line 25 buy command file first written with one shot init at: " + DateTime.Now.ToString(), 10); } // BuyCommandFileCreationTime = File.GetCreationTime(cmdBuyFilePath); // WriteToDebugFile("line 28 BuyCommandFileCreationTime: " + BuyCommandFileCreationTime.ToString(), 10); BuyCommandFileModificationTime = File.GetLastWriteTime(cmdBuyFilePath); WriteToDebugFile("line 31 BuyCommandFileModificationTime: " + BuyCommandFileModificationTime.ToString(), 10); BuyCommandFilePreviousModificationTime = BuyCommandFileModificationTime; // a26 January, 2026 begin // buyCmdFileChanged = new TimeSeries(bars.DateTimes, 0); // buyCmdFileChanged[bars.Count - 1] = 1; // a26 January, 2026 end // a28 January, 2026 begin initialize the array for (int i = 0; i < 3200; i++) { buyCmdFileChangesArray[i] = 0; } // a28 January, 2026 end } // a28 January, 2026 note: this little section spoofs the signaling app by causing a change in the command file // every ten passes write to the buy command file to update its modification time if (initCounter % 10 == 0) { using (StreamWriter sw = new StreamWriter(cmdBuyFilePath, true)) { string line = $"buy command file update at {DateTime.Now.ToString()}"; sw.WriteLine(line); WriteToDebugFile("line 39 buy command file updated at: " + DateTime.Now.ToString(), 10); } BuyCommandFileModificationTime = File.GetLastWriteTime(cmdBuyFilePath); WriteToDebugFile("line 43 BuyCommandFileModificationTime: " + BuyCommandFileModificationTime.ToString(), 10); } // // int BuyCommandFileModTimeChanged = DateTime.Compare(BuyCommandFileModificationTime, BuyCommandFilePreviousModificationTime); int BuyCommandFileModTimeChanged = DateTime.Compare(BuyCommandFileModificationTime, BuyCommandFilePreviousModificationTime); if (BuyCommandFileModTimeChanged > 0) { WriteToDebugFile("line 52 BuyCommandFile Modification time has changed", 10); BuyCommandFilePreviousModificationTime = BuyCommandFileModificationTime; // a28 January, 2026 begin buyCmdFileChangesArray[bars.Count -1] = 1; // buyCmdFileChanged[bars.Count - 1] = 1; // a28 January, 2028 end // a26 January, 2026 end } // a28 January, 2026 begin // a26 January, 2026 begin buyCmdFileChanged = new TimeSeries(bars.DateTimes, 0); // a28 January, 2026 begin for (int n = 2; n < bars.Count; n++) { buyCmdFileChanged[n] = buyCmdFileChangesArray[n]; } // buyCmdFileChanged[bars.Count - 1] = 1; // a28 January, 2028 end // a28 January, 2026 end } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { // one pass section begin <<<<<<<<<< if (idx == 0) { WriteToDebugFile("line 23 idx 0 time: " + DateTime.Now.ToString(), 10); } else if (idx < bars.Count - 1) { // do nothing } else { // one pass code is here WriteToDebugFile("line 100 buyCmdFileChanged[bars.Count-1]: " + buyCmdFileChanged[bars.Count - 1].ToString(), 10); WriteToDebugFile("line 100 [bars.Count-1]: " + (bars.Count - 1).ToString(), 10); } // one pass section end >>>>>>>>>>>>> // trade part begin <<<<<<<<<<<<<<<<< // check for buy command file date change and if different initiater a buy // check for sell command file date change and if different initiater a sell // trade part end >>>>>>>>>>>>>>>>>>> if (!HasOpenPosition(bars, PositionType.Long)) { //code your buy conditions here if ((idx % 10 == 0) || (buyCmdFileChanged[idx] == 1)) // if (buyCmdFileChanged[idx] == 1) { // WriteToDebugFile("line 90 Buy command issued at idx: " + idx.ToString() + " max: " + (bars.Count - 1).ToString(), 10); PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } } else { //code your sell conditions here if ((idx % 10 != 0) && (idx % 5 == 0)) { // WriteToDebugFile("line 100 Sell command issued at idx: " + idx.ToString() + " max: " + (bars.Count - 1).ToString(), 10); PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } } } public override void Cleanup(BarHistory bars) { int filled = GetPositionsAllSymbols().Count; int all = GetPositionsAllSymbols(true).Count; int notFilled = all - filled; // a27 January, 2026 begin int closedPositionsCount = 0; foreach (Position pos in GetPositionsAllSymbols()) { if (pos.IsOpen == false) { closedPositionsCount++; } } // a27 January, 2026 end DrawHeaderText("Filled Positions = " + filled, WLColor.White, 14); DrawHeaderText("Unfilled Positions in = " + notFilled, WLColor.White, 14); DrawHeaderText("Closed Positions in = " + closedPositionsCount, WLColor.White, 14); } //declare private variables below // a25 January, 2026 begin static int initCounter = 0; static bool initOneShot = true; // used once in the first init for example for file creation static string cmdBuyFilePath = @"C:\Users\Hal\Documents\iDOTS2012\WL8 Trade Code Experiments\buy sell cmd status files\BuyCmdFile.txt"; static string statusBuyFileFullPath = @"C:\Users\Hal\Documents\iDOTS2012\WL8 Trade Code Experiments\buy sell cmd status files\BuyStatusFile.txt"; static string cmdSellFilePath = @"C:\Users\Hal\Documents\iDOTS2012\WL8 Trade Code Experiments\buy sell cmd status files\SellCmdFile.txt"; static string statusSellFileFullPath = @"C:\Users\Hal\Documents\iDOTS2012\WL8 Trade Code Experiments\buy sell cmd status files\SellStatusFile.txt"; // static DateTime BuyCommandFileCreationTime = DateTime.MinValue; static DateTime BuyCommandFileModificationTime = DateTime.MinValue; static DateTime BuyCommandFilePreviousModificationTime = DateTime.MaxValue; // a25 January, 2026 end // a26 January, 2026 begin private TimeSeries buyCmdFileChanged; // a26 January, 2026 end // a28 January, 2026 begin declare an array for the whole session 390 minutes x 6 = 2340 + 800 depth = 3140 element array int[] buyCmdFileChangesArray = new int[3200]; // a28 January, 2026 end // static string tenSecondDebugFilePath = @"C:\Users\Hal\Documents\iDOTS2012\WL8 Trade Code Experiments\debugTC\debugTC.txt"; static int WriteToDebugFile(string message, int WL1Mor5S) { if (WL1Mor5S == 10) { try { File.AppendAllText(tenSecondDebugFilePath, "\r\n" + message); return 10; } catch (System.IO.IOException fileName) { return -1; } } else { return -1; } } } }
Rename
Currently there are no replies yet. Please check back later.
Your Response
Post
Edit Post
Login is required