void VIXTrader::run(void) throw(TraderException) { TA ta; _invested_days = days(0); Series::EODSeries::const_iterator vix_iter(_vix_db.begin()); advance(vix_iter, 100); // Skip first 100 bars to give enough room too BBANDS calculation for( ; vix_iter != _vix_db.end(); ++vix_iter ) { try { // Calculate current invested days if( ! _miPositions.open().empty() ) _invested_days = _invested_days + days(1); // Calculate VIX BBANDS TA::BBRes resBBANDS3 = ta.BBANDS(_vix_db.close(vix_iter, 100), 100, 3, 3); TA::BBRes resBBANDS1 = ta.BBANDS(_vix_db.close(vix_iter, 100), 100, 1, 1); // Check buy signal if( _miPositions.open().empty() && vix_iter->second.close > resBBANDS3.upper_band[0] ) { // Buy tomorrow's close Series::EODSeries::const_iterator iter_entry = _spx_db.after(vix_iter->first); if( iter_entry == _spx_db.end() ) { cerr << "Can't open position after " << vix_iter->first << endl; continue; } buy(_spx_db.name(), iter_entry->first, Price(iter_entry->second.open)); } // Check sell signal if( ! _miPositions.open().empty() && vix_iter->second.close < resBBANDS1.upper_band[0] ) { // Get next bar Series::EODSeries::const_iterator iter_exit = _spx_db.after(vix_iter->first); if( iter_exit == _spx_db.end() ) { cerr << "Can't close position after " << vix_iter->first << endl; continue; } // Close all open positions at tomorrow's close PositionSet ps = _miPositions.open(); for( PositionSet::const_iterator pos_iter = ps.begin(); pos_iter != ps.end(); ++pos_iter ) { PositionPtr pPos = (*pos_iter); close(pPos->id(), iter_exit->first, Price(iter_exit->second.open)); } // end of all open positions } } catch( std::exception& e ) { cerr << e.what() << endl; continue; } } // for each bar }
void Report::max_cons_neg( void ) const { if( _rf.num() == 0 ) return; // avoid exception during report PositionSet pset = _rf.max_cons_neg(); cout << "Max cons neg: "; if( pset.empty() ) { cout << 0 << endl; return; } cout << (int)pset.size(); _begin_end(pset); }
void Report::max_dd( void ) const { if( _rf.num() == 0 ) return; // avoid exception during report PositionSet pset = _rf.dd(); cout << "Max drawdown: "; if( pset.empty() ) { cout << 0 << endl; return; } ReturnFactors ddrf(pset); cout << ddrf.roi()*100 << '%'; _begin_end(pset); }
bool operator()(const PositionSet& pset1, const PositionSet& pset2) const { return pset1.realized() < pset2.realized(); }
bool operator()(const PositionSet& pset1, const PositionSet& pset2) const { return pset1.size() < pset2.size(); }