Quantopain Provides required API functions,Data,Helpful-community as well as batteries included Web-based Dashboard to play with Algorithmic-Trading, Create Your own trading Strategies, and launch your Trading model in live Market.
Here I will only talk about code and how it should be written to create your own Trading Strategy.
There are basically Two methods.
initialize() and handle_data()
initialize act as initializer for various variables. same as __init__ method in Python.https://wordpress.com/post/arshpreetsingh.wordpress.com/1215
Now what kind of variables we have to declare in initialize() function is dependent on your strategy. we can select limited number of stocks,days,type of trading,variables required for Algorithms.
A very simple example of initialize() code could look like as follows:
def initialize(context): # consider context just as 'self' in Python
context.stocks = [sid(24),sid(46632)] # sid stands for stock_id
Momentum Strategy:(Common Trading Strategy)
In this strategy we consider Moving average price of stock as an important factor to make decision to put a security price in Long or Short.
Here is simple explanation of momentum Strategy:
● If the current price is greater than the moving average, long the security
● If the current price is less than the moving average, short the security
Now we will use Quantopian API to implement this strategy for Trading. instead, our algorithm here is going to be a little more sophisticated. We’re going to look at two moving averages: the 50 day moving average and the 200 day moving average.
David Edwards writes that “the idea is that stocks with similar 50 & 200 day moving averages are more likely to be fairly valued and the algorithm will avoid some of the wild swings that plague momentum strategies. The 50/200 day crossover is also a very common signal, so stocks might be more likely to continue in the direction of the 50day MA because a lot of investors enter and exit positions at that threshold.”
The decision-making behind Moving-average is as follows:
● If the 50 day moving averages is greater than the 200 day moving average, long the security/stock.
● If the 50 day moving average is less than the 200 day moving average, short the security/stock
So now Let’s make a Trading Bot!
1. First we have to create our initialize() function:
‘’’Set universe is inbuilt function by Quantopian which provide us the stocks with-in required universe. Here we have selected stocks those we have DollarVolumeUniverse with 99.5% and 100% as our floor and ceiling. This means that we’ll be selecting the top 99.5 ~ 100% stocks of our universe with the highest dollar*volume scores.
Please read the comments in the code.
context.stocks_to_long = 5
context.stocks_to_short = 5
context.rebalance_date = None # we will get today's date then we will keep positions active for 10 days here
context.rebalance_days = 10 # it is just an assumption now for 10 days or finer value
Now we have defined required __init__ parameters in initiliaze() let’s move to
# if rebalnce date is not null then set next_date for changing the # position of algorithm
next_date = context.rebalance_date + timedelta(days=context.rebalnce_days)
# next_date should be that days away from rebalnce_date
if context.rebalance_date==None or next_date==get_datetime():
# if today is that day after 10 days when we market long/short for #out stock
context.rebalnce_date = get_datetime()
#set rebalnce_date for today so next_date will be set to again 10 #days ahead from rebalnce_date
historical_data = history(200,'1d',price)
The `order_target_percent` method allows you to order a % target of your portfolio in that security. So this means that if 0% of your total portfolio belongs in AAPL and you order 30%, it will order 30%. But if you had 25% already and you tried ordering 30%, it will order 5%.
You can order using three different special order methods if you don’t want a normal market order:
#`stop_price`: Creates a stop order
#`limit_price`: Creates a limit order
#`StopLimitOrder`: Creates a stoplimit order
How Trading Differentiates from Gambling:
How Trading Differentiates from Gambling:
Most of times when you find that you are able to get good returns from your capital you try to beat the market, Beating the market means most of the traders tried to earn much more than fine earnings are being returned by the market for your stock, Such beating the market process can be done by various actions like reversing the momentum or looking for bad happenings in the market(which is also called finding the shit!)Some people are really good at this kung-fu but as you are just budding trader and you have only limited money of yours, So here one important thing should be remembered, “”Protect your capital””. — That’s what most of the Big banks do and if they will hire you as their Quant or Trading-Execution person they will expect same from you. Big banks have billions of dollars that they don’t want to loose but definitely want to used that money to get good returns from market.
So they follow one simple rule for most of the times.
Guaranteed returns even if those are low.
[Make sure returns should be positive after subtracting various costs like brokerage,leverage etc, Because getting positive returns by neglecting market costs is far easy but such strategies should not be used with real money.]
So the real key is think like a computer programmer at first place, something like it should work at first place, so first thing to make sure is getting returns even low but stable returns by calculating various risk-factors.
I am quoting some of informative things from SentDex Tutorial:
Most individual traders are trading on account sizes of somewhere between maybe $25,000 and $100,000 USD, so their motives are to hopefully increase that account size as much as possible, so this person is more likely to take part in High Risk High Yield (HRHY).
Most people who use HRHY strategies, tend to ignore the HR (High Risk) part, focusing on the HY (High Yield).
The same is common with gamblers,even over astronomical odds with things like the lottery.
In other words, always ask yourself — what’s about the market that makes my strategy work? Because, at the end of the day, algorithmic trading is more about trading than about algorithm.