Pivot points add exit strategy - pivot

I found this strategy on Tradingview based on pivot points. I tried to add an exit strategy on R3 / S3 but it doesn't work. Can someone help me?
strategy.risk.allow_entry_in(tradeDirection)
longCondition = crossover(Source, R1) and inDateRange
shortCondition = crossunder(Source, S1) and inDateRange
strategy.entry("Buy", strategy.long, when=longCondition, oca_name="oca",
oca_type=strategy.oca.cancel)
strategy.exit("Exit Long", stop=R3)
strategy.entry("Sell", strategy.short, when=shortCondition, oca_name="oca",
oca_type=strategy.oca.cancel)
strategy.exit("Exit Short", stop=S3)

use limit for stop
if (buy)
strategy.entry("My Long Entry Id", strategy.long)
strategy.exit("Exit Long", from_entry="My Long Entry Id",limit=R1)
if (sell)
strategy.entry("My Short Entry Id", strategy.short)
strategy.exit("Exit short", from_entry="My Short Entry Id",limit=S1)

Related

Dropdown list with Adodb query

I would like to create the dropdown list with the results from my query. I'm looking for help please because I don't know how to display this results on the list.
The exemple of the list:
My query is :
'DROPDOWN LIST
Private Sub cb_gest_Change()
If Not FSD.cb_gest.MatchFound And FSD.cb_gest <> "" Then
MsgBox "Saisie impossible, le gestionnaire n'existe pas !", , "ContrĂ´le
Gestionnaire"
FSD.cb_gest = ""
Else
FSD.Cells(29, COL_DATA) = FSD.cb_gest
End If
End Sub
'DROPDOWN LIST
Sub init_combo()
Dim Resultat As ADODB.Recordset
Dim Requete As String
FSD.cb_gest.Clear
Requete = "select lb_gestion from DB_GESTIONNAIRE "
Requete = Requete + "WHERE (d_deb_valid <= TRUNC(SYSDATE) OR d_deb_valid IS
NULL) AND (d_fin_valid >= TRUNC(SYSDATE) OR d_fin_valid IS NULL)"
Requete = Requete + " ORDER BY LB_GESTION"
Set Resultat = New ADODB.Recordset
Resultat.ActiveConnection = oBase
Resultat.Source = Requete
Resultat.Open
While Not Resultat.EOF
FSD.cb_gest.AddItem Resultat!lb_gestion
Resultat.MoveNext
Wend
If FSD.Cells(29, COL_DATA).Value <> "" Then
FSD.cb_gest = FSD.Cells(29, COL_DATA).Value
Else
FSD.Cells(29, COL_DATA).Value = ""
End If
End Sub
Thank you for your help !
Consider a different, codeless approach:
Add a new sheet to the host document / workbook
Import the external data from the "Data" Ribbon tab (select "From SQL Server")
Excel creates a ListObject table backed with a QueryTable object that uses a WorkbookConnection that can be configured to automatically refresh on open, or left alone as a one-time pull.
Select the produit column in the ListObject/table; Excel highlights the entire column content and leaves the heading un-selected.
From the "Formulas" Ribbon tab, click the "Define Name" command in the "Defined Names" group.
Name the range ProductsList, verify it refers to TableName[produit] so that it automatically grows and shrinks to fit the column contents.
Change the data validation list to =ProductsList.
Hide the worksheet housing the query and table, if needed.
No code needed, and the validation list will always keep up with the query results as they are refreshed.
Side note, the query appears to be making inefficient cross-joins, and at least one of them is a where-join that can be expressed as an inner join. Are you sure the query is yielding the expected records (I'm suspecting it's yielding a ton of duplicates, depending on how many records exist in the cross-joined tables)?
SELECT prod.cd_produit AS produit
FROM db_dossier sousc, db_produit prod, db_protocole proto, db_tiers tiers, db_personne pers
WHERE sousc.cd_dossier = 'SOUSC' AND sousc.lp_etat_doss NOT IN ('ANNUL','A30','IMPAY') AND sousc.is_produit = prod.is_produit
Instinct would be to remove the tables we're not selecting or filtering anything from - if this query produces the same expected output, then assuming primary and foreign keys are defined I believe its execution plan would be more efficient:
SELECT prod.cd_produit AS produit
FROM db_dossier AS sousc
INNER JOIN db_produit AS prod ON sousc.is_produit = prod.is_produit
WHERE sousc.cd_dossier = 'SOUSC' AND sousc.lp_etat_doss NOT IN ('ANNUL','A30','IMPAY')

SQLite cannot update multiple tables at once

UPDATE X, Y
SET X.3="Updated String", Y.3="Updated String"
WHERE X.1 = Y.1
AND X.2 = Y.2;
The query above is working fine with MySQL. I'm having problems migrating it to SQLite as SQLite doesn't support multiple tables update at once (Subquerying might work, but I have no idea how to do it). I am trying to run this query using python's sqlite3 module.
Please consider X and Y as tables with columns 1,2,3.
Can anyone help me in this?
Thanks in advance
You can do it, but it requires trickiness. Make a view that joins the tables of interest (I assume that's a sensible thing to do) and set an INSTEAD OF UPDATE trigger on the view. Trigger programs can modify multiple different tables.
CREATE VIEW xy AS
SELECT X.3 AS x3, Y.3 AS y3, X.1 AS x1, X.2 AS x2
FROM X JOIN Y ON X.1 = Y.1 AND X.2 = Y.2;
CREATE TRIGGER xy_update
INSTEAD OF UPDATE ON xy
BEGIN
UPDATE X SET X.3 = NEW.x3 WHERE X.1 = NEW.x1 AND X.2 = NEW.x2;
UPDATE Y SET Y.3 = NEW.x3 WHERE Y.1 = NEW.x1 AND Y.2 = NEW.x2;
END;
Then you can do this:
UPDATE xy SET x3 = 'Updated String', y3 = 'Updated String';
-- No select required, but you can add one if you want
If you only need this for a short while, consider making xy and its trigger be TEMPORARY.
Multi table updates are not supported by SQLite.
It can be done only with 2 separate update statements:
UPDATE X
SET X.3 = 'Updated String'
WHERE EXISTS (SELECT 1 FROM Y WHERE X.1 = Y.1 AND X.2 = Y.2);
UPDATE Y
SET Y.3 = 'Updated String'
WHERE EXISTS (SELECT 1 FROM X WHERE X.1 = Y.1 AND X.2 = Y.2);

Power Query - Get Data from AnalysisServices (Cube) - Filter with User-Function result

I have the following problem. I'm getting Data (Excel 365 - Power Query) from a Cube with "Get Data from Analysis Services". -> Selling Qty an Values for filtered years by week.
Every thing is fine if I use Filter, updatetime ca. 3-4 seconds:
#"Filtered Rows1" = Table.SelectRows(#"Added Items", each
(Cube.AttributeMemberId([#"Date.Year (4-4-5)"]) = "[Date].[Year 4-4-5].&["&
Number.ToText(2019) &"]" meta
[DisplayName = Number.ToText(2019)]
or Cube.AttributeMemberId([#"Date.Year (4-4-5)"]) = "[Date].[Year 4-4-5].&["&
Number.ToText(2020) &"]" meta
[DisplayName = Number.ToText(2020)]
)
Now I like to do that dynamic, so that I can get the years from a cell in excel. I use the following M-Function "fktGetNamedCellValue" for this:
let
Source = (FieldInput as text) =>
let Quelle = Excel.CurrentWorkbook(){[Name=FieldInput]}[Content],
Inhalt = Number.From(Quelle{0}[Column1])
in Inhalt
in Source
I replaced the years in the Filter-Step with the function.
The cells are named "cell_Prev_Year" and "cell_Plan_Year"
The cells in Excel formated as Numbers (and there are only Numbers in it)
The updatetime now -> endless!!!
#"Filtered Rows1" = Table.SelectRows(#"Added Items", each
(Cube.AttributeMemberId([#"Date.Year (4-4-5)"]) = "[Date].[Year 4-4-5].&["&
Number.ToText(fktGetNamedCellValue("cell_Prev_Year") &"]" meta
[DisplayName = Number.ToText(fktGetNamedCellValue("cell_Prev_Year"))]
or Cube.AttributeMemberId([#"Date.Year (4-4-5)"]) = "[Date].[Year 4-4-5].&["&
Number.ToText(fktGetNamedCellValue("cell_Plan_Year")) &"]" meta
[DisplayName = Number.ToText(fktGetNamedCellValue("cell_Plan_Year"))]
)
If I use a "normal" parameter with the value "2019" or "2020" everything is fine.
Only if I use the fktGetNamedCellValue it will not run correctly.
I`ed Trim an Clean the result. Formated it as Text and Number... nothing helped.
I have to use userfriendly Parameter (not set in Power Query) for this, so I hope for some help :)
Best Regards
Chris
(PS: I hope u understand my english)
I solved this problem as follows.
Since it is not an good idea to use an user-function as parameter to get data from an cube. I think this method disables query-folding or is called a lot of times in the process, I decided to use an power-query parameter.
I change this Parameter with vba by checking the worksheet_change event and calling this sub:
Sub refresh_Parameter(ParameterName As String, ParameterValue As Variant)
Dim strOldFormula As String
Dim strParametersMeta As String
strOldFormula = ThisWorkbook.Queries(ParameterName).Formula
strParametersMeta = Mid(strOldFormula, InStr(1, strOldFormula, "meta"), Len(strOldFormula))
ThisWorkbook.Queries(ParameterName).Formula = ParameterValue & " " & strParametersMeta
Debug.Print strOldFormula
Debug.Print strParametersMeta
End Sub
The parameters for the sub arte the PARAMETER Name in PowerQuery and the VALUE which should be set. For this the sub extracts the meta-data from the query-formula and combines it with the new Value.
Maybe someone needs this :)
Best regards chris

Power query recursive function: add more steps after the recursive step

Background:
I have posted a question regarding a custom function in Power Query that I found in a blog by Chris Webb which I have already got an answer to.But now I have another question related to the same custom function.
One of the amazing steps in that custom function is the recursive step at the end named "OutputTable" which calls itself using a if statement, basically making it a loop. Below is the step:
OutputTable = if NextColumnNumber>(Table.ColumnCount(ExpandedTable)-1) then ExpandedTable else ExpandAll(ExpandedTable, NextColumnNumber)
Question:
Now what I would like to do after this step is to be able to add more transformation on the OutputTable.
For Example, I would like to add a column with just "A" in all the rows. The syntax to do that would be AddNewColumn = Table.AddColumn(OutputTable, "Test", each "A"). But when I do this this gives me an error saying that the column "Test" already exists. But i'm sure that there is no other column with name "Test". Even if I try changing the name of the column to anything else, I get the same error.
Note: Although the actual step I want to add is not AddColumn, I think I can figure out that part If I get a solution for this.
SourceCode:
let
Source = (TableToExpand as table, optional ColumnNumber as number) =>
let
ActualColumnNumber = if (ColumnNumber=null) then 0 else ColumnNumber,
ColumnName = Table.ColumnNames(TableToExpand){ActualColumnNumber},
ColumnContents = Table.Column(TableToExpand, ColumnName),
ColumnsToExpand = List.Select(List.Distinct(List.Combine(List.Transform(ColumnContents, each if _ is table then Table.ColumnNames(_) else {}))), each (_ = "view" or _ = "viewfolder" or _ = "Attribute:name")),
NewColumnNames = List.Transform(ColumnsToExpand, each ColumnName & "." & _),
CanExpandCurrentColumn = List.Count(ColumnsToExpand)>0,
ExpandedTable = if CanExpandCurrentColumn then Table.ExpandTableColumn(TableToExpand, ColumnName, ColumnsToExpand, NewColumnNames) else TableToExpand,
NextColumnNumber = if CanExpandCurrentColumn then ActualColumnNumber else ActualColumnNumber+1,
OutputTable = if NextColumnNumber>(Table.ColumnCount(ExpandedTable)-1) then ExpandedTable else ExpandAll(ExpandedTable, NextColumnNumber)
in
OutputTable
in
Source
I'm guessing it's throwing the error due to the recursive nature of the function calling itself and trying to apply the new column twice, once in the innermost loop and once in the outermost loop.
Let's say we have a table with two columns Col1 and Col2 that need to be expanded. If you add the new column after the OutputTable step, you'll get:
Start: Col0, Col1, Col2
OutputTable(1): Col0, Col1.a, Col1.b, Col2
OutputTable(2): Col0, Col1.a, Col1.b, Col2.x, Col2.y, Col2.z, Test
AddNewColumn: Col0, Col1.a, Col1.b, Col2.x, Col2.y, Col2.z, Test, Test
Here are a couple of approaches to try:
1. Only try to add the column when recursion is finished.
I think you can do this by changing your OutputTable line as follows:
OutputTable = if NextColumnNumber>(Table.ColumnCount(ExpandedTable)-1)
then Table.AddColumn(ExpandedTable, "Test", each "A")
else ExpandAll(ExpandedTable, NextColumnNumber)
2. Check if the column exists before trying to add it.
AddNewColumn = if Table.HasColumns(OutputTable, "Test")
then OutputTable
else Table.AddColumn(OutputTable, "Test", each "A")

Filtering a Pivot Table Automatically in Excel

I'm building a DB for school activities in the afternoon. I'm trying to create a search option through a form that controls a pivot table in which the user can filter on the class type and/or the age group and or the school year.
I wrote this code in VBA and it's not working. When i tried to write a code to filter on only one of the above (such as class type) it worked but when I expanded it to 3 filter options it isn't working. It fails when no value is inserted in one of the options.
search_class.Hide
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").ClearAllFilters
If IsNull(Range("type_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").CurrentPage = "(All)"
Else: ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").CurrentPage = Range("type_search").Value
End If
type_box = "pick a class type"
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").ClearAllFilters
If IsNull(Range("target_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").CurrentPage = "(All)"
Else: ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").CurrentPage = Range("target_search").Value
End If
target_box = "pick a group age"
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").ClearAllFilters
If IsNull(Range("year_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").CurrentPage = "(All)"
Else:
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").CurrentPage = Range("year_search").Value
End If
year_search_box = "pick a school year"
ActiveSheet.PivotTables("PivotSearchClass").PivotCache.Refresh
Does anyone know ehat the problem is and how to fix it?
I think it will work if you change your tests to either:
If IsEmpty(Range("type_search").Value)
or
If Range("type_search").Value = ""
IsNull is used to test whether a variant variable contains a null value, which won't be true with either a blank or filled cell.
I don't have Excel on this computer and pivot tables are my weakness but I'll try to help since you seem alone on this. First make sure your script is indeed running the "True" case of your if statement and then I would edit the value of CurrentPage to see if your issue is there (i.e., change the "(all)" value to something else). Where does the error lie and what is the error description?

Resources