Opening Stop Order at 1 Bar High & Position Size Calculation

Forums ProRealTime English forum ProOrder support Opening Stop Order at 1 Bar High & Position Size Calculation

Viewing 15 posts - 1 through 15 (of 19 total)
  • #108132

    I have just started to learn Pro Real code and unfamiliar with lots of stuff. All I am trying to do at this stage is automate order placement. I prefer stop orders rather than market order. I have managed to put together the code but I have few things that I am not sure about.

    • i want a buy stop order at 1 Bar high – the code i have probably does the job but my question is what if the previous bar closed lower than current price. Brokers need the stop orders to be certain distance from the market price (for example AUDNZD need quite a large distance for stop orders). What do i do to get around this?

     

    • The below code has money management section, I have the STOP LOSS level set at low of the recent few bars. I want the position size to be calculated based on this STOP LOSS Size. If the low of the previous bar is 10 pips, the i want the position size to be based on this risk. And if it is 15 pips, it will give smaller position size. I have used StopLoss = Close – LL . Here I am assuming that we are calculating pips by getting the difference in close price and the stop level LL. But i think there is some problem with this as the resulting position size i am getting does not look accurate. To be clearer – if my max risk amount is $50, I want to buy as much quantity as I can while i place the stop at LL as long as the risk is still $50. Because the stop level for each trade will be different – some large and some small allowing different quantities to be ordered, how can i make the code work.

     

    • Would it be possible to trail the stop level to the low of each stochastic swing? Trailing done after the recent high is taken out following stochastic bullish reversal. After trailing it for 2 stochastic swings, then trail the stop to 1 Bar Low position. Please note that the code below is for Long entry only.

     

     

     

    #108136

    s00071609 – please follow the forum rules and always use the ‘Insert PRT Code’ button when posting code to make it easier for others to read. I have tidied up your post for you. 🙂

    #108144

    To abide by the distance requirements you can calculate your entry price as:

    you’ll replace DISTANCE with a variable of your choice or a constant in pips, such as 10*pipsize.

    #108145

    As for entry price, also SL requires to be no less than X, I think 5 pips is to small as SL.

    It’s quite difficult to trail such small quantities with code, since having a 10-pip SL and start trailing at a 5-pip profit would keep a 5-pip SL which is too close to price!

    I suggest using native SET STOP PTRAILING 10 (or whatever else you need, provided it’s not too small). This will grant you a 1 pip trailing.

    1 user thanked author for this post.
    #108163

    Hi Robert and Vonasi

    thanks for suggestions regarding the entry and the pip. I am fine with entry as suggested.

    I was just giving an example about pip and i don’t use fixed SL size, but use the low of recent candles in hourly TF.

    Is the above code for STOP LOSS LL correct? When i graph it into the chart, it shows graph line at correct value just below the lowest candle, however I am doubtful that it is working correctly as its giving too high win rate – i think the stop is being ignored. I want the stop to be under the low of 5-6 candles.

    Also the position size seems to be working correctly in some stocks but not in Forex, so i am not sure where i have got it wrong?

    Any help would be great.

     

    Thanks

     

     

    #108165

    No, pipsize has no value by itself, it’s just a converter.

    Number * pipsize returns a (difference in) price.

    Price / pipsize returns (a difference in) pips

    You cannot set a SL to a price, but to a difference in price instead.

    you can then use either:

     

    #108168

    Great thanks for your help. It appears to be working correctly.

    I am stuck with the position size calculation which seem to change with different instrument.

    With the above capital, loss should be around $50 but its giving loss of around $6 in forex. Is my code StopLoss (used in PositionSize calculation) correct? The idea was to get the value for maxrisk/StopLoss in the position calculation code above. Could i have just used Maxrisk/SL instead and remove the StopLoss?

    I was trying to graph the SL level in the price chart to see what its doing, is there a way to plot the SL level in chart. Above graph on price does not plot anything.

     

    Thanks

     

    #108169

    I can’t test your calculations until tomorrow.

    As for GRAPHONPRICE, it plots your SL where it is.

    If your SL 20, then it will be plotted at level 20, if it is 0.0020 it will be plotted at 0.0020, either use GRAPH or shrink the chart scale.

    #108170

    No problem, i will wait until tomorrow. A quick test would be great as this is my first code, I might be overlooking some big mistakes.

    Graph does plot it as you have said and shows 0.0040 etc which seem to match with distance from low to close of entry bar – this looks fine

    #108277

    You should replace *PIPSIZE with /PIPSIZE at line 14, then rounding it:

    Adding these lines could also help you a lot:

     

    #108285

    Thanks for that. I changed the code for positionsize now the loss is way over the required amount. I tested in GBP CAD Mini. A single losing trade has CAD 284 loss. I suspect the linked code to Positionsize calculation is not right and I am unable to figure out where the bug is.

    I believe that no trade should exceed a loss of $50 as per the 0.1% risk. I excluded the Running Profit, even then size is incorrect.

    Buy the way i got the code snippet for Position size from https://www.prorealcode.com/blog/learning/money-management-prorealtime-code/

    Exact code was used above ie. *Pipsize. I changed it as suggested but its still not giving right quantity.

    #108496

    I am totally Lost with STOP LOSS Calculation,

    I did a forward test on this code and it put the stop for Short Order in right place. But when i check in forums, it says with STOP LOSS code, it should be like Close – Someprice and not like above.

    Is the syntax above correct? The below screenshot is GOLD. Is it working only because point and price difference are same for this instrument?

    Getting so confused.

    #108523

    Use extensively GRAPH to monitor all variables you need to, in your case you should add:

    this might help you a lot.

     

    #110359

    With the graph function. I am unable to understand why these orders are placed. I am testing MACD so i can use it to cut bad trades that should not have been entered. Its a very simple rule, if MACD is bearish in 4 Hour TF, do not enter Long. Entry timeframe is 1 hour. I am testing this code using graph, and in these sections, 4 hour MACD is clearly bearish and I am using close with MACD. MACFILTER is showing as 1 which means condition is true, it should have been 0 for false. I is working in other sections, not here,

     

    Please see the image for illustration,

    #110377

    Because you compare the MACD Close value with its own value that change over time during  a 4-hours bar!

    If you want to use the Close value of the MACD during its bar, you should add this to the timeframe instruction:

     

Viewing 15 posts - 1 through 15 (of 19 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login