SHARE
TWEET

Untitled

a guest Jun 20th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from ctypes import *
  2. from enum import IntEnum
  3.  
  4.  
  5. ########## start. enum , struct
  6.  
  7.  
  8. class CyMAXNM_ARRVALUE_CySTCM(IntEnum):
  9.      value = 10
  10.  
  11. class CyMAXNUM_SYMBOLS(IntEnum):
  12.     value = 10
  13.  
  14. class CyMAXNUM_FORECAST(IntEnum):
  15.     value = 100
  16.  
  17. class CyMAXNUM_FORECASTMULTI_TYPE(IntEnum):
  18.     value = 5
  19.  
  20. class CyMAXNUM_FORECASTMULTI_STEP(IntEnum):
  21.     value = 20
  22.  
  23. class CyENUM_SYMBOL(IntEnum):
  24.     cyenum_EURUSD = 1
  25.     cyenum_GBPUSD = 2
  26.     cyenum_EURGBP = 3
  27.     cyenum_NONE = 10000
  28.  
  29. GetCyENUM_SYMBOL_from_int = {
  30.     1: CyENUM_SYMBOL.cyenum_EURUSD,  
  31.     2: CyENUM_SYMBOL.cyenum_GBPUSD,  
  32.     3: CyENUM_SYMBOL.cyenum_EURGBP,  
  33.     10000: CyENUM_SYMBOL.cyenum_NONE  
  34. }
  35.  
  36. class CyENUM_TIMEFRAMES(IntEnum):
  37.     PERIOD_M1 = 1
  38.     PERIOD_M5 = 5
  39.     PERIOD_M10 = 10
  40.     PERIOD_M15 = 15
  41.     PERIOD_H1 = 16385
  42.     PERIOD_H4 = 16388
  43.     PERIOD_D1 = 16408
  44.  
  45. GetCyENUM_TIMEFRAMES_from_int = {
  46.     1: CyENUM_TIMEFRAMES.PERIOD_M1,  
  47.     5: CyENUM_TIMEFRAMES.PERIOD_M5,  
  48.     10: CyENUM_TIMEFRAMES.PERIOD_M10,  
  49.     15: CyENUM_TIMEFRAMES.PERIOD_M15,  
  50.     16385: CyENUM_TIMEFRAMES.PERIOD_H1,  
  51.     16388: CyENUM_TIMEFRAMES.PERIOD_H4,  
  52.     16408: CyENUM_TIMEFRAMES.PERIOD_D1  
  53. }    
  54.  
  55. class CyST_POSITION(Structure):
  56.     _pack_ = 4
  57.     _fields_ = [("PriceUnit_Sell", c_double),
  58.                 ("ProfitUnit_Sell",c_double), # = (PriceUnit_Sell - PriceAsk)
  59.                 ("QtyTotal_Sell",c_double), # unit LOT. minumum 0.01 LOT
  60.                 ("PriceLastEntry_Sell",c_double),
  61.                 ("PriceUnit_Buy", c_double),
  62.                 ("ProfitUnit_Buy",c_double), # = (PriceBid - PriceUnit_Buy)
  63.                 ("QtyTotal_Buy",c_double), # unit LOT
  64.                 ("PriceLastEntry_Buy",c_double)
  65.                ]
  66.  
  67. class CyST_DATATICK(Structure): # the struct for the function cyfinfoca.Get_DataTick_CyFinForecast()
  68.     _pack_ = 4
  69.     _fields_ = [("i_symbol", c_int),
  70.                 ("pstr_symbol",c_wchar_p), # symbol name. ex. EURUSD,...
  71.                 ("price_ask", c_double),
  72.                 ("price_bid", c_double)
  73.                ]
  74.  
  75.  
  76. class CyST_CANDLE_forMULTI(Structure):
  77.     _pack_ = 4
  78.     _fields_ = [("Open", c_double),("High", c_double),("Low", c_double),("Close", c_double), # OHLC for one candle.
  79.                 ("MA", c_double * CyMAXNM_ARRVALUE_CySTCM.value), # MA5, MA20, MA40, MA80, MA160
  80.                 ("PriceExt", c_double * CyMAXNM_ARRVALUE_CySTCM.value), # ex. Kalman Filtered.
  81.                 ("ValueExt", c_double * CyMAXNM_ARRVALUE_CySTCM.value) # ex. ATR etc.
  82.                ]
  83.  
  84.  
  85. class CyST_DATACANDLE_SYMBOLMULTI(Structure): # the struct for the function cyfinfoca.Get_DataCandle
  86.     _pack_ = 4
  87.     _fields_ = [("ID_SEQUENCE", c_int),
  88.                 ("i_timeframe", c_int),
  89.                 ("num_symbols", c_int),
  90.                 ("iarr_symbols",c_int * CyMAXNUM_SYMBOLS.value),
  91.                 ("starr_candle",CyST_CANDLE_forMULTI * CyMAXNUM_SYMBOLS.value),
  92.                 ("starr_position",CyST_POSITION * CyMAXNUM_SYMBOLS.value)
  93.                ]
  94.  
  95.  
  96. # CyFinForecast v.1.1 2019.06.07
  97. class CyST_DATAFORECAST_forMULTI(Structure):
  98.     _pack_ = 4
  99.     _fields_ = [
  100.                 ("arr1d_forecast",c_double * CyMAXNUM_FORECAST.value),
  101.                 ("arr2d_forecast",c_double * CyMAXNUM_FORECASTMULTI_TYPE.value * CyMAXNUM_FORECASTMULTI_STEP.value), # 2d array
  102.                 ("trade_sell_entry",c_int), # more than 1 = entry sell. 0 = no trade . 1 means 0.01 lot
  103.                 ("trade_sell_exit",c_int),  # 1 = exit "all" sell positions. 0=no trade
  104.                 ("trade_buy_entry",c_int),  # more than 1 = entry buy, 0 = no action . 1 means 0.01 lot
  105.                 ("trade_buy_exit",c_int)  # 1 = exit "all" buy positions, 0 = no action
  106.                ]
  107.  
  108. # CyFinForecast v.1.1 2019.06.07
  109. class CyST_DATAFORECAST_SYMBOLMULTI(Structure): # the struct for sending forecast results.
  110.     _pack_ = 4
  111.     _fields_ = [("ID_SEQUENCE", c_int),
  112.                 ("i_timeframe", c_int),
  113.                 ("num_symbols", c_int),
  114.                 ("iarr_symbols",c_int * CyMAXNUM_SYMBOLS.value),
  115.                 ("num_step_arr1d",c_int),
  116.                 ("num_type_arr2d",c_int),
  117.                 ("num_step_arr2d",c_int),
  118.                 ("starr_forecast",CyST_DATAFORECAST_forMULTI * CyMAXNUM_SYMBOLS.value)
  119.                ]
  120.  
  121.  
  122. ########## end. enum , struct
  123.  
  124.  
  125. cyst_DataTick = CyST_DATATICK() #to get tick via function call Get_DataTick_CyFinForecast() only in while
  126.  
  127. cyst_DataCandleSymbolMulti = CyST_DATACANDLE_SYMBOLMULTI() #to get candle data
  128.  
  129. cyst_DataForecastSymbolMulti = CyST_DATAFORECAST_SYMBOLMULTI() # to send results (forecasted data)
  130.  
  131. cyfinfoca = windll.LoadLibrary("d:\\CyFin\\64bit\\CyFinForecast.dll")
  132. cyfinfoca.OpenApi_CyFinForecast()  
  133.  
  134. def OnReceive_DataCandles():
  135.     cyfinfoca.Get_DataCandle_SymbolMulti_CyFinForecast(byref(cyst_DataCandleSymbolMulti)) # geting candle data into struct cyst_DataCandleSymbolMulti
  136.     cyenum_timeframe = GetCyENUM_TIMEFRAMES_from_int[cyst_DataCandleSymbolMulti.i_timeframe]
  137.     id_sequence = cyst_DataCandleSymbolMulti.ID_SEQUENCE;
  138.  
  139.     print("\n########### Received New Candle. timeframe = ",cyenum_timeframe, " ID_SEQUENCE = ", id_sequence)
  140.  
  141.     for idx_symbol in range(0,cyst_DataCandleSymbolMulti.num_symbols):
  142.  
  143.         cyenum_symbol = GetCyENUM_SYMBOL_from_int[cyst_DataCandleSymbolMulti.iarr_symbols[idx_symbol]]
  144.        
  145.         print("Symbol:", cyenum_symbol.name)
  146.  
  147.         ########## Get info price
  148.         price_o = cyst_DataCandleSymbolMulti.starr_candle[idx_symbol].Open
  149.         price_h = cyst_DataCandleSymbolMulti.starr_candle[idx_symbol].High
  150.         price_l = cyst_DataCandleSymbolMulti.starr_candle[idx_symbol].Low
  151.         price_c = cyst_DataCandleSymbolMulti.starr_candle[idx_symbol].Close
  152.  
  153.         ema5 = cyst_DataCandleSymbolMulti.starr_candle[idx_symbol].MA[0]
  154.         ema20 = cyst_DataCandleSymbolMulti.starr_candle[idx_symbol].MA[1]
  155.         ema80 = cyst_DataCandleSymbolMulti.starr_candle[idx_symbol].MA[3]
  156.  
  157.         atr = cyst_DataCandleSymbolMulti.starr_candle[idx_symbol].ValueExt[0] # Average true range
  158.            
  159.         print("[PRICE]", " Open:%.5f" % price_o , ", High:%.5f" % price_h, ", Low:%.5f" % price_l, ", Close:%.5f" % price_c , "ema5:%.5f" % ema5 , ", ema20:%.5f" % ema20, ", ATR:%.5f" % atr) # OHLC test print. EMA5 , EMA20 , ATR test out
  160.        
  161.         ########## Get info position
  162.         priceunit_sell = cyst_DataCandleSymbolMulti.starr_position[idx_symbol].PriceUnit_Sell
  163.         profitunit_sell = cyst_DataCandleSymbolMulti.starr_position[idx_symbol].ProfitUnit_Sell
  164.         qtytotal_sell = cyst_DataCandleSymbolMulti.starr_position[idx_symbol].QtyTotal_Sell
  165.         pricelastentry_sell = cyst_DataCandleSymbolMulti.starr_position[idx_symbol].PriceLastEntry_Sell
  166.        
  167.         priceunit_buy = cyst_DataCandleSymbolMulti.starr_position[idx_symbol].PriceUnit_Buy
  168.         profitunit_buy = cyst_DataCandleSymbolMulti.starr_position[idx_symbol].ProfitUnit_Buy
  169.         qtytotal_buy = cyst_DataCandleSymbolMulti.starr_position[idx_symbol].QtyTotal_Buy
  170.         pricelastentry_buy = cyst_DataCandleSymbolMulti.starr_position[idx_symbol].PriceLastEntry_Buy
  171.        
  172.        
  173.         print("[POSITION SELL]", "PriceUnit:%.5f" % priceunit_sell, " , ProfitUnit:%.5f" % profitunit_sell, ", QtyTotal:%.2f Lot" % qtytotal_sell,", PriceLastEntry:%.5f" % pricelastentry_sell)
  174.         print("[POSITION BUY]", "PriceUnit:%.5f" % priceunit_buy, " , ProfitUnit:%.5f" % profitunit_buy, ", QtyTotal:%.2f Lot" % qtytotal_buy,", PriceLastEntry:%.5f" % pricelastentry_buy)
  175.  
  176.         ########### !!! do something here for forecast processing.
  177.  
  178.  
  179.         ########### Making forecast data to Send MT5 via CyFinForecast.dll -> CyFinDevice.dll -> MT5
  180.              
  181.         ###### for sending multi data multi symbol forecasted data.
  182.         cyst_DataForecastSymbolMulti.ID_SEQUENCE = id_sequence # received ID_SEQUEENCE.
  183.         cyst_DataForecastSymbolMulti.i_timeframe = cyenum_timeframe
  184.         cyst_DataForecastSymbolMulti.num_symbols = 3    # number of symbols.
  185.         cyst_DataForecastSymbolMulti.iarr_symbols[idx_symbol] = cyenum_symbol #  
  186.         cyst_DataForecastSymbolMulti.num_step_arr1d = 8 # the number of forecasted step. maximum step is 100.
  187.         cyst_DataForecastSymbolMulti.num_type_arr2d = 5 # ex. H, L, C, ATR etc, maximum number is 5(CyMAXNUM_FORECASTMULTI_TYPE)
  188.         cyst_DataForecastSymbolMulti.num_step_arr2d = 5 # the number of forecasted step. maximum step is 20
  189.  
  190.         for idx_foca in range(0,cyst_DataForecastSymbolMulti.num_step_arr1d):  # forecasted value series. these values can be shown on chart.
  191.             cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].arr1d_forecast[idx_foca] = ema5  #
  192.  
  193.         for idx_foca in range(0,cyst_DataForecastSymbolMulti.num_step_arr2d): # forecasted multi value series.
  194.             cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].arr2d_forecast[0][idx_foca] = price_h # forecasted hgh value series.
  195.             cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].arr2d_forecast[1][idx_foca] = price_l # forecasted low value series.
  196.             cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].arr2d_forecast[2][idx_foca] = price_c # forecasted close value series.
  197.             cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].arr2d_forecast[3][idx_foca] = 0 # forecasted D2 value series.
  198.             cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].arr2d_forecast[4][idx_foca] = 0 # forecasted any value series.
  199.             cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].trade_sell_entry = 0
  200.             cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].trade_sell_exit = 0
  201.             cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].trade_buy_entry = 0
  202.             cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].trade_buy_exit = 0
  203.            
  204.             # test trade example. sell entry for EURUSD . both sell and buy entry at every candle
  205.             if cyenum_symbol == CyENUM_SYMBOL.cyenum_EURUSD and cyenum_timeframe == CyENUM_TIMEFRAMES.PERIOD_M1: # ordering for only EURUSD and timeframe M1
  206.                 # examples. sell position close(=exit)
  207.                 if profitunit_sell > 0.00050: # morethan 50 points
  208.                     cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].trade_sell_exit = 1 # exit all sell position.
  209.                 # examples. sell entry first
  210.                 if qtytotal_sell < 0.01: # if no position sell.
  211.                     cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].trade_sell_entry = 10 # 10 means (10*0.01 = 0.1) lot quantity
  212.                 # examples. sell entry add
  213.                 if price_c > (pricelastentry_sell + 0.00050):
  214.                     cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].trade_sell_entry = 10 # 10 means (10*0.01 = 0.1) lot quantity
  215.                
  216.                 # examples. buy position close(=exit)
  217.                 if profitunit_buy > 0.00050: # morethan 50 points
  218.                     cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].trade_buy_exit = 1 # exit all buy position.
  219.                 # examples. buy entry first
  220.                 if qtytotal_buy < 0.01: # if no position buy.
  221.                     cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].trade_buy_entry = 10 # 10 means (10*0.01 = 0.1) lot quantity
  222.                 # examples. buy entry add
  223.                 if price_c <  (pricelastentry_buy- 0.00050):
  224.                     cyst_DataForecastSymbolMulti.starr_forecast[idx_symbol].trade_buy_entry = 10  # 10 means (10*0.01 = 0.1) lot quantity
  225.  
  226.         ## end of "for idx_symbol"
  227.     ## end of def OnReceive_DataCandles():
  228.  
  229. while 1: # wait for receiving data
  230.  
  231.     iretv = cyfinfoca.WaitNewData_CyFinForecast() # !!! Mandatory pausing until receiving data
  232.  
  233.     if iretv == 1: # on received data tick.
  234.         cyfinfoca.Get_DataTick_CyFinForecast(byref(cyst_DataTick))  # getting tick data into struct pyst_DataTick
  235.         print("Received Tick. Symbol = ",cyst_DataTick.pstr_symbol,"(",cyst_DataTick.i_symbol,"), ask = %.5f , " % cyst_DataTick.price_ask,"bid = %.5f" % cyst_DataTick.price_bid) # test
  236.  
  237.  
  238.     elif iretv == 2: # on received data candle.
  239.  
  240.         OnReceive_DataCandles()
  241.  
  242.         cyfinfoca.Send_DataForecastSymbolMulti_CyFinForecast(byref(cyst_DataForecastSymbolMulti)) # !!! important call this function after end of "loop for idx_symbol"        
  243.  
  244.         ## end of elif iretv == 2: # on received data candle.
  245.  
  246.     cyfinfoca.AvailableReceiveNextData_CyFinForecast() # !!! Mandatory  notify to Dll , the next data receive available.
  247.     ## end of while 1
  248.  
  249. cyfinfoca.CloseApi_CyFinForecast() # not mandatory for python application.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top