Skip to content

Commit

Permalink
Rearrange Code
Browse files Browse the repository at this point in the history
  • Loading branch information
askmrsinh committed Nov 25, 2019
1 parent f0c5fd1 commit 935a983
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class InvestmentBroker(private val spark: SparkSession,
// TODO: Cleanup, rethink calculation approach

/**
* A dummy investment strategy to buy and sell stocks on any given day (observation)
* A dummy investment strategy to buy and sell stocks on any given day (observation).
*
* On the first day, equal amount of money is allocated for buying each of the symbols
* in the portfolio. There after, stocks are bought/sold when the mean percentage change
Expand Down Expand Up @@ -80,8 +80,8 @@ class InvestmentBroker(private val spark: SparkSession,
})
allStockEstimatesDF.show(252 * startingPriceMap.size)

println(f"Investment amount: ${investmentValue}")
println(f"Amount at the end of simulation: ${remainingFunds}")
println(f"Investment amount: $investmentValue")
println(f"Amount at the end of simulation: $remainingFunds")
println(f"change: ${(remainingFunds - investmentValue) * 100 / investmentValue}%%")
}

Expand All @@ -98,7 +98,7 @@ class InvestmentBroker(private val spark: SparkSession,
quantityBought = math.floor(marketCost / predictedValue)
} else {
val day = observation.getInt(0)
println(f"Insufficient funds to buy ${observation.getString(1)} @ ${predictedValue} on ${day}.")
println(f"Insufficient funds to buy ${observation.getString(1)} @ $predictedValue on $day.")
}
remainingFunds = remainingFunds - (quantityBought * predictedValue)
observation.toSeq ++ Seq(quantityBought, quantityBought * predictedValue, remainingFunds, "BUY")
Expand All @@ -116,10 +116,9 @@ class InvestmentBroker(private val spark: SparkSession,
def sell(observation: Row,
mean_pct_change: Double, predictedValue: Double): Seq[Any] = {
var quantity = 0d
investmentResults.filter(_.getString(1) == observation.getString(1)).foreach(
x => {
quantity = quantity + x.getDouble(6)
}
investmentResults.filter(_.getString(1) == observation.getString(1)).foreach(x => {
quantity = quantity + x.getDouble(6)
}
)
var marketCost = 0d
var quantitySold = 0d
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class MontecarloSimulation(private val spark: SparkSession,
}

/**
* Calculates the expected daily return percent based on stock's deviation, drift.
* Calculates the expected daily return based on stock's deviation, drift.
*
* @param distribution probability distribution to use.
* @param drift calculated drift for the stock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ object RunMontecarloSimulation {
val simulation = new MontecarloSimulation(spark, stocksDataFolderPath, stockSymbol)
val stockDF: DataFrame = simulation.readStockFile

stockDF.show(5)
if (logger.isDebugEnabled()) {
stockDF.createOrReplaceTempView("stockDF")
stockDF.printSchema()
stockDF.show(5)
}

val (variance, deviation, mean, drift) =
Expand All @@ -51,14 +51,17 @@ object RunMontecarloSimulation {
val dailyReturnArrayDF: DataFrame = simulation.formDailyReturnArrayDF(spark,
timeIntervals, iterations, new NormalDistribution(0, 1), drift, deviation
)
dailyReturnArrayDF.show(5)
val priceListArrayDF: DataFrame = simulation.formPriceListsArrayDF(spark,
stockDF, timeIntervals, iterations, dailyReturnArrayDF
)
priceListArrayDF.show(5)

val priceListDF: DataFrame = simulation.transormArrayDataframe(spark,
priceListArrayDF, iterations)
simulation.summarizeSimulationResult(stockSymbol, priceListDF)
priceListDF.show(5)

simulation.summarizeSimulationResult(stockSymbol, priceListDF)
priceListDFs += (stockSymbol -> priceListDF)
})

Expand Down

0 comments on commit 935a983

Please sign in to comment.