Experiment on Simple Probabilistic Model

Is the stock market really random? Can we see it as a probabilistic system where the prices rise and fall with certain possibilities in certain times? Should we really trust numbers? (This is important for quants :)

I tried (again) Apple and decided to buy if a random number between 0 and 1 is larger than the probability of price rise within the last 5 days. Note that if we assume uniform distribution, that corresponds to the fact that we buy with exactly the same probability for the price to rise the next day. I used batch transform to make the code more readable. (Thanks to Thomas for the advice)

The result turned out to be pretty nice. I tweaked the window length to 10, and it didn't change much. Seems that the market can at least be roughly treated as a probabilistic model.

58
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
import random

# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def initialize(context):
context.stock=sid(24)
context.bet_amount=100000
context.max_notional=1000000.1
context.min_notional=-100000.0

# Will be called on every trade event for the securities you specify.
def handle_data(context, data):
# Implement your algorithm logic here.
stock=context.stock

if probofGain(data, stock) is None:
log.info("undefined probability")
return
else:
prob=probofGain(data, stock)
random_number=random.random()

cur_price=data[stock].price
notional=context.portfolio.positions[stock].amount*cur_price

if (random_number<prob) and notional>context.min_notional:
order(stock, -context.bet_amount/cur_price)
elif (random_number>=prob) and notional<context.max_notional:
order(stock, context.bet_amount/cur_price)

@batch_transform(refresh_period=1,window_length=5)
def probofGain(datapanel, sid):
prices=datapanel['price'][sid]
flag=0
for i in range(4):
if prices[i] is None:
return None
elif prices[i]<prices[i+1]:
flag+=1
return (flag/4)

# data[sid(X)] holds the trade event data for that security.
# data.portfolio holds the current portfolio state.

# Place orders with the order(SID, amount) method.


This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
1 response

HI,

How do you define the probability of price rise within last 5 days? Just trying to understand your approach to your experiment. Thanks.