Login Page - Create Account

Support Board


Date/Time: Sat, 15 Nov 2025 16:16:26 +0000



Post From: Exporting Fib tools Data

[2025-09-16 19:09:56]
User676363 - Posts: 87
Here's a code example for exporting the risk/reward tool to the message log.
Should be relatively easy to modify it to work with the other tools especially with the use of an LLM to assist.

It produces a log entry like this for each line and some summary info at the end for all the entries.

2025-09-16 14:51:08.660 | Chart: NQZ25_FUT_CME [CBV][M] 15 Sec #4 | Study: Log Risk/Reward Drawings | Reward/Risk Tool | Stop=24635.00 (6 ticks)@2025-09-16 04:45:30 | Entry=24636.50@2025-09-16 04:46:30 | Target=24644.75 (33 ticks)@2025-09-16 04:48:30 | R=+5.50 | Duration=2.0 min | Line=-395
2025-09-16 14:51:08.660 | Chart: NQZ25_FUT_CME [CBV][M] 15 Sec #4 | Study: Log Risk/Reward Drawings | Reward/Risk Tool | Stop=24638.75 (16 ticks)@2025-09-16 05:15:30 | Entry=24634.75@2025-09-16 05:16:30 | Target=24620.50 (57 ticks)@2025-09-16 05:30:00 | R=+3.56 | Duration=13.5 min | Line=-397
2025-09-16 14:51:08.660 | Chart: NQZ25_FUT_CME [CBV][M] 15 Sec #4 | Study: Log Risk/Reward Drawings | Reward/Risk Tool | Stop=24532.00 (11 ticks)@2025-09-16 14:42:15 | Entry=24534.75@2025-09-16 14:44:30 | Target=24544.75 (40 ticks)@2025-09-16 14:50:30 | R=+3.64 | Duration=6.0 min | Line=-415
2025-09-16 14:51:08.660 | Chart: NQZ25_FUT_CME [CBV][M] 15 Sec #4 | Study: Log Risk/Reward Drawings | Session Totals | Total Duration=21.5 min | Total R=12.70 | Max Risk=16 ticks | Max Profit=57 ticks


Save the file as LogRiskReward.cpp or whatever you like and then compile it and load it. Set the Run to Yes and apply the study. It will run once, for all the RR tools on the chart and set itself to No. If you later add more, you can just open the study properties and select yes and it will export the new data. If you want your duration to be correct, you will need to ensure that the target or the stop is actually where you closed the trade and the entry will need to be adjusted to the candle where it was executed. If you want your journal to show a losing trade, you need to make sure the stop of the RR tool is the last point (in terms of date and time on the time axis) - just move it to where you were stopped out and put the target to a point before that stop in terms of time and the log above will show a -1 R for that line and it will set the trade duration.

#include "sierrachart.h"

SCDLLName("LogRiskReward")

SCSFExport scsf_LogRiskRewardDrawings(SCStudyInterfaceRef sc)
{
// === Inputs ===
SCInputRef Input_Run = sc.Input[0];

if (sc.SetDefaults)
{
sc.GraphName = "Log Risk/Reward Drawings";
sc.AutoLoop = 0;
sc.GraphRegion = 0; // use primary chart region

Input_Run.Name = "Run Reward/Risk Logging";
Input_Run.SetYesNo(0);

return;
}

// Only run if user enabled input and hasn't run yet
if (Input_Run.GetYesNo() == 0)
return;

int DrawingIndex = 0;
s_UseTool Tool;

double totalR = 0.0;
double totalDurationMinutes = 0.0;
double maxRiskTicks = 0.0;
double maxProfitTicks = 0.0;

while (sc.GetUserDrawnChartDrawing(sc.ChartNumber, DRAWING_REWARD_RISK, Tool, DrawingIndex) != 0)
{
double StopPrice = Tool.BeginValue;
double EntryPrice = Tool.EndValue;
double TargetPrice = Tool.ThirdValue;

SCDateTime StopDT = Tool.BeginDateTime;
SCDateTime EntryDT = Tool.EndDateTime;
SCDateTime TargetDT = Tool.ThirdDateTime;

// Determine which point came last in time (to identify win/loss)
SCDateTime LastDT;
double LastPrice;
if (StopDT > TargetDT) {
LastDT = StopDT;
LastPrice = StopPrice;
} else {
LastDT = TargetDT;
LastPrice = TargetPrice;
}

double RiskTicks = fabs(EntryPrice - StopPrice) / sc.TickSize;
double RewardTicks = fabs(TargetPrice - EntryPrice) / sc.TickSize;

// Calculate R for the trade (+R for win, -R for loss)
double tradeR = (LastPrice == TargetPrice) ? (RewardTicks / RiskTicks) : (-RiskTicks / RiskTicks);

// Calculate duration from entry to last point
double durationMinutes = (LastDT.GetAsDouble() - EntryDT.GetAsDouble()) * 24.0 * 60.0;

// Update session totals
totalR += tradeR;
totalDurationMinutes += durationMinutes;
maxRiskTicks = max(maxRiskTicks, RiskTicks);
if (tradeR > 0) // only consider winning trades for max profit
maxProfitTicks = max(maxProfitTicks, RewardTicks);


// Format times
SCString StopTimeStr = sc.FormatDateTime(StopDT);
SCString EntryTimeStr = sc.FormatDateTime(EntryDT);
SCString TargetTimeStr = sc.FormatDateTime(TargetDT);

// Prepare log message
SCString Msg;
Msg.Format(
"Reward/Risk Tool | Stop=%.2f (%d ticks)@%s | Entry=%.2f@%s | Target=%.2f (%d ticks)@%s | R=%+.2f | Duration=%.1f min | Line=%d",
StopPrice, (int)RiskTicks, StopTimeStr.GetChars(),
EntryPrice, EntryTimeStr.GetChars(),
TargetPrice, (int)RewardTicks, TargetTimeStr.GetChars(),
tradeR,
durationMinutes,
Tool.LineNumber
);
sc.AddMessageToLog(Msg, 0);

DrawingIndex++;
}

// Log session totals
SCString TotalMsg;
TotalMsg.Format(
"Session Totals | Total Duration=%.1f min | Total R=%.2f | Max Risk=%d ticks | Max Profit=%d ticks",
totalDurationMinutes,
totalR,
(int)maxRiskTicks,
(int)maxProfitTicks
);
sc.AddMessageToLog(TotalMsg, 0);

// Mark as done
Input_Run.SetYesNo(0);
}