how can I set take profit to double stop loss - mql4

I am setting up a pending order in the following way in my project:
int ticketSell = OrderSend(Symbol(),OP_SELLLIMIT,lotSize, bottom,0, top,0,"SellOrder",magicnumber,0,Red);
How can I make the take profit, double the pips for stop loss? In other words I want a 2:1 win ratio for my pending order.
Also how can I set the entry price which is bottom say, 6 pips below bottom's value?

Related

Dynamically find a value in between a range of Values in Google Sheets

In google sheets, I have a data set that identifies historical pricing multiplyers for a product in certain size ranges. I'm using square inches in this case. The data is set up in a table like this:
Data set
What I've done is create a formula that allows me to specify any width, height and quantity, find the current square inches, then find the corresponding price for this specific sized product based on where it falls on the range of sizes that I've provided in the data set.
For example, if the square inches equals 150, the price for this size is .6 (or 60 cents) per square inch. (90)
I've already solved this. This is the formula I have:
=(A10*B10)*C10)*(filter(C2:C6,A2:A6<=((A10*B10)*C10),B2:B6>((A10*B10)*C10))
In this example, A10 = height, B10 = width, and C10 = quantity
So, like I said, this works just fine. My problem is that a product with a square inch value of 105 will ultimately have the same cost per square inch as a product with a square inch value of 214. It's only after it crosses the 250 square inch threshold that my cost per square inch will change.
Is there a way to dynamically find the appropriate multiplyer, so that, for instance, a square inch value of 175, would have a multiplyer between .8 and .6 since it falls in the middle of 100 square inches and 250 square inches?
I will try to assume that you have some kind of calculator for calculating coefficients.
If value of the area in C13 then in A16:C17 we can get the calculate range. Put in to A16
=ARRAY_CONSTRAIN(OFFSET(A2:C6,MATCH(C13,A2:A6,1)-2,0),2,3)
After that we can calculate part of price
=IF(ISNUMBER(A16),C16-(C16-C17)*(C13-A17)/(B17-A17),C17)
One formula solution
=VLOOKUP(C13,B2:C6,2)-
(VLOOKUP(C13,B2:C6,2)-VLOOKUP(C13,A2:C6,3))*
(C13-VLOOKUP(C13,A2:C6,1))/
(VLOOKUP(C13,A2:C6,2)-VLOOKUP(C13,A2:C6,1))
The best solution is use a TREND. It's very clear.
=TREND(
OFFSET(B2,MATCH(C13,B2:B3)-1,1,2,1),
OFFSET(B2,MATCH(C13,B2:B3)-1,0,2,1),
C13
)
My sample
References
VLOOKUP
ARRAY_CONSTRAIN
OFFSET
TREND
I trust you'll forgive me if I don't explain the math involved, but this formula should produce the result you want:
=(A10*B10*C10)*(VLOOKUP(A10*B10*C10,A2:C6,3,TRUE)+(((A10*B10*C10)-VLOOKUP(A10*B10*C10,A2:A6,1,TRUE))*(ABS(VLOOKUP(A10*B10*C10,A2:C6,3,TRUE)-INDEX(C2:C6,MATCH(VLOOKUP(A10*B10*C10,A2:C6,3,TRUE),C2:C6,0)-1))/(VLOOKUP(A10*B10*C10,A2:B6,2,TRUE)-VLOOKUP(A10*B10*C10,A2:A6,1,TRUE)))))

PineScript is exiting strategy and opening a double sized order on the same bar

Using the pinescript editor in TradingView, I've noticed a slightly strange occurrence. When there are triggers to exit a long trade (trailing stop) and a trigger to enter a new short trade on the same bar, both open, however, the short has 2x the position size than it should.
Here's the relevant code:
// When uptrend, use upper band as a stop market buy
if (uptrend)
strategy.entry("Long", strategy.long, stop = h)
// Exit longs with a trailing stop. Assume longStopPrice is calculated per bar
if (strategy.position_size > 0)
strategy.exit("Trailing Stop", "Long", stop = longStopPrice)
// When downtrend, use lower band as a stop market sell
if (downtrend)
strategy.entry("Short", strategy.short, stop = l)
// Exit shorts with a trailing stop. Assume shortStopPrice is calculated per bar
if (strategy.position_size < 0)
strategy.exit("Trailing Stop", "Short", stop = shortStopPrice)
Here is the output on the UI:
Notice that the long position size is 15.358918 and a few bars later, this is closed with a trailing stop size -15.358918. However a new short trade is also opened on the same bar size -30.70059 (It should not be -30 it should be -15).
A little bit later on this second trade is closed by trailing stop, size +15.34 (but -30 was opened).
The strategy leaves you net short -15 units for the entire duration of the backtest. Ideally i'd like the strategy.enter to only enter a single position size & trailing stop to close the entire position, not to enter a double position & close half.
Any ideas?
Its because your exit condition does not have the same id as the entry condition. For TV, the entry id has still not closed. So, change the exit id from 'Trailing Stop' to 'Long' and 'Short' as those are the ids used during the entry.
Theres some logic behind but this happens when you dont close a position before opening a new one, what helped me was just moving the close strategy above the open, so every loop also checks to confirm to always close a position before opening a new one
Btw, also dont forget that on one way mode position your 2nd trade must have to be 2x the previous one, since lets say you go +$25 long, then -$25 for short, you would end with $0 for the next trade. Then having 2x the previous one, you would go +$25 long, then -$50 for short, you would end with -$25 short for your next trade, then +$50 long again you would end +$25 next trade
add to if downtrend this--
and strategy.position_size == 0

Value iteration not converging - Markov decision process

I am having an issue with the results I am getting from performing value iteration, with the numbers increasing to infinity so I assume I have a problem somewhere in my logic.
Initially I have a 10x10 grid, some tiles with a reward of +10, some with a reward of -100, and some with a reward of 0. There are no terminal states. The agent can perform 4 non-deterministic actions: move up, down, left, and right. It has an 80% chance of moving in the chosen direction, and a 20% chance of moving perpendicularly.
My process is to loop over the following:
For every tile, calculate the value of the best action from that tile
For example to calculate the value of going north from a given tile:
self.northVal = 0
self.northVal += (0.1 * grid[x-1][y])
self.northVal += (0.1 * grid[x+1][y])
self.northVal += (0.8 * grid[x][y+1])
For every tile, update its value to be: the initial reward + ( 0.5 * the value of the best move for that tile )
Check to see if the updated grid has the changed since the last loop, and if not, stop the loop as the numbers have converged.
I would appreciate any guidance!
What you're trying to do here is not Value Iteration: value iteration works with a state value function, where you store a value for each state. This means, in value iteration, you don't keep an estimate of each (state,action) pair.
Please refer the 2nd edition of Sutton and Barto book (Section 4.4) for explanation, but here's the algorithm for quick reference. Note the initialization step: you only need a vector storing the value for each state.

How To Find Remaining Distance To Move UIView Between 2 Points

SCENARIO
I am working on an application that keeps track of blood glucose levels into a graph. On the graph there are "markings" (ex: -200mg) going in vertical order along the y axis on the right side of the screen and "hours" (ex: -12:00 PM) will be along the x axis on the bottom of the graph. I have to plot out little 'dots' to display what the blood glucose level was throughout the way.
ISSUE
I am trying to calculate how to position the 'dots' in the correct time and mg level and I'm having difficulty calculating the positions. I can access the "markings" and retrieve it's marking.center.x to indicate which 'Time Slot' (x axis) and the marking.center.y to indicate which 'MG Level' the 'dot' needs to go into. Problem is it isn't always exactly 12:00 PM or 200mg where it will need to be placed. In fact that would be very rare.
WHAT I NEED
Based on the following variables:
dot.mgLevel
The dot will already know where it needs to go based on the information retrieved from the medical device. It will know the time and mgLevel to assign itself.
marking.mgLevel
The markings will each have evenly distributed values that such as -100mg, -200mg, -300mg ect...
timemarking.timeslot
Each time marking on the bottom will each have evenly distributed times allocated every 30 min. Such as -12:00PM, -12:30PM, -1:00PM ect...
If the dot has a mg Level of 330mg and the closest marking on the mg Level is 300mg, then I need to be able to calculate how much further up the dot needs to move from 300 going towards the 400mg marker.
SO...
If the distance between the markings are 100pt and the dot's mgLevel is 330mg, then I know that I need to move the dot from the 300mg marking toward the 400mg marking by exactly 30pt. That's because it's simple math because the distance between the markings is 100. But in real life it isn't 100, so I need to be able to calculate this.
MY ULTIMATE QUESTION
Say distance between markings is 241 and each marking represents multiples of a hundred. Say my dot has a mgLevel of 412. How do I calculate how far I need to move the dot so that it will be in the correct place?
I THINK?
I think I need to make 241 equal 100%. But I need help.
Distance between markings is 241pt
Markings are multiples of 100mg
1mg will occupy 2.41pt. So 412mg will occupy (2.41 * 412) pt. To know how much to move for the next dot, take the difference in mg and multiply by 2.41.
In general, if distance between 2 markings in points is d, markings are multiples of m, and desired accuracy is k decimal places, 1mg will occupy g:
let divisor = pow(10.0, Double(k))
let g = round((d/m)*divisor) / divisor

XCode iOS UISlider - Custom Scaling Min and Max

I want to have a different scale using a slider for the Min and Max. 0 should be in the middle, but I want to have a scale 0-50 for max and -200-0 for min.
Is that possible? How could I achieve that?
I don't think that this is possible out of the box.
But what you can do is, e.g. setup the slider in a way that for min it has 0 and for max 1000. Now you will have to check in which part of the part, user left the slider. If it's in the lower half, you will have to normalize those values to the desired range, the same for the upper range. The exact middle could be a bit tricky because you will have more points in the slider than in the ranges you are providing, but I guess with some fiddling around you can do this. :)
Hope that helped :)
I believe this is quite possible.
Here is my idea of how you can do it
Take a slider with values -200 to 200
For values > 0. Divide value by 4. (i.e. 200 / 4 = 50)
This will make your positive range from 0 to 50 but negative range from -200 to 0
Similarly while setting value to slider in positive manner you can multiply it by 4.

Resources