/*
Compile: g++ -Wall -O2 -std=c++11 BuySell.cpp
NOTE: before compiling, please make sure both GBM.cpp and BSM.cpp are present in the same directory as this file, and that their main() functions are commented out
Usage Example: ./a.out 90 100 30 252 7800 > replays.csv
The example will simulate buying 1 CALL option (exercise price $100, 252 days till expiration, 30% stock volatility) when stock price is $90
then simulate brownian stock movement for 7800 bars (10 days)
them sell 1 CALL option at the current fair price
This scenario is then replayed 1000 times, with everything being the same, except different random path is generated for the stock.
For comparison the price of PUT is also calculated.
After 1000 replays the average profit of all CALL and PUT trades is printed out.
The detailed information about all 1000 replays has been redirected into replays.csv file which can be opened and further analyzed with Excel.
*/
#include "GBM.cpp" // make sure main() in GBM.cpp is commented out in this file
#include "BSM.cpp" // make sure main() in BSM.cpp is commented out in this file
int main(int argc, char* argv[])
{
if (argc < 6) { printf("%s dbSpot dbStrike dbAnnVola%% dbExpDays holdBars\n", argv[0]); return 1; }
const double dbSpot = atof(argv[1]);
const double dbStrike = atof(argv[2]);
const double dbAnnVolaPct = atof(argv[3]);
const double dbExpDays = atof(argv[4]);
const double holdBars = atof(argv[5]);
const int replayCycles = 1000;
const int nBarsPerDay = 780;
const double holdDays = (double)holdBars / nBarsPerDay;
double totalCallProfit = 0;
double totalPutProfit = 0;
printf("ReplayCycle, stockStartPrice, callBuyPrice, putBuyPrice, stockEndPrice, callSellprice, putSellPrice, CALL PROFIT, PUT PROFIT\n");
for(int c = 1; c <= replayCycles; c++)
{
BSM B;
//
// Start
//
double stockStartPrice = dbSpot;
B.calc(stockStartPrice, dbStrike, dbAnnVolaPct, dbExpDays);
double callBuyPrice = B.Call.Value;
double putBuyPrice = B.Put.Value;
// Move stock by n holdBars
GBM G(dbSpot, dbAnnVolaPct, nBarsPerDay);
for (size_t b = 1; b <= holdBars; ++b)
{
G.generate();
}
//
// End
//
double stockEndPrice = G.get_cur();
B.calc(stockEndPrice, dbStrike, dbAnnVolaPct, dbExpDays-holdDays);
double callSellPrice = B.Call.Value;
double putSellPrice = B.Put.Value;
double callProfit = callSellPrice - callBuyPrice;
double putProfit = putSellPrice - putBuyPrice;
totalCallProfit += callProfit;
totalPutProfit += putProfit;
printf("%d, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f\n",
c, stockStartPrice, callBuyPrice, putBuyPrice, stockEndPrice, callSellPrice, putSellPrice, callProfit, putProfit);
}
double avgCallProfit = totalCallProfit / replayCycles;
double avgPutProfit = totalPutProfit / replayCycles;
fprintf(stderr, "=== SUMMARY ===\n");
fprintf(stderr, "After %d replays the AVG CALL PROFIT=$%.6f and AVG PUT PROFIT=$%.6f\n", replayCycles, avgCallProfit, avgPutProfit);
return 0;
}