Back to Community
First day and First Algorithm!


This is my first day on quantopian and I've created my first algorithm based on a theory I've had for a while now.

Theory: ERX and ERY are direct inverse mirrors of each other. As one goes up the other goes down. If I can setup an algorithm that simply buys one on its way up and sells the other on its way down, profits can be made no matter what the market condition.

Reality: As you will see when you test my algorithm the return is nearly none existent, if not a complete loss.

I guess I'm looking for any sort of comments and or suggestions; coding related and or theory related. As noted above, this is my first go at a trading algorithm and I'm very new to technical trading so any ideas or suggestions would be much appreciated.


ERX / ERY - 10/30 cross

Theory: ERX and ERY are inverse of each other. If they are bought and sold in an inverse manor money can be made regardless if the market is going up or down.

Created: 6/4/2017


Start of Algorithm

def initialize(context):
#define ERX and ERY sid's
context.ery = sid(37044)
context.erx = sid(37513)
#schedule a single trade everyday 15min after market open
schedule_function(erx_ery_cross, date_rules.every_day(), time_rules.market_open(minutes = 15))

define the trading logic

def erx_ery_cross(context, data):
#set var for security to be traded
stk_erx = context.erx
stk_ery = context.ery
#populate moving average information for both securities
MA10_erx = data.history(stk_erx, 'price', 10,'1d').mean()
MA30_erx = data.history(stk_erx, 'price', 30,'1d').mean()
MA10_ery = data.history(stk_ery, 'price', 10,'1d').mean()
MA30_ery = data.history(stk_ery, 'price', 30,'1d').mean()
#define current amount of stock owned for each security
current_erx = context.portfolio.positions[stk_erx].amount
current_ery = context.portfolio.positions[stk_ery].amount
#tradeing logic: compare MA and look for cross. perform this for BOTH securites
if (MA10_erx > MA30_erx) and current_erx == 0:
order_target_percent(stk_erx, .25)
elif (MA10_erx < MA30_erx) and current_erx != 0:
order_target(stk_erx, 0)
if (MA10_ery > MA30_ery) and current_ery == 0:
order_target_percent(stk_ery, .25)
elif (MA10_ery < MA30_ery) and current_ery != 0:
order_target(stk_ery, 0)

1 response

I don't see a backtest, so I really don't know.

But order_target_percent(stk_erx, 0.25) will only invest 25% into your account, i.e. if you have $100 you would only invest in $25. I would look closely at how the money is being managed. Change 0.25 to 1.0, and you will invest the full account on your signal. It might inflate the returns, and you might invest more then what is in your account.

I just helped this guy who was borrowing to much. If you use def count_positions(context,data): you will know more about the money management.

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
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
# Backtest ID: 593aff379d368f528afe872c
There was a runtime error.