Support Board
Date/Time: Sat, 19 Apr 2025 10:37:34 +0000
Post From: C2447 error when compiling Advanced Custom Studies
[2025-03-25 01:28:59] |
User595847 - Posts: 5 |
Thanks so much, cmet, you have pointed me in the right direction. I am now testing in Sim, and have updated the script to reflect my full strategy. Script is below, in case it's of any benefit to you or others. It is an ES/MES momentum strategy, where I use the predefined level (ie your chosen support/resistance), opening range and short term momentum from tick bar closes as filters for trend/momentum, with the ability to also set a specific time when the trade triggers eg 1 hour after opening range to avoid whipsaws. Then using trailing stop with initial stop referencing the opening range high/low. As I live in Australia, I wanted to automate my strategy for trading the US Cash Session for ES/MES, as I'm typically sleeping at this time. #include "sierrachart.h" SCDLLName("Trade Logic Strategy"); SCSFExport scsf_TradeLogicStrategy(SCStudyInterfaceRef sc) { // inputs SCInputRef Input_Level = sc.Input[0]; SCInputRef Input_TimeRestrictionEnabled = sc.Input[1]; // Enable/Disable Time Restriction SCInputRef Input_RestrictionStartTime = sc.Input[2]; // Start Time for Time Restriction SCInputRef Input_Stop = sc.Input[3]; // Stop Loss in points from opening range SCInputRef Input_Target = sc.Input[4]; // Target Profit in ticks SCInputRef Input_Quantity = sc.Input[5]; // Order Quantity SCInputRef Input_OpeningRangeDuration = sc.Input[6]; // Opening Range Duration in minutes SCInputRef Input_OpeningRangeStartTime = sc.Input[7]; // Opening Range Start Time if (sc.SetDefaults) { sc.GraphName = "Trade Logic Strategy"; sc.StudyDescription = "Automated strategy using predefined levels and opening range."; sc.AutoLoop = 1; // Define Inputs Input_Level.Name = "Predefined Level"; Input_Level.SetFloat(4000.0f); Input_TimeRestrictionEnabled.Name = "Enable Time Restriction"; Input_TimeRestrictionEnabled.SetYesNo(1); // Default to enabled Input_RestrictionStartTime.Name = "Time Restriction Start Time"; Input_RestrictionStartTime.SetTime(HMS_TIME(11, 0, 0)); // Default start time at 11:00 AM ET Input_Stop.Name = "Stop Loss (Points from Opening Range)"; Input_Stop.SetInt(3); // Default stop loss: 3 points from the opening range Input_Target.Name = "Target Profit (Ticks)"; Input_Target.SetInt(20); // Default target profit: 20 ticks Input_Quantity.Name = "Order Quantity"; Input_Quantity.SetInt(1); // Default quantity: 1 contract Input_OpeningRangeDuration.Name = "Opening Range Duration (Minutes)"; Input_OpeningRangeDuration.SetInt(60); // Default duration: 60 minutes Input_OpeningRangeStartTime.Name = "Opening Range Start Time"; Input_OpeningRangeStartTime.SetTime(HMS_TIME(9, 30, 0)); // Default start time for U.S. session (9:30 AM ET) sc.AllowMultipleEntriesInSameDirection = false; sc.MaximumPositionAllowed = 1; sc.SupportReversals = false; sc.SendOrdersToTradeService = false; return; } if (sc.Index == 0) return; // Input values float predefinedLevel = Input_Level.GetFloat(); int orderQuantity = Input_Quantity.GetInt(); int stopLossPoints = Input_Stop.GetInt(); // Stop loss points from the opening range int targetProfitTicks = Input_Target.GetInt(); int openingRangeDuration = Input_OpeningRangeDuration.GetInt(); SCDateTime openingRangeStartTime = Input_OpeningRangeStartTime.GetTime(); bool timeRestrictionEnabled = Input_TimeRestrictionEnabled.GetYesNo() !=0; // Time restriction enabled/disabled SCDateTime restrictionStartTime = Input_RestrictionStartTime.GetTime(); // Time restriction start time // Opening range variables const int date = sc.BaseDateTimeIn[sc.Index].GetDate(); int& r_LastDate = sc.GetPersistentInt(0); float& r_High = sc.GetPersistentFloat(0); float& r_Low = sc.GetPersistentFloat(1); int& r_OpeningRangeSet = sc.GetPersistentInt(2); // Reset range on new session if (date != r_LastDate) { r_LastDate = date; r_OpeningRangeSet = 0; r_High = sc.High[sc.Index]; r_Low = sc.Low[sc.Index]; } // Opening range logic SCDateTime openingRangeEndTime = openingRangeStartTime + (openingRangeDuration * 60); SCDateTime now = sc.BaseDateTimeIn[sc.Index]; if (r_OpeningRangeSet == 0 && now >= openingRangeStartTime && now < openingRangeEndTime) { r_High = sc.High[sc.Index] > r_High ? sc.High[sc.Index] : r_High; r_Low = sc.Low[sc.Index] < r_Low ? sc.Low[sc.Index] : r_Low; } else if (now >= openingRangeEndTime) { r_OpeningRangeSet = 1; } // Wait for opening range to be set if (r_OpeningRangeSet == 0) return; // Time restriction logic if (timeRestrictionEnabled && now < restrictionStartTime) return; // Exit if time restriction is enabled and before the restriction start time // Trade conditions float lastPrice = sc.Close[sc.Index]; bool isAboveConditions = (lastPrice > predefinedLevel) && (lastPrice > r_High); bool isBelowConditions = (lastPrice < predefinedLevel) && (lastPrice < r_Low); bool tradeConditionsMet = isAboveConditions || isBelowConditions; // Execution logic if (tradeConditionsMet) { s_SCNewOrder NewOrder; NewOrder.OrderType = SCT_ORDERTYPE_MARKET; NewOrder.OrderQuantity = orderQuantity; NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED; // Set stop loss and target from opening range NewOrder.Stop1Offset = stopLossPoints * sc.TickSize; NewOrder.Target1Offset = targetProfitTicks * sc.TickSize; // Create and execute buy/sell orders if (isAboveConditions) sc.BuyEntry(NewOrder); else if (isBelowConditions) sc.SellEntry(NewOrder); } } |