[BUG BOUNTY] 7 Steem Reward for Debugging and making more Efficient my Python CodesteemCreated with Sketch.

in #programming7 years ago (edited)


py.png


I am working on a market analysis indicator, that will factor in many things, and will try to predict the most efficient time when to buy a cryptocurrency. By efficient I mean when the risk/reward ratio will be the best, so it's a risk/reward analysis indicator. We set an arbitrary treshold (which will be calibrated later on, but for now user set) and if the indicator has a value above that, then we buy the currency, and we will hold it for random amounts of times, so this script is basically a monte carlo simulator for this indicator, it's a pseudo-trading bot.

I will keep the indicator proprietary, it's not ready yet anyway and for now it's only based on the price and the volume, I'll do the research later, for now I just need to make this bot. The indicator is basically a python script named PROFITINDICATORv1, but I won't show it here.

Here is the code (Python 3.5+):


    import json
    import statistics
    import math
    from random import randint

    ##################[TRADE VARIABLES]##############################################
    TRIGGER=0.5
    SETSIZE=30
    ################################################################################

    with open('moneroday') as data_file:       
        data = json.load(data_file)   
   
   
    close     = [item['close'] for item in data]   
    btcvolume = [item['volume'] for item in data]   
    total     = len(close)   
   
   
   
    opentrade=0 # logical operator to establish whether trade is open or not   
    buyprice=0  # set and record the buy price each time   
    profit=0   # add together total profit   
    trades=0   # count nr of trades   
    randclose=0   
    for i in range(10,total):    # go through all except the first 10, so the indicator has enough data to initialize itself   
     priceset  =  close[0:i]        
     volumeset =  btcvolume[0:i]    
   
     indicator = PROFITINDICATORv1(priceset,volumeset)   
   
     if(indicator>TRIGGER and opentrade==0):   
      buyprice = priceset[i-1] # -1 because set starts from 0, this is index not count   
      opentrade= 1   
      randclose = i + randint(1,SETSIZE)   
   
   
     if((i==randclose or i==total) and opentrade==1):   
      profit+= (priceset[i-1] - buyprice)  # -1 because set starts from 0, this is index not count   
      opentrade= 0   
      trades+=1   
   
    print(profit)   
    print(trades)

Description

  • The TRIGGER variable will be the arbitrary trigger above which the indicator should be in order to signal a buy order
  • The SETSIZE is the maximum size of the random set upon which we group our trades
  • We open a .JSON file in this case daily trade history of XMR/BTC market
  • We set the close and btcvolume variables from the loaded JSON data as lists object of the size of the data, and total will be the size of it
  • Then we set various other variables that are initialized with 0
  • A for loop goes through the entire dataset, but starting from 10 so that the indicator will have enough samplesize to work with.
  • The priceset and volumeset variables extract a subset of the price and volume list variable up until the current element in the for indicated by i. This is then passed to the indicator as parameter and data to work with. I won't show the indicator, but I made sure that it works fine and it returns a value to the indicator variable after it's called
  • Then the buy condition is tested, if the indicator signals a buy order and there are no open trades currently, then we enter in a trade, which means we record the buy price, set the open trade flag to 1, and set a random point in the future when the trade will be closed, which is minimum 1 step in the future (since it makes no sens to open and close the trade at the same price) and maximum the SETSIZE, specified earlier. Obviously only 1 trade is open at a time.
  • Then we test the trade close condition, if we hit the random index number at which the trade gets closed or if we hit the end of the data (and there is a pending trade not closed yet), and if there is an open trade, then we calculate the profit (current price - buyprice), set the open trade flag to 0, and add 1 to the count of nr trades
  • Then the for loop repeats itself until we hit the end of the data
  • Then the profit and the nr of trades is shown in the console.

Task

I would like if some professional programmers would look at my code, check it for bugs, especially look at the index numbers, I may have messed up those.

Or if you can make my code more efficient, since it's pretty slow as the indicator has to be calculated at each step. I would love to see some efficiency improvement to make my code faster and less memory intensive.

I will reward 7 Steem for people who help me, if there are more people, then the reward will be shared.

Sort:  
Loading...

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.030
BTC 56740.37
ETH 3000.36
USDT 1.00
SBD 2.19