---
# System prepended metadata

title: 'Stock Algorithm: Short Selling'
tags: [Documentation, Templates]

---

---
title: 'Stock Algorithm: Short Selling'
disqus: hackmd
---

Stock Algorithm: Short Selling
===


## Table of Contents

[TOC]

## Backtesting the Algorithm
This module will contain backtest results of algorithm, as well as the paramaters and methods of execution and testing.

## Creation of Algorithm
This will also contain the syntax of our code. The structure goes like this:

**Setting Data Variables**
```def initialize(context, data):```
This is the part where we pass the data in.

**Processing and Signal Data**
```def handle_data(context, data)```
We pass the data here to see what decisions to make when interpreting said data. I assume this is the algorithm part.

**Execution Data**
```def run_algorithm(func=handle_data, date rules):```
This is the part where we assign the temporal execution of the code for our API. The handle_data code already processes the data in different dates of data. The signal is what we need to extract. 

## Trading Brokerage and Its API

The brokerage being used is Interactive Brokers. It has a subset of software called Traders Workstation. We will set up TWS or IB Gateway (IBG is better because its avoids issues like time wasting and IB system restarts).

* **Download and Install Traders Workstation**

1. Set up account and go paper trading.
2. Activate "ActiveX and Socket Clients", change port "7497" (for paper trade), or change port "7496" (for real trade; default port). (Make sure to change this is `settings.`) 
4. Uncheck "Read-Only API".
5. Keep program open

## Operating with IBridgePy
I don't know what this is. It says its a wrapper which can also pull in data. Thus it takes both data and helps executes order. It's an interactive IDE.

This was documented on Quantra. We shall use this: *Automated Trading with IBridgePy using Interactive Brokers Platform*. Be more familiar with conda. 

After review, the syntax and functions are similar to the structure of Quantopian. Where we define certain schedules, data handling, algorithm, and order execution. This is cool.

Download here: https://ibridgepy-portal.herokuapp.com/

### Install Science Package
**Install Anaconda**
A platform used to help with package and version control. Widely used in science community.

Downloaded [here](https://repo.anaconda.com/archive/Anaconda3-2020.11-Windows-x86_64.exe)

**Create New Environment**

* Open CMD terminal
* type `conda create -n Python3.7 python=3.7 anaconda`

:::info
Also this: `conda create -n <environment name> python=<python package version>`
:::

**Activate Environment**
* `conda activate Python3.7`
:::info
Also this: `conda activate <env_name>`
:::

### Get IBridgePy
Register and download package.

Link [here](https://ibridgepy.com/).
:::info
Make sure that python version in anaconda is the **same** as python version on IBridgePy.
:::

### Connect Strategy
```sequence
Strategy->IBridgePy + SpyderIDE: Convert

IBridgePy + SpyderIDE-->IB TWS: Connect
```
* *Open* **RUN_ME.py** on **Spyder** via **Anaconda**.

#### Load Algorithm
* Load trading algorithm to make prediction
  * *include filename of strategy in RUN_ME.py*

* Enter RUN_ME.py. Enter name of .py file.
Ex: edit ``'example_show_positions.py'`` w/ `*any_strategy_name.py`

* Enter `accountCode` variable to the code found in TWS (top right). Usually begins with `DU`.

* Run (or F5)

:::info
All strategies should go to IBridgePy *strategies* folder. This connects the IDE running to TWS.
:::



## Stream Live Data
* Use IB's live data. IBridgePy has their own built-in function to call data.

## Generate Trading Signals
* If trading algorithm has it, signals automatically generated.

## Send Orders
* Go through API and syntax


###### tags: `Templates` `Documentation`
