Python Datetime 'strftime()' | Convert Date to Text Format
01:00 - 03:30 | Get Stock Data With Yahoo Finance 03:31 - 06:04 | Convert Date Format To Text 06:05 - 06:43 | Slicing doesn't work due to keyword "&" "and"
Quick note:
I couldn't slice the dataframe based on column 'df['new_date']' because the keyword "and"
was not used when coding in Python IDLE.
In Sublime Text 3, I used "&" instead of "AND" and the slicing worked.
So, remember to use "and" instead of "&" when coding in Python IDLE.
First of all, thank Matt Skaggs for his article regarding Google Chrome Installation Other Than C Drive: http://smallbusiness.chron.com/change-installation-path-google-chrome-42689.html
He introduced the way to use the "Junction" program to create link between the original Chrome installation location (normally, "%LocalAppData%\Google\Chrome" ) and the destination folder
(wherever you want, e.g. D:\Program Files\Google\Chrome)
I am happy with Sublime Text 3 for my Python coding.
But since VSCode is free, I'd like to give it a try.
To be honest, the task runner setup was more complicated that I expected.
Anyhow, I managed to set this up on a USB.
Python Environment
1) Python 3.6.3 - Anaconda , with environment being created with Anaconda Navigator
2) Operating System: Windows 10
Steps
1) Install Anaconda (location being set to D drive (drive other than default C))
2) Create Environment With Anaconda Navigator (Optional)
This step is optional if you're happy with the Anaconda root installation.
I tend to create different projects in different environments (Python virtual environment),
just a matter of habit.
3) Download VSCode and Setup
This can be quite tricky when you need to setup the task runner to run Python code.
Just walkthrough the whole process of creating Python backtest in Anaconda, Windows 10.
The process is broken down into 5 major steps:
00x | Install Anaconda
10x | Get online stock (e.g. AAPL) | Market Data
20x | Create trade strategy | Strategy Class
30x | Generate trade signal | Signal Class
40x | Create investment portfolio | Portfolio Class
50x | Perfomance review | PnL + Graphing
A timeline is provided below for easy navigation:
In case you just want to use the Anaconda root setup, skip step 'Ana002'
mm:ss
00:00 - 03:02 ||Ana001| Install Anaconda
03:04 - 10:02 ||Ana002|Setup different environment other than root - optional step*
10:04 - 19:02 ||Ana101| Get Apple stock data from Yahoo Finance, save to local drive as Excel
19:04 - 29:11 ||Ana201| Create Strategy - calculate moving averages
29:12 - 35:53 ||Ana202| Create Strategy - get stock trend
35:55 - 41:22 ||Ana301| Get Trade Signal and Generate Order
41:24 - 49:50 ||Ana401402| Create Investment Portfolio and Trade Details
49:52 - 59:03 ||Ana501502| Calculate Profit and Loss and Graphing
* install optional Python packages that your project requires
** the strategy mentioned assumes zero borrowing cost so that whenever short position occurs,
we can borrow unlimited amount of money without needing to pay a penny of interest.
This is not real in real world. So just be aware of that.
Here's the 59-min video(Complete Walkthrough - Jupyter Notebook Anaconda):
*** this video is rendered in 1.05x speed; feel free to visit the original speed videos below.
Install Anaconda 3.5.0.1(Ana001)
Setup Environment In Anaconda 3.5.0.1(Ana002) - Optional
Get Online Stock Data + Save To Local Drive(Ana101)
Form Trade Strategy - Calculate Moving Average (Ana201)
Form Trade Strategy - Get Stock Trend (Ana202)
Generate Trade Signal - Get Stock Trend (Ana301)
Create Investment Portfolio (Ana401402)
Measuring Performance & Graphing (Ana501502)
Backtest With Python Default Editor
My Thought
Comparing to buidling Python strategy backtest in default Python editor,
the Anaconda Jupyter Notebook takes a bit of time to get used to.
It takes time to load up the Jupyter Notebook while such load time is minimal in Python default editor.
But from layman's perspective, setting up multiple environments and installing packages
through the Anaconda Navigator are intuitive.
Anaconda basically organizes the folders under the 'env' folder.
That's good folder management.
In the future, I'll try using Sublime Text 3 to route to the Anaconda's environment folder.
See if I can take advantage of Sublime Text' speed while maintaining proper environment folder
management.
If you find this article helpful, feel free to discuss on my Facebook:
www.facebook.com/clueple
Disclaimer:
Everything shown in this video is just the author's personal experience with Anaconda.
All mentioned assumptions and tactics are used as demonstration for education purpose.
Trade details, not limited to, leverage level, deposit amount, transactions fees, and
any costs associated with the strategy are not meant to be complete.
No investment decision should be based on any information provided in this video.
No professional advice are provided or implied whatsoever.
Viewers are solely responsible for any loss associated to consumption of this video.
Finally finished up my 1st backtest with Python.
Just shoot this video to help remind myself the basics
and hopefully, this can help people get their feet wet
on Python coding ... (advanced users feel free to jump around using the time table below)
Here, I am showing you 2 videos where the first one is the Python setup (from scratch)
and the second one is the backtest creation.
YouTube video link - Install Python & Related Packages:
*Note that I installed Python to specific folder "py362" but you can feel free to decide the destination folder location.
**Also, I setup the Sublime Text 3 as my code editor. Again, you can pick your own editor.
In the backtest creation video, I used the Python built-in editor (IDLE).
***I created the virtualenv (virtual environment) for my trade algo.
You don't need that for backtest. But it's highly recommended if you want to build Python projects, other than trade algo.
YouTube video link - Creating backtest:
https://www.youtube.com/watch?v=D73NyNb2YZs
Time Table |English Version
hh:mm:ss
00:00:09 - 00:08:24 | part 1| Get stock data from Yahoo Finance + Save to Excel
00:08:26 - 00:14:49 | part 2.1| Strategy class - defininng basic parameters
00:14:50 - 00:21:21 | part 2.2| Strategy class - calculate moving averages
00:21:22 - 00:30:53 | part 2.3| Strategy class - calculate trend details
00:30:54 - 00:37:58 | part3.1| Signal class - calculate trade signal and order
00:30:59 - 00:45:51 | part4.1| Portfolio class - calculate trade amount
00:45:52 - 00:52:26 | part4.2| Portfolio class - calculate cash balance and asset position
00:52:26 - 00:57:56| part 5.1| calculate profit and loss + plot graph
For part 4.2 "cash_balance and asset position",
you can read Isaac Kwan's code for traditional stock trade style.
He offered great help in explaining the use of abstract-based class of Python (I am still figuring that out :))
https://gitlab.com/snippets/1681494
Python script: (underlying stock being changed to 0388.HK)
https://github.com/clueple/algo/blob/master/hkex_full_example.py
Isaac Kwan's link on part 4.2 - cash balance and asset position
https://gitlab.com/snippets/1681494
Finally, please read the video disclaimer
and by no means, this video be used for real trade.
Just wonder if Win 10 Creator breaks my Asus laptop*
My laptop broke down for the 1st time after forced Win 10 Assistant Update
A few days ago, should be before Oct 29, 2017,
forced Win 10 Assistant Update again; breaks down on Nov 1, 2017
Asus laptop breaks down again on Nov 1, 2017
Asus laptop 1st break-down on Oct 11, 2017; Repair back on Oct 22
*
Asus A555 UQ-DM044T
i7-6500U
120 SSD + 1TB HDD
Purchase Date: Dec 08, 2016
1st Break-Down: Oct 11, 2017
1st Repair: Oct 22, 2017
2nd Break-Down: Nov 01, 2017
Test iOS 11.0.3 Screen Recording On iPhone SE 2017
Testing the iOS 11.0.3 update - screen recording feature Device : iPhone SE (16 GB); Rendering platform: Facebook
Setup steps: 1) General ... Control Center ... add screen recording 2.1) General ... Restrictions ...type code to get in... 2.2) turn 'Screen Recording' to green ...Screen Recording inside 'Game Center'...
Recording Steps: ...starting from the main screen... 1) swipe the phone from the bottom 2) press the button ...screen recording button... 3) When you see flashing red ...see video start of this video..., you're recording 4) Once you finish recording, go back to step1... and press the button to stop
I was just looking for this setup for long.
Finally, found that pointing the path in the Sublime Text 3 build file (video 05:14 - 08:07)
can make it work.
The reason why I setup virtual environment because I need to separate different projects
based on purposes.
In this video, I pip install 2 packages - pandas and xlrd - to my virtual environment "myTradeEnv" to read the stock data from a csv file.
When you're in Sublime Text, type your Python code and save as a new Python file.
Then you go to Tools -> new build system and paste your code there.
Once you save that Sublime Text build file, get back to your Python file
and hit [Ctrl] + [B] (build) to run your code.
00:00 - 01:23 | Download and install Python
01:23 - 03:01| install virtualenv to system
03:19 - 04:09| create virtual environment myTradeEnv
04:22 - 05:11| download sublime text 3
05:14 - 08:07| setup sublime text 3 to run Python virtual environment 08:10 - 11:10 | test running python code in virtual environment to grab stock data from a csv file
Just tried using Python on USB via WinPython.
It's pretty handy.
Simply double-click the WinPython execute file downloaded from its official website.
And you'll see a zip folder.
Unzip it to wherever you want (in my case, a USB stick).
You're ready to code.
Simply double-click the icon "Spyder" to open up the code editor.
To use the program, simply extract the downloads to your destination drive.
Get into the Shotcut folder and double click the "Shotcut" exe file.
When you get into Shotcut, you will see a blank interface.
The first thing to do is adding a timeline (see 02:54 - 03:43 of video)
Basic Principles
I am talking about the fancy effects here.
Simply learn the hotkey to
1) move along the playhead (cursor of timeline) (forward, pause, rewind)
2) cut (called "split" in Shotcut) and ripple delete (delete part of the clip and snap the following one to the previous clip)
3) transition (from one clip to another)
4) change clip speed (make the video faster or slower, applicable to audio as well)
and do
5) picture in picture (PIP, that means, put an image/video on top of another video track)
The details are shown in the YouTube video below.
Conclusion
If you used other editor before, you might notice the lack of keyframming feature in Shotcut. It's the major disadvantage of this little giant. But still, I like the hotkey and ability to add effects (filters) with simple CSS and HTML. Shotcut might not be suitable for big projects though.
Anyhow, It's a good starting point to learn video editing.
Forza! Disclaimer: All features are applicable to my setup which might not function the same in your cases. Use at your own risk.
In this case, my downloaded whl file "opencv_python-3.2.0-cp36-cp36m-win_amd64.whl" is in my folder "Downloads" inside "F" drive
Step 2 - Activate Virtual Environment
If you haven't setup a virtual environment, please follow along the following video
(00:09 sec - 00:17 sec) to install virtualenv
and
(00:18 sec - 01:16 sec) to virtual environment setup
Go to the virtual environment that you want to use the whl file
In this case, my virtual environment "testGraphics" inside
folder "graphics" in "F" drive.
Go to "F" drive (if you're from default C drive,
simply type "F:"; if you're already in "F" drive,
type "cd \" to get back to the top directory of "F" drive
Activate your virtual environment with the script below:
.\graphics\testGraphics\Scripts\activate.bat
where "graphics" is the folder that holds the virtual environment
and "testGraphics" is the virtual environment folder
Step 3 - Get to the Whl File Location
Once you've activated the virtual environment ("testGraphics"),
go to the file location that your downloaded whl file sits:
In this case, my whl file ("opencv_python-3.2.0-cp36-cp36m-win_amd64.whl")
sits inside folder (F:\Downloads).
Since I am in F drive already, I simply type:
cd Downloads
to get to the "F:\Downloads" folder
Step 4 - Pip Install The whl File And Import Package
Now, you're in F:\Downloads folder.
Type the code below to install the whl file:
My desktop (name it "deskA") can't run certain programs that I target to capture.
But that program can be run on my laptop (name it "lapB") .
Therefore, I decide to play the program on "lapB" and mirror everything there to "deskA"
To do this I prepare the following hardware route:
1) "lapB"(laptop) - running the program
1.1) HDMI male-to-male cable
2) capture card - take in what's done in on "lapB" via the HDMI-in
2.1) capture card (micro-usb 2.0 out to "deskA" (desktop))
3) "deskA" - turn on the recording program to record everything
passed through the capture card in (2)
Step-by-Step Instruction
Step 1 - Install capture card to your recording PC ("deskA")
In my case, I have a legacy capture card, Avermedia C875 (LGP) that can record up to 1080p @60fps.
1.1)Plug the usb cable (given along with your capture card) to "deskA" .
1.2)Switch to the PC mode (in this case, the blue light indicates PC mode is on whereas red light
signals PC-free mode is on)
1.3) Go download the driver at the following site (depends on your capture card)
https://www.avermedia.com/gaming/download/live_gamer_portable
1.4) download ALL the drivers and then follow the installation (basically double click the firmware and follow along the installation process)
Now, you're done and you're in PC mode (that means your PC ("deskA") will be able to take in video and audio signal via the capture card.
What you'll see is the capture card with Blue light on (fixed no flashing blue light)
That capture card is hooked up to "deskA" (your recording machine) via micro-USB cable like below
Since we're going to capture what's done from another computer ("lapB"), we need to setup
our capture card to capture "Other PC"
Step 2 - Hookup your video source (e.g. "lapB") to the capture card
Before that, you need to hookup your video source device (e.g. "lapB") to the capture card's via HDMI. It'll be like this:
At that point, you're hooking up the machine like this:
Recording PC ("deskA") -> micro-USB -> capture card -> HDMI(in) -> Video Source (e.g. "lapB" HDMI(out))
Step 3 - Setup capture card on recording PC ("deskA") to grab video
Now, go back to "deskA".
Turn on the setup program (in our case, the RECentral),
Click the "Wizard" and go to "Platform", select "Other PC" like below
This means "asking "deskA" to get signal from "Other PC", which is "lapB" that we just hooked up".
Click "Video Source" and you'll see what's playing on "lapB" like below:
If you're playing game or YouTube, whatever, then, you'll see them in "deskA" also.
Remember, the above screen is on "deskA".
Then move on to "Audio Source" and you'll see below:
At this stage, your "deskA" (recording PC) is ready to capture both video and audio from
"lapB" (video source PC).
But, things get tricky here.
Normally, you "lapB" (laptop) won't be playing sound from the capture card
(you'll only hook up the capture card to your laptop ONLY WHEN YOU NEED TO RECORD)
So, we need to jump to step 4
Step 4 - Route the video source PC's audio ("lapB") to play through capture card
You're now back to your video source ("lapB" laptop that you want to capture)
Normally, you play your audio with the default speaker.
First of all, from your laptop ("lapB"), move your cursor to the "speaker" icon and then "right click" it.
Then, click the "Recording Device" tab.
Your default audio recording device (which in my case, the default speaker) has a green "tick" beside.
Double click the default device (e.g. the default speaker) icon and you can select "listen to"
devices (like the right hand side of the above image)
"Tick" the "listen-to device" and select your capture card (e.g. Avermedia C875 capture card) like below
Now, both the video and audio you play on "lapB" will be streamed to "deskA"
Step 4 - Record on "deskA"
Go back to "deskA" and start recording:
At this stage, you have 2 options:
1) Use the capture card's software (e.g. RECentral) to record to "deskA"
OR
2) Use other recording software to record the "screen being streammed from "lapB" to "deskA")
I prefer the route (2) because I can use software like OBS Studio (download here) to add overlay and other tweaking on my video rather than purely recording the raw screen being streamed from "lapB".
Again, this is a personal preference.
Do whatever you see fit.
Final Note
The way of capturing other PC from one PC is called "Dual PC setup".
It's mainly adopted in gaming on legacy PC.
Let's say if I want to game on my more advanced laptop ("lapB") but I don't want to push too hard
to it. Then I will record my laptop with another desktop ("deskA").
Of cause it might seem costly but if, in my case, I got an extra legacy desktop and capture card,
I could use the dual PC setup to capture much better video and audio.