<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> import oandapy <EOL> from datetime import datetime <EOL> import pandas as pd <EOL> class ForexSystem ( oandapy . Streamer ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> oandapy . Streamer . __init__ ( self , * args , ** kwargs ) <EOL> self . oanda = oandapy . API ( kwargs [ "<STR_LIT>" ] , <EOL> kwargs [ "<STR_LIT>" ] ) <EOL> self . instrument = None <EOL> self . account_id = None <EOL> self . qty = <NUM_LIT:0> <EOL> self . resample_interval = '<STR_LIT>' <EOL> self . mean_period_short = <NUM_LIT:5> <EOL> self . mean_period_long = <NUM_LIT:20> <EOL> self . buy_threshold = <NUM_LIT:1.0> <EOL> self . sell_threshold = <NUM_LIT:1.0> <EOL> self . prices = pd . DataFrame ( ) <EOL> self . beta = <NUM_LIT:0> <EOL> self . is_position_opened = False <EOL> self . opening_price = <NUM_LIT:0> <EOL> self . executed_price = <NUM_LIT:0> <EOL> self . unrealized_pnl = <NUM_LIT:0> <EOL> self . realized_pnl = <NUM_LIT:0> <EOL> self . position = <NUM_LIT:0> <EOL> self . dt_format = "<STR_LIT>" <EOL> def begin ( self , ** params ) : <EOL> self . instrument = params [ "<STR_LIT>" ] <EOL> self . account_id = params [ "<STR_LIT>" ] <EOL> self . qty = params [ "<STR_LIT>" ] <EOL> self . resample_interval = params [ "<STR_LIT>" ] <EOL> self . mean_period_short = params [ "<STR_LIT>" ] <EOL> self . mean_period_long = params [ "<STR_LIT>" ] <EOL> self . buy_threshold = params [ "<STR_LIT>" ] <EOL> self . sell_threshold = params [ "<STR_LIT>" ] <EOL> self . start ( ** params ) <EOL> def on_success ( self , data ) : <EOL> time , symbol , bid , ask = self . parse_tick_data ( <EOL> data [ "<STR_LIT>" ] ) <EOL> self . tick_event ( time , symbol , bid , ask ) <EOL> def parse_tick_data ( self , dict_data ) : <EOL> time = datetime . strptime ( dict_data [ "<STR_LIT:time>" ] , <EOL> self . dt_format ) <EOL> ask = float ( dict_data [ "<STR_LIT>" ] ) <EOL> bid = float ( dict_data [ "<STR_LIT>" ] ) <EOL> instrument = dict_data [ "<STR_LIT>" ] <EOL> return time , instrument , bid , ask <EOL> def tick_event ( self , time , symbol , bid , ask ) : <EOL> midprice = ( ask + bid ) / <NUM_LIT> <EOL> self . prices . loc [ time , symbol ] = midprice <EOL> resampled_prices = self . prices . resample ( <EOL> self . resample_interval , <EOL> how = '<STR_LIT>' , <EOL> fill_method = "<STR_LIT>" ) <EOL> mean_short = resampled_prices . tail ( <EOL> self . mean_period_short ) . mean ( ) [ <NUM_LIT:0> ] <EOL> mean_long = resampled_prices . tail ( <EOL> self . mean_period_long ) . mean ( ) [ <NUM_LIT:0> ] <EOL> self . beta = mean_short / mean_long <EOL> self . perform_trade_logic ( self . beta ) <EOL> self . calculate_unrealized_pnl ( bid , ask ) <EOL> self . print_status ( ) <EOL> def perform_trade_logic ( self , beta ) : <EOL> if beta > self . buy_threshold : <EOL> if not self . is_position_opened or self . position < <NUM_LIT:0> : <EOL> self . check_and_send_order ( True ) <EOL> elif beta < self . sell_threshold : <EOL> if not self . is_position_opened or self . position > <NUM_LIT:0> : <EOL> self . check_and_send_order ( False ) <EOL> def check_and_send_order ( self , is_buy ) : <EOL> if self . place_market_order ( self . qty , is_buy ) : <EOL> if is_buy : <EOL> self . position += self . qty <EOL> else : <EOL> self . position -= self . qty <EOL> if self . position == <NUM_LIT:0> : <EOL> self . is_position_opened = False <EOL> self . calculate_realized_pnl ( is_buy ) <EOL> else : <EOL> self . opening_price = self . executed_price <EOL> self . is_position_opened = True <EOL> def calculate_realized_pnl ( self , is_buy ) : <EOL> self . realized_pnl += self . qty * ( <EOL> ( self . opening_price - self . executed_price ) <EOL> if is_buy else <EOL> ( self . executed_price - self . opening_price ) ) <EOL> def calculate_unrealized_pnl ( self , bid , ask ) : <EOL> if self . is_position_opened : <EOL> pos = self . oanda . get_position ( self . account_id , <EOL> self . instrument ) <EOL> units = pos [ "<STR_LIT>" ] <EOL> side = pos [ "<STR_LIT>" ] <EOL> avg_price = float ( pos [ "<STR_LIT>" ] ) <EOL> self . unrealized_pnl = units * ( <EOL> ( bid - avg_price ) <EOL> if ( side == "<STR_LIT>" ) <EOL> else ( avg_price - ask ) ) <EOL> else : <EOL> self . unrealized_pnl = <NUM_LIT:0> <EOL> def place_market_order ( self , qty , is_buy ) : <EOL> side = "<STR_LIT>" if is_buy else "<STR_LIT>" <EOL> response = self . oanda . create_order ( <EOL> account_id , <EOL> instrument = self . instrument , <EOL> units = qty , <EOL> side = side , <EOL> type = '<STR_LIT>' ) <EOL> if response is not None : <EOL> self . executed_price = float ( response [ "<STR_LIT>" ] ) <EOL> print "<STR_LIT>" % ( side , qty , self . instrument ) <EOL> return True <EOL> return False <EOL> def print_status ( self ) : <EOL> print "<STR_LIT>" % ( <EOL> datetime . now ( ) . time ( ) , <EOL> self . instrument , <EOL> self . position , <EOL> round ( self . beta , <NUM_LIT:5> ) , <EOL> self . realized_pnl , <EOL> self . unrealized_pnl ) <EOL> def on_error ( self , data ) : <EOL> print data <EOL> self . disconnect ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> key = "<STR_LIT>" "<STR_LIT>" <EOL> account_id = <NUM_LIT> <EOL> system = ForexSystem ( environment = "<STR_LIT>" , access_token = key ) <EOL> system . begin ( accountId = account_id , <EOL> instruments = "<STR_LIT>" , <EOL> qty = <NUM_LIT:1000> , <EOL> resample_interval = "<STR_LIT>" , <EOL> mean_period_short = <NUM_LIT:5> , <EOL> mean_period_long = <NUM_LIT:20> , <EOL> buy_threshold = <NUM_LIT:1.> , <EOL> sell_threshold = <NUM_LIT:1.> ) </s>
