Hi,
How would I access the current plot of an MC indicator externally (e.g. with python)?
Is there a way to access the memory location of a globalvariable or EL Collection using external python script?
Currently I can print the indicator plot to file and have that read.... but am looking for a memory based solution.
Thanks!
How to access indicator values externally, from python?
- Smoky
- Posts: 518
- Joined: 03 Dec 2010
- Location: Thailand
- Has thanked: 99 times
- Been thanked: 123 times
Re: How to access indicator values externally, from python?
Under review 60 votes for a long time now
https://www.multicharts.com/pm/public/m ... ues/MC-755
you are right , shared memory should be the best ! many other langage can use it !
Thanks
https://www.multicharts.com/pm/public/m ... ues/MC-755
you are right , shared memory should be the best ! many other langage can use it !
Thanks
Re: How to access indicator values externally, from python?
Hi
if you store values in EasyLanguage using shared var DLL or Global Variable DLL.
THen you can access those stored values in Python. Here was my script I wrote sometime back to access values in Python from Easy Language.
THen , inside easy language, You can use LUA at close of every bar to run a Python script so that those stored values are ready by Python.
However this has a great deal of over head and takes 2to 3 seconds per bar.
Its Much faster to use a Python Bridge.
Here is my python script if you plan to access values using Global variable DLL or Shared Var DLL
if you store values in EasyLanguage using shared var DLL or Global Variable DLL.
THen you can access those stored values in Python. Here was my script I wrote sometime back to access values in Python from Easy Language.
THen , inside easy language, You can use LUA at close of every bar to run a Python script so that those stored values are ready by Python.
However this has a great deal of over head and takes 2to 3 seconds per bar.
Its Much faster to use a Python Bridge.
Here is my python script if you plan to access values using Global variable DLL or Shared Var DLL
Code: Select all
import ctypes
import pandas as pd
from ctypes import *
from sklearn.ensemble import RandomForestClassifier
import numpy as np
import joblib
import datetime
from datetime import datetime
def rm_main():
barint = "1"
lib = cdll.LoadLibrary("SharedVar-w64.dll")
linkID =(lib.svInit("ID_" + barint))
link1 =(lib.svInit("VOpen_" + barint))
link2 =(lib.svInit("VHigh_" + barint))
link3 =(lib.svInit("VLow_" + barint))
link4 =(lib.svInit("VClose_" + barint))
link5 =(lib.svInit("VVolume_" + barint))
link6 =(lib.svInit("VTicks_" + barint))
link99 = (lib.svInit("PredCompleted_" + barint))
link100 =(lib.svInit("Pred_" + barint))
lib.svGetDouble.restype = c_double
lib.svVarName.restype = c_wchar_p
MaxBars = lib.svVarCount(linkID)
data=[]
for x in range(0,MaxBars):
BarDateTime = lib.svVarName(linkID, x)
#print(BarDateTime)
varID = lib.svGetInt(linkID,BarDateTime)
#print(varID)
varValue1 = lib.svGetDouble(link1,BarDateTime)
varValue2 = lib.svGetDouble(link2,BarDateTime)
varValue3 = lib.svGetDouble(link3,BarDateTime)
varValue4 = lib.svGetDouble(link4,BarDateTime)
varValue5 = lib.svGetDouble(link5,BarDateTime)
varValue6 = lib.svGetDouble(link6,BarDateTime)
data.append((varID,BarDateTime,varValue1,varValue2,varValue3,varValue4,varValue5,varValue6))
cols = ['ID','DateTime','VOpen','VHigh','VLow','VClose','VVolume','VTicks']
cols_train = ['VOpen','VHigh','VLow','VClose','VVolume','VTicks']
df = pd.DataFrame(data, columns=cols)
df = df.sort_values(['ID'], ascending=True)
df.set_index('ID',inplace=True)
df = df.iloc[-2:]
df[cols_train] = df[cols_train].pct_change()
df.dropna(inplace=True)
import sys
import os
this_dir = os.path.dirname(__file__) # Path to loader.py
#sys.path.append(os.path.join(this_dir, <rel_path_to_foo.py>))
rf = joblib.load("/rfDummyTestModel.joblib")
df.replace([np.inf,-np.inf],0,inplace=True)
pred = rf.predict(df[cols_train])[0]
#pred = 1.0
pred_c = c_double(pred)
lib.svSetValue.restype = c_double
PredDateTime = df['DateTime'].values[0]
lib.svSetValue(link100, PredDateTime, pred_c)
lib.svIncValue(link99, PredDateTime, 1)
df['pred'] = pred
# -------------------------------------------------------------------------------------
path = "D:\ADE\Data\\"
file = "lua_BTCUSDT_1min.csv"
file1= "lua_BTCUSDT_1minP.csv"
file2= "lua_BTCUSDT_1tickP.csv"
df1 = pd.read_csv(path + file)
df2 = df1.iloc[-1000:]
df2[cols_train] = df2[cols_train].pct_change()
df2 = df2.iloc[1:]
df2 = df2.replace([np.inf,-np.inf],0)
df2 = df2.fillna(0)
pred1 = rf.predict(df2[cols_train])
collist = ['Date','Time','VClose','VVolume']
collist1= ['Date','Time','Close','Volume']
df2 = df2.rename(columns ={'VClose':'Close','VHigh':'High', 'VLow':'Low','VOpen':'Open','VTicks':'Tick','VVolume':'Volume'})
df2.drop(columns=['Tick'], inplace=True)
df2['Volume'].loc[df2.index] = pred1
df2['Volume'].replace({1:2,-1:1},inplace=True)
df2['UpVol'] = 101
df2['DownVol'] = 201
#df2['UpTicks'] = 102
#df2['DownTicks'] = 202
#df2['TotalTicks'] = 302
df2.to_csv("D:\ADE\Data\Lua\Out_min\\"+file1,index=False)
df2[collist1].to_csv("D:\ADE\Data\Lua\Out_tick\\"+file2,index=False)
#df2['pred'] = pred
#df2.to_csv("D:\ADE\Data\Lua\Out_min\\"+file1,index=False)
#df2[collist].to_csv("D:\ADE\Data\Lua\Out_tick\\"+file2,index=False)
# -------------------------------------------------------------------------------------
barint = "1"
import ctypes
lib = cdll.LoadLibrary("D:\MC64\\GlobalVariable.dll")
User32 = ctypes.WinDLL("D:\MC64\\GlobalVariable.dll")
User32.GV_GetNamedDouble("VOpen_" + barint,-99)
set =lib.GV_SetNamedDouble("VOpen_" + barint,10000)
get =lib.GV_GetNamedDouble('VOpen_' + barint,-99)
link1 = lib.GV_GetNamedDouble("VHigh_" + barint)
link2 =(lib.svInit("VHigh_" + barint))
link3 =(lib.svInit("VLow_" + barint))
link4 =(lib.svInit("VClose_" + barint))
link5 =(lib.svInit("VVolume_" + barint))
link6 =(lib.svInit("VTicks_" + barint))
link99 = (lib.svInit("PredCompleted_" + barint))
link100 =(lib.svInit("Pred_" + barint))
lib.svGetDouble.restype = c_double
lib.svVarName.restype = c_wchar_p
MaxBars = lib.svVarCount(linkID)
data=[]
for x in range(0,MaxBars):
BarDateTime = lib.svVarName(linkID, x)
#print(BarDateTime)
varID = lib.svGetInt(linkID,BarDateTime)
#print(varID)
varValue1 = lib.svGetDouble(link1,BarDateTime)
varValue2 = lib.svGetDouble(link2,BarDateTime)
varValue3 = lib.svGetDouble(link3,BarDateTime)
varValue4 = lib.svGetDouble(link4,BarDateTime)
varValue5 = lib.svGetDouble(link5,BarDateTime)
varValue6 = lib.svGetDouble(link6,BarDateTime)
data.append((varID,BarDateTime,varValue1,varValue2,varValue3,varValue4,varValue5,varValue6))
cols = ['ID','DateTime','VOpen','VHigh','VLow','VClose','VVolume','VTicks']
cols_train = ['VOpen','VHigh','VLow','VClose','VVolume','VTicks']
df = pd.DataFrame(data, columns=cols)
df = df.sort_values(['ID'], ascending=True)
df.set_index('ID',inplace=True)
df = df.iloc[-2:]
df[cols_train] = df[cols_train].pct_change()
df.dropna(inplace=True)
rf = joblib.load("/rfDummyTestModel.joblib")
df.replace([np.inf,-np.inf],0,inplace=True)
pred = rf.predict(df[cols_train])[0]
#pred = 1.0
pred_c = c_double(pred)
lib.svSetValue.restype = c_double
PredDateTime = df['DateTime'].values[0]
lib.svSetValue(link100, PredDateTime, pred_c)
lib.svIncValue(link99, PredDateTime, 1)
df['pred'] = pred
return (df)
if __name__ == "__main__":
#df = pd.read_csv('df.csv')
df = rm_main()
df.to_csv('df.csv')
Re: How to access indicator values externally, from python?
Alternatively, I have built a C++ Python Bridge which allows you to litereally send any values from Easy Language to Python and back within Milliseconds.
You can send strings, variables, Open High Low Close, Net Profit Max Drawdownm etc ( basically any Easy language Series) all to python and carry out Machine learning reinforecement learning or any other fancy data science / AI analaysis and return the result back to python.
You can also use the Python Bridge to execute any other External functions such as SFTP uploads, writing to csv , Sending Orders to excahnges not supported in Multicharts. and So On.
You can send strings, variables, Open High Low Close, Net Profit Max Drawdownm etc ( basically any Easy language Series) all to python and carry out Machine learning reinforecement learning or any other fancy data science / AI analaysis and return the result back to python.
You can also use the Python Bridge to execute any other External functions such as SFTP uploads, writing to csv , Sending Orders to excahnges not supported in Multicharts. and So On.