from datetime import datetime, timedelta
import pandas as pd
import oandapy
import configparser

config = configparser.ConfigParser()
config.read('../config/config_v1.ini')
account_id = config['oanda']['account_id']
api_key = config['oanda']['api_key']

oanda = oandapy.API(environment="practice", 
                    access_token=api_key)

trade_expire = datetime.now() + timedelta(days=1)
trade_expire = trade_expire.isoformat("T") + "Z"
trade_expire

"""
Explanation: <!--NAVIGATION-->
< Account Information | Contents | Trade Management >
Order Management
Orders
Creating Orders
create_order(self, account_id, **params)
End of explanation
"""


response = oanda.create_order(account_id,
                              instrument = "AUD_USD",
                              units=1000,
                              side="buy",
                              type="limit",
                              price=0.7420,
                              expiry=trade_expire)
print(response)

pd.Series(response["orderOpened"])

order_id = response["orderOpened"]['id']

"""
Explanation: For a detailed explanation of the above, please refer to Rates Information.
End of explanation
"""


response = oanda.get_orders(account_id)
print(response)

pd.DataFrame(response['orders'])

"""
Explanation: Getting Open Orders
get_orders(self, account_id, **params)
End of explanation
"""


response = oanda.get_orders(account_id)
id = response['orders'][0]['id']

oanda.get_order(account_id, order_id=id)

"""
Explanation: Getting Specific Order Information
get_order(self, account_id, order_id, **params)
End of explanation
"""


response = oanda.get_orders(account_id)
id = response['orders'][0]['id']

oanda.modify_order(account_id, order_id=id, price=0.7040)

"""
Explanation: Modify Order
modify_order(self, account_id, order_id, **params)
End of explanation
"""


response = oanda.get_orders(account_id)
id = response['orders'][0]['id']

oanda.close_order(account_id, order_id=id)

"""
Explanation: Close Order
close_order(self, account_id, order_id, **params)
End of explanation
"""


oanda.get_orders(account_id)

"""
Explanation: Now when we check the orders. The above order has been closed and removed without being filled. There is only one outstanding order now.
End of explanation
"""


