Q26 ALPHA-TRADING SYSTEM
Public Member Functions | Public Attributes | List of all members
PORTFOLIO Class Reference

Public Member Functions

def __init__ (self, initialDeposit=10000, leverage=1, currency="USD", positions="long & short", marginCallTreeshold=100, marginMinimum=50, minimumBalance=200, maximumProfit=10000, maximumDrawDown=70, maximumConsecutiveLoss=5000, maximumConsecutiveGain=10000, maximumNumberOfConsecutiveGains=30)
 
def addSymbol (self, symbol)
 Description : More...
 
def placeOrder (self, symbolName, action="long", orderType="MKT", volume=0.1, stoploss=None, takeprofit=None, lmtPrice=None, auxPrice=None)
 Description : More...
 
def editSLOrder (self, symbolName, order, stoploss=None)
 Description : More...
 
def editTPOrder (self, symbolName, order, takeprofit=None)
 Description : More...
 
def cancelOrder (self, symbolName, order)
 Description : More...
 
def closePosition (self, symbolName, order)
 Description : More...
 
def getActivePositions (self, symbolName)
 Description : More...
 
def getHistoricalData (self, symbolName, dateIni, dateEnd, timeframe, onlyOpen=True)
 Description : More...
 
def getLastPrice (self, symbolName)
 Description : More...
 
def message (self, messageStr, category="Strategy Message", kwargs=dict())
 Portfolio system functions.
 
def error (self, errorStr, category="Portfolio Error.", kwargs=dict())
 
def newTurn (self, index_, portfolio_state=None, simulation_state=None)
 
def initiate (self, simulation=None, portfolio=None)
 
- Public Member Functions inherited from SLIPPAGE
def slippageModel (self, model="gaussian relative", parameters={"std" :0.0001})
 Description : More...
 
- Public Member Functions inherited from SYSTEM
def write_log (self, action, nargs, args, kwargs, result)
 
def init_write_log (self, action, nargs, args, kwargs, mode="a")
 
def init (function)
 
def message (function)
 
def error (function)
 
def step (function)
 
def placeOrder (function)
 
def closePosition (function)
 
def editSLOrder (function)
 
def editTPOrder (function)
 
def cancelOrder (function)
 
def getActivePositions (function)
 
def getHistoricalData (function)
 
def getLastPrice (function)
 

Public Attributes

 initialDeposit
 
 leverage
 
 currency
 
 positions
 
 marginCallTreeshold
 
 marginMinimum
 
 minimumBalance
 
 maximumProfit
 
 maximumDrawDown
 
 maximumConsecutiveLoss
 
 maximumConsecutiveGain
 
 maximumNumberOfConsecutiveGains
 
 balance
 
 availableMargin
 
 usedMargin
 
 equity
 
 marginLevel
 
 openPositions
 
 closedPositions
 
 pendingOrders
 
 executedOrders
 
 equityCurve
 
 currentValueLossSerie
 
 currentValueGainSerie
 
 currentDrawDown
 
 currentMaximumNumberOfConsecutiveGains
 
 symbols
 
 historicalDataPrice
 ! More...
 
 historicalDataTimeframe
 
 tradeAuthorisation
 
 verbose
 
 log_step_every
 
 database
 
 log
 
 trading_log_actions
 
- Public Attributes inherited from SLIPPAGE
 slippageMdl
 

Detailed Description


Q26 - QuanTester module - PORTFOLIO object.

Description :

This class is dedicated in initializing a trading PORTFOLIO. 

Examples :

Planned developments :

Known bugs :

Member Function Documentation

◆ addSymbol()

def addSymbol (   self,
  symbol 
)

Description :

Function allowing to add a new SYMBOL object in the portfolio 
symbols dictionnary.

Parameters :

- symbol [class SYMBOL()] : A SYMBOL object 

Returns :

None 

◆ cancelOrder()

def cancelOrder (   self,
  symbolName,
  order 
)

Description :

This function allows to cancel an ORDER that have been placed but 
not executed yet given the symbol name and the pending ORDER. 
If the ORDER is a parent order, the parent ORDER AND the children 
ORDERs will be cancelled. If the ORDER is a children ORDER, only 
the provided order will be cancelled. 

Parameters :

- symbolName [string] : 
    Name of the symbol 
- order [class ORDER()] : 
    The ORDER object that have been placed 

Returns :

None 

◆ closePosition()

def closePosition (   self,
  symbolName,
  order 
)

Description :

This function allows to close a POSITION object, the symbol name 
and the main ORDER object (the one which allowed to open the 
POSITION object) being provided. 

Parameters :

- symbolName [string] : 
    Name of the symbol 
- order [class ORDER()] : 
    The ORDER object that have been placed 

Returns :

None 

◆ editSLOrder()

def editSLOrder (   self,
  symbolName,
  order,
  stoploss = None 
)

Description :

This function allows to edit the stoploss value of a placed order. 
To do it, the user has to provide the stoploss ORDER object (not the 
main ORDER object), and the new stoploss value. 

Parameters :

- symbolName [string] : 
    Name of the symbol 
- order [class ORDER()] : 
    The pending stoploss ORDER object that have been placed 
- stoploss [float] : 
    The new stoploss value 

Returns :

None 

◆ editTPOrder()

def editTPOrder (   self,
  symbolName,
  order,
  takeprofit = None 
)

Description :

This function allows to edit the takeprofit value of a placed order. 
To do it, the user has to provide the takeprofit ORDER object (not the 
main ORDER object), and the new takeprofit value. 

Parameters :

- symbolName [string] : 
    Name of the symbol 
- order [class ORDER()] : 
    The pending takeprofit ORDER object that have been placed 
- takeprofit [float] : 
    The new takeprofit value 

Returns :

None 

◆ getActivePositions()

def getActivePositions (   self,
  symbolName 
)

Description :

Returns the list of currently active positions given a symbol name. 

Parameters :

- symbolName [string] :
    Name of the symbol

Returns :

List of active POSITION objects.  

◆ getHistoricalData()

def getHistoricalData (   self,
  symbolName,
  dateIni,
  dateEnd,
  timeframe,
  onlyOpen = True 
)

Description :

This function returns the historical dataset available to the 
portfolio according the different constraints provided as a 
parameter. 

Parameters :

- symbolName [string] : 
    Name of the symbol 
- dateIni [datetime/integer] : 
    If datetime: The initial dataset datetime value 
    If integer: The number of price candle in the past, 0 being the 
                last available candle. 
    Note : dateIni and dateEnd have to have the same type 
- dateEnd [datetime/integer] : 
    If datetime: The latest dataset datetime value 
    If integer: The number of price candle in the past, 0 being the 
                last available candle. 
    Note : dateIni and dateEnd have to have the same type  
- timeframe [integer] : 
    Timeframe sampling of the historical data in minute unit. 
    0 means the simulation base timeframe. 
    Note : The requested timeframe have to have been prepared 
           in the simulation header. 

Returns :

The function returns a dictionnary of the following shape : 

    {askopen : list(float), 
     askhigh : list(float), 
     asklow  : list(float), 
     askclose: list(float), 
     bidopen : list(float), 
     bidhigh : list(float), 
     bidlow  : list(float), 
     bidclose: list(float), 
     volume  : list(float), 
     time    : list(datetime)}

◆ getLastPrice()

def getLastPrice (   self,
  symbolName 
)

Description :

This function returns the last simulation price available to the 
portfolio given the name of the symbol. 
Note : In certain cases, because of the subloop models, the 
       portfolio can return prices from the future that have 
       theoretically not been released yet. To avoid this 
       potential time ahead-bias, please for any action from 
       the STRATEGY object, use only the askprice/bidprice to 
       know the last existing price. 
       If the subLoopModel is "close only", everything is fine, 
       there is no risk of time ahead bias. 

Parameters :

- symbolName [string] : 
    Name of the symbol 

Returns :

This function returns a dictionnary of the following shape : 

{"askopen" : float, 
 "askhigh" : float, 
 "asklow"  : float, 
 "askclose": float, 
 "askprice": float,
 "bidopen" : float, 
 "bidhigh" : float, 
 "bidlow"  : float, 
 "bidclose": float,
 "bidprice": float, 
 "date"    : datetime, 
 "volume"  : float, 
 "market state" : string ("open" or "closed") }

◆ placeOrder()

def placeOrder (   self,
  symbolName,
  action = "long",
  orderType = "MKT",
  volume = 0.1,
  stoploss = None,
  takeprofit = None,
  lmtPrice = None,
  auxPrice = None 
)

Description :

This function allows to place an order, the symbol name being 
provided. Three types of orders can be placed : market, market if 
touched and limit order. For every placed order, three orders are 
placed, the immediately desired transaction and two pending orders 
simulating the stoploss and takeprofit values. If stoploss and/or 
takeprofit is not required, the user can put an extraordinary value 
so that the security pending orders will never be triggered. 

Parameters :

- symbolName [string] : 
    Name of the symbol as registered in the portfolio. 
- action [string] = "long" : 
    "long" or "short", direction of the position.
- orderType [string] = "MKT" : 
    "MKT" (Market) : The order is placed as soon as possible. 
    "MIT" (Market If Touched) : 
        If action = "long", the order is executed if price < lmtprice 
        If action = "short", the order is executed if price > lmtPrice 
    "LMT" (Limit) : 
        If action = "long", the order is executed if price > lmtPrice 
        If action = "short", the order is executed if price < lmtPrice 
- volume [float] = 0.1 : 
    Volume of the asset to be traded in the unit defined by the 
    associated SYMBOL object in the portfolio symbol dictionnary. 
- stoploss [float] : 
    Stoploss value 
- takeprofit [float] : 
    Takeprofit value 
- lmtPrice [float] : 
    Secondary price value which becomes usefull in case of 
    MIT or LMT order 
- auxPrice [float] : 
    This variable is not used for instance 

Returns :

If the order is correctly placed, the function returns a list of 
three ORDER objects where the first element is the desired 
transction while the second and the third are the stoploss and 
takeprofit orders respectively. 
Else, the function returns a list of Falses, of size 3.

Member Data Documentation

◆ availableMargin

availableMargin

Available margin

Type : integer/float
Simulation variable Description :
The portfolio available margin all along the simulation.

◆ balance

balance

Portfolio balance

Type : integer/float
Simulation variable
Description :
The balance of cash available to the portfolio all along the simulation.

◆ closedPositions

closedPositions

Closed positions list

Type : list(class POSITION)
Simulation variable
Description :
This list stores all the POSITIONs that have been closed by the current portfolio or automatically by the simulated broker.

◆ currency

currency

Portfolio Currency

Type : string
Defaut value : "USD"
Description :
The portfolio devise. !!!This feature is not implemented yet, the only available devise in USD for instance!!!

◆ currentDrawDown

currentDrawDown

Current drawdown

Type : float
Simulation variable
Description :
The current drawdown is calculated as a percentage value of the portfolio balance.

◆ currentMaximumNumberOfConsecutiveGains

currentMaximumNumberOfConsecutiveGains

Current maximum number of consecutive gains

Type : integer
Simulation variable
Description :
Each time a new gain is registered, this variable is incremented by an unitary value. If the last transation was a loss, this variable becomes 0.

◆ currentValueGainSerie

currentValueGainSerie

Current value gain serie

Type : float
Simulation variable
Description :
Current value gain serie. 0 if the last transaction was a loss.

◆ currentValueLossSerie

currentValueLossSerie

Current value loss serie

Type : float
Simulation variable
Description :
Current value loss serie. 0 if the last transaction was a gain.

◆ database

database

Database associated with the portfolio

Type: alphatrading.system.database.DATABASE
Description:
 This object corresponds to the client connector to the simulated portfolio database

◆ equity

equity

Equity balance

Type : integer/float
Simulation variable
Description :
The portfolio equity all along the simulation. The equity variable represents the portfolio balance but also takes in account the instant potential gains/loses of the currently actyive positions.

◆ equityCurve

equityCurve

Portfolio equity curve

Type : list(float)
Simulation variable
Description :
This list represent the value of the portfolio equity at each time a position is closed. It is in particular involved in the calculation of the portfolio current draw down and other gain/loss series.

◆ executedOrders

executedOrders

Executed orders list

Type : list(class ORDER)
Simulation variable
Description :
Every executed or cancelled order is stored here. This list allows to have an history of every portfolio trading action.

◆ historicalDataPrice

historicalDataPrice

!

Historical available data price

Type : dictionnary(symbolName : dictionnary(price : list(value)))
Simulation variable
Description :
This variable contains all the historical data price available to the portfolio according the actual time date. This is the place where historical data price can be accessed from STRATEGY classes. This variable is structured as follows :

{Symbol Name 1: {askopen : list(float),
askhigh : list(float),
asklow : list(float),
askclose: list(float),
bidopen : list(float),
bidhigh : list(float),
bidlow : list(float),
bidclose: list(float),
volume : list(float),
time : list(datetime)},
Symbol Name 2 : ...}

◆ historicalDataTimeframe

historicalDataTimeframe

Simulation data timeframe

Type : integer

Description :
Timeframe (in minutes unit) of the not-resampled simulation data.

◆ initialDeposit

initialDeposit

Initial client desposit amount

Type : float/integer
Defaut value : 10000
Description :
Determine the initial portfolio value

◆ leverage

leverage

Portfolio leverage

Type : float/integer
Defaut value : 1
Description :
A selected leverage.

◆ log

log

Add a default log to the database

Type: boolean
Description :
 If True, a default log table will be considered in the database and all  the actions stored in the trading_log_actions list will be written in the portfolio database at each simulation time step. One should use it only for debug mode.

◆ marginCallTreeshold

marginCallTreeshold

Margin call treeshold

Type : float
Defaut value : 100
Description :
Defines the treeshold below which the trading is deactivated for the current portfolio. The margin rate is expressed as a percentage. If marginLevel < marginCallTreeshold : Warning (no more trading allowed)

◆ marginLevel

marginLevel

Margin level

Type : float
Simulation variable
Description :
The margin level is defined by : availableMargin / usedMargin * 100 and is expressed as a percentage.

◆ marginMinimum

marginMinimum

Margin Minimum treeshold

Type : float
Defaut value : 50
Description :
Defines the treeshold below which the simulator starts to automatically close the most losing positions, and this until the portfolio margin goes up to the minimum allowed value. The value is expressed as a percentage.

◆ maximumConsecutiveGain

maximumConsecutiveGain

Maximum consecutive gain

Type : integer/float
Defaut value : 10000
Description :
If the portfolio meet a consecutive gain higher than the maximum allowed consecutive gain, the trading is deactivated.

◆ maximumConsecutiveLoss

maximumConsecutiveLoss

Maximum consecutive loss

Type : integer/float
Defaut value : 5000
Description :
If the portfolio meet a consecutive loss higher than the maximum allowed consecutive loss, the trading is deactivated.

◆ maximumDrawDown

maximumDrawDown

Maximum allowed drawdown

Type : integer/float
Defaut value : 70
Description :
If the current looses generate a drawdown percentage higher than the maximum allowed, the trading is deactivated.

◆ maximumNumberOfConsecutiveGains

maximumNumberOfConsecutiveGains

Maximum number consecutive gain

Type : integer
Defaut value : 30
Description :
If the portfolio meet a a number of consecutive gain higher than the maximum allowed number of consecutive gain, the trading is deactivated.

◆ maximumProfit

maximumProfit

Maximum allowed profit

Type : integer/float
Defaut value : 1000
Description :
If the current portfolio makes more profit than the maximum allowed profit, the trading is deactivated.

◆ minimumBalance

minimumBalance

Minimum balance

Type : integer/float
Defaut value : 200
Description :
If the portfolio balance goes below the minimum allowed balance, the trading is deactivated.

◆ openPositions

openPositions

Open positions list

Type : list(class POSITION)
Simulation variable
Description :
This list stores all the currently active POSITION taken by the current portfolio.

◆ pendingOrders

pendingOrders

Pending orders list

Type : list(class ORDER)
Simulation variable
Description :
Every submitted ORDER is stored here. If conditions are met, the order is executed. It means that it is removed from this list and placed in the executedOrders list, and a POSITION object is generated in the openPositions list.

◆ positions

positions

Allowed positions types

Type : string
Defaut value : "long & short"
Description :
If "long", only long positions are allowed, if "short", only short positions are allowed. If "long & short", both direction positions are allowed. !!! Not working yet !!! For instance "long & short" is the only possible value.

◆ symbols

symbols

Symbols

Type : dictionnary(string : class SYMBOL)
Description :
This variable stores all the SYMBOL objects involved in the simulation.

◆ tradeAuthorisation

tradeAuthorisation

Trading authorisation

Type : boolean
Description :
If False, trading is unauthorised.

◆ trading_log_actions

trading_log_actions

Trading log actions

Type: list(string)
Description:
 This list contains a list of actions which will be recorded in the log table for the database if the attribute self.log is set to True. Each element of the list record a specific action but all the elements follows the same structure in the default log table.

◆ usedMargin

usedMargin

Used margin

Type : integer/float
Simulation variable
Description :
The portfolio used margin all along the simulation.

◆ verbose

verbose

Verbose, debug mode

Type : boolean
Description :
If True, a lot of technical informations are printed during the simulation.


The documentation for this class was generated from the following file: