Pine Script Back testing - scripting

I am writing a simple pine script to go Long before 10 mins hourly candle closure and close short after 10 mins after candle closure.
But the script does not apply to previous date / time but only applied from the time I add it to the chart.
I would like to back testing of this simple strategy. Can some one help please?
I am very new to scripting. Appreciate any help.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © jayaguru
//#version=4
strategy("My Strategy", overlay=true)
longCondition = minute(timenow)
if (longCondition == 55)
strategy.entry("Long", strategy.long, comment="Long")
if (longCondition == 14 or strategy.openprofit < -15 or strategy.openprofit > 40)
strategy.close_all()

You're using the timenow variable, which is the current date/time (as in 'now').
Instead, you need to use time which returns the time of when the bar opens.
Like this:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © jayaguru
//#version=4
strategy("My Strategy", overlay=true)
longCondition = minute(time)
if (longCondition == 55)
strategy.entry("Long", strategy.long, comment="Long")
if (longCondition == 14 or strategy.openprofit < -15 or strategy.openprofit > 40)
strategy.close_all()
See Access bar times programmatically in TradingView Pine for more information.

Related

HERE How to get more than 100 places with discover

I'm developing a system to show the user all the activities in an area, i'm using Developer HERE api's using the discover api request. Discover limits at 100 activities per request and this is fine but i would like to know if it is possible to ask for the rest of the places in a second api call to get them all.
Like there are 130 resturants near my user, i first ask for the first 100 and then for the other 30 so in this way the user gets the whole picture.
The discover API does not currently support pagination to serve this requirement.
This is explained here:
https://developer.here.com/documentation/geocoding-search-api/migration_guide/migration-places/topics-api/search.html
However, you can implement pagination on client side from the discover result. There is a sample that I have created for purely reference that might help you.
Code Snippet:
function getResultCount(arr) {
let arrLength = arr.length;
let noOfPagination = arrLength / 10;
let reminder = arrLength % 10;
if (reminder > 0) {
noOfPagination = noOfPagination + 1;
}
createPagination(noOfPagination);
}
Complete Sample Example: https://jsfiddle.net/raj0665/f5w12u9s/4/

Stella Architect - System Dynamics - resetting smooth and delay functions

I would love your technical advice: In the simplified example attached (https://cloudstor.aarnet.edu.au/plus/s/qhfDnUAdnYw6wxs - EDU link), I wish to reset the values in the smooth and delay functions to their initial values every 52 weeks (430 and 50 respectively). Indeed, in my more complex model, I wish to reset all the model’s values and parameters on a regular basis.
Would you mind helping me with that? Thanks a lot!
I used COUNTER to track when to reset to the initial value. See below:
Converter 3: SMTH3(Stock_3, 10, 430)
smthstock3: IF Converter_2 > 0 THEN SMTH3(Stock_3, 10, 430) ELSE INIT(Converter_3)
Converter 2: COUNTER(0, 52)

How to carry out multiple SI acquisition automatically?

I would like to write a Gatan DigitalMicrograph script to acquire multiple EELS spectrum images continuously. If there is a command to obtain Spectrum Image (SI) data, and store the three-dimensional image to an array, I will achieve my plan. However, I could not find appropriate commands for SI imaging mode from reference manual. What command should I use in this case? Do you have a helpful knowledge for my purpose? It will be appreciated if you share some wisdom.
A short demo-script of how to use the SIAcquisition commands for multiple iterations of SI acquisition and "renaming" the acquired data sets.
// Assumptions:
// - GMS 2.3 used
// - Valid survey image and ROI already assigned
// - SI array size already defined
// - Signals and other SI settings made
number SIx, SIy
SIGetFieldValue( "2D Array, X samples", SIx )
SIGetFieldValue( "2D Array, Y samples", SIy )
Result("\n SI size:"+ SIx + "x" + SIy )
// Start SI
number nSI = 3
for (number i=0; i<nSI; i++ )
{
SIInvokeButton( "Start/Stop", 1 )
while(SIIsAcquisitionActive()) yield()
sleep(0.5) // Small delay needed to allow DM finish SI acquisition clean-up
// Find (and rename) SI DataSets
number nImgDoc = CountImageDocuments()
string findStr = "Spectrum Image"
for (number d=0; d<nImgDoc; d++ )
{
string docName = GetImageDocument(d).ImageDocumentGetName()
if ( find(docName,findStr) == (len(docName)-len(findStr)) )
{
GetImageDocument(d).ImageDocumentSetName( docName + "#"+(d+1) )
}
}
}
OKDialog( "Done" )
If you have the spectrum-imaging plugin installed, then the F1 help file will have a section on STEM SI scripting commands here...
However, the commands described there will allow the acquisition of one SI after the other. Each will start anew with the same overhead you get when repeatedly starting SI acquisition via the UI.
I've got the impression you want to get a "faster" repeated SI.
Unfortunately, no commands exist which could easily give you that.
However, you might be able to create a "work-arround" solution by the following idea (untested):
Set up a STEM SI with multiple-frames (Each frame pass will be summed into the same container)
Use the "SI HookUP Scripts" on a per-pixel basis (Pixel end) to catch the "last" acquired SI point (before the new frame starts). Use this to copy the existing data into a new container and set the orignal back to zero.
Alternative:
You might also be able to use the "Correction start" HookUp script point, if you set spatial drift-correction to perform at end of frame...
The above will only work with software-synchronized SI's. For hardware-synchronized, it becomes more tricky, but you might be able to do something similar with a "ImageUpdate" event-listener.

Value of Numeric control in script, LabView

I am trying to control a KEITHLEY 2612A SourceMeter using labview. I have installed the appropriate drivers and I managed to connect to the instrument using VISA. Currently I am just experimenting with the scripting language, which the instrument uses.
Is it possible to use a Numeric Controller - a Knob for example - and use its value in the script to be loaded to the instrument? I don't have enough reputation points to add images.
EDIT
ON = 1
OFF = 0
function hello()
display.clear()
display.setcursor (1,7)
display.settext ("DONE :)")
end
smub.reset()
smub.source.output = ON
--Set the measurement integration time
smub.measure.nplc = 1
smub.measure.delay = 0.05
--Configure the reading buffers
smub.nvbuffer1.clear()
smub.nvbuffer1.appendmode = 1
smub.nvbuffer1.collecttimestamps = 1
smub.nvbuffer1.collectsourcevalues = 0
smub.nvbuffer1.fillmode = smub.FILL_ONCE
for i = 0, 0.5, 0.01 do
smub.source.levelv = i
reading = smub.measure.i (smub.nvbuffer1)
end
delay(5)
hello()
smub.source.output = OFF
delay(1)
display.clear()
display.setcursor(1,1)
display.settext(string.format("%g", smub.nvbuffer1[1]))
delay(5)
display.clear()
display.settext(string.format("%g", smub.nvbuffer1[50]))
Block diagram: http://i.imgur.com/pgu0ous.png
Front panel: http://i.imgur.com/DuHUdpo.png
LabVIEW has standard string manipulation primitives, and you can accomplish your goal by using string substitution: place a sentinel string in your script and replace it with the value from the knob.
Example
Here, I've used __BUFFER_NUMBER__ as a unique string in the Script Format input terminal. LabVIEW searches for that string and replaces it with the knob's current value.
Block diagram
You add the knob control, wire the value to the number-to-string VI (http://zone.ni.com/reference/en-XX/help/371361M-01/glang/number_to_fract_string/
), and wire that to the "Search and replace string VI" (http://zone.ni.com/reference/en-XX/help/371361M-01/glang/search_and_replace_string/) you search for the value in the script that you want to replace.

Calculate end date based on # of days and start date

I am working on script where users can make certain type of orders. Now when users make an order they can choose how long they wont it to last in # of days. Once the order is placed I need to approve their order and that approval date is recorded inside my database. Now what I need is to show inside their user panel how much days their package will last since the day of my approval. So for example if I approved their order September 08, 2013 and they choosed for the order to last 7 days, I wont them to see inside they panel for every next day they login how much days they have left, so 7days, 6days, 5days, etc... all the way to "0 Days" when they come to their panel on September 16, 2013.
I have following variables for those two values:
$row_ordersResults['date'] - the date I approved the order
$row_ordersResults['drip_feed'] - # of days they wont for their order to last
I did tried to lots of combinations by myself but I am totally stuck with this and cant make it work.
Thanks for help!
The libraries at momentjs.com is pretty cool. But if you just wanted something simple to calculate the "date difference" between two time values, there is a relatively simple way to do it. I assume you wanted it done in Javascript.
All you need to do is to clear out the hour/minute/second/millisecond fields of the two dates, calculate their differences in days. Put the script below in any web browser and you'll see how it works.
<script>
function foo() {
var d1 = new Date(2013, 8, 12, 13, 40, 1, 333); // any date value, last 4 params can be anything
var d2 = new Date(2013, 9, 3, 11, 42, 32, 533);
d1.setHours(0); d1.setMinutes(0); d1.setSeconds(0); d1.setMilliseconds(0);
d2.setHours(0); d2.setMinutes(0); d2.setSeconds(0); d2.setMilliseconds(0);
daysLeft = (d2.getTime() - d1.getTime())/(24*60*60*1000);
alert('Dear customer, there is(are) ' + daysLeft + ' day(s) left on your order!' );
}
</script>
Show Remaining Days on Order
EDIT: adding PHP version
<?php
$d1 = New DateTime('2013-08-28 06:25:00');
$d2 = new DateTime(); // now
$drip = 55;
$interval = $d2->diff($d1); // time difference
$days_left = $drip - $interval->format('%a'); // in days, subtract from drip
echo "There are $days_left days left\n";
?>
I hope I don't get marked down for not suggesting a specific answer, but time and date calculations are very tedious and JavaScript's Date() provides limited options. So rather than offer some ugly code, I suggest you take a look at moment.js at momentjs.com. Once you attach the script to your pages, you can easily manage all kind of date formats, and set up a function that will allow you to do math on dates and automatically generate your date ranges - it will even let you format them in to user friendly formats like "in 3 days", which I think is what you want. If your app has anything to do with time, and most do, I can't recommend Moment highly enough.