How can I keep the ATR of a specific candlestick inside a variable without it changing with each new candlestick - thinkscript

Here is my code:
def TrueRange = ATR(14)[1];
On each new candlestick, thinkorswim generates a new ATR value and TrueRange fills with the ATR value of the previous candlestick.
I'm looking for a way to keep the ATR value of the first candlestick on the chart inside TrueRange without it changing with every new candlestick.
Can anyone help me?

You can use a recursive variable:
def TrueRange;
if BarNumber() == 1 {
TrueRange = ATR(14)[1];
} else {
TrueRange = TrueRange[1];
}
The first line declares the variable.
If you're at the first bar, set the variable to the value for that bar.
Otherwise, keep the variable at the value it was before.
Edit: you can also do this in one line:
def TrueRange = ( if BarNumber() == 1 then ATR(14)[1] else TrueRange[1] );
Edit 2: if you're using multiple offset/length values, thinkScript will override length/offset values on variables and plots to use the highest value present in the script, rather than what is specified. In that case, you would need to use CompoundValue to force the script to use the correct offset.
def TrueRange = CompoundValue(length=1, "visible data"=if BarNumber() == 1 then ATR(14)[1] else TrueRange[1], "historical data"=Double.NaN);
Reference my CompoundValue discussion at Understanding & Converting ThinkScripts CompoundValue Function

Related

Google app script IF condition not matching 0, empty and null

I have issues with Google app script IF condition.
Problem i am facing its not returning value TRUE rather going to next/ Else statements.
Code i am having:
const numberOfRowsToUpdate = deliveryDate.length;
// For each item making the for loop to work
for (i=0 ; i < numberOfRowsToUpdate;i++) {
debugger;
var dp = depositAmount[i];
if(dp!==""|| dp!==0 || dp !==null || dp!==isblank())
{ .... <statements>
}
}
I want to check whether particular cell of the array is empty / zero / returning null value.
thanks in advance for the help.
SUGGESTION
I have used a similar script I'm using for a spreadsheet in which I need to search through every row for some data, but obviously adpating it to your case, and since I don't have your full code (and still can't comment asking for more info due to my recent joining in SO), I had to simplify it, in hope it will work for you.
What I did was use your incrementing i index from the for loop and use it to scan every row, while adjusting it to fit your array index, because we can't have i = 0 as a row index, and it would skip the first value on the array if left as i = 1).
SCRIPT
function test(){
const n = 6;
var depositAmount = [7,2,0,2,0,8];
// For each item making the for loop to work
var ss = SpreadsheetApp.getActive();
Logger.log(ss.getName());
for (var i=1 ; i <= n ;i++) {
debugger;
ss.getRange("A"+i).setValue(1);
var dp = depositAmount[i-1];
Logger.log(dp)
if(dp != "" || dp != 0 /*|| dp != null || dp != isblank()*/)
{
ss.getRange("B"+i).setValue(dp);
}
else
{
ss.getRange("C"+i).setValue("VOID")
Logger.log(i-1+"th index of array is "+ss.getRange("C"+i).getValue());
}
}
};
RESULTS
After running it with the four original conditions you used, i didn't get the expected result, as you must have, leading to this:
.
While studying your original code, I stumbled upon this question about the differences between == and ===, as well as != and !==.
So before I used this in our favor, I tried the old trial and error method, using only one condition at a time, and then stacking them up. Not only I managed to find out the !== operator didn't work properly for this case, but also the comparison with null and the isblank() function (at least in my case, because i haven't defined it, and I'm not sure it is a built-in function) also don't work with either operator.
Therefore, using the != operator helps you better than the strict !==.
The result of the final script is that:
.
NOTES
I also tried using a null value within the array ([7,2,0,2,,8]), but it would always break away from the loop, never scanning the whole array, and I don't know how to circle that.
Here is the Execution Log for this script:
EDIT
While fooling around, I found this question and the answer by Etienne de Villers might be even faster to apply, or at least more useful for your purposes.

How can I set default values to QDoubleSpinBox

I am doing ballistic calculations for a projectile.
so in that I have a check box and based on check box I have to take the inputs
i.e if check box is enabled then read values from double-spinbox and do ballistics calculations
else go by the default values of the double spin box for that I have written this code but I end up the error in setValue()
so for my requirement wt method should I take.
if(ui->checkBox->isChecked())
{
//if it is checked then take the values given on UI
altitude= ui-doubleSpinBox_1>text();
b_pressure= ui-doubleSpinBox_2>text();
r_humidity= ui-doubleSpinBox_3>text();
temp= ui-doubleSpinBox_4>text();
}
else
{
///else take the default values
altitude=ui-doubleSpinBox_1>setValue(0);
b_pressure=ui-doubleSpinBox_2>setValue(29.53);
r_humidity=ui-doubleSpinBox_3>setValue(0.78);
temp=ui-doubleSpinBox_4>setValue(78);
}
QDoubleSpinBox::setValue returns a (lack of) value of type void, for which there are no conversions to anything. You are trying to assign to (double?) variables, and the compiler is telling you this is impossible.
Instead, you should conditionally set the default values, then unconditionally read the values. This keeps the (disabled?) ui up to date.
if(!ui->checkBox->isChecked())
{
// set the default values
ui->doubleSpinBox_1->setValue(0);
ui->doubleSpinBox_2->setValue(29.53);
ui->doubleSpinBox_3->setValue(0.78);
ui->doubleSpinBox_4->setValue(78);
}
altitude = ui->doubleSpinBox_1->value();
b_pressure = ui->doubleSpinBox_2->value();
r_humidity = ui->doubleSpinBox_3->value();
temp = ui->doubleSpinBox_4->value();
Alternately, you could conditionally set the variables with your defaults, and unconditionally set the UI from the variables
if(!ui->checkBox->isChecked())
{
// set the default values
altitude = 0;
b_pressure = 29.53;
r_humidity = 0.78;
temp = 78;
}
ui->doubleSpinBox_1->setValue(altitude);
ui->doubleSpinBox_2->setValue(b_pressure);
ui->doubleSpinBox_3->setValue(r_humidity);
ui->doubleSpinBox_4->setValue(temp);

Accessing a Combobox inside a dataGridView Column?

I'm working on a scheduling program, and inside the dataGridView, we have a few ComboBox Columns that are populated by 3 entries upon creation, but I wanted to be able to add more as the user creates them, but I have no idea how you would access the combobox data. Any help is appreciated!
// this is initialized in a separate part.
/* System::Windows::Forms::DataGridView^ dataGridView;*/
System::Windows::Forms::DataGridViewComboBoxColumn^ newCol =
(gcnew System::Windows::Forms::DataGridViewComboBoxColumn());
dataGridView->Columns->AddRange(gcnew cli::array< System::Windows::Forms::DataGridViewComboBoxColumn^ >(1) {newCol});
// add the choices to the boxes.
newCol->Items->AddRange("User inputted stuff", "More stuff", "Add New...");
Solution
If you have access to the data from the user entry and you know the column index for the DataGridViewComboBoxColumn, you should be able to just do the following wherever needed:
DataGridViewComboBoxColumn^ comboboxColumn = dataGridView->Columns[the_combobox_column_index];
if (comboboxColumn != nullptr)
{
comboboxColumn->Items->Add("the new user entry");
}
Comments Response
how could you change the selected index of that combobox (the one that
the edit was triggered on)? [...] we want it so that when the new item
is added the selected index is set to that new item).
Couple of ways come to mind.
Add a single line within the if-statement of the above code. This will set the default displayed value for each DataGridViewComboBoxCell in the DataGridViewComboBoxColumn.
if (comboboxColumn != nullptr)
{
comboboxColumn->Items->Add("the new user entry");
comboboxColumn->DefaultCellStyle->NullValue = "the new user entry";
}
Pros: Clean, efficient. Previous user-selected values are left intact. The cell's FormattedValue will display the new user value by default if no other selection has been made.
Cons: Doesn't actually set a cell's selected value, so Value will return null on cells not explicitly user-selected.
Actually set the value of certain cells (based on your criteria) to the user-added value.
if (comboboxColumn != nullptr)
{
comboboxColumn->Items->Add("the new user entry");
for (int i = 0; i < dataGridView->Rows->Count; i++)
{
DataGridViewComboBoxCell^ cell = dataGridView->Rows[i]->Cells[the_combobox_column_index];
if ( cell != nullptr /* and your conditions are met */ )
{
cell->Value = "the new user entry";
}
}
}
Pros: The Value of targeted cells is actually set to the new user value.
Cons: Logic deciding which cells should be affected is more complicated.

C++ CListCtrl - GetItemData() returning wrong value?

I have a C++ application with an SQL backend, and have been storing the row id of any retrieved columns (an integer, primary key bigint in the database) with SetItemData() on list control rows as required. This is then retrieved with GetItemData() if that ID needs to be queried.
I am now getting a weird problem in that, in this one scenario, GetItemData() is returning a random 7-digit number instead of the stored ID. When I add the row I use the following code:
CListCtrl& lc = GetListCtrl();
for (int i = 0; i < vInsertItems.size(); i++) {
int j = lc.InsertItem(i,i.strName);
DWORD dwdRowID = (DWORD)cammms,nRowID;
lc.SetItemData(j,dwdRowID);
}
To retrieve and check the value I can do the following (where I have determined that nCurrentlySelectedIndex is correct):
CListCtrl& lc = GetListCtrl();
int msgID = lc.GetItemData(nCurrentlySelectedIndex);
CString debugInt; debugInt.Format(_T("debugInt = %d"),msgID);
AfxMessageBox(debugInt);
What is bizarre, is that if I run the second batch of code directly after the first, it is all fine. But if I run it in a separate function, msgID becomes set to a set of random 7 digits, different every time.
Does anyone have any idea what could be causing this?

Comparing Substrings to JList Strings

In advance, please forgive me if I do not give adequate background information for my question. Long time reader, first time asker.
I am making a program where one has a database of cars accessed through a tab delimited .txt file (we did something like this recently in my programming class, so I wanted to expand upon it).
Instead of using the terminal window, my format is displaying the Car objects (containing make, model, year, price, etc.) in ArrayList. I'm using JFrame, a JList, and a ListModel since I'm using an array of Car objects.
In my program, I wanted to create a delete method where the user could delete items from the database. Initially they would select the item from the JList and then would click on the delete button. This invokes the delete() method, which is the tab shown below...
void delete()
{
int i = list.getSelectedIndex();
String string = (String)listModel.getElementAt(i);
for(Car c : cars)
{
String year = String.valueOf(c.getYear());
String conditionReport = String.valueOf(c.getConditionReport());
String price = String.valueOf(c.getPrice());
if(c.getMake().indexOf(string) != -1 && c.getModel().indexOf(string) != -1 && year.indexOf(string) != -1 && conditionReport.indexOf(string) != -1 && price.indexOf(string) != -1 && c.getWarranty().indexOf(string) != -1 && c.getComments().indexOf(string) != -1)
{
int choice = JOptionPane.showConfirmDialog(null, "Are you sure you would like to remove the " + cars.get(i).getYear() + " " + cars.get(i).getMake() + " " + cars.get(i).getModel() + "?", "Choose One", JOptionPane.YES_NO_OPTION);
if(choice == JOptionPane.NO_OPTION || choice == JOptionPane.CLOSED_OPTION)
{
return;
} else
{
cars.remove(c);
listModel.removeElementAt(i);
}
}
}
writeFile();
}
I have pinpointed my issue to be inside the if statement. (I printed things before and after to try to find where the program is lying. 'list' is my JList and 'listmodel' is my default list model. Car is an object I created that contains the elements (as seen by the get methods). The elements shown in the listModel are merely Strings that show getMake(), getModel(), and so forth... (Each 'get' item is separated by about 10 spaces.)
What am I doing wrong in the if statement? I figured that the getMake() and getModel() (and so forth) would be substrings of the index selected.
Thank you so much for your assistance! Any input regarding ways I could make further questions more specific and clear would be greatly appreciated!
It seems like you are doing this to find the selected Car in some kind of data structure. You would be better off doing something like programming a custom list model that had access to cars itself. Then you could retrieve the selection more immediately. If cars is an ArrayList that list merely parallels I also don't see why you can't do something to the effect of cars.remove(list.getSelectedIndex());. Or since JList can display any object, override Car's toString to display what the list currently displays and have the list display Cars. Then you can cars.remove((Car)list.getSelectedValue());.
But aside from that, based on your description it sounds like you mean to do the evaluation the other way. It's the list item that should contain all of the Car attributes, rather than all of the Car attributes containing the list item. So something like
if( string.contains(c.getMake()) && string.contains(year) // and so on
(Or with indexOf but since contains merely returns indexOf > -1, using contains makes your code somewhat shorter.)