Login Page - Create Account

Support Board


Date/Time: Sun, 24 Nov 2024 13:44:45 +0000



Post From: code help

[2024-06-01 23:07:44]
User411320 - Posts: 289
Hello, I'm not a developer but I'm trying to write a code for sierra and can't figure out what I'm doing wrong. Any help would be greatly appreciated

The top of every source code file must include this line
#include "sierrachart.h"

// For reference, refer to this page:
// Advanced Custom Study Interface and Language (ACSIL)

// This line is required. Change the text within the quote
// marks to what you want to name your group of custom studies.
SCDLLName("Ocean")

//This is the basic framework of a study function. Change the name 'TemplateFunction' to what you require.
SCSFExport scsf_TemplateFunction(SCStudyInterfaceRef sc)
{
  // Section 1 - Set the configuration variables and defaults
  if (sc.SetDefaults)
  {
    #include <iostream>
#include <vector>
#include <cmath>

// Function to calculate EMA
std::vector<double> calculateEMA(const std::vector<double>& values, int length) {
std::vector<double> ema(values.size());
double alpha = 2.0 / (length + 1);
ema[0] = values[0]; // Initialize the first value of EMA with the first value in the series

for (size_t i = 1; i < values.size(); ++i) {
ema[i] = alpha * values[i] + (1 - alpha) * ema[i - 1];
}

return ema;
}

int main() {
// Example source data (e.g., closing prices)
std::vector<double> src = {1.0, 1.1, 1.2, 1.3, 1.4, 1.5}; // Replace with actual data
int length = 14; // Length period

// Step 1: Calculate ln (logarithm of source data scaled by 1000)
std::vector<double> ln(src.size());
for (size_t i = 0; i < src.size(); ++i) {
ln[i] = std::log(src[i]) * 1000;
}

// Step 2: Calculate oi
std::vector<double> oi(src.size(), 0.0); // Initialize with zeros
for (size_t i = 1; i < src.size(); ++i) {
oi[i] = (ln[i] - ln[i - 1]) * 100;
}

// Step 3: Calculate nmrSum
std::vector<double> nmrSum(src.size(), 0.0);
for (size_t i = 0; i < src.size(); ++i) {
if (i > 0) {
nmrSum[i] = nmrSum[i - 1] + oi[i] * (std::sqrt(i + 1) - std::sqrt(i));
} else {
nmrSum[i] = oi[i] * std::sqrt(1);
}
}

// Step 4: Calculate the first EMA of nmrSum
std::vector<double> ema1 = calculateEMA(nmrSum, length);

// Step 5: Calculate the second EMA of the first EMA
std::vector<double> ema2 = calculateEMA(ema1, length);

// Output the result
for (size_t i = 0; i < ema2.size(); ++i) {
std::cout << "ema2[" << i << "] = " << ema2[i] << std::endl;
}

return 0;
}
  
  // Section 2 - Do data processing here
  
  
}