Skip to content

Instantly share code, notes, and snippets.

@dpasca
Created February 24, 2018 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpasca/c75bc5ef1cd8d1ef4f4c50c500f274db to your computer and use it in GitHub Desktop.
Save dpasca/c75bc5ef1cd8d1ef4f4c50c500f274db to your computer and use it in GitHub Desktop.
//==================================================================
// curTimeUS: current time in micro-seconds
// idxB : index of the currency we're looking to buy (for BTC/USD: 0=BTC, 1=USD)
bool BTT_Agent::checkShouldBuyLineFit( TimeUS curTimeUS, const size_t idxB ) const
{
c_auto &tdis = mBTTask.GetCurTDIStream();
c_auto &entries = tdis.GetCurBatch().mTDEntries;
c_auto timeMaxS = TimeUSToSecs( curTimeUS );
c_auto timeMinS = timeMaxS - 60.0 * 60.0 * 12; // from previous N seconds
c_auto [i1, i2] = TD_FindEntriesInTimeRange( entries, timeMinS, timeMaxS );
if ( i1 == i2 ) // nothing in range ?
return false;
// create the (time,price) points
// NOTE: price in data is in Currency 1 (i.e. USD), so needs to be inverted for
// the prospective of the trend of a buyer of Currency 1
DVec<Double2> pts( i2 - i1 );
for (auto i=i1; i < i2; ++i)
{
// NOTE: negation of price works better than reciprocal, for some reason...
// (probably making up for different behavior of the currency)
c_auto &e = entries[i];
pts[i-i1] = { e.time_period_end.CalcTimeS(),
idxB == 0
? e.price_close
: -e.price_close };
}
// normalize the points to 0..1 range for both time and price
normalizePts( pts );
// calc the line fit
c_auto [m, b] = SU_CalcLineFit( pts );
// line segment for Xs in the selected time range ( timeMinS ... timeMaxS )
c_auto p1 = Double2( timeMinS, timeMinS * m + b );
c_auto p2 = Double2( timeMaxS, timeMaxS * m + b );
c_auto diff = p2 - p1;
// angle (in degrees, for readability)
c_auto ang = atan2( diff[1], diff[0] ) * 180.0 / M_PI;
// empirically determined threshold angles to issue the buy signal
return idxB == 0 ? ang > 1.5 : ang > 3.0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment