from __future__ import print_function
import logging
import requests,json
import argparse

log = logging.getLogger(__name__)
print = log.info
logging.getLogger("requests").setLevel(logging.WARNING)
logging.basicConfig(level = logging.INFO,format = '%(asctime)s [%(levelname)s] %(message)s', datefmt = '%Y-%m-%d %H:%M:%S')

PRIVATE_KEY = 'ffcf6286cb14d46ea3c14f659f8b5d14-3ab610cb0f3a4e6494362960ab73d04f'
AUTH_TOKEN = {'Authorization':'Bearer '+ PRIVATE_KEY}

def get_account_info():
    "Get Account infomation."
    url = 'https://api-fxpractice.oanda.com/v1/accounts'
    output = requests.get(url, headers = AUTH_TOKEN)
    return output.text

def store_price_info(instrument,start_time,end_time,granularity):
    '''
Get price info and dump it into local json file. 

Parameter:
  instrument: Currency instrument. e.g. EUR_USD, USD_JPY, etc
  start_time: start time of price info dump. format: 2012-01-01T00%3A00%3A00Z
  end_time : end time for price info dump
  granularity: time period. e.g. H1, M30,M15,D, etc 

Output:
  a json file with the above parameter price info is created. json file name is as price_<instrument>_<start_time>_<granularity>.json

GET "https://api-fxpractice.oanda.com/v1/candles?instrument=EUR_USD&start=2012-01-01T00%3A00%3A00Z&end=2012-05-31T23%3A59%3A59Z&granularity=H1"
    '''
    url = "https://api-fxpractice.oanda.com/v1/candles?" + \
          "instrument=" + instrument + \
          "&start=" + start_time + \
          "&end=" + end_time + \
          "&granularity=" + granularity
    #print(url)
    output = requests.get(url, headers = AUTH_TOKEN)
    json_filename = "price_" + instrument + "_" + start_time + "_" + granularity +".json"
    f = open(json_filename,'w')
    f.write(output.text)
    f.close()

def parse_input():
    """Parses command line input."""
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('-d', '--dump_price', action = 'store_true',
                        help="dump price info into local json file")

    return parser.parse_args()


if __name__ == '__main__':
    print("Start simple automatic trading tool...")
    args = parse_input()
    account_info = get_account_info()
    
    if args.dump_price:
        print("Dumping price info into local file...")
        store_price_info('EUR_USD','2012-01-01T00%3A00%3A00Z','2012-06-30T23%3A59%3A59Z','H1') # dump H1 EUR_USD price for the first half year of 2012
        store_price_info('EUR_USD','2012-07-01T00%3A00%3A00Z','2012-12-31T23%3A59%3A59Z','H1') # dump H1 EUR_USD price for the second half year of 2012 

@@ -82,16 +82,28 @@
 argparse
+%0Aimport glob
 %0A%0Alog = 
@@ -723,20 +723,21 @@
 y):%0A    
-'''%0A
+%22%22%22%0A 
 Get pric
@@ -779,16 +779,17 @@
 file. %0A%0A
+ 
 Paramete
@@ -1027,16 +1027,17 @@
 , etc %0A%0A
+ 
 Output:%0A
@@ -1171,16 +1171,17 @@
 %3E.json%0A%0A
+ 
 GET %22htt
@@ -1324,11 +1324,11 @@
     
-'''
+%22%22%22
 %0A   
@@ -1767,16 +1767,296 @@
 lose()%0A%0A
+def store_price_into_memory(instrument, granularity):%0A    %22%22%22%0A Store the price info from the dumped file in the same folder to memory %0A    %22%22%22%0A    path = %22./price_%22 + instrument + %22_*_%22 + granularity + %22.json%22%0A    files=glob.glob(path)%0A    for name in files:%0A        print(name)%0A%0A
 def pars
@@ -2550,16 +2550,79 @@
 o()%0A    
+store_price_into_memory(instrument=%22EUR_USD%22, granularity=%22H1%22)
 %0A    if 

Add price storing function(draft)