How to write this piece of code in function? - pine-script-v5

I am using the "Divergence Indicator" script written by Tradingview with Macd, stoch and rsi at the same time.
I use the code below duplicate 3 times (for Macd, stoch and rsi)
plFound = na(ta.pivotlow(rsi, lbL, lbR)) ? false : true
phFound = na(ta.pivothigh(rsi, lbL, lbR)) ? false : true
// Regular Bullish
oscHL = rsi[lbR] > ta.valuewhen(plFound, rsi[lbR], 1) and _inRange(plFound[1])
priceLL = close[lbR] < ta.valuewhen(plFound, close[lbR], 1)
bullCond = priceLL and oscHL and plFound
// Regular Bearish
oscLH = rsi[lbR] < ta.valuewhen(phFound, rsi[lbR], 1) and _inRange(phFound[1])
priceHH = close[lbR] > ta.valuewhen(phFound, close[lbR], 1)
bear = priceHH and oscLH and phFound
Is it possible to use a function to avoid duplicating the same code 3 times?

Very simple
f_divergences(_src) =>
plFound = na(ta.pivotlow(_src, lbL, lbR)) ? false : true
phFound = na(ta.pivothigh(_src, lbL, lbR)) ? false : true
// Regular Bullish
oscHL = _src[lbR] > ta.valuewhen(plFound, _src[lbR], 1) and _inRange(plFound[1])
priceLL = close[lbR] < ta.valuewhen(plFound, close[lbR], 1)
bullCond = priceLL and oscHL and plFound
// Regular Bearish
oscLH = _src[lbR] < ta.valuewhen(phFound, _src[lbR], 1) and
_inRange(phFound[1])
priceHH = close[lbR] > ta.valuewhen(phFound, close[lbR], 1)
bear = priceHH and oscLH and phFound
[bullCond, bear]
// Calling the function three times
[rsi_bull, rsi_bear] = f_divergences(rsi)
[stoch_bull, stoch_bear] = f_divergences(stoch)
[macd_bull, macd_bear] = f_divergences(macd)

Related

NuSMV stucked for none reason

I'm writing a model in NuSMV. However, there is a problem. When I try to simulate the model in using the interactive mode using ./NuSMV -int, it stucks on a state saying that there is not any further reachable states. But, this should not be true according the way I write the model and the transition from that state. For more information I put here the entire model, the execution trace and the entire last state of the trace.
For the model I put the link of pastebin Model. The intereseted part of the model is the MODULE PI and the transition from the pc = 53 to the pc = 54 in the TRANS section.
This is the trace of the simulation
Trace Type: Simulation
-> State: 13.1 <-
ca.x1 = None
ca.x2 = None
ca.PK = None
ca.x3 = None
ca.len = 0
cb.x1 = None
cb.PK = None
cb.x2 = None
cb.len = 0
IniCommitAB = FALSE
IniRunningAB = FALSE
ResRunningAB = FALSE
ResCommitAB = FALSE
p_initial.PIni_process1.slef = None
p_initial.PIni_process1.party = None
p_initial.PIni_process1.nonce = None
p_initial.PIni_process1.runable = FALSE
p_initial.PIni_process1.g1 = None
p_initial.PIni_process1.pc = 1
p_initial.PIni_process2.slef = None
p_initial.PIni_process2.party = None
p_initial.PIni_process2.nonce = None
p_initial.PIni_process2.runable = FALSE
p_initial.PIni_process2.g1 = None
p_initial.PIni_process2.pc = 1
p_initial.PRes_process.slef = None
p_initial.PRes_process.nonce = None
p_initial.PRes_process.g2 = None
p_initial.PRes_process.g3 = None
p_initial.PRes_process.runable = FALSE
p_initial.PRes_process.pc = 1
p_initial.PI_process.kNa = FALSE
p_initial.PI_process.kNb = FALSE
p_initial.PI_process.k_Na_Nb__A = FALSE
p_initial.PI_process.k_Na_A__B = FALSE
p_initial.PI_process.k_Nb__B = FALSE
p_initial.PI_process.x1 = None
p_initial.PI_process.x2 = None
p_initial.PI_process.x3 = None
p_initial.PI_process.pc = 1
p_initial.PI_process.runable = FALSE
p_initial.pc = 1
cb_is_empty = TRUE
ca_is_empty = TRUE
p_initial.PI_process.x3_I = FALSE
p_initial.PI_process.check = TRUE
-> Input: 13.2 <-
_process_selector_ = p_initial
running = FALSE
p_initial.running = TRUE
p_initial.PI_process.running = FALSE
p_initial.PRes_process.running = FALSE
p_initial.PIni_process2.running = FALSE
p_initial.PIni_process1.running = FALSE
cb.running = FALSE
ca.running = FALSE
-> State: 13.2 <-
p_initial.pc = 2
-> Input: 13.3 <-
-> State: 13.3 <-
p_initial.PIni_process1.slef = A1
p_initial.PIni_process1.party = I
p_initial.PIni_process1.nonce = Na
p_initial.PIni_process1.runable = TRUE
p_initial.pc = 4
-> Input: 13.4 <-
-> State: 13.4 <-
p_initial.PRes_process.slef = B
p_initial.PRes_process.nonce = Nb
p_initial.PRes_process.runable = TRUE
p_initial.pc = 5
-> Input: 13.5 <-
-> State: 13.5 <-
p_initial.PI_process.runable = TRUE
p_initial.pc = 6
-> Input: 13.6 <-
_process_selector_ = p_initial.PIni_process1
p_initial.running = FALSE
p_initial.PIni_process1.running = TRUE
-> State: 13.6 <-
p_initial.PIni_process1.pc = 4
-> Input: 13.7 <-
-> State: 13.7 <-
p_initial.PIni_process1.pc = 5
-> Input: 13.8 <-
-> State: 13.8 <-
p_initial.PIni_process1.pc = 6
-> Input: 13.9 <-
-> State: 13.9 <-
ca.x1 = A1
ca.x2 = Na
ca.PK = A1
ca.x3 = I
ca.len = 1
p_initial.PIni_process1.pc = 7
ca_is_empty = FALSE
-> Input: 13.10 <-
_process_selector_ = p_initial.PI_process
p_initial.PI_process.running = TRUE
p_initial.PIni_process1.running = FALSE
-> State: 13.10 <-
p_initial.PI_process.pc = 52
-> Input: 13.11 <-
-> State: 13.11 <-
ca.x1 = None
ca.x2 = None
ca.PK = None
ca.x3 = None
ca.len = 0
p_initial.PI_process.x1 = Na
p_initial.PI_process.x2 = A1
p_initial.PI_process.x3 = I
p_initial.PI_process.pc = 53
ca_is_empty = TRUE
p_initial.PI_process.x3_I = TRUE
While this is the entire last state of the execution
ca.x1 = None
ca.x2 = None
ca.PK = None
ca.x3 = None
ca.len = 0
cb.x1 = None
cb.PK = None
cb.x2 = None
cb.len = 0
IniCommitAB = FALSE
IniRunningAB = FALSE
ResRunningAB = FALSE
ResCommitAB = FALSE
p_initial.PIni_process1.slef = A1
p_initial.PIni_process1.party = I
p_initial.PIni_process1.nonce = Na
p_initial.PIni_process1.runable = TRUE
p_initial.PIni_process1.g1 = None
p_initial.PIni_process1.pc = 7
p_initial.PIni_process2.slef = None
p_initial.PIni_process2.party = None
p_initial.PIni_process2.nonce = None
p_initial.PIni_process2.runable = FALSE
p_initial.PIni_process2.g1 = None
p_initial.PIni_process2.pc = 1
p_initial.PRes_process.slef = B
p_initial.PRes_process.nonce = Nb
p_initial.PRes_process.g2 = None
p_initial.PRes_process.g3 = None
p_initial.PRes_process.runable = TRUE
p_initial.PRes_process.pc = 1
p_initial.PI_process.kNa = FALSE
p_initial.PI_process.kNb = FALSE
p_initial.PI_process.k_Na_Nb__A = FALSE
p_initial.PI_process.k_Na_A__B = FALSE
p_initial.PI_process.k_Nb__B = FALSE
p_initial.PI_process.x1 = Na
p_initial.PI_process.x2 = A1
p_initial.PI_process.x3 = I
p_initial.PI_process.pc = 53
p_initial.PI_process.runable = TRUE
p_initial.pc = 6
cb_is_empty = TRUE
ca_is_empty = TRUE
p_initial.PI_process.x3_I = TRUE
The problem is that NuSMV stucks on this state. I don't know why, since the condition to step in the next state is that (x3 = I) that is true, in fact p_initial.PI_process.x3_I = TRUE. Moreover, for every simulation it stops at most at the 11th state. These are the command that I give to execute
read_model -i <model>
flatten_hierarchy
encode_variables
build_model
pick_state -r
simulate -r -p -k 12
I know that it is not a simple problem. But I need help, I'm stucked on this problem since weeks.

MACRO Debug for pivot table

Trying to create a MACRO to run a pivot table and I need help debugging. Here is the script:
Sheets.Add
newsheet = ActiveSheet.Name
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
dataname, Version:=xlPivotTableVersion10). _
CreatePivotTable TableDestination:=newsheet & "!R3C1", TableName:="PivotTable2" _
, DefaultVersion:=xlPivotTableVersion10
Sheets(newsheet).Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable2")
.ColumnGrand = True
.HasAutoFormat = True
.DisplayErrorString = False
.DisplayNullString = True
.EnableDrilldown = True
.ErrorString = ""
.MergeLabels = False
.NullString = ""
.PageFieldOrder = 2
.PageFieldWrapCount = 0
.PreserveFormatting = True
.RowGrand = True
.SaveData = True
.PrintTitles = False
.RepeatItemsOnEachPrintedPage = True
.TotalsAnnotation = False
.CompactRowIndent = 1
.InGridDropZones = True
.DisplayFieldCaptions = True
.DisplayMemberPropertyTooltips = False
.DisplayContextTooltips = True
.ShowDrillIndicators = True
.PrintDrillIndicators = False
.AllowMultipleFilters = True
.SortUsingCustomLists = True
.FieldListSortAscending = False
.ShowValuesRow = True
.CalculatedMembersInFilters = False
.RowAxisLayout xlTabularRow
End With
With ActiveSheet.PivotTables("PivotTable2").PivotCache
.RefreshOnFileOpen = False
.MissingItemsLimit = xlMissingItemsDefault
End With
ActiveSheet.PivotTables("PivotTable2").RepeatAllLabels xlRepeatLabels
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Status")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Status"), "Count of Status", xlCount
End Sub
The error message I receive is run-time error 1004
application-defined or object-declined error
The line that is the issue is below:
.DisplayMemberPropertyTooltips = False
How do I correct? Thanks!!!

if statement is true but doesnt go into the block

This is my coffe script:
jQuery ->
if $('#infinite-scrolling').size() > 0
$(window).on 'scroll', ->
window_top = $(window).scrollTop()
doc_height = $(document).height()
window_height = $(window).height()
window_bottom = window_top + window_height
scroll = window_bottom / doc_height > 0.9
next_page = $('.next_page').attr('href')
console.log(next_page)
console.log($('#infinite-scrolling').size() > 0)
if next_page && scroll
$.getScript next_page
return
return
It doesnt get into the if block allthough the statement in the if should be true because when I remove the if then I can see in the console that its true through this line:
console.log($('#infinite-scrolling').size() > 0)
Anyone any idea what I am doing wrong?
The if statement looks fine.
The code works for me once I fix the indentation, except that .size() is deprecated in the latest jquery (3.3.1). I replaced it with .length.
But I can't test the second if block without your HTML.
Are you sure there are no errors thrown?
Here is my version:
jQuery ->
if $('#infinite-scrolling').length > 0
$(window).on 'scroll', ->
window_top = $(window).scrollTop()
doc_height = $(document).height()
window_height = $(window).height()
window_bottom = window_top + window_height
scroll = window_bottom / doc_height > 0.9
next_page = $('.next_page').attr('href')
console.log(next_page)
console.log($('#infinite-scrolling').length > 0)
if next_page && scroll
$.getScript next_page
return
return
And here as a codepen

How to remove the following character "/" from service path

Good Morning!
Currently I have set up my structure in Fiware saving my historical records in MongoDB, for this I have been using Mlab as a hosting.
I attache the configuration file of my agent, the problem comes in that due to the mandatory character "/" of the service path I can not access the generated historical data, since it is a character not allowed for collections in MongoDB.
agent_1.conf
cygnus-ngsi.sources = http-source
cygnus-ngsi.sinks = mongo-sink
cygnus-ngsi.channels = mongo-channel
cygnus-ngsi.sources.http-source.channels = mongo-channel
cygnus-ngsi.sources.http-source.type = org.apache.flume.source.http.HTTPSource
cygnus-ngsi.sources.http-source.port = 5050
cygnus-ngsi.sources.http-source.handler = com.telefonica.iot.cygnus.handlers.NGSIRestHandler
cygnus-ngsi.sources.http-source.handler.notification_target = /notify
cygnus-ngsi.sources.http-source.handler.default_service = default
cygnus-ngsi.sources.http-source.handler.default_service_path = /sevilla
cygnus-ngsi.sources.http-source.handler.events_ttl = 2
cygnus-ngsi.sources.http-source.interceptors = ts
cygnus-ngsi.sources.http-source.interceptors.ts.type = timestamp
cygnus-ngsi.sinks.mongo-sink.type = com.telefonica.iot.cygnus.sinks.NGSIMongoSink
cygnus-ngsi.sinks.mongo-sink.channel = mongo-channel
cygnus-ngsi.sinks.mongo-sink.enable_encoding = false
cygnus-ngsi.sinks.mongo-sink.enable_grouping = false
cygnus-ngsi.sinks.mongo-sink.enable_name_mappings = false
cygnus-ngsi.sinks.mongo-sink.enable_lowercase = false
cygnus-ngsi.sinks.mongo-sink.data_model = dm-by-service-path
cygnus-ngsi.sinks.mongo-sink.attr_persistence = row
cygnus-ngsi.sinks.mongo-sink.mongo_hosts = ds******.mlab.com:35866
cygnus-ngsi.sinks.mongo-sink.mongo_username = my_user
cygnus-ngsi.sinks.mongo-sink.mongo_password = ********
cygnus-ngsi.sinks.mongo-sink.db_prefix = sth_
cygnus-ngsi.sinks.mongo-sink.collection_prefix = sth_
cygnus-ngsi.sinks.mongo-sink.batch_size = 1
cygnus-ngsi.sinks.mongo-sink.batch_timeout = 30
cygnus-ngsi.sinks.mongo-sink.batch_ttl = 10
cygnus-ngsi.sinks.mongo-sink.data_expiration = 0
cygnus-ngsi.sinks.mongo-sink.collections_size = 0
cygnus-ngsi.sinks.mongo-sink.max_documents = 0
cygnus-ngsi.sinks.mongo-sink.ignore_white_spaces = true
cygnus-ngsi.channels.mongo-channel.type = com.telefonica.iot.cygnus.channels.CygnusMemoryChannel
cygnus-ngsi.channels.mongo-channel.capacity = 1000
cygnus-ngsi.channels.mongo-channel.transactionCapacity = 100
Is there any way for Cygnus to remove the "/" character from the service path?
Error: http://www.subirimagenes.com/imagedata.php?url=http://s2.subirimagenes.com/imagen/9827048captura-de-pantalla.png
SOLUTION: You just have to change the enconding to true in the agent configuration
cygnus-ngsi.sinks.mongo-sink.enable_encoding = true
Thank you very much!

Select and update a PostgreSQL in the same query in order to optimize

I am trying to create a query to dynamically update some values from a table Y from a table X.
I'm currenly dealing with a DB that hosts 10kk rows and I would like to optimize the amount of time of the processing.
Select:
SELECT cycle_completa.numcycle AS actual_cycle,
min(cycle_completa.pump1pre) AS min_pump1pre,
min(cycle_completa.pump2pre) AS min_pump2pre,
min(cycle_completa.pump3pre) AS min_pump3pre,
min(cycle_completa.slidevel) AS min_slidevel,
min(cycle_completa.slidepos) AS min_slidepos,
min(cycle_completa.slideforce) AS min_slideforce,
min(cycle_completa.slideservopilotpre) AS min_slideservopilotpre,
min(cycle_completa.slidecentralpre) AS min_slidecentralpre,
min(cycle_completa.slidelateralpre) AS min_slidelateralpre,
min(cycle_completa.slideringpre) AS min_slideringpre,
min(cycle_completa.slidepistonpre) AS min_slidepistonpre,
min(cycle_completa.slidey42pre) AS min_slidey42pre,
min(cycle_completa.cushionforce) AS min_cushionforce,
min(cycle_completa.cushionservopilotpre) AS min_cushionservopilotpre,
min(cycle_completa.cushionpistonpre) AS min_cushionpistonpre,
min(cycle_completa.cushionringpre) AS min_cushionringpre,
min(cycle_completa.pfillaccumpre) AS min_pfillaccumpre,
min(cycle_completa.pfillcentralpre) AS min_pfillcentralpre,
min(cycle_completa.pfilllateralpre) AS min_pfilllateralpre,
min(cycle_completa.pfillunlockingpre) AS min_pfillunlockingpre,
min(cycle_completa.reducedauxpumppre) AS min_reducedauxpumppre,
min(cycle_completa.tankoiltemp) AS min_tankoiltemp
FROM cycle_completa
WHERE slidestage = 17
GROUP BY cycle_completa.numcycle
UPDATE:
UPDATE cycle_parametros
SET seg3_min_valor_fmant_pump1pre = min_pump1pre,
seg3_min_valor_fmant_pump2pre = min_pump2pre,
seg3_min_valor_fmant_pump3pre = min_pump3pre,
seg3_min_valor_fmant_slidevel = min_slidevel,
seg3_min_valor_fmant_slidepos = min_slidepos,
seg3_min_valor_fmant_slideforce = min_slideforce,
seg3_min_valor_fmant_slideservopilotpre = min_slideservopilotpre,
seg3_min_valor_fmant_slidecentralpre = min_slidecentralpre,
seg3_min_valor_fmant_slidelateralpre = min_slidelateralpre,
seg3_min_valor_fmant_slideringpre = min_slideringpre,
seg3_min_valor_fmant_slidepistonpre = min_slidepistonpre,
seg3_min_valor_fmant_slidey42pre = min_slidey42pre,
seg3_min_valor_fmant_cushionforce = min_cushionforce,
seg3_min_valor_fmant_cushionservopilotpre = min_cushionservopilotpre,
seg3_min_valor_fmant_cushionpistonpre = min_cushionpistonpre,
seg3_min_valor_fmant_cushionringpre = min_cushionringpre,
seg3_min_valor_fmant_pfillaccumpre = min_pfillaccumpre,
seg3_min_valor_fmant_pfillcentralpre = min_pfillcentralpre,
seg3_min_valor_fmant_pfilllateralpre = min_pfilllateralpre,
seg3_min_valor_fmant_pfillunlockingpre = min_pfillunlockingpre,
seg3_min_valor_fmant_reducedauxpumppre = min_reducedauxpumppre,
seg3_min_valor_fmant_tankoiltemp = min_tankoiltemp
WHERE numcycle = actual_cycle
Thank you!
The following query should work:
WITH selected as (
SELECT cycle_completa.numcycle AS actual_cycle,
min(cycle_completa.pump1pre) AS min_pump1pre,
min(cycle_completa.pump2pre) AS min_pump2pre,
min(cycle_completa.pump3pre) AS min_pump3pre,
min(cycle_completa.slidevel) AS min_slidevel,
min(cycle_completa.slidepos) AS min_slidepos,
min(cycle_completa.slideforce) AS min_slideforce,
min(cycle_completa.slideservopilotpre) AS min_slideservopilotpre,
min(cycle_completa.slidecentralpre) AS min_slidecentralpre,
min(cycle_completa.slidelateralpre) AS min_slidelateralpre,
min(cycle_completa.slideringpre) AS min_slideringpre,
min(cycle_completa.slidepistonpre) AS min_slidepistonpre,
min(cycle_completa.slidey42pre) AS min_slidey42pre,
min(cycle_completa.cushionforce) AS min_cushionforce,
min(cycle_completa.cushionservopilotpre) AS min_cushionservopilotpre,
min(cycle_completa.cushionpistonpre) AS min_cushionpistonpre,
min(cycle_completa.cushionringpre) AS min_cushionringpre,
min(cycle_completa.pfillaccumpre) AS min_pfillaccumpre,
min(cycle_completa.pfillcentralpre) AS min_pfillcentralpre,
min(cycle_completa.pfilllateralpre) AS min_pfilllateralpre,
min(cycle_completa.pfillunlockingpre) AS min_pfillunlockingpre,
min(cycle_completa.reducedauxpumppre) AS min_reducedauxpumppre,
min(cycle_completa.tankoiltemp) AS min_tankoiltemp
FROM cycle_completa
WHERE slidestage = 17
GROUP BY cycle_completa.numcycle
)
UPDATE cycle_parametros cp
SET cp.seg3_min_valor_fmant_pump1pre = s.min_pump1pre,
cp.seg3_min_valor_fmant_pump2pre = s.min_pump2pre,
cp.seg3_min_valor_fmant_pump3pre = s.min_pump3pre,
cp.seg3_min_valor_fmant_slidevel = s.min_slidevel,
cp.seg3_min_valor_fmant_slidepos = s.min_slidepos,
cp.seg3_min_valor_fmant_slideforce = s.min_slideforce,
cp.seg3_min_valor_fmant_slideservopilotpre = s.min_slideservopilotpre,
cp.seg3_min_valor_fmant_slidecentralpre = s.min_slidecentralpre,
cp.seg3_min_valor_fmant_slidelateralpre = s.min_slidelateralpre,
cp.seg3_min_valor_fmant_slideringpre = s.min_slideringpre,
cp.seg3_min_valor_fmant_slidepistonpre = s.min_slidepistonpre,
cp.seg3_min_valor_fmant_slidey42pre = s.min_slidey42pre,
cp.seg3_min_valor_fmant_cushionforce = s.min_cushionforce,
cp.seg3_min_valor_fmant_cushionservopilotpre = s.min_cushionservopilotpre,
cp.seg3_min_valor_fmant_cushionpistonpre = s.min_cushionpistonpre,
cp.seg3_min_valor_fmant_cushionringpre = s.min_cushionringpre,
cp.seg3_min_valor_fmant_pfillaccumpre = s.min_pfillaccumpre,
cp.seg3_min_valor_fmant_pfillcentralpre = s.min_pfillcentralpre,
cp.seg3_min_valor_fmant_pfilllateralpre = s.min_pfilllateralpre,
cp.seg3_min_valor_fmant_pfillunlockingpre = s.min_pfillunlockingpre,
cp.seg3_min_valor_fmant_reducedauxpumppre = s.min_reducedauxpumppre,
cp.seg3_min_valor_fmant_tankoiltemp = s.min_tankoiltemp
FROM selected AS s
WHERE cp.numcycle = s.actual_cycle