Forex Forecasting with Random Forests 4/N – Precision Rating

Economy
### Trade Simulation
def trade_simulation(dfx,leverage,start_balance):
    i = 0
    for index,item in dfx.iterrows():
        # Margin required for 10,000 EUR (1 ticket) trade
        dfx.loc[index,'margin_per_eur'] = item['eurusd_Close']*item['usdjpy_Close']*10000/leverage

        # Bank balance
        if i <= 1:
            dfx.loc[index,'balance'] = start_balance 
        else:
            dfx.loc[index,'balance'] = dfx.loc[index-1,'balance'] + dfx.loc[index-1,'profit_loss']

        # Num
        num = dfx.loc[index,'balance']/2/dfx.loc[index,'margin_per_eur']
        if num>100:
            num = 100
        dfx.loc[index,'num']  = num

        # Required Margin
        dfx.loc[index,'margin']  = dfx.loc[index,'margin_per_eur']*dfx.loc[index,'num']

        # Investment Residuals after Position Making
        dfx.loc[index,'capacity']  = dfx.loc[index,'balance']-dfx.loc[index,'margin']

        # Rate fluctuation tolerance
        dfx.loc[index,'rate_fluctuation_tolerance'] = dfx.loc[index,'capacity']/(dfx.loc[index,'num']*10000)

        # Profit and loss
        if i <= 1:
            dfx.loc[index,'profit_loss'] = 0 
        else:
            dfx.loc[index,'profit_loss'] = dfx.loc[index-2,'pred']*(dfx.loc[index,'eurusd_Close']-dfx.loc[index-2,'eurusd_Close'])*dfx.loc[index-2,'num']*10000*dfx.loc[index,'usdjpy_Close']

        i = i+1
    return dfx
### Trade Simulation
def trade_simulation(dfx,leverage,start_balance):
    i = 0
    for index,item in dfx.iterrows():
        # Margin required for 10,000 EUR (1 ticket) trade
        dfx.loc[index,'margin_per_eur'] = item['eurusd_Close']*item['usdjpy_Close']*10000/leverage

        # Bank balance
        if i <= 1:
            dfx.loc[index,'balance'] = start_balance 
        else:
            dfx.loc[index,'balance'] = dfx.loc[index-1,'balance'] + dfx.loc[index-1,'profit_loss']

        # Num
        num = dfx.loc[index,'balance']/2/dfx.loc[index,'margin_per_eur']
        if num>100:
            num = 100
        dfx.loc[index,'num']  = num

        # Required Margin
        dfx.loc[index,'margin']  = dfx.loc[index,'margin_per_eur']*dfx.loc[index,'num']

        # Investment Residuals after Position Making
        dfx.loc[index,'capacity']  = dfx.loc[index,'balance']-dfx.loc[index,'margin']

        # Rate fluctuation tolerance
        dfx.loc[index,'rate_fluctuation_tolerance'] = dfx.loc[index,'capacity']/(dfx.loc[index,'num']*10000)

        # Profit and loss
        if i <= 1:
            dfx.loc[index,'profit_loss'] = 0 
        else:
            dfx.loc[index,'profit_loss'] = dfx.loc[index-2,'pred']*(dfx.loc[index,'eurusd_Close']-dfx.loc[index-2,'eurusd_Close'])*dfx.loc[index-2,'num']*10000*dfx.loc[index,'usdjpy_Close']

        i = i+1
    return dfx
### Trade Simulation
def trade_simulation(dfx,leverage,start_balance):
    i = 0
    for index,item in dfx.iterrows():
        # Margin required for 10,000 EUR (1 ticket) trade
        dfx.loc[index,'margin_per_eur'] = item['eurusd_Close']*item['usdjpy_Close']*10000/leverage

        # Bank balance
        if i <= 1:
            dfx.loc[index,'balance'] = start_balance 
        else:
            dfx.loc[index,'balance'] = dfx.loc[index-1,'balance'] + dfx.loc[index-1,'profit_loss']

        # Num
        num = dfx.loc[index,'balance']/2/dfx.loc[index,'margin_per_eur']
        if num>100:
            num = 100
        dfx.loc[index,'num']  = num

        # Required Margin
        dfx.loc[index,'margin']  = dfx.loc[index,'margin_per_eur']*dfx.loc[index,'num']

        # Investment Residuals after Position Making
        dfx.loc[index,'capacity']  = dfx.loc[index,'balance']-dfx.loc[index,'margin']

        # Rate fluctuation tolerance
        dfx.loc[index,'rate_fluctuation_tolerance'] = dfx.loc[index,'capacity']/(dfx.loc[index,'num']*10000)

        # Profit and loss
        if i <= 1:
            dfx.loc[index,'profit_loss'] = 0 
        else:
            dfx.loc[index,'profit_loss'] = dfx.loc[index-2,'pred']*(dfx.loc[index,'eurusd_Close']-dfx.loc[index-2,'eurusd_Close'])*dfx.loc[index-2,'num']*10000*dfx.loc[index,'usdjpy_Close']

        i = i+1
    return dfx
## Combine explanatory variables with predicted results of test data
# Data framing of eries type data
test_y2 = pd.DataFrame(Y_test)
test_y2['pred'] = model.predict(X_test)

# Prediction Result Storage
col = ['Date','eurusd_Open', 'eurusd_High', 'eurusd_Low', 'eurusd_Close', 'usdjpy_Close', 'Maguro', 'pred']
df_test_result = pd.merge(df_drop_add, test_y2['pred'], left_index=True, right_index=True)[col]


## Combining explanatory variables with the predicted results of train data
# Data framing of Series type data
train_y2 = pd.DataFrame(Y_train)
train_y2['pred'] = model.predict(X_train)

# Prediction Result Saving
col = ['Date','eurusd_Open', 'eurusd_High', 'eurusd_Low', 'eurusd_Close', 'usdjpy_Close', 'Maguro', 'pred']
df_train_result = pd.merge(df_drop_add, train_y2['pred'], left_index=True, right_index=True)[col]
Xiofx
Xiofx

An experienced Machine Learning and Deep Learning professional and logistics improvement entrepreneur in Tokyo, Japan, with an interest in economies around the world. She likes travel very much.

XiofxFollow
EconomyFXPythonTechnology

Comments

Letteralpha