C++ Retrieving Vector values causes fatal error

How to create strategies and indicators
Message
Author
Mickatron
Posts: 10
Joined: Sat May 30, 2015 8:07 pm

C++ Retrieving Vector values causes fatal error

#1 Postby Mickatron » Mon Jun 01, 2015 2:51 pm

Hi,
I've sent this question to support but I thought asking it here may get me an answer quicker or maybe some different suggestions.

I'm creating a TMA indicator and am trying to store the previous periods SMA values in an array type structure to calculate the final TMA line. Creating and adding values to the vector seems to work fine, but when I go to retrieve values with .at(i) FT errors out and asks me if I'd like to continue/abort...

My implementation is as below. I've also tried int as the dataType of the loops counter. I see there's an undocumented GetMA function which may work out as an alternative but I'm not sure how to use it in C++, besides storing the values has to be quicker than calculating them each and every period.

Code: Select all

#include <windows.h>
#include "IndicatorInterfaceUnit.h"
#include "TechnicalFunctions.h"
#include <vector>
using std::vector;

... Calculate () {

    if (Bars() >= Len) {
        for (i = 0; i < (Len); i++) {
            totalClosePrice = totalClosePrice + Close(i);
        }
        //add SMA to the vector Arr
        arr.push_back(totalClosePrice / Periods);
    }
   
    if (Bars() >= first) {
        //get TMA of period.
        //int x = 0;
        double totalSMAPrice = 0;

        for (std::vector<double>::size_type x = 0; x < Len; x++) {
            //readSMA from the Vector Array
            totalSMAPrice = totalSMAPrice + arr.at(x);
        }
       TMABuffer[index] = totalSMAPrice / Periods;
    }
}


I'm just picking up C++ so this may be due to a misunderstanding of how the language works. Any suggestions or advice would be awesome.
Last edited by Mickatron on Mon Jun 01, 2015 3:44 pm, edited 1 time in total.

Mickatron
Posts: 10
Joined: Sat May 30, 2015 8:07 pm

Re: C++ Vector .at() method causes fatal error in FT

#2 Postby Mickatron » Mon Jun 01, 2015 3:40 pm

Some debug info;
I've debugged the vector.size() = 247. Calling vector.at(0) or vector[0] returns a value, changing the index to anything past 0 seems to cause the error. I've since seen "subscript out of range" errors.

I think this maybe a C++ issue or rather my lack of understanding of the language.

Still not resolved though so any help would be rad.

Thanks

Mickatron
Posts: 10
Joined: Sat May 30, 2015 8:07 pm

Re: C++ Retrieving Vector values causes fatal error

#3 Postby Mickatron » Mon Jun 01, 2015 6:10 pm

This was a C++ coding issue, I've since debugged it. Working but messy code below.



Code: Select all

if (Bars() >= Len) {
      //get SMA of period.
      int i = 0;

      double totalClosePrice = 0;

      for (i = 0; i < Len; i++) {
         totalClosePrice = totalClosePrice + Close(i);
      }
      double avg = totalClosePrice / Len;
      sprintf(buff, "tcp: %f avg: %f ", totalClosePrice, avg);
      Print(buff);
      //add SMA to the vector Arr
      arr.insert(arr.begin(), avg);
   }

   
   if (Bars() >= first) {
      //get TMA of period.
      double totalSMAPrice = 0;

      for (int x = 0; x < Len; x++) {
         totalSMAPrice = totalSMAPrice + arr[x];
         double arx = arr[x];

      }
      double avg = totalSMAPrice / Len;
      sprintf(buff, "tsmap: %f, avg: %f", totalSMAPrice, avg);
      Print(buff);

      TMABuffer[index] = avg;
   }


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 9 guests