need help coding simple D open strategy

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #150147 quote
    ullle73
    Participant
    Senior

    Hi, could anyone help out coding this strategy found on forexfactory: https://www.forexfactory.com/thread/1032108-dos-daily-open-strategy-easy-to-trade-simple

     

    here are conditions:

    • If difference in pips between Open to Close (or Close to Open) of previous daily closed bar is more than 110 pips, then at the open of current day – open a market order to the opposite direction of previous day.
    • i.e: if yesterday’s daily bar is Bullish (moved up) and BODY (Close price – Open price) is more than 110 pips, then today at the Open of new daily bar, at 00:00, open Sell market order.
      Note: if spread at midnight is too high (more than 4.0 pips) – Don’t open a trade yet. Wait with market order till spread narrows back. It may take even 1-2 hours but usually it will take only a few minutes. So once spread is less than 4.0 pips, open the trade.
    • TP = 20 to 50 pips, depends on currency’s volatility.
    • No SL.
    • If price goes from last entry against the direction of the trade, open at the Open of every new day an additional market order with same lot size as original trade. No TP for additional trade.
    • Close all trades together when sum of all open profitable and losing trades (“basket“) is positive (may be 0.1$ or 1$).
    • Instead of SL: If sum of all open trades (“basket”) is losing more than 25% of account balance – close all trades. *This may happen once in a decade if at all.
    #150167 quote
    robertogozzi
    Moderator
    Master

    It’s not possible to know what the spread is, usually pretty high around midnight.

    What TF do you plan to use?

    #150186 quote
    ullle73
    Participant
    Senior

    yes, i understand that, dont mind the spread for now. Planing on using daily tf 🙂

     

    thanks for fast answer

    #150201 quote
    robertogozzi
    Moderator
    Master

    There you go:

    DefParam CumulateOrders = true
    Capital = 10000000
    Equity  = Capital + StrategyProfit
    Body    = abs(close - open)
    Bullish = close > open
    c1      = Bullish AND (Body > 110 * PipSize)
    If c1 AND Not OnMarket THEN
       SellShort 1 Contract at Market
       Set Target pProfit 50
    ElsIf ShortOnMarket AND PositionPerf < 0 Then
       SellShort 1 Contract at Market
    Endif
    If PositionPerf > 0 Then
       ExitShort at Market
    Endif
    MoneyGained = abs(PositionPerf * PositionPrice / PipSize * PipValue)
    IF (MoneyGained >= (Equity * 0.25)) AND PositionPerf < 0 Then
       ExitShort at Market
    Endif
    ullle73 thanked this post
    #150247 quote
    ullle73
    Participant
    Senior

    you are the best! thanks roberto

    #150256 quote
    ullle73
    Participant
    Senior

    tried adding long conditions as well, but then it does not take any shorts… could you help me with that as well? 🙂

    #150268 quote
    robertogozzi
    Moderator
    Master

    This should do (not tested):

    DefParam CumulateOrders = true
    Capital = 10000000
    Equity  = Capital + StrategyProfit
    Body    = abs(close - open)
    Bullish = close > open
    Bearish = close < open
    c1      = Bullish AND (Body > 110 * PipSize)
    c2      = Bearish AND (Body > 110 * PipSize)
    If c1 AND Not OnMarket THEN
       SellShort 1 Contract at Market
       Set Target pProfit 50
    ElsIf ShortOnMarket AND PositionPerf < 0 Then
       SellShort at Market
    ElsIF c2 AND Not OnMarket THEN
       Buy 1 Contract at Market
       Set Target pProfit 50
    ElsIf LongOnMarket AND PositionPerf < 0 Then
       Sell at Market
    Endif
    If PositionPerf > 0 Then
       ExitShort at Market
    Endif
    MoneyGained = abs(PositionPerf * PositionPrice / PipSize * PipValue)
    IF (MoneyGained >= (Equity * 0.25)) AND PositionPerf < 0 Then
       ExitShort at Market
       Sell      at Market
    Endif
    #150295 quote
    ullle73
    Participant
    Senior

    thanks!! long and short short, long does not scale up though, and the panic 25% of capital SL does not seem to work

    roberto.jpg roberto.jpg
    #150300 quote
    robertogozzi
    Moderator
    Master

    Adjust Capital to the actual size.

    #150331 quote
    ullle73
    Participant
    Senior

    still, code does not sell ALL pos as soon as in profit. You can see in picture that it does sell each individual trade when in profit. Is it possible to ALL open shorts when trade is in profit. In the example in the picture, it should probably have been 7 contracts before reaching BE, but after 5 it sold some and bought some

    roberto2.jpg roberto2.jpg
    #150334 quote
    robertogozzi
    Moderator
    Master

    Individual SL’s and TP’s are sent each time a position is added, so every position has it’s own life, if one hits the target or the stop loss it’s closed and POSITIONPERF is updated, so there’s no way to know the whole profit until ALL positions are close (that’s the time STRATEGYPROFIT is updated, not earlier).

    Managing accumulation of positions is not easy and, unless there’s only one global TP or SL, it’s impossible to know exactly what the performance is, since some positions may still be opened and some may have already been closed.

    But I have spotted a missing line, add this line just AFTER, or BEFORE, line 21:

    SELL AT Market
    ullle73 thanked this post
    #150338 quote
    ullle73
    Participant
    Senior

    oh okay, i see. Do you know why strategy does not increase long positions? I have capital and initial capital the same.

    roberto3.jpg roberto3.jpg
    #150341 quote
    robertogozzi
    Moderator
    Master

    Try removing AND Not OnMarket from lines 9 and 14.

    #150362 quote
    ullle73
    Participant
    Senior
    DefParam CumulateOrders = true
    Capital = 100000
    Equity  = Capital + StrategyProfit
    Body    = abs(close - open)
    Bullish = close > open
    Bearish = close < open
    c1      = Bullish AND (Body > 110 * PipSize)
    c2      = Bearish AND (Body > 110 * PipSize)
    If c1  THEN
    SellShort 1 Contract at Market
    Set Target pProfit 50
    ElsIf ShortOnMarket AND PositionPerf < 0 Then
    SellShort at Market
    ElsIF c2  THEN
    Buy 1 Contract at Market
    Set Target pProfit 50
    ElsIf LongOnMarket AND PositionPerf < 0 Then
    sell at Market
    Endif
    
    If PositionPerf > 0 Then
    SELL AT Market
    ExitShort at Market
    Endif
    MoneyGained = abs(PositionPerf * PositionPrice / PipSize * PipValue)
    IF (MoneyGained >= (Equity * 0.25)) AND PositionPerf < 0 Then
    ExitShort at Market
    Sell      at Market
    Endif
    

    Does not work :/

    roberto4.jpg roberto4.jpg
    #150376 quote
    robertogozzi
    Moderator
    Master

    This should match the requirements (I made numeric values variable):

    DefParam CumulateOrders = true
    ONCE BodySize = 110                                //110    Pips (zize of candle's body)
    ONCE TP       = 30                                 //30     Take Profit
    ONCE MaxLoss  = 25                                 //25%    max. loss
    ONCE Capital  = 100000                             //100000 starting Capital
    Equity        = Capital + StrategyProfit
    Body          = abs(close - open)
    Bullish       = close > open
    Bearish       = close < open
    c1            = Bullish AND (Body > BodySize * PipSize)
    c2            = Bearish AND (Body > BodySize * PipSize)
    // - SHORT
    If c1 THEN
       SellShort 1 Contract at Market
       Set Target pProfit TP
    ElsIF ShortOnMarket AND PositionPerf < 0 THEN
       SellShort 1 Contract at Market
    endif
    // - LONG
    IF c2  THEN
       Buy 1 Contract at Market
       Set Target pProfit TP
    ElsIF LongOnMarket AND PositionPerf < 0 THEN
       Buy 1 Contract at Market
    endif
    /////////////////////////////////////////////
    MoneyGained = PositionPerf * PositionPrice / PipSize * PipValue
    IF (abs(MoneyGained) >= (Equity * (MaxLoss / 100))) AND PositionPerf < 0 Then
       ExitShort at Market
       Sell      at Market
    Endif
    //
    //graph CountOfPosition
    //graph PositionPerf
    //graph MoneyGained
    ullle73 thanked this post
Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.

need help coding simple D open strategy


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
ullle73 @jonas_rydqvist Participant
Summary

This topic contains 15 replies,
has 2 voices, and was last updated by ullle73
5 years, 3 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/11/2020
Status: Active
Attachments: 4 files
Logo Logo
Loading...