Support Board
Date/Time: Tue, 26 Nov 2024 08:44:36 +0000
Post From: ACSIL code generated by Chat GPT
[2023-12-20 16:06:16] |
j4ytr4der_ - Posts: 938 |
And just to illustrate, I got tired of using spreadsheets to get Open P&L, so I whipped up my own Open P&L study, using chatGPT to do most of the actual coding. Took about 20 minutes total and now works great. Here's the chat in case it's helpful to anyone else looking to do the same type of thing: User I need a study which will output the current open profit & loss to a subgraph. In the SC documentation I found the following example code, how could it be modified such that it outputs the Open P&L number into a subgraph? sc.CreateProfitLossDisplayString() Type: Function void CreateProfitLossDisplayString(double ProfitLoss, int Quantity, PositionProfitLossDisplayEnum ProfitLossFormat, SCString& Result); The sc.CreateProfitLossDisplayString() function creates a Profit/Loss text string based on the provided parameters. The Result parameter is passed by reference and receives the formatted Profit/Loss text string. Parameters ProfitLoss: The profit or loss value as a floating-point number. Quantity: The Position or Trade quantity. ProfitLossFormat: The profit or loss format. The possibilities are: PPLD_DO_NOT_DISPLAY, PPLD_CURRENCY_VALUE, PPLD_POINTS, PPLD_POINTS_IGNORE_QUANTITY, PPLD_TICKS, PPLD_TICKS_IGNORE_QUANTITY, PPLD_CV_AND_POINTS, PPLD_CV_AND_POINTS_IGNORE_QUANTITY, PPLD_CV_AND_TICKS, PPLD_CV_AND_TICKS_IGNORE_QUANTITY. For descriptions of these, refer to Profit/Loss Format. Result: A SCString variable to receive the formatted profit/loss string. Example if (ShowOpenPL.GetYesNo()) { SCString PLString; double Commission = RTCommissionRate * PositionData.PositionQuantity / 2.0; sc.CreateProfitLossDisplayString(PositionData.OpenProfitLoss-Commission, PositionData.PositionQuantity, ProfitLossDisplayFormat, PLString); Text.Format("Open PL: %s", PLString.GetChars()); } ChatGPT To output the current open profit and loss to a subgraph, you can use the CreateProfitLossDisplayString function and set the result to a subgraph value. Here's an example modification of the provided code: cpp Copy code SCSubgraphRef Subgraph_OpenPL = sc.Subgraph[0]; // Assuming you have defined a subgraph in your study // ... (other variable declarations and study initialization) if (ShowOpenPL.GetYesNo()) { SCString PLString; double Commission = RTCommissionRate * PositionData.PositionQuantity / 2.0; // Calculate Open P&L double OpenPL = PositionData.OpenProfitLoss - Commission; // Create profit/loss display string sc.CreateProfitLossDisplayString(OpenPL, PositionData.PositionQuantity, ProfitLossDisplayFormat, PLString); // Output the Open P&L to the subgraph Subgraph_OpenPL[sc.Index] = OpenPL; // Optionally, you can also output the formatted string to the debug subgraph Subgraph_Debug[sc.Index] = PLString; // ... (rest of your code) } In this example, Subgraph_OpenPL is assumed to be a subgraph that you have defined in your study. Adjust the subgraph index accordingly based on your actual subgraph setup. The Open P&L value is calculated, and the result is assigned to the subgraph at the current index (sc.Index). Additionally, the formatted profit/loss string is output to a debug subgraph (Subgraph_Debug), but you can omit this part if not needed. Make sure to adapt the code to fit into your existing study structure and variable definitions. User How are you calculating commissions here? Where does RTCommissionRate come from? ChatGPT I apologize for the oversight. It seems there was an assumption in the provided code regarding the presence of RTCommissionRate, which wasn't defined in your original code. To calculate commissions, you need to use the commission rate applicable to your trading scenario. You can replace RTCommissionRate with the actual commission rate you want to apply. If your commissions are fixed per trade, you can replace it with a fixed commission value. Here's an example assuming a fixed commission per trade: cpp Copy code double CommissionRate = 0.01; // Replace with your actual commission rate (e.g., 1%) double Commission = CommissionRate * PositionData.PositionQuantity; In the modified code snippet: cpp Copy code SCSubgraphRef Subgraph_OpenPL = sc.Subgraph[0]; // Assuming you have defined a subgraph in your study // ... (other variable declarations and study initialization) if (ShowOpenPL.GetYesNo()) { SCString PLString; // Calculate Open P&L without commission double OpenPL = PositionData.OpenProfitLoss; // Calculate commission (replace 0.01 with your actual commission rate) double CommissionRate = 0.01; // Replace with your actual commission rate (e.g., 1%) double Commission = CommissionRate * PositionData.PositionQuantity; // Subtract commission from Open P&L OpenPL -= Commission; // Create profit/loss display string sc.CreateProfitLossDisplayString(OpenPL, PositionData.PositionQuantity, ProfitLossDisplayFormat, PLString); // Output the Open P&L to the subgraph Subgraph_OpenPL[sc.Index] = OpenPL; // Optionally, you can also output the formatted string to the debug subgraph Subgraph_Debug[sc.Index] = PLString; // ... (rest of your code) } Replace 0.01 with your actual commission rate, and adjust the code as needed based on your commission structure. User This appears to be working with a display string, when all I want is the value stored in a subgraph that I can reference from elsewhere. Is this still the right way to go about that? ChatGPT I appreciate your clarification. If you want to store the open profit and loss directly in a subgraph without using a display string, you can modify the code accordingly. Here's the updated code snippet: cpp Copy code SCSubgraphRef Subgraph_OpenPL = sc.Subgraph[0]; // Assuming you have defined a subgraph in your study // ... (other variable declarations and study initialization) if (ShowOpenPL.GetYesNo()) { // Calculate Open P&L without commission double OpenPL = PositionData.OpenProfitLoss; // Calculate commission (replace 0.01 with your actual commission rate) double CommissionRate = 0.01; // Replace with your actual commission rate (e.g., 1%) double Commission = CommissionRate * PositionData.PositionQuantity; // Subtract commission from Open P&L OpenPL -= Commission; // Output the Open P&L to the subgraph Subgraph_OpenPL[sc.Index] = OpenPL; // ... (rest of your code) } In this modification, OpenPL is directly assigned to the subgraph at the current index (sc.Index). Now, you can reference the values stored in the Subgraph_OpenPL from elsewhere in your study. Ensure that you have defined the subgraph appropriately in your study, and adjust the subgraph index and other details based on your specific requirements. User We don't appear to be declaring PositionData ChatGPT You're correct, and I appreciate your diligence. The variable PositionData wasn't defined in the provided code snippets. The PositionData typically represents the information about the current position, and it's assumed to be available in Sierra Chart. However, if it's not available directly, you might need to access it through appropriate Sierra Chart functions. Here's an example of how you might declare and use PositionData: cpp Copy code SCFloatArrayRef PositionProfitLoss = sc.GetPersistentFloatArray(1); // ... (other variable declarations and study initialization) if (ShowOpenPL.GetYesNo()) { s_SCPositionData PositionData; sc.GetTradePosition(PositionData); // Calculate Open P&L without commission double OpenPL = PositionData.OpenProfitLoss; // Calculate commission (replace 0.01 with your actual commission rate) double CommissionRate = 0.01; // Replace with your actual commission rate (e.g., 1%) double Commission = CommissionRate * PositionData.PositionQuantity; // Subtract commission from Open P&L OpenPL -= Commission; // Output the Open P&L to the subgraph Subgraph_OpenPL[sc.Index] = OpenPL; // Store PositionData in a persistent array for future reference if needed PositionProfitLoss[sc.Index] = OpenPL; // ... (rest of your code) } This assumes that Sierra Chart provides a function GetTradePosition to fetch position information. If such a function doesn't exist, you may need to check the Sierra Chart documentation or use other available functions to retrieve the necessary position data. |