Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
{
"cells": [
{
"cell_type": "markdown",
"id": "excellent-valentine",
"metadata": {},
"source": [
"# Add lags function"
]
},
{
"cell_type": "markdown",
"id": "novel-function",
"metadata": {},
"source": [
"***\n",
"_num_lags_ - number of lags to perform on each feature in the data-set \n",
"\n",
"**Input**: num_lags \n",
"**Output**: original data-set extended with lagged features, list of lagged columns<br>\n",
"***\n",
"1. **Function add_lags**(num_lags)\n",
"2. &emsp;$result\\gets$ copy training data set\n",
"3. &emsp;$laggedCols\\gets []$\n",
"4. &emsp;$colsToLag\\gets STORE$ columns from result in list except columns 'price' and 'returns' \n",
"5. &emsp;**for** column **IN** colsToLag **do**\n",
"6. &emsp;&emsp;**for** lag **IN** **range**(1, num_lags+1) **do**\n",
"7. &emsp;&emsp;&emsp;laggedCol = {column}Lag{lag}\n",
"8. &emsp;&emsp;&emsp;result[laggedCol] = result[column].shift(lag)\n",
"9. &emsp;&emsp;&emsp;laggedCols.append(laggedCol)\n",
"10. &emsp;&emsp;**end for**\n",
"11. &emsp;**end for**\n",
"12. &emsp;**return** $result, laggedCols$"
]
},
{
"cell_type": "markdown",
"id": "annoying-newark",
"metadata": {},
"source": [
"# Reward function and sub-functions"
]
},
{
"cell_type": "markdown",
"id": "structured-attendance",
"metadata": {},
"source": [
"***\n",
"_action_ - list of actions available to agent(hold, buy, sell) **OR** agent's chosen action \n",
"_states_ - numpy arrays of all states \n",
"_data_ - training data set \n",
"_marketDir_ - direction of the market(either market going **up** or **down**) at current time step as opposed to previous time step \n",
"_mutual position column_ - mutual position of the SMAC+BB or MACD+BB strategy at the current time step\n",
"\n",
"**Input**: action \n",
"**Output**: nextState, reward, done, rewardedAction<br>\n",
"***\n",
"1. **Function get_reward**(action)\n",
"2. &emsp;index += 1\n",
"3. &emsp;**if** $index > length(data set)-1$ **then**\n",
"4. &emsp;&emsp; $index\\gets 0$\n",
"5. &emsp;$nextState\\gets$ next state from current index\n",
"7. &emsp;$currentDir\\gets$ current market direction based on index\n",
"8. &emsp;$currentStratPos\\gets$ current strategy position based on index\n",
"9. &emsp;$nextMutualPositions\\gets getNextMutualPositions()$\n",
"10. &emsp;$reward\\gets 0$\n",
"11. &emsp;$shortlistedActions\\gets []$\n",
"12. &emsp;**if** action is list **then**&emsp;**# agent is exploring the environment with each available action**\n",
"13. &emsp;&emsp;**for** a **in** range(length(action)) **do**\n",
"14. &emsp;&emsp;&emsp;$currentAction\\gets action[a]$\n",
"15. &emsp;&emsp;&emsp;**if** currentAction == sell **then** \n",
"16. &emsp;&emsp;&emsp;&emsp;**if** currentDir == down **OR** currentStratPos == sell **OR** nextMutualPositions == sell **then**\n",
"17. &emsp;&emsp;&emsp;&emsp;&emsp; $reward\\gets 1$ \n",
"18. &emsp;&emsp;&emsp;&emsp;&emsp; $shortlistedActions.append(sell)$ \n",
"19. &emsp;&emsp;&emsp;&emsp; **end if**\n",
"20. &emsp;&emsp;&emsp; **end if**\n",
"20. &emsp;&emsp;&emsp;**if** currentAction == buy **then**\n",
"21. &emsp;&emsp;&emsp;&emsp;**if** currentDir == up **OR** currentStratPos == buy **OR** nextMutualPositions == buy **then**\n",
"22. &emsp;&emsp;&emsp;&emsp;&emsp; $reward\\gets 1$\n",
"23. &emsp;&emsp;&emsp;&emsp;&emsp; $shortlistedActions.append(buy)$\n",
"24. &emsp;&emsp;&emsp;&emsp; **end if**\n",
"25. &emsp;&emsp;&emsp; **end if**\n",
"25. &emsp;&emsp;&emsp;**if** currentAction == hold **then**\n",
"25. &emsp;&emsp;&emsp;&emsp;**if** currentStratPos == hold **OR** nextMutualPositions == hold **then**\n",
"26. &emsp;&emsp;&emsp;&emsp;&emsp; $reward\\gets 1$\n",
"27. &emsp;&emsp;&emsp;&emsp;&emsp; $shortlistedActions.append(hold)$\n",
"28. &emsp;&emsp;&emsp;&emsp; **end if**\n",
"29. &emsp;&emsp;&emsp; **end if**\n",
"30. &emsp;&emsp; **end for**\n",
"31. &emsp; **end if**\n",
"31. &emsp;**else if** action is a number **then**&emsp;**# agent is exploiting the environment with one chosen action**\n",
"32. &emsp;&emsp;**if** currentAction == sell **then**\n",
"33. &emsp;&emsp;&emsp;**if** currentDir == down **OR** currentStratPos == sell **OR** nextMutualPositions == sell **then**\n",
"34. &emsp;&emsp;&emsp;&emsp; $reward\\gets 1$ \n",
"35. &emsp;&emsp;&emsp;&emsp; $shortlistedActions.append(sell)$\n",
"19. &emsp;&emsp;&emsp; **end if**\n",
"20. &emsp;&emsp; **end if**\n",
"36. &emsp;&emsp;**if** currentAction == buy **then**\n",
"21. &emsp;&emsp;&emsp;**if** currentDir == up **OR** currentStratPos == buy **OR** nextMutualPositions == buy **then**\n",
"22. &emsp;&emsp;&emsp;&emsp; $reward\\gets 1$\n",
"23. &emsp;&emsp;&emsp;&emsp; $shortlistedActions.append(buy)$\n",
"19. &emsp;&emsp;&emsp; **end if**\n",
"20. &emsp;&emsp; **end if**\n",
"25. &emsp;&emsp;**if** currentAction == hold **then**\n",
"25. &emsp;&emsp;&emsp;**if** currentStratPos == hold **OR** nextMutualPositions == hold **then**\n",
"26. &emsp;&emsp;&emsp;&emsp; $reward\\gets 1$\n",
"27. &emsp;&emsp;&emsp;&emsp; $shortlistedActions.append(hold)$\n",
"19. &emsp;&emsp;&emsp; **end if**\n",
"20. &emsp;&emsp; **end if**\n",
"20. &emsp; **end else if**\n",
"54. $rewardedAction\\gets chooseAction(shortlistedActions)$\n",
"55. $done\\gets$ True **if** index reaches end of data set (environment) **else** False\n",
"56. **return** nextState, reward, done, rewardedAction"
]
},
{
"cell_type": "markdown",
"id": "younger-sweet",
"metadata": {},
"source": [
"***\n",
"_nextMutualPositions_ - list of next 5 mutual positions from the current index \n",
"\n",
"\n",
"**Output**: action<br>\n",
"***\n",
"1. **Function getNextMutualPositions**()\n",
"2. &emsp;$nextMutualPositions\\gets$ values of next 5 mutual positions from the current index\n",
"3. &emsp;$mostOccured\\gets$ most frequent position from the nextMutualPositions list\n",
"4. &emsp; **if** mostOccured has only one position **then**\n",
"5. &emsp;&emsp; **return** that position\n",
"6. &emsp; **else** **# equal number of most frequent positions (i.e. any two positions have the same number of occurences)**\n",
"7. &emsp;&emsp; **return** 0\n",
"8. &emsp; **end if**"
]
},
{
"cell_type": "markdown",
"id": "pretty-character",
"metadata": {},
"source": [
"***\n",
"_actions_ - list of actions passed \n",
"\n",
"**Input**: actions \n",
"**Output**: next_state, next_price, reward, done, rewarded_action<br>\n",
"***\n",
"1. **Function chooseAction**(actions)\n",
"2. &emsp;**if** actions list is empty **then**\n",
"3. &emsp;&emsp; **return** 0\n",
"4. &emsp;**else if** only 1 action in actions list **then**\n",
"5. &emsp;&emsp; **return** that action\n",
"6. &emsp;**else if** 2 actions in action list **then**\n",
"7. &emsp;&emsp; **return** numpy.sign(first + second action of actions list)\n",
"8. &emsp;**else if** 3 actions in action list **then**\n",
"9. &emsp;&emsp; **return** 0\n",
"10. &emsp;**end if**"
]
},
{
"cell_type": "markdown",
"id": "timely-picnic",
"metadata": {},
"source": [
"# Sliding window algorithm"
]
},
{
"cell_type": "markdown",
"id": "everyday-inspiration",
"metadata": {},
"source": [
"***\n",
"_data_ - data set containing all columns and values\n",
"_iterationNum_ - lopp-incremented number\n",
"_windowSizePlusN_ - number of window size plus number n\n",
"\n",
"**Input**: data, iterationNum, windowSizePlusN \n",
"**Output**: numpy array of arrays of all states from data set<br>\n",
"***\n",
"1. **Function getWindow**(data, iterationNum, windowSizePlusN)\n",
"2. &emsp;$index\\gets$ iterationNum - windowSizePlusN + 1\n",
"3. &emsp;$slidingWindowArr\\gets$ []&emsp;**# length of array will be number of columns in data set * window size**\n",
"4. &emsp;**for** columnName, columnValues **in** data set **do**\n",
"5. &emsp;&emsp;$block\\gets$ arrays of size 6 filled with $columnValues$ of each $columnName$ according to **sliding window algorithm** where the first array of each $columnName$ is filled with the first value of that column\n",
"6. &emsp;&emsp;**for** i **in** range($windowSizePlusN$ - 1) **do**\n",
"7. &emsp;&emsp;&emsp;$slidingWindowArr$.append(next newest element **minus** previous newest element in $block$ array based on index for all arrays of column values of the data set columns)\n",
"8. &emsp;&emsp;**end for**\n",
"9. &emsp;**end for**\n",
"10. &emsp;**return** numpy.array([$slidingWindowArr$])"
]
},
{
"cell_type": "markdown",
"id": "thrown-grove",
"metadata": {},
"source": [
"***\n",
"_data_ - data set containing all columns and values \n",
"_windowSize_ - number of window size = 5 in our case \n",
"\n",
"**Input**: data, windowSize \n",
"**Output**: numpy array of arrays of all states from data set<br>\n",
"***\n",
"1. **Function window_diff**(data, windowSize)\n",
"2. &emsp;$dataLen\\gets$ length(data)\n",
"3. &emsp;$allStates\\gets$ []\n",
"4. &emsp;$scaledData\\gets$ sklearn's **StandardScaler** scaled data set\n",
"5. &emsp;**for** i **in** range($dataLen$) **do**\n",
"6. &emsp;&emsp;$state\\gets$ $getWindow$(scaledData, i, windowSize + 1)\n",
"7. &emsp;&emsp;$allStates$.append($state$)\n",
"8. &emsp;**end for**\n",
"9. &emsp;**return** $allStates$"
]
},
{
"cell_type": "markdown",
"id": "presidential-memphis",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "horizontal-monitoring",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}