QPILE

7 2018 smart-lab.ru | /
. .
. . .

  
 cAccount=7600lll  
 cClassName=SPBFUT  
 cProfit=7500  
 cProfShift=100   

cProfSpr=500 
 cStopLoss=400  
 cSLSpr=500 
 :

PORTFOLIO_EX VFAutoStop;
 DESCRIPTION VFAutoStop;
 CLIENTS_LIST ALL_CLIENTS;
 FIRMS_LIST ALL_FIRMS;
 USE_CASE_SENSITIVE_CONSTANTS;

PROGRAM

New_Global(CurLogLine,1)
 New_Global(gLastPos,CREATE_MAP ()) '  

ClassesList = get_classes_list()

cAccount=7600lll
 cClassName=SPBFUT
 cProfit=7500
 cProfShift=100
 cProfSpr=500
 cStopLoss=400
 cSLSpr=500

cLogFile=C:\VFAutoStop.log

FUNC WriteLog (pTitle, pMessage)
 writeln(cLogFile, get_value(GET_DATETIME(), Datetime) & " " & pTitle & " > " & pMessage)
 END FUNC

func SendTrans(pTransParams)
 trans_result = SEND_TRANSACTION (30, pTransParams)
 'LogData(pTransParams,trans_result)
 if get_value (trans_result, RESULT)+0.0=0 then
 ' WriteLog(pTransParams,get_value (trans_result, RESULT_EX) & "|" & get_value (trans_result, DESCRIPTION))
 WriteLog(pTransParams,trans_result)
 end if
 end func

Func ActiveStopOrder(pSecCode)
 nOrd=Get_number_of(STOP_ORDERS)
 result=CREATE_MAP ()
 for iOrd from 1 to nOrd
 asoOrder = get_item(STOP_ORDERS, iOrd)
 if get_value(asoOrder, STATUS)=ACTIVE and get_value(asoOrder, SECCODE)=pSecCode
 result = asoOrder
 end if
 end for
 End Func

FUNC SendStopOrderTPSL (pSecCode, pClassCode, pOper, pProfPrice, pProfOffs, pProfSprm, pStopPrice,pPrice,pSize)
 trans_params = ""
 trans_params = set_value (trans_params, TRANS_ID, 1)
 trans_params = set_value (trans_params, ACTION, NEW_STOP_ORDER)
 trans_params = set_value (trans_params, STOP_ORDER_KIND, TAKE_PROFIT_AND_STOP_LIMIT_ORDER)
 trans_params = set_value (trans_params, OFFSET_UNITS, PRICE_UNITS)
 trans_params = set_value (trans_params, SPREAD_UNITS, PRICE_UNITS)
 trans_params = set_value (trans_params, CLASSCODE, pClassCode)
 trans_params = set_value (trans_params, SECCODE, pSecCode)
 trans_params = set_value (trans_params, ACCOUNT, cAccount)
 trans_params = set_value (trans_params, OPERATION, pOper)
 trans_params = set_value (trans_params, STOPPRICE, pProfPrice)
 trans_params = set_value (trans_params, STOPPRICE2, pStopPrice)
 trans_params = set_value (trans_params, PRICE, pPrice)
 trans_params = set_value (trans_params, OFFSET, pProfOffs)
 trans_params = set_value (trans_params, SPREAD, pProfSprm)
 trans_params = set_value (trans_params, QUANTITY, pSize)
 trans_params = set_value (trans_params, EXPIRY_DATE, GTC)
 SendTrans(trans_params)
 end func

Func LogData(Prm1,Prm2)
 output = CREATE_MAP ()
 output = SET_VALUE (output, Param0, GET_INFO_PARAM(SERVERTIME))
 'GET_VALUE(GET_DATETIME(), DATETIME)
 output = SET_VALUE (output, Param1, Prm1)
 output = SET_VALUE (output, Param2, Prm2)
 ADD_ITEM (CurLogLine, output)
 CurLogLine=CurLogLine+1
 End Func

nLimPos=GET_NUMBER_OF(FUTURES_CLIENT_HOLDINGS)

FOR iLP FROM 1 to nLimPos
 LimPos= GET_ITEM (FUTURES_CLIENT_HOLDINGS, iLP)
 nTotNet=GET_VALUE(LimPos,TOTAL_NET)+0
 cSecCode=GET_VALUE(LimPos,SECCODE)
 if nTotNet<>0 then '   
 ASO=ActiveStopOrder(cSecCode)
 cLastPrice=GET_PARAM(ClassesList, cSecCode, LAST)+0
 if get_value(ASO, STATUS)<>ACTIVE '  -?
 if get_value(gLastPos,cSecCode) & "!" <>nTotNet & "!" 
 nDir=nTotNet/abs(nTotNet)
 if nDir>0
 cOPer=S
 else
 cOper=B
 end if
 nProfit=cLastPrice+nDir*cProfit
 nStop=cLastPrice-nDir*cStopLoss
 SendStopOrderTPSL(cSecCode, cClassName, cOper, nProfit, cProfShift, cProfSpr, nStop, cLastPrice-nDir*(cStopLoss+cSLSpr), abs(nTotNet))
 LogData(nTotNet,nProfit & "/" & nStop)
 gLastPos=set_value(gLastPos,cSecCode,nTotNet)
 end if
 end if
 else
 gLastPos=set_value(gLastPos,cSecCode,nTotNet)
 end if
 end for

END_PROGRAM

PARAMETER Param0;
 PARAMETER_TITLE ;
 PARAMETER_DESCRIPTION ;
 PARAMETER_TYPE STRING (200);
 END

PARAMETER Param1;
 PARAMETER_TITLE -;
 PARAMETER_DESCRIPTION -;
 PARAMETER_TYPE STRING (200);
 END

PARAMETER Param2;
 PARAMETER_TITLE ;
 PARAMETER_DESCRIPTION ;
 PARAMETER_TYPE STRING (200);
 END

END_PORTFOLIO_EX

/ https://smart-lab.ru/blog/456543.php
http://elitetrader.ru/index.php?newsid=387711