9

I'm converting a TradeStation EasyLanguage indicator code to a C++ DLL. Using the TradeStation API it's possible to access market data in the C++ DLL like so:

double currentBarDT = pELObject->DateTimeMD[iDataNumber]->AsDateTime[0];

My question is:

Is it possible in C++ to somehow 'watch' or 'listen' for when the variable 'currentBarDT' has its value changed/updated? I would like to use the changing of the value as a trigger to generate a signal with Boost.Signals2.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
GoFaster
  • 835
  • 1
  • 12
  • 23

2 Answers2

3

You can use condition variable that fit your need.

http://en.cppreference.com/w/cpp/thread/condition_variable/notify_all

in the signals you update your market data (i)

in the wait you put a condition variable on i (is the stock under a certain level for example)

Tell me if you need more info I can detail and make it more explicit.

#include <stdlib.h>     /* srand, rand */
#include <iostream>
#include <condition_variable>
#include <thread>
#include <chrono>
#include <atomic>
std::condition_variable cv;
std::mutex cv_m;
double StockPrice;//price of the stock
std::atomic<int> NbActiveThreads=0;//count the number of active alerts to the stock market

void waits(int ThreadID, int PriceLimit)
{
      std::unique_lock<std::mutex> lk(cv_m);
      cv.wait(lk, [PriceLimit]{return StockPrice >PriceLimit ;});
      std::cerr << "Thread "<< ThreadID << "...Selling stock.\n";
      --NbActiveThreads;
}

void signals()
{
    while (true)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        std::cerr << "GettingPrice "<<std::endl;
        std::unique_lock<std::mutex> lk(cv_m);
        /* generate secret number between 1 and 10: */
        StockPrice = rand() % 100 + 1;  
        std::cerr << "Price =" << StockPrice << std::endl;
        cv.notify_all();//updates the price and sell all the stocks if needed
        if (NbActiveThreads==0)
        {
            std::cerr <<"No more alerts "<<std::endl;
            return;
        }
    }

}

int main()
{
    NbActiveThreads=3;
    std::thread t1(waits,1,20), t2(waits,2,40), t3(waits,3,95), t4(signals);
    t1.join(); 
    t2.join(); 
    t3.join();
    t4.join();
    return 0;
}

Hope that helps

Gabriel
  • 3,564
  • 1
  • 27
  • 49
  • Thanks for the code. Will look it over. The principle I planned to work with is that `pELObject->DateTimeMD[iDataNumber]->AsDateTime[0];` points to a market data DateTime value buried in TradeStation's API. When that DateTime increments, i.e. time has moved on by a specified interval, then a signal would be broadcast to carry out further calculations. – GoFaster Jul 22 '13 at 21:05
  • ok then you need to store the time in a variable (instead of stock price) and the condition variable will be on it. If you like the answer, can you accept is as valid. That will help other people (and upgrade my stats also :-)) – Gabriel Jul 22 '13 at 21:28
0

You can create some kind of storage class and implement operator= so in calls notifier. Might work with templates but you'd need to keep specifications in header. Also possible to create similar interface and override notifier call.