共享指针多态对象的引用未正确传递



我有一个程序,其主要方式调用模拟类对象。

int number_of_sims = std::stoi(num_sims)/MAX_THREADS_SIM_THREADS;
for (int inst=0; inst<Instruments.size(); inst++)
    {
        Simulation Simulations(Instruments[inst], Symbols[inst], number_of_sims);
        SimVector.push_back(Simulations);
    }

模拟类依次调用类分析和类策略,并具有以下骨架。

class Simulation
{
public:
    Simulation();
    Simulation(Instrument& instrument, String& Symbol, int runs);
    virtual ~Simulation();
    boost::shared_ptr<Strategy> getStrategyName(String& StratName);
    bool constructTimeSeries();
    bool InitializeStrategy();
    bool getMeanMTM();
    bool printStrategyTrades(int run);
    bool printAggregateMTM();
    bool Run();
private:
    String Symbol_;
    double Mean_;
    int runs_;
    Instrument instrument_;
    std::map<String, DoubleVector> AggregateMTM_;
    boost::shared_ptr<Analysis> analysis_;
    boost::shared_ptr<Strategy> strategy_;
};

构造函数看起来像这样。

Simulation::Simulation(Instrument& instrument, String& Symbol, int runs) 
: instrument_(instrument), Symbol_(Symbol), analysis_(new Analysis(instrument)), runs_(runs)
{
    String filelocation = "/else/abc/xyz/klmn/file.config";
    ConfigReader Config(filelocation, "=", false);
    Config.parseFile();
    String StrategyName = Config.getValue("strategyname");
    Logger::getLogger().log(DEBUG, "The Strategy name is: " + StrategyName);
    strategy_ = getStrategyName(StrategyName);
}

现在我在配置文件中对 XYZ 策略进行多态调用,如下所示。

boost::shared_ptr<Strategy> Simulation::getStrategyName(String& StratName)
{
    if (StratName.compare("XYZ") == 0)
        return (boost::shared_ptr<Strategy>(new XYZ(instrument_, Symbol_)));  
}

我的 XYZ 策略类继承了策略基础并填充 TS_(LongStruct 向量(成员变量,该变量是类 Strategy 的受保护成员,并使用它来执行某些功能,其中之一是获取向量的大小。

这两个类的骨架如下所示。

class Strategy
{
public:
    Strategy(Instrument& instrument, String& Symbol);
    virtual ~Strategy();
    virtual bool Initialize();
    virtual bool printData(std::ofstream& outputfile);
    virtual double getMeanMTM();
protected:
    virtual int CalculateTradeSignal(double& lower_limit, double& upper_limit, double& tau, double& meanTau, bool trade);
    virtual double CalculateMTM(double& EntryPrice, double& ExitPrice);
    std::map<int, String> Side_; 
    std::vector<String> TradeVector_;
    Instrument instrument_;
    LongToSymbolInfoPairVector TS_;
    String Symbol_, start_, end_, tradeend_;
    int numticks_, tradeticks_, tickinterval_;
    double MeanMTM_;
};

XYZ:

class XYZ : public Strategy 
{
public:
    XYZ(Instrument& instrument, String& Symbol);
    virtual ~XYZ();
    bool Calculate(); 
    bool CalculateTau();
    bool Initialize();
    bool updateNetPosition(int SigVal, int i, double& tau);
    double getMeanMTM();
    bool printData(std::ofstream& outputfile);
private:
    // Initializers
    bool entry_, trade_;
    int netposition_;
    long unsigned int tauMA_, lokBK_;
    long entryT_, exitT_;
    // TODO: Move the temporary variables, entryP, exitP, MTM to local variables 
    double entryP_, exitP_, MTM_, stoploss_, takeprofit_;
    double lowtau_, hightau_;    
    LongVector entrytimeVector_, exittimeVector_;
    IntVector posVector_;
    DoubleVector entryPriceVector_, exitPriceVector_, MTMVector_, tauVector_;
};

在 CalculateTau 函数中,我尝试获取已按以下方式初始化的TS_向量的大小。

bool XYZ::CalculateTau()
    {
        Logger::getLogger().log(DEBUG, "Calculating Tau  .. ");
        Logger::getLogger().log(DEBUG, "The size of the TS is: " + std::to_string(TS_.size()));
        return true;
    }
Strategy::Strategy(Instrument& instrument, String& Symbol) 
: instrument_(instrument)
{
}
bool Strategy::Initialize()
{
    TS_ = instrument_.GetTS();
return true;
}

在这里,获取 TS 执行以下操作:

class Instrument
{
    public:
    Instrument();
    virtual ~Instrument();
    LongToSymbolInfoPairVector GetTS() { return newTS_; }
private:
    LongToSymbolInfoPairVector TS_, newTS_;
};

现在的问题是我得到这个大小的值为 0。

请协助让我知道,我在这里哪里出错了。

我已经能够通过减少从模拟->分析->仪器到仪器->模拟->分析然后分析->策略的间接性来解决这个问题

相关内容

  • 没有找到相关文章

最新更新