Pine Script - Exit When Open Price > Entry Price "aka first profitable open" - exit

Thank you once again in advance for your assistance.
Trying to get market order to execute at first profitable open
As recommended, tried several 'process_orders_on_close'. Set to true, fixes original problem of exiting one bar late (perfect!) But, it breaks the entry, first image below, in that entry is on condition as opposed to bar after condition is met. Image One below.
[![Image One][1]][1]
For image two, the intent was to toggle 'process_orders_on_close' from "na" to "true". Fixed entry but original problem exiting one bar late returns. Results and code in Image Two below.
[![Image Two][2]][2]
Thank you once again.
//CODE FOR IMAGE ONE
//#version=4
strategy(title="Pattern Tester - FPO)", process_orders_on_close = true,
overlay=true)
OS = (high > high[1] and low < low[1] and close < close[1])
if (OS)
process_orders_on_close = true, strategy.entry(id="FPO", long=true)
if strategy.position_size > 0
strategy.close("FPO", when = open > strategy.position_avg_price)
//CODE FOR IMAGE TWO
//#version=4
strategy(title="Pattern Tester - FPO)", process_orders_on_close = na,
overlay=true)
OS = (high > high[1] and low < low[1] and close < close[1])
if (OS)
strategy.entry(id="FPO", long=true)
if strategy.position_size > 0
process_orders_on_close = true, strategy.close("FPO", when = open > strategy.position_avg_price)
[1]: https://i.stack.imgur.com/4VLcQ.jpg
[2]: https://i.stack.imgur.com/LcXsZ.jpg

You can use ``` before and after your code for monospace.
You can use process_orders_on_close=true with your strategy() declaration statement.

Related

Strategy Condition (2nd bar close above/below condition)

I am literally brand new to pine script and have stumbled my way to this point but I really am stumped. Thanks in advance for any direction.
I am trying to create a simple strategy around EMA/SMA. I am looking to force the strategy to not take the long or short exit unless exitcondition(long&short)1 has 2 consecutive closes below/above the condition defined. The strategy is working as intended otherwise but I was hoping I would be able to add this variable to test different setups.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bubblyApple52984
//#version=5
strategy("EMA strategy", overlay=true, process_orders_on_close = true, margin_long=100, margin_short=100)
ema_input1 = input.int(1, "Fast")
ema_input2 = input.int(2, "Slow")
sma_input3 = input.int(3, "SuperSlow")
plot(ta.ema(close, ema_input1),"fast",color.green)
plot (ta.ema(close, ema_input2),"slow",color.white)
plot (ta.sma(close, sma_input3), "superslow",color.red)
shortCondition1 = close < ta.sma(close, sma_input3)
shortCondition2 = ta.crossunder(ta.ema(close, ema_input1), ta.ema(close, ema_input2))
shortCondition3 = ta.ema(close,ema_input1) < ta.sma(close, sma_input3)
shortCondition4 = ta.ema(close,ema_input2) < ta.sma(close, sma_input3)
exitConditionShort1 = close >= ta.ema(close, ema_input2)
//exitConditionShort2 = ta.crossover(ta.ema(close, ema_input1), ta.ema(close, ema_input2))
inTradeShort = strategy.position_size < 0
if (shortCondition1 and shortCondition2 and shortCondition3 and shortCondition4)
strategy.entry("My Short Entry Id", strategy.short)
if (exitConditionShort1 and inTradeShort)
strategy.close(id="My Short Entry Id", comment="CloseShort")
longCondition1 = close > ta.sma(close, sma_input3)
longCondition2 = ta.crossover(ta.ema(close, ema_input1), ta.ema(close, ema_input2))
longCondition3 = ta.ema(close,ema_input1) > ta.sma(close, sma_input3)
longCondition4 = ta.ema(close,ema_input2) > ta.sma(close, sma_input3)
exitConditionlong1 = close <= ta.ema(close, ema_input2)
//exitConditionlong2 = ta.crossunder(ta.ema(close, ema_input1), ta.ema(close, ema_input2))
inTradelong = strategy.position_size > 0
if (longCondition1 and longCondition2 and longCondition3 and longCondition4)
strategy.entry("My Long Entry Id", strategy.long)
if (exitConditionlong1 and inTradelong)
strategy.close(id="My Long Entry Id", comment="CloseLong")
This is what I have and it works but it completes the "strategy.close" off the 1st candle close above/below the ema_input2. I need help making this strategy close the position based on the 2nd consectutive bar close above/below the ema_input2.
Thanks
You could try with the previous value of exitConditionShort1, and the current value
Something like that
if (exitConditionShort1 and exitConditionShort1[1] and inTradeShort)
strategy.close(id="My Short Entry Id", comment="CloseShort")

How I can zoom-in for a sankey plot in R-shiny?

I have been trying to create a R shiny dashboard including a sankey network. It is working without any problem, however some of the plots seem quite clutter due to high number of nodes and connections based on some input parameters. As an alternative solution, i am trying to implement zooming feature triggered by double click by the user, but I am getting an error of "unused element" by shiny.
The part of the ui code including double click is:
sankeyNetworkOutput("diagram",dblclick = "plot1_dblclick",
brush = brushOpts(id = "plot1_brush",resetOnNew = TRUE)
The server side code is:
output$diagram<- renderSankeyNetwork({
sankeyNetwork(Links = rv1()$links, Nodes = rv1()$nodes,
Source = "IDsource", Target = "IDtarget",
Value = "value", NodeID = "name",
iterations = 0, sinksRight=TRUE,
fontSize=13, nodePadding=20)
observeEvent(input$plot1_dblclick, {
brush <- input$plot1_brush
if (!is.null(brush)) {
ranges$x <- c(brush$xmin, brush$xmax)
ranges$y <- c(brush$ymin, brush$ymax)
} else {
ranges$x <- NULL
ranges$y <- NULL
}
})
})
I would really appreciate if I can get any help regarding to that!
Thanks!

Google Script .getvalue() Not Working With Cells With a Formula In It

I have this google script for google sheets that moves rows of data from "Sheet1" to "Sheet2" when column 15 says "tracking", and it works perfectly fine when I type in "tracking" but I would like that column to be an IF equation something like IF(G:G="tracking not available at this time","","tracking"). But the code does not seem to recognize the formula change from "" to "tracking". Do I need to change the getvalue()? Or is there a different workaround to this issue? I've used =query(importrange) withing the spreadsheet to copy over data with a trigger word, but I really want this to be more of an archive system and add a row to the bottom of "Sheet2" whenever row15 on "sheet1"Thanks! Here is the code:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Sheet1" && r.getColumn() == 14 && r.getValue() == "tracking") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet2");
if(targetSheet.getLastRow() == targetSheet.getMaxRows()) {
targetSheet.insertRowsAfter(targetSheet.getLastRow(), 20);
}
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
I had an issue with this recently
I spent about 3 hours debugging something yesterday and this was the culprit.
try using r.getDisplayValue() instead of r.getValue
I am still new to this myself, and feel free to correct me if I am wrong, because if there is a different reason I would really love to know!!!
It seems that if a value in a cell is not typed in but placed there through a formula such as =query() or a similar method, I don't think it actually sees that there is a value in the cell. (I got null values or the formula itself)
If you use getDisplayValue, it "should" get the value that you actually see in the cell.
The correct way to get formulas, instead of displayed values, is with getFormulas rather than getValues

What does the "throughput-deadline-time" configuration option do?

I've stumbled on the throughput-deadline-time configuration property for Akka dispatchers, and it looks like an interesting option, however the only mention of it I could find in the whole documentation is the following:
# Throughput deadline for Dispatcher, set to 0 or negative for no deadline
throughput-deadline-time = 0ms
I think we can agree that this is not very helpful.
So what does throughput-deadline-time control, and what impact does it have when on my dispatcher?
So I had a look at the Akka source code, and found this method in the Mailbox that seems to implement the behavior of throughput-deadline-time:
/**
* Process the messages in the mailbox
*/
#tailrec private final def processMailbox(
left: Int = java.lang.Math.max(dispatcher.throughput, 1),
deadlineNs: Long = if (dispatcher.isThroughputDeadlineTimeDefined == true) System.nanoTime + dispatcher.throughputDeadlineTime.toNanos else 0L): Unit =
if (shouldProcessMessage) {
val next = dequeue()
if (next ne null) {
if (Mailbox.debug) println(actor.self + " processing message " + next)
actor invoke next
if (Thread.interrupted())
throw new InterruptedException("Interrupted while processing actor messages")
processAllSystemMessages()
if ((left > 1) && ((dispatcher.isThroughputDeadlineTimeDefined == false) || (System.nanoTime - deadlineNs) < 0))
processMailbox(left - 1, deadlineNs)
}
}
This piece of code makes it clear: throughput-deadline-time configures the maximum amount of time that will be spent processing the same mailbox, before switching to the mailbox of another actor.
In other words, if you configure a dispatcher with:
my-dispatcher {
throughput = 100
throughput-deadline-time = 1ms
}
Then the mailbox of the actors will process at most 100 messages at a time, during at most 1ms, whenever the first of those limits is hit, Akka switches to another actor/mailbox.

Changing Mavlink Message Rate ArduPilotMega

I am working on a project that uses Mavlink protocol (in c++) to communicate with the ArduPilotMega (2.6).
I am able to read messages such as ATTITUDE for example. The current message rate (for all messages) is 2Hz and I would like to increase this rate.
I found out that I should probably set MESSAGE_INTERVAL using MAV_CMD_SET_MESSAGE_INTERVAL in order to change it.
So my question is:
How do I send this command message using mavlink in c++?
I tried doing it using the code below but it did not work. I guess I have to use the command I mentioned above, but I don't know how.
mavlink_message_t command;
mavlink_message_interval_t interval;
interval.interval_us = 100000;
interval.message_id = 30;
mavlink_msg_message_interval_encode(255, 200, &command, &interval);
p_sensorsPort->write_message(command);
Update: I also tried this code below, maybe I am not giving it the right system id or component id.
mavlink_message_t command;
mavlink_command_long_t interval;
interval.param1 = MAVLINK_MSG_ID_ATTITUDE;
interval.param2 = 100000;
interval.command = MAV_CMD_SET_MESSAGE_INTERVAL;
interval.target_system = 0;
interval.target_component = 0;
mavlink_msg_command_long_encode(255, 0, &command, &interval);
p_sensorsPort->write_message(command);
Maybe I am missing something about the difference between target_system, target_component and sysid, compid. I tried few values for each but nothing worked.
Is there any ACK that will be able to tell me if it even got the command?
I guess you missed start_stop field. the below sample is working.
final msg_request_data_stream msg = new msg_request_data_stream ();
msg.req_message_rate = rate;
msg.req_stream_id = (short) streamId;
msg.target_component = (short)compID;
msg.target_system = (short)sysID;
/*
GCS_COMMON.cpp contains code that sends when value =1
and stop when value = 0
that is it.
*/
if (rate > 0) {
msg.start_stop = 1;
} else {
msg.start_stop = 0;
}
From Robotis Stack Exchange answer,
In order to change the message rate, the simplest way is to change the SR_* parameters value using Mission Planner. The maximum rate is 10Hz.
For example, in order to change the ATTITUDE message rate to be 10Hz I just had to change the SR_EXTRA1 parameter to be 10.
For more information about which parameter changes each message see GCS_Mavlink.cpp file in ArduCopter firmware.